test_release_cleanup_messages.py
python
sha256:22ea76418f13ef089b10088cb0c77fe3cd3f3da50b325cb412073c7f2f46f7f5
fix(release): cleanup shows an execution command only when …
Opus 5.5
patch
12 days ago
| 1 | """Cleanup tells the operator the truth about what it will let them do (musehub#114 Phase 6D, D5). |
| 2 | |
| 3 | The ``--execute --confirm <digest>`` hint must appear only when ``execute_cleanup`` would accept it. |
| 4 | Never run against live infrastructure: these use DirStorage in a temp directory. |
| 5 | """ |
| 6 | |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import json |
| 10 | import pathlib |
| 11 | |
| 12 | import pytest |
| 13 | |
| 14 | from deploy.release_safety import ( |
| 15 | DirStorage, |
| 16 | ExpectedPointer, |
| 17 | ReleaseSafetyError, |
| 18 | activate, |
| 19 | cleanup_guidance, |
| 20 | execute_cleanup, |
| 21 | main, |
| 22 | plan_cleanup, |
| 23 | publish, |
| 24 | ) |
| 25 | from tests.release_fixtures import CHECKOUT, NOW, make_artifact, make_report, sha256_of |
| 26 | |
| 27 | VERSIONS = ("0.2.1rc15", "0.2.1rc16", "0.2.1rc17", "0.2.1rc18", "0.2.1rc19") |
| 28 | |
| 29 | |
| 30 | def bucket(tmp: pathlib.Path, *, activate_both: bool = True) -> DirStorage: |
| 31 | store = DirStorage(tmp / "bucket") |
| 32 | shas = {} |
| 33 | for v in VERSIONS: |
| 34 | (tmp / v).mkdir() |
| 35 | art = make_artifact(tmp / v, v) |
| 36 | publish(store, art, make_report(art), now=NOW, checkout=CHECKOUT) |
| 37 | shas[v] = sha256_of(art) |
| 38 | if activate_both: |
| 39 | activate(store, "staging", "0.2.1rc19", shas["0.2.1rc19"], expect=ExpectedPointer.absent(), now=NOW) |
| 40 | activate(store, "production", "0.2.1rc19", shas["0.2.1rc19"], expect=ExpectedPointer.absent(), now=NOW, approve_production=True) |
| 41 | return store |
| 42 | |
| 43 | |
| 44 | def run_cli(store: DirStorage, capsys: pytest.CaptureFixture[str], *extra: str, keep: str = "1") -> tuple[int, str, str]: |
| 45 | code = main(["--dir", str(store.root), "cleanup", "--keep", keep, *extra]) |
| 46 | out = capsys.readouterr() |
| 47 | return code, out.out, out.err |
| 48 | |
| 49 | |
| 50 | def test_executable_dry_run_shows_the_confirmation_command_and_it_works(tmp_path: pathlib.Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 51 | store = bucket(tmp_path) |
| 52 | code, out, err = run_cli(store, capsys) |
| 53 | plan = plan_cleanup(store, keep=1) |
| 54 | assert code == 0 and plan.deletions |
| 55 | assert f"--execute --confirm {plan.digest}" in err and "would be deleted" in err |
| 56 | assert cleanup_guidance(plan).state == "executable" |
| 57 | assert execute_cleanup(store, plan, confirm=plan.digest) == plan.deletions # the advertised command is accepted |
| 58 | |
| 59 | |
| 60 | @pytest.mark.parametrize("damage", ["missing_production", "missing_staging", "malformed_staging", "wrong_env"]) |
| 61 | def test_blocked_cleanup_never_prints_a_confirmation_command_and_fails(tmp_path: pathlib.Path, capsys: pytest.CaptureFixture[str], damage: str) -> None: |
| 62 | store = bucket(tmp_path) |
| 63 | if damage == "missing_production": |
| 64 | (store.root / "pointers/production.json").unlink() |
| 65 | elif damage == "missing_staging": |
| 66 | (store.root / "pointers/staging.json").unlink() |
| 67 | elif damage == "malformed_staging": |
| 68 | (store.root / "pointers/staging.json").write_text("{not json") |
| 69 | else: |
| 70 | (store.root / "pointers/production.json").write_text((store.root / "pointers/staging.json").read_text()) |
| 71 | before = store.list_keys() |
| 72 | code, out, err = run_cli(store, capsys) |
| 73 | plan = plan_cleanup(store, keep=1) |
| 74 | assert plan.blocked and code == 1 |
| 75 | assert "--confirm" not in err and "--execute" not in err and "BLOCKED" in err |
| 76 | assert json.loads(out)["blocked"] and json.loads(out)["deletions"] == [] |
| 77 | with pytest.raises(ReleaseSafetyError, match="blocked"): |
| 78 | execute_cleanup(store, plan, confirm=plan.digest) |
| 79 | assert store.list_keys() == before |
| 80 | |
| 81 | |
| 82 | def test_blocked_execute_is_refused_and_deletes_nothing(tmp_path: pathlib.Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 83 | store = bucket(tmp_path, activate_both=False) # no pointers at all |
| 84 | before = store.list_keys() |
| 85 | code, _, err = run_cli(store, capsys, "--execute", "--confirm", "x") |
| 86 | assert code == 1 and "BLOCKED" in err and store.list_keys() == before |
| 87 | |
| 88 | |
| 89 | def test_empty_deletion_set_says_so_and_offers_no_execution(tmp_path: pathlib.Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 90 | store = bucket(tmp_path) |
| 91 | code, _, err = run_cli(store, capsys, keep="10") |
| 92 | assert code == 0 and "NOTHING TO DELETE" in err |
| 93 | assert "--confirm" not in err and "--execute" not in err |
| 94 | assert cleanup_guidance(plan_cleanup(store, keep=10)).state == "empty" |
| 95 | |
| 96 | |
| 97 | def test_protected_artifacts_are_reported_and_never_listed_for_deletion(tmp_path: pathlib.Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 98 | store = bucket(tmp_path) |
| 99 | code, out, err = run_cli(store, capsys, keep="0") |
| 100 | deletions = json.loads(out)["deletions"] |
| 101 | assert code == 0 and "muse-0.2.1rc19.tar.gz" not in deletions and "muse-0.2.1rc19.release.json" not in deletions |
| 102 | assert "protected as active/rollback" in err |
| 103 | |
| 104 | |
| 105 | def test_only_protected_releases_means_nothing_to_delete(tmp_path: pathlib.Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 106 | store = DirStorage(tmp_path / "b") |
| 107 | (tmp_path / "v").mkdir() |
| 108 | art = make_artifact(tmp_path / "v", "0.2.1rc19") |
| 109 | publish(store, art, make_report(art), now=NOW, checkout=CHECKOUT) |
| 110 | activate(store, "staging", "0.2.1rc19", sha256_of(art), expect=ExpectedPointer.absent(), now=NOW) |
| 111 | activate(store, "production", "0.2.1rc19", sha256_of(art), expect=ExpectedPointer.absent(), now=NOW, approve_production=True) |
| 112 | code, _, err = run_cli(store, capsys, keep="0") |
| 113 | assert code == 0 and "NOTHING TO DELETE" in err and "--confirm" not in err |
| 114 | |
| 115 | |
| 116 | def test_unverified_artifacts_are_reported_as_preserved_and_do_not_hide_a_valid_hint(tmp_path: pathlib.Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 117 | store = bucket(tmp_path) |
| 118 | (store.root / "muse-0.2.1rc16.release.json").write_text("garbage") # an unknown dependency: cannot be proven safe |
| 119 | code, out, err = run_cli(store, capsys) |
| 120 | plan = plan_cleanup(store, keep=1) |
| 121 | assert "muse-0.2.1rc16.tar.gz" not in plan.deletions |
| 122 | assert "unverified artifact(s) preserved" in err and "muse-0.2.1rc16.tar.gz" in err |
| 123 | assert code == 0 and (f"--confirm {plan.digest}" in err) == bool(plan.deletions) |
| 124 | |
| 125 | |
| 126 | def test_everything_unverified_is_empty_not_executable(tmp_path: pathlib.Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 127 | store = bucket(tmp_path) |
| 128 | for v in VERSIONS: |
| 129 | (store.root / f"muse-{v}.release.json").write_text("garbage") |
| 130 | code, _, err = run_cli(store, capsys, keep="0") |
| 131 | assert code == 0 and "NOTHING TO DELETE" in err and "--confirm" not in err |
File History
1 commit
sha256:22ea76418f13ef089b10088cb0c77fe3cd3f3da50b325cb412073c7f2f46f7f5
fix(release): cleanup shows an execution command only when …
Opus 5.5
patch
12 days ago