gabriel / musehub public
test_release_migration.py python
185 lines 8.9 KB
Raw
sha256:675003834b19b109f8cb126e543a064a7bd6256930dd4a80ffcfe87c6eff3f17 fix(release): idempotent concurrent publish (D3) and compar… Opus 5.5 minor ⚠ breaking 12 days ago
1 """Migration compatibility: old (mtime) image vs new (active.json) image (musehub#114 Phase 6b).
2
3 Question answered: in what order may a legacy instance -- tarballs only, no pointer, no
4 ledger -- be moved to the pointer-selecting image? ``legacy_select`` is a frozen copy of
5 ``_latest_published_muse_version`` as of a6bba69f (the image currently on staging).
6 Everything uses temporary directories and in-memory storage; nothing live is touched.
7 """
8
9 from __future__ import annotations
10
11 import hashlib
12 import json
13 import os
14 import pathlib
15
16 import pytest
17
18 from deploy.release_safety import ReleaseSafetyError, activate, plan_cleanup, publish
19 from musehub.api.routes.musehub.install import _latest_published_muse_version
20 from musehub.config import settings
21 from musehub.release_pointer import ReleaseSelectionError
22 from tests.release_fixtures import observed, CHECKOUT, NOW, FakeStorage, make_artifact, make_report, sha256_of
23
24 FALLBACK = "FALLBACK-MUSE_VERSION"
25
26
27 def legacy_select(releases_dir: pathlib.Path) -> str:
28 tarballs = list(releases_dir.glob("muse-*.tar.gz"))
29 if tarballs:
30 latest = max(tarballs, key=lambda p: p.stat().st_mtime)
31 return latest.name.removesuffix(".tar.gz").removeprefix("muse-")
32 return FALLBACK
33
34
35 def _sha(data: bytes) -> str:
36 return hashlib.sha256(data).hexdigest()
37
38
39 def _put(releases: pathlib.Path, version: str, age: int = 0) -> str:
40 """Write a tarball whose mtime is *age* seconds in the past (larger = older)."""
41 body = f"sdist {version}".encode()
42 path = releases / f"muse-{version}.tar.gz"
43 path.write_bytes(body)
44 when = 1_800_000_000 - age
45 os.utime(path, (when, when))
46 return _sha(body)
47
48
49 def _pointer_text(version: str, sha: str, env: str = "staging") -> str:
50 return json.dumps({
51 "schema": 1, "environment": env, "version": version, "sha256": sha,
52 "previous": None, "activated_at": "2026-11-03T12:00:00+00:00",
53 })
54
55
56 @pytest.fixture
57 def releases(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path:
58 """A legacy staging volume: rc17/rc18/rc19, rc19 newest by mtime."""
59 d = tmp_path / "legacy-volume"
60 d.mkdir()
61 _put(d, "0.2.1rc17", age=300)
62 _put(d, "0.2.1rc18", age=200)
63 _put(d, "0.2.1rc19", age=100)
64 monkeypatch.setattr(settings, "musehub_releases_dir", str(d))
65 return d
66
67
68 def new_select() -> str:
69 return _latest_published_muse_version()
70
71
72 class TestMigrationMatrix:
73 def test_1_old_image_no_pointer(self, releases: pathlib.Path) -> None:
74 assert legacy_select(releases) == "0.2.1rc19"
75
76 def test_2_old_image_valid_pointer_ignores_it(self, releases: pathlib.Path) -> None:
77 sha = _sha(b"sdist 0.2.1rc19")
78 (releases / "active.json").write_text(_pointer_text("0.2.1rc19", sha))
79 assert legacy_select(releases) == "0.2.1rc19"
80 # even a pointer naming an OLDER release does not move the old image
81 (releases / "active.json").write_text(_pointer_text("0.2.1rc18", _sha(b"sdist 0.2.1rc18")))
82 assert legacy_select(releases) == "0.2.1rc19"
83
84 def test_2b_old_image_ignores_pointer_garbage_and_ledger_files(self, releases: pathlib.Path) -> None:
85 (releases / "active.json").write_text("not json")
86 (releases / "active.json.new").write_text("partial")
87 (releases / "muse-0.2.1rc19.release.json").write_text("{}")
88 assert legacy_select(releases) == "0.2.1rc19"
89
90 def test_3_new_image_no_pointer_fails_closed(self, releases: pathlib.Path) -> None:
91 with pytest.raises(ReleaseSelectionError, match="no active release pointer"):
92 new_select()
93
94 def test_4_new_image_valid_pointer(self, releases: pathlib.Path) -> None:
95 (releases / "active.json").write_text(_pointer_text("0.2.1rc19", _sha(b"sdist 0.2.1rc19")))
96 assert new_select() == "0.2.1rc19"
97
98 @pytest.mark.parametrize("text", ["", "not json", "[]", "{}", '{"schema": 2}'])
99 def test_5_new_image_malformed_pointer(self, releases: pathlib.Path, text: str) -> None:
100 (releases / "active.json").write_text(text)
101 with pytest.raises(ReleaseSelectionError):
102 new_select()
103
104 def test_6_new_image_pointer_to_missing_artifact(self, releases: pathlib.Path) -> None:
105 (releases / "active.json").write_text(_pointer_text("0.2.1rc20", "0" * 64))
106 with pytest.raises(ReleaseSelectionError, match="missing its tarball"):
107 new_select()
108
109 def test_7_new_image_pointer_with_wrong_sha(self, releases: pathlib.Path) -> None:
110 (releases / "active.json").write_text(_pointer_text("0.2.1rc19", "0" * 64))
111 with pytest.raises(ReleaseSelectionError, match="does not match"):
112 new_select()
113
114 def test_8_new_image_valid_pointer_missing_ledger_still_serves(self, releases: pathlib.Path) -> None:
115 """The server never reads the bucket ledger; selection depends only on the instance volume."""
116 (releases / "active.json").write_text(_pointer_text("0.2.1rc19", _sha(b"sdist 0.2.1rc19")))
117 assert new_select() == "0.2.1rc19" # no records/pointers anywhere
118
119 def test_9_new_image_valid_pointer_complete_ledger(self, releases: pathlib.Path, tmp_path: pathlib.Path) -> None:
120 storage = FakeStorage()
121 art = make_artifact(tmp_path, "0.2.1rc19", b"sdist 0.2.1rc19")
122 publish(storage, art, make_report(art), now=NOW, checkout=CHECKOUT)
123 pointer = activate(storage, "staging", "0.2.1rc19", sha256_of(art), expect=observed(storage, "staging"), now=NOW)
124 (releases / "active.json").write_text(pointer.to_json())
125 assert new_select() == "0.2.1rc19"
126
127 def test_10_rollback_to_old_image_with_pointer_and_ledger_left_in_place(self, releases: pathlib.Path) -> None:
128 sha = _sha(b"sdist 0.2.1rc19")
129 (releases / "active.json").write_text(_pointer_text("0.2.1rc19", sha))
130 (releases / "muse-0.2.1rc19.release.json").write_text("{}")
131 assert new_select() == legacy_select(releases) == "0.2.1rc19"
132
133 def test_10b_old_image_diverges_from_pointer_once_a_newer_tarball_lands(self, releases: pathlib.Path) -> None:
134 """Hazard: after migration a *publish* of rc20 (additive, pointer unchanged) makes a rolled-back
135 old image serve rc20 -- the new image keeps serving rc19. Roll back only before any further publish,
136 or re-establish rc19 as newest by mtime as part of the rollback."""
137 (releases / "active.json").write_text(_pointer_text("0.2.1rc19", _sha(b"sdist 0.2.1rc19")))
138 _put(releases, "0.2.1rc20", age=0)
139 assert new_select() == "0.2.1rc19"
140 assert legacy_select(releases) == "0.2.1rc20"
141
142
143 class TestLedgerInitialisationForPreexistingArtifacts:
144 """The live bucket holds rc17/18/19 with no .release.json and no pointers/."""
145
146 def _legacy_bucket(self, tmp_path: pathlib.Path) -> tuple[FakeStorage, pathlib.Path]:
147 art = make_artifact(tmp_path, "0.2.1rc19", b"sdist 0.2.1rc19")
148 storage = FakeStorage({
149 "muse-0.2.1rc17.tar.gz": b"sdist 0.2.1rc17",
150 "muse-0.2.1rc18.tar.gz": b"sdist 0.2.1rc18",
151 "muse-0.2.1rc19.tar.gz": art.read_bytes(),
152 "db-backups/x.sql": b"unrelated",
153 })
154 return storage, art
155
156 def test_publish_of_identical_bytes_only_adds_the_record(self, tmp_path: pathlib.Path) -> None:
157 storage, art = self._legacy_bucket(tmp_path)
158 before = storage.snapshot()
159 result = publish(storage, art, make_report(art), now=NOW, checkout=CHECKOUT)
160 assert result.status == "completed"
161 assert storage.writes() == [("put_new", "muse-0.2.1rc19.release.json")]
162 assert storage.deletes() == []
163 assert {k: v for k, v in storage.objects.items() if k in before} == before
164
165 def test_first_activation_has_no_rollback_target(self, tmp_path: pathlib.Path) -> None:
166 storage, art = self._legacy_bucket(tmp_path)
167 publish(storage, art, make_report(art), now=NOW, checkout=CHECKOUT)
168 pointer = activate(storage, "staging", "0.2.1rc19", sha256_of(art), expect=observed(storage, "staging"), now=NOW)
169 assert pointer.previous is None
170 assert storage.writes()[-1] == ("put_new", "pointers/staging.json")
171
172 def test_legacy_releases_cannot_be_activated_without_a_record(self, tmp_path: pathlib.Path) -> None:
173 storage, _ = self._legacy_bucket(tmp_path)
174 with pytest.raises(ReleaseSafetyError, match="no valid release record"):
175 activate(storage, "staging", "0.2.1rc18", _sha(b"sdist 0.2.1rc18"), expect=observed(storage, "staging"), now=NOW)
176
177 def test_cleanup_stays_blocked_until_production_has_a_pointer_and_never_selects_legacy_artifacts(
178 self, tmp_path: pathlib.Path
179 ) -> None:
180 storage, art = self._legacy_bucket(tmp_path)
181 publish(storage, art, make_report(art), now=NOW, checkout=CHECKOUT)
182 activate(storage, "staging", "0.2.1rc19", sha256_of(art), expect=observed(storage, "staging"), now=NOW)
183 plan = plan_cleanup(storage, keep=0)
184 assert plan.blocked and "production" in plan.blocked
185 assert plan.deletions == []
File History 3 commits
sha256:675003834b19b109f8cb126e543a064a7bd6256930dd4a80ffcfe87c6eff3f17 fix(release): idempotent concurrent publish (D3) and compar… Opus 5.5 minor ⚠ 12 days ago
sha256:04db68e43d9007348dbd726b2a3e52f73e83c5646a4511c07e19cb9a66e28b5f fix(release): gate provenance is observed from the checkout… Opus 5.5 minor ⚠ 12 days ago
sha256:ecd25846051497bb4ccfb9bebc57e52975ba0d384c76bece0812b0152cfb21f4 test(release): migration compatibility matrix between the m… Opus 5.5 12 days ago