78 lines
2.9 KiB
Python
78 lines
2.9 KiB
Python
"""predeploy/deploy: прод только с копией, --no-pull, rollback floor не сбрасывает базу."""
|
||
import os
|
||
import subprocess
|
||
from pathlib import Path
|
||
|
||
ROOT = Path(__file__).resolve().parent.parent
|
||
PREDEPLOY = ROOT / "scripts" / "predeploy.sh"
|
||
DEPLOY = ROOT / "scripts" / "deploy.sh"
|
||
|
||
|
||
def _run(script, *args, env=None):
|
||
e = dict(os.environ)
|
||
e["APP_DIR"] = str(ROOT)
|
||
e.update(env or {})
|
||
return subprocess.run(["bash", str(script), *args], env=e, capture_output=True,
|
||
text=True, timeout=60)
|
||
|
||
|
||
def test_prod_without_copy_is_refused():
|
||
r = _run(PREDEPLOY, "--prod", "--dry-run")
|
||
assert r.returncode == 2
|
||
assert "--db" in r.stderr
|
||
|
||
|
||
def test_prod_copy_must_exist_and_not_be_prod(tmp_path):
|
||
missing = _run(PREDEPLOY, "--prod", "--db", str(tmp_path / "no.db"), "--dry-run")
|
||
assert missing.returncode == 2
|
||
assert "нет копии" in missing.stderr
|
||
|
||
|
||
def test_prod_dry_run_checks_copy_without_touching(tmp_path):
|
||
copy = tmp_path / "copy.db"
|
||
copy.write_bytes(b"")
|
||
r = _run(PREDEPLOY, "--prod", "--db", str(copy), "--dry-run")
|
||
assert r.returncode == 0, r.stderr
|
||
assert "migrate_admin_v2.py" in r.stdout
|
||
assert "admin_gate.py" in r.stdout
|
||
assert "pytest" in r.stdout
|
||
|
||
|
||
def test_deploy_check_no_pull_and_floor_are_non_destructive():
|
||
before = subprocess.run(["git", "-C", str(ROOT), "rev-parse", "HEAD"],
|
||
capture_output=True, text=True).stdout.strip()
|
||
r = _run(DEPLOY, "--no-pull", "--check", env={"ROLLBACK_TAG": "HEAD",
|
||
"ROLLBACK_FLOOR": "HEAD"})
|
||
assert r.returncode == 0, r.stderr
|
||
assert "no_pull=1" in r.stdout
|
||
assert "check rollback" in r.stdout
|
||
after = subprocess.run(["git", "-C", str(ROOT), "rev-parse", "HEAD"],
|
||
capture_output=True, text=True).stdout.strip()
|
||
assert before == after, "check сдвинул HEAD"
|
||
|
||
|
||
def test_deploy_bad_floor_is_refused_without_reset():
|
||
r = _run(DEPLOY, "--no-pull", "--check", env={"ROLLBACK_FLOOR": "does-not-exist"})
|
||
assert r.returncode != 0
|
||
assert "ROLLBACK_FLOOR" in r.stderr
|
||
|
||
|
||
def test_rollback_below_floor_refused_and_above_allowed():
|
||
below = _run(DEPLOY, "--no-pull", "--check",
|
||
env={"ROLLBACK_TAG": "HEAD~1", "ROLLBACK_FLOOR": "HEAD"})
|
||
assert below.returncode != 0
|
||
assert "ниже/вне rollback floor" in below.stderr
|
||
above = _run(DEPLOY, "--no-pull", "--check",
|
||
env={"ROLLBACK_TAG": "HEAD", "ROLLBACK_FLOOR": "HEAD~1"})
|
||
assert above.returncode == 0, above.stderr
|
||
|
||
|
||
def test_prod_db_must_not_be_the_same_file(tmp_path):
|
||
app = tmp_path / "app"
|
||
app.mkdir()
|
||
(app / "smeta-web.db").write_bytes(b"")
|
||
r = _run(PREDEPLOY, "--prod", "--db", str(app / "smeta-web.db"), "--dry-run",
|
||
env={"APP_DIR": str(app)})
|
||
assert r.returncode == 2
|
||
assert "боевую базу" in r.stderr or "inode" in r.stderr
|