165 lines
6.2 KiB
Python
165 lines
6.2 KiB
Python
"""predeploy/deploy: прод только с копией, --no-pull, rollback floor не сбрасывает базу."""
|
||
import os
|
||
import subprocess
|
||
from datetime import datetime, timezone
|
||
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
|
||
|
||
|
||
def _git(*args, cwd=None):
|
||
result = subprocess.run(["git", *args], cwd=cwd, text=True, capture_output=True,
|
||
check=True)
|
||
return result.stdout.strip()
|
||
|
||
|
||
def _deployment_repo(tmp_path):
|
||
origin = tmp_path / "origin.git"
|
||
app = tmp_path / "app"
|
||
_git("init", "--bare", "-b", "main", str(origin))
|
||
_git("clone", str(origin), str(app))
|
||
_git("config", "user.email", "test@example.invalid", cwd=app)
|
||
_git("config", "user.name", "Test", cwd=app)
|
||
(app / "tracked.txt").write_text("initial\n")
|
||
_git("add", "tracked.txt", cwd=app)
|
||
_git("commit", "-m", "initial", cwd=app)
|
||
_git("push", "-u", "origin", "main", cwd=app)
|
||
return app, origin
|
||
|
||
|
||
def _stamp(app, head):
|
||
now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
||
(app / ".predeploy-stamp").write_text(f"{head}\n{now}\nfull: not-ui+ui\n")
|
||
|
||
|
||
def test_deploy_branch_preflight_accepts_published_main_without_moving_head(tmp_path):
|
||
app, _ = _deployment_repo(tmp_path)
|
||
head = _git("rev-parse", "HEAD", cwd=app)
|
||
_stamp(app, head)
|
||
result = _run(DEPLOY, "--preflight", env={"APP_DIR": str(app)})
|
||
assert result.returncode == 0, result.stderr
|
||
assert "branch preflight ok" in result.stdout
|
||
assert _git("rev-parse", "HEAD", cwd=app) == head
|
||
|
||
|
||
def test_deploy_branch_preflight_accepts_fast_forward_target(tmp_path):
|
||
app, origin = _deployment_repo(tmp_path)
|
||
old = _git("rev-parse", "HEAD", cwd=app)
|
||
author = tmp_path / "author"
|
||
_git("clone", str(origin), str(author))
|
||
_git("config", "user.email", "test@example.invalid", cwd=author)
|
||
_git("config", "user.name", "Test", cwd=author)
|
||
(author / "tracked.txt").write_text("next\n")
|
||
_git("commit", "-am", "next", cwd=author)
|
||
_git("push", cwd=author)
|
||
_stamp(app, _git("rev-parse", "HEAD", cwd=author))
|
||
result = _run(DEPLOY, "--preflight", env={"APP_DIR": str(app)})
|
||
assert result.returncode == 0, result.stderr
|
||
assert _git("rev-parse", "HEAD", cwd=app) == old
|
||
|
||
|
||
def test_deploy_branch_preflight_refuses_unpublished_commit(tmp_path):
|
||
app, _ = _deployment_repo(tmp_path)
|
||
remote_head = _git("rev-parse", "HEAD", cwd=app)
|
||
(app / "tracked.txt").write_text("server only\n")
|
||
_git("commit", "-am", "server only", cwd=app)
|
||
_stamp(app, remote_head)
|
||
result = _run(DEPLOY, "--preflight", env={"APP_DIR": str(app)})
|
||
assert result.returncode != 0
|
||
assert "origin/main" in result.stderr
|
||
|
||
|
||
def test_deploy_branch_preflight_refuses_other_branch(tmp_path):
|
||
app, _ = _deployment_repo(tmp_path)
|
||
_stamp(app, _git("rev-parse", "HEAD", cwd=app))
|
||
_git("switch", "-c", "feature", cwd=app)
|
||
result = _run(DEPLOY, "--preflight", env={"APP_DIR": str(app)})
|
||
assert result.returncode != 0
|
||
assert "main" in result.stderr
|
||
|
||
|
||
def test_deploy_branch_preflight_requires_matching_full_gate(tmp_path):
|
||
app, _ = _deployment_repo(tmp_path)
|
||
result = _run(DEPLOY, "--preflight", env={"APP_DIR": str(app)})
|
||
assert result.returncode != 0
|
||
assert "predeploy" in result.stderr
|
||
|
||
|
||
def test_deploy_no_pull_cannot_bypass_branch_guard(tmp_path):
|
||
app, _ = _deployment_repo(tmp_path)
|
||
_stamp(app, _git("rev-parse", "HEAD", cwd=app))
|
||
result = _run(DEPLOY, "--no-pull", "--preflight", env={"APP_DIR": str(app)})
|
||
assert result.returncode != 0
|
||
assert "--no-pull" in result.stderr
|