test_release_identity.py
python
sha256:369f691c7aaec3ca84a864ad1d099d5409045ddc12d185a922b3c84b9e2a1bad
merge: 100% passing, parallelized MuseHub test suite (task/…
Sonnet 5.5
patch
3 days ago
| 1 | """MuseHub release identity (docs/versioning.md): a build is named by its declared SemVer version, never a counter. |
| 2 | |
| 3 | Covers deploy/release_identity.py, its wiring in deploy/push.sh, deploy/deploy.sh and the Dockerfile, and the additive |
| 4 | ``release`` block of ``GET /protocol``. |
| 5 | """ |
| 6 | from __future__ import annotations |
| 7 | |
| 8 | import json |
| 9 | import os |
| 10 | import pathlib |
| 11 | import re |
| 12 | import shutil |
| 13 | import subprocess |
| 14 | import sys |
| 15 | |
| 16 | import pytest |
| 17 | |
| 18 | from deploy.release_identity import ( |
| 19 | COMPONENTS, |
| 20 | ReleaseIdentityError, |
| 21 | build_manifest, |
| 22 | declared_version, |
| 23 | identity, |
| 24 | main, |
| 25 | write_once, |
| 26 | ) |
| 27 | from musehub.protocol.responses import ProtocolInfoResponse, build_protocol_info, compute_protocol_hash, release_identity |
| 28 | |
| 29 | ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 30 | PUSH_SH = ROOT / "deploy" / "push.sh" |
| 31 | DEPLOY_SH = ROOT / "deploy" / "deploy.sh" |
| 32 | DOCKERFILE = ROOT / "Dockerfile" |
| 33 | REV = "sha256:" + "ab" * 32 |
| 34 | ENGINE = "sha256:" + "cd" * 32 |
| 35 | DIGEST = "sha256:" + "ef" * 32 |
| 36 | |
| 37 | |
| 38 | def _pyproject(tmp_path: pathlib.Path, version: str) -> pathlib.Path: |
| 39 | p = tmp_path / "pyproject.toml" |
| 40 | p.write_text(f'[project]\nname = "musehub"\nversion = "{version}"\n', encoding="utf-8") |
| 41 | return p |
| 42 | |
| 43 | |
| 44 | def _manifest(**over: object) -> dict[str, object]: |
| 45 | kwargs: dict[str, object] = dict( |
| 46 | pep440="0.2.3rc1", revision=REV, branch="dev", built_at="2026-11-11T19:30:00Z", image_tag="abababab-20261111193000", |
| 47 | engine_revision=ENGINE, registry_digests={"992382692655.dkr.ecr.us-east-1.amazonaws.com": DIGEST}, environments=["staging"], |
| 48 | ) |
| 49 | kwargs.update(over) |
| 50 | return build_manifest(**kwargs) # type: ignore[arg-type] |
| 51 | |
| 52 | |
| 53 | # ── version derivation ───────────────────────────────────────────────────────── |
| 54 | |
| 55 | |
| 56 | class TestDeclaredVersion: |
| 57 | def test_the_repository_declaration_has_a_release_identity(self) -> None: |
| 58 | ident = identity(declared_version(ROOT / "pyproject.toml")) |
| 59 | assert ident["tag"] == "v" + ident["semver"] and ident["image_tag"] == ident["semver"] |
| 60 | |
| 61 | @pytest.mark.parametrize(("pep440", "semver"), [ |
| 62 | ("0.2.3rc1", "0.2.3-rc.1"), ("0.2.3", "0.2.3"), ("0.2.2", "0.2.2"), ("0.3.0rc2", "0.3.0-rc.2"), |
| 63 | ("0.2.1rc9", "0.2.1-rc9"), # historical undotted base, kept as tagged (deploy/pep440_to_semver_tag.py) |
| 64 | ]) |
| 65 | def test_semver_form(self, tmp_path: pathlib.Path, pep440: str, semver: str) -> None: |
| 66 | assert identity(declared_version(_pyproject(tmp_path, pep440)))["semver"] == semver |
| 67 | |
| 68 | @pytest.mark.parametrize("raw", ["0.2.3-rc1", "v0.2.3", "0.2.3RC1", "not-a-version"]) |
| 69 | def test_a_non_canonical_or_invalid_declaration_is_refused(self, tmp_path: pathlib.Path, raw: str) -> None: |
| 70 | with pytest.raises(ReleaseIdentityError): |
| 71 | declared_version(_pyproject(tmp_path, raw)) |
| 72 | |
| 73 | @pytest.mark.parametrize("pep440", ["0.2.3.dev1", "0.2.3rc1+local", "0.2.3.post1", "1!0.2.3"]) |
| 74 | def test_a_version_without_a_release_form_cannot_be_released(self, pep440: str) -> None: |
| 75 | with pytest.raises(ReleaseIdentityError, match="cannot be released"): |
| 76 | identity(pep440) |
| 77 | |
| 78 | def test_cli_version(self, tmp_path: pathlib.Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 79 | assert main(["version", str(_pyproject(tmp_path, "0.2.3rc1"))]) == 0 |
| 80 | assert json.loads(capsys.readouterr().out) == {"pep440": "0.2.3rc1", "semver": "0.2.3-rc.1", "tag": "v0.2.3-rc.1", "image_tag": "0.2.3-rc.1"} |
| 81 | |
| 82 | |
| 83 | class TestSingleSourceOfTruth: |
| 84 | """release_identity.py is standard-library only (push.sh runs it with the operator's python3); its conversion must |
| 85 | stay identical to deploy/pep440_to_semver_tag.py, which uses ``packaging``.""" |
| 86 | |
| 87 | CORPUS = ["0.2.3rc1", "0.2.3", "0.2.2", "0.2.1rc9", "0.2.0rc15", "0.2.1", "0.3.0rc2", "1.0.0", "1.2.3a1", "1.2.3b4", |
| 88 | "10.20.30rc0", "0.2.3.dev1", "0.2.3rc1+local", "0.2.3.post1", "1!0.2.3", "0.2", "1.2.3.4", "0.2.3rc1.dev2"] |
| 89 | |
| 90 | def test_only_standard_library_imports(self) -> None: |
| 91 | import ast |
| 92 | import sys |
| 93 | tree = ast.parse((ROOT / "deploy" / "release_identity.py").read_text()) |
| 94 | names = {a.name.split(".")[0] for n in ast.walk(tree) if isinstance(n, ast.Import) for a in n.names} |
| 95 | names |= {n.module.split(".")[0] for n in ast.walk(tree) if isinstance(n, ast.ImportFrom) and n.module and n.level == 0} |
| 96 | assert names - {"__future__"} <= set(sys.stdlib_module_names), names - set(sys.stdlib_module_names) |
| 97 | |
| 98 | @pytest.mark.parametrize("pep440", CORPUS) |
| 99 | def test_identity_equals_the_packaging_based_converter(self, pep440: str) -> None: |
| 100 | from deploy.pep440_to_semver_tag import pep440_to_semver_tag |
| 101 | try: |
| 102 | want: str | None = pep440_to_semver_tag(pep440) |
| 103 | except ValueError: |
| 104 | want = None |
| 105 | try: |
| 106 | got: str | None = identity(pep440)["tag"] |
| 107 | except ReleaseIdentityError: |
| 108 | got = None |
| 109 | assert got == want |
| 110 | |
| 111 | @pytest.mark.parametrize("raw", CORPUS + ["0.2.3-rc1", "v0.2.3", "0.2.3RC1", "0.2.3rc01", "01.2.3", "0.2.3 ", "x"]) |
| 112 | def test_canonical_check_equals_packaging(self, tmp_path: pathlib.Path, raw: str) -> None: |
| 113 | from packaging.version import InvalidVersion, Version |
| 114 | try: |
| 115 | canonical = str(Version(raw)) == raw |
| 116 | except InvalidVersion: |
| 117 | canonical = False |
| 118 | try: |
| 119 | declared_version(_pyproject(tmp_path, raw)) |
| 120 | ok = True |
| 121 | except ReleaseIdentityError: |
| 122 | ok = False |
| 123 | assert ok is canonical |
| 124 | |
| 125 | @pytest.mark.parametrize("text", [ |
| 126 | '[tool.x]\nversion = "9.9.9"\n', # no [project] |
| 127 | '[project]\nname = "m"\n[tool.x]\nversion = "0.2.3"\n', # version outside [project] |
| 128 | '[project]\nversion = "0.2.3"\nversion = "0.2.4"\n', # duplicate |
| 129 | "[project]\nversion = '0.2.3'\n", # not the double-quoted form we read |
| 130 | ]) |
| 131 | def test_the_declaration_is_read_only_from_project(self, tmp_path: pathlib.Path, text: str) -> None: |
| 132 | p = tmp_path / "pyproject.toml" |
| 133 | p.write_text(text, encoding="utf-8") |
| 134 | with pytest.raises(ReleaseIdentityError): |
| 135 | declared_version(p) |
| 136 | |
| 137 | def test_agrees_with_tomllib_on_the_real_pyproject(self) -> None: |
| 138 | import tomllib |
| 139 | assert declared_version(ROOT / "pyproject.toml") == tomllib.loads((ROOT / "pyproject.toml").read_text())["project"]["version"] |
| 140 | |
| 141 | def test_runs_without_packaging(self) -> None: |
| 142 | """What push.sh does: a plain interpreter where ``packaging`` cannot be imported.""" |
| 143 | code = ("import sys, importlib.abc\n" |
| 144 | "class Block(importlib.abc.MetaPathFinder):\n" |
| 145 | " def find_spec(self, name, path, target=None):\n" |
| 146 | " if name == 'packaging' or name.startswith('packaging.'): raise ModuleNotFoundError(name)\n" |
| 147 | "sys.meta_path.insert(0, Block())\n" |
| 148 | "from deploy.release_identity import main\n" |
| 149 | f"sys.exit(main(['version', {str(ROOT / 'pyproject.toml')!r}]))\n") |
| 150 | proc = subprocess.run([sys.executable, "-c", code], cwd=ROOT, capture_output=True, text=True, timeout=60) |
| 151 | assert proc.returncode == 0, proc.stderr |
| 152 | assert json.loads(proc.stdout)["pep440"] == declared_version(ROOT / "pyproject.toml") |
| 153 | |
| 154 | |
| 155 | # ── manifest ─────────────────────────────────────────────────────────────────── |
| 156 | |
| 157 | |
| 158 | class TestManifest: |
| 159 | def test_records_every_identity_field(self) -> None: |
| 160 | m = _manifest() |
| 161 | assert m["product"] == "musehub" and m["schema"] == 1 |
| 162 | assert m["version"] == {"pep440": "0.2.3rc1", "semver": "0.2.3-rc.1", "tag": "v0.2.3-rc.1"} |
| 163 | assert m["source"] == {"revision": REV, "branch": "dev"} |
| 164 | assert m["built_at"] == "2026-11-11T19:30:00Z" |
| 165 | assert m["provenance"] == {"builder": "deploy/push.sh", "muse_engine_revision": ENGINE} |
| 166 | assert m["intended_environments"] == ["staging"] |
| 167 | comps = m["components"] |
| 168 | assert isinstance(comps, list) and [c["name"] for c in comps] == list(COMPONENTS) |
| 169 | for c in comps: |
| 170 | assert (c["os"], c["arch"]) == ("linux", "amd64") |
| 171 | assert c["images"] == [{"registry": "992382692655.dkr.ecr.us-east-1.amazonaws.com", "repository": "musehub/musehub", |
| 172 | "tag": "abababab-20261111193000", "semver_tag": "0.2.3-rc.1", "digest": DIGEST}] |
| 173 | |
| 174 | def test_deployment_state_is_never_part_of_the_build_record(self) -> None: |
| 175 | text = json.dumps(_manifest()) |
| 176 | assert "deployed" not in text and "active" not in text |
| 177 | |
| 178 | @pytest.mark.parametrize("over", [ |
| 179 | {"revision": "abc"}, {"engine_revision": "sha256:xyz"}, {"image_tag": "abababab"}, {"image_tag": "cdcdcdcd-20261111193000"}, |
| 180 | {"built_at": "2026-11-11 19:30"}, {"registry_digests": {"r": "sha256:short"}}, {"registry_digests": {}}, |
| 181 | {"environments": []}, {"branch": ""}, {"pep440": "0.2.3.dev1"}, |
| 182 | ]) |
| 183 | def test_invalid_inputs_are_refused(self, over: dict[str, object]) -> None: |
| 184 | with pytest.raises(ReleaseIdentityError): |
| 185 | _manifest(**over) |
| 186 | |
| 187 | def test_written_once_identical_rewrite_is_a_no_op_and_a_different_build_is_refused(self, tmp_path: pathlib.Path) -> None: |
| 188 | out = tmp_path / "rel" / "musehub-0.2.3-rc.1.release.json" |
| 189 | assert write_once(_manifest(), out) == "written" |
| 190 | first = out.read_bytes() |
| 191 | assert write_once(_manifest(), out) == "unchanged" and out.read_bytes() == first |
| 192 | with pytest.raises(ReleaseIdentityError, match="never reused"): |
| 193 | write_once(_manifest(registry_digests={"992382692655.dkr.ecr.us-east-1.amazonaws.com": "sha256:" + "01" * 32}), out) |
| 194 | assert out.read_bytes() == first |
| 195 | |
| 196 | def test_cli_manifest(self, tmp_path: pathlib.Path) -> None: |
| 197 | out = tmp_path / "m.json" |
| 198 | argv = ["manifest", "--pyproject", str(_pyproject(tmp_path, "0.2.3rc1")), "--revision", REV, "--branch", "dev", |
| 199 | "--built-at", "2026-11-11T19:30:00Z", "--image-tag", "abababab-20261111193000", "--engine-revision", ENGINE, |
| 200 | "--registry-digest", f"reg.example={DIGEST}", "--environment", "staging", "--out", str(out)] |
| 201 | assert main(argv) == 0 and json.loads(out.read_text())["version"]["semver"] == "0.2.3-rc.1" |
| 202 | assert main([*argv[:-1], str(tmp_path / "bad.json"), "--registry-digest", "no-equals"]) == 1 |
| 203 | |
| 204 | |
| 205 | # ── wiring: Dockerfile, push.sh, deploy.sh ───────────────────────────────────── |
| 206 | |
| 207 | |
| 208 | def _code(path: pathlib.Path) -> str: |
| 209 | return "\n".join(line for line in path.read_text().splitlines() if not line.strip().startswith("#")) |
| 210 | |
| 211 | |
| 212 | class TestWiring: |
| 213 | def test_the_image_declares_its_version_and_carries_its_revision(self) -> None: |
| 214 | src = DOCKERFILE.read_text() |
| 215 | runtime = src.split("AS runtime", 1)[1] |
| 216 | assert 'ARG MUSEHUB_VERSION=""' in runtime and 'org.opencontainers.image.version="${MUSEHUB_VERSION}"' in runtime |
| 217 | env = runtime.index("ENV MUSEHUB_REVISION=${MUSEHUB_REVISION}") |
| 218 | assert env > runtime.index("COPY --chown=musehub:musehub musehub/musehub/ ./musehub/"), "set after the cached layers" |
| 219 | |
| 220 | def test_push_derives_the_version_and_refuses_an_image_that_reports_another(self) -> None: |
| 221 | src = _code(PUSH_SH) |
| 222 | build = src.index("| docker build") |
| 223 | assert src.index("python3 -m deploy.release_identity version") < build |
| 224 | assert '--build-arg "MUSEHUB_VERSION=${MUSEHUB_VERSION}"' in src[build:] |
| 225 | checks = src.index('"org.opencontainers.image.version"') |
| 226 | runtime = src.index("from musehub.protocol.version import MUSE_VERSION") |
| 227 | assert build < checks < runtime < src.index('docker save "$LOCAL_TAG"'), "label and runtime checked before anything leaves the machine" |
| 228 | |
| 229 | def test_the_semver_tag_is_create_only_and_names_the_pushed_digest(self) -> None: |
| 230 | src = _code(PUSH_SH) |
| 231 | exists = src.index('crane digest "${image}:${SEMVER_TAG}"') |
| 232 | assert exists < src.index('crane push "$CRANE_TAR" "${image}:${IMAGE_TAG}"') < src.index('crane tag "${image}:${IMAGE_TAG}" "$SEMVER_TAG"') |
| 233 | assert "never reused" in src |
| 234 | assert src.index("python3 -m deploy.release_identity manifest") > src.index('crane tag "${image}:${IMAGE_TAG}" "$SEMVER_TAG"') |
| 235 | |
| 236 | def test_rollback_mode_is_unchanged(self) -> None: |
| 237 | src = _code(PUSH_SH) |
| 238 | rollback = src[src.index('if [ -n "${IMAGE_TAG:-}" ]; then'):src.index("else", src.index('if [ -n "${IMAGE_TAG:-}" ]; then'))] |
| 239 | assert "SKIP_BUILD=true" in rollback and "release_identity" not in rollback |
| 240 | |
| 241 | def test_the_new_slot_reports_its_version_before_traffic_moves(self) -> None: |
| 242 | src = _code(DEPLOY_SH) |
| 243 | static = src.index('static_check "http://127.0.0.1:${NEW_PORT}" "$NEW_SLOT"') |
| 244 | version = src.index('version_check "http://127.0.0.1:${NEW_PORT}" "$NEW_SLOT"') |
| 245 | assert static < version < src.index('nginx_point_to "$NEW_SLOT"') |
| 246 | |
| 247 | @pytest.mark.parametrize(("label", "protocol", "passes", "says"), [ |
| 248 | ("0.2.3rc1", {"version": "0.2.3rc1", "release": {"revision": REV}}, True, "reports version 0.2.3rc1"), |
| 249 | ("0.2.3rc1", {"version": "0.2.2", "release": {"revision": REV}}, False, "not switching traffic"), |
| 250 | ("0.2.3rc1", {"version": "0.2.3rc1", "release": {"revision": None}}, False, "not switching traffic"), |
| 251 | ("0.2.3rc1", {"version": "0.2.3rc1"}, False, "not switching traffic"), |
| 252 | ("<no value>", {"version": "0.2.2"}, True, "check skipped"), # pre-0.2.3rc1 image: still a valid rollback target |
| 253 | ("", {"version": "0.2.2"}, True, "check skipped"), |
| 254 | ]) |
| 255 | def test_version_check(self, tmp_path: pathlib.Path, label: str, protocol: dict[str, object], passes: bool, says: str) -> None: |
| 256 | func = re.search(r"^version_check\(\) \{.*?^\}", DEPLOY_SH.read_text(), re.M | re.S) |
| 257 | assert func |
| 258 | bindir = tmp_path / "bin" |
| 259 | bindir.mkdir() |
| 260 | (bindir / "curl").write_text(f"#!/bin/sh\nprintf '%s' '{json.dumps(protocol)}'\n") |
| 261 | labels = {"org.opencontainers.image.version": label, "org.opencontainers.image.revision": REV} |
| 262 | (bindir / "sudo").write_text('#!/bin/sh\nshift 3; fmt="$2"\n' |
| 263 | + "".join(f'case "$fmt" in *"{k}"*) printf "%s" "{v}";; esac\n' for k, v in labels.items())) |
| 264 | for f in ("curl", "sudo"): |
| 265 | (bindir / f).chmod(0o755) |
| 266 | script = (f'set -euo pipefail\nlog() {{ echo "$*"; }}\ndie() {{ echo "$*" >&2; exit 1; }}\nFULL_IMAGE=img:tag\n' |
| 267 | f'{func[0]}\nversion_check http://127.0.0.1:1338 green\n') |
| 268 | proc = subprocess.run(["bash", "-c", script], capture_output=True, text=True, timeout=30, |
| 269 | env={**os.environ, "PATH": f"{bindir}:{pathlib.Path(shutil.which('python3') or sys.executable).parent}:/usr/bin:/bin"}) |
| 270 | assert (proc.returncode == 0) is passes, proc.stdout + proc.stderr |
| 271 | assert says in proc.stdout + proc.stderr |
| 272 | |
| 273 | |
| 274 | # ── GET /protocol: additive release block ────────────────────────────────────── |
| 275 | |
| 276 | |
| 277 | class TestProtocolRelease: |
| 278 | def test_absent_or_malformed_identity_is_null_never_invented(self) -> None: |
| 279 | assert release_identity({}).model_dump() == {"revision": None, "image_tag": None} |
| 280 | assert release_identity({"MUSEHUB_REVISION": "abc", "RELEASE_VERSION": "latest"}).model_dump() == {"revision": None, "image_tag": None} |
| 281 | |
| 282 | def test_a_built_image_reports_its_revision_and_tag(self) -> None: |
| 283 | got = release_identity({"MUSEHUB_REVISION": REV, "RELEASE_VERSION": "abababab-20261111193000"}) |
| 284 | assert got.model_dump() == {"revision": REV, "image_tag": "abababab-20261111193000"} |
| 285 | |
| 286 | def test_the_original_fields_and_hash_are_unchanged(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 287 | monkeypatch.setenv("MUSEHUB_REVISION", REV) |
| 288 | info = build_protocol_info(3, 4, {"events": ["x"]}) |
| 289 | dumped = info.model_dump() |
| 290 | assert set(dumped) == {"version", "protocol_hash", "event_count", "tool_count", "release"} |
| 291 | assert dumped["protocol_hash"] == compute_protocol_hash({"events": ["x"]}) and dumped["release"]["revision"] == REV |
| 292 | # A client that only knows the original four fields still parses a response without the block. |
| 293 | assert ProtocolInfoResponse(version="0.2.2", protocol_hash="h", event_count=0, tool_count=0).release.revision is None |
File History
4 commits
sha256:369f691c7aaec3ca84a864ad1d099d5409045ddc12d185a922b3c84b9e2a1bad
merge: 100% passing, parallelized MuseHub test suite (task/…
Sonnet 5.5
patch
3 days ago
sha256:8166b51ae53275831023a7ce334228b3ccc8237b55d68a35172389db9ff46ad5
test: 100% passing MuseHub suite, parallelized (xdist, one …
Sonnet 5.5
patch
3 days ago
sha256:dd6439fc2ba2a90b18964d0bd1e112f6155992872f09024de3a7962cb8911441
fix(release): release_identity runs on the operator's plain…
Opus 5.5
minor
⚠
5 days ago
sha256:c8e165f9c0e0c8c2f888cea908ff5b6b51548acd1b6dbdf7ed11a06211cbe0eb
feat(release): MuseHub builds carry a SemVer release identi…
Opus 5.5
minor
⚠
5 days ago