test_release_checks.py
python
sha256:675003834b19b109f8cb126e543a064a7bd6256930dd4a80ffcfe87c6eff3f17
fix(release): idempotent concurrent publish (D3) and compar…
Opus 5.5
minor
⚠ breaking
12 days ago
| 1 | """Packaging gates (deploy/release_checks.py) against synthetic sdists, and the storage backends (musehub#114 Phase 6).""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import hashlib |
| 6 | import io |
| 7 | import pathlib |
| 8 | import tarfile |
| 9 | import textwrap |
| 10 | |
| 11 | import pytest |
| 12 | |
| 13 | from deploy import release_checks as rc |
| 14 | from deploy.release_safety import DirStorage, ObjectExists, S3Storage, StaleState, StorageUnavailable |
| 15 | |
| 16 | |
| 17 | def sdist(tmp: pathlib.Path, version: str = "1.2.3", *, extras: tuple[str, ...] = ("all",), requires: bool = True, |
| 18 | contract: int = 1, info_version: str | None = None) -> pathlib.Path: |
| 19 | lines = [f"Metadata-Version: 2.4", "Name: muse", f"Version: {info_version or version}", "Requires-Python: >=3.14"] |
| 20 | if requires: |
| 21 | lines.append("Requires-Dist: click>=8") |
| 22 | lines += [f"Provides-Extra: {e}" for e in extras] |
| 23 | files = { |
| 24 | f"muse-{version}/PKG-INFO": "\n".join(lines) + "\n", |
| 25 | f"muse-{version}/muse/core/install_manifest.py": f"INSTALLER_CONTRACT = {contract}\n", |
| 26 | } |
| 27 | path = tmp / f"muse-{version}.tar.gz" |
| 28 | with tarfile.open(path, "w:gz") as tf: |
| 29 | for name, text in files.items(): |
| 30 | raw = text.encode() |
| 31 | info = tarfile.TarInfo(name) |
| 32 | info.size = len(raw) |
| 33 | tf.addfile(info, io.BytesIO(raw)) |
| 34 | return path |
| 35 | |
| 36 | |
| 37 | class TestPackagingChecks: |
| 38 | def test_good_sdist_passes_metadata_checks(self, tmp_path: pathlib.Path) -> None: |
| 39 | rc.dependency_metadata(sdist(tmp_path)) |
| 40 | |
| 41 | def test_missing_all_extra_fails(self, tmp_path: pathlib.Path) -> None: |
| 42 | with pytest.raises(rc.CheckFailed, match="all"): |
| 43 | rc.dependency_metadata(sdist(tmp_path, extras=())) |
| 44 | |
| 45 | def test_no_requires_dist_fails(self, tmp_path: pathlib.Path) -> None: |
| 46 | with pytest.raises(rc.CheckFailed, match="Requires-Dist"): |
| 47 | rc.dependency_metadata(sdist(tmp_path, requires=False)) |
| 48 | |
| 49 | def test_version_must_agree_across_filename_pkginfo_and_pyproject(self, tmp_path: pathlib.Path) -> None: |
| 50 | repo = tmp_path / "repo" |
| 51 | repo.mkdir() |
| 52 | (repo / "pyproject.toml").write_text('[project]\nversion = "1.2.3"\n') |
| 53 | rc.version(sdist(tmp_path), repo) |
| 54 | with pytest.raises(rc.CheckFailed, match="disagreement"): |
| 55 | rc.version(sdist(tmp_path, info_version="1.2.4"), repo) |
| 56 | (repo / "pyproject.toml").write_text('[project]\nversion = "1.2.4"\n') |
| 57 | with pytest.raises(rc.CheckFailed, match="disagreement"): |
| 58 | rc.version(sdist(tmp_path), repo) |
| 59 | |
| 60 | def test_non_canonical_version_fails(self, tmp_path: pathlib.Path) -> None: |
| 61 | repo = tmp_path / "repo" |
| 62 | repo.mkdir() |
| 63 | (repo / "pyproject.toml").write_text('[project]\nversion = "1.2.3-rc1"\n') |
| 64 | with pytest.raises(rc.CheckFailed, match="canonical"): |
| 65 | rc.version(sdist(tmp_path, "1.2.3-rc1"), repo) |
| 66 | |
| 67 | def test_manifest_contract_mismatch_fails(self, tmp_path: pathlib.Path) -> None: |
| 68 | hub = tmp_path / "hub" / "musehub" / "api" / "routes" / "musehub" |
| 69 | hub.mkdir(parents=True) |
| 70 | (hub / "install.py").write_text(" installer_contract=1,\n") |
| 71 | rc.manifest_contract(sdist(tmp_path), tmp_path / "hub") |
| 72 | with pytest.raises(rc.CheckFailed, match="mismatch"): |
| 73 | rc.manifest_contract(sdist(tmp_path, contract=2), tmp_path / "hub") |
| 74 | |
| 75 | def test_cli_exits_nonzero_on_failure(self, tmp_path: pathlib.Path) -> None: |
| 76 | assert rc.main(["dependency-metadata", str(sdist(tmp_path, extras=()))]) == 1 |
| 77 | assert rc.main(["dependency-metadata", str(sdist(tmp_path))]) == 0 |
| 78 | assert rc.main(["nonsense"]) == 2 |
| 79 | |
| 80 | |
| 81 | class _Body: |
| 82 | def __init__(self, data: bytes) -> None: |
| 83 | self._d = data |
| 84 | |
| 85 | def read(self) -> bytes: |
| 86 | return self._d |
| 87 | |
| 88 | |
| 89 | class _FakeS3: |
| 90 | """Just enough of boto3's S3 client; models If-None-Match: * and error codes.""" |
| 91 | |
| 92 | def __init__(self) -> None: |
| 93 | self.objects: dict[str, bytes] = {} |
| 94 | self.calls: list[tuple[str, str]] = [] |
| 95 | self.fail_with: str | None = None |
| 96 | |
| 97 | def _err(self, code: str) -> Exception: |
| 98 | from botocore.exceptions import ClientError |
| 99 | |
| 100 | return ClientError({"Error": {"Code": code, "Message": code}}, "op") |
| 101 | |
| 102 | def _maybe_fail(self) -> None: |
| 103 | if self.fail_with: |
| 104 | raise self._err(self.fail_with) |
| 105 | |
| 106 | def head_object(self, Bucket: str, Key: str) -> dict[str, object]: |
| 107 | self._maybe_fail() |
| 108 | if Key not in self.objects: |
| 109 | raise self._err("404") |
| 110 | return {} |
| 111 | |
| 112 | def get_object(self, Bucket: str, Key: str) -> dict[str, _Body]: |
| 113 | self._maybe_fail() |
| 114 | if Key not in self.objects: |
| 115 | raise self._err("NoSuchKey") |
| 116 | return {"Body": _Body(self.objects[Key]), "ETag": self._etag(Key)} |
| 117 | |
| 118 | def _etag(self, key: str) -> str: |
| 119 | return f'"{hashlib.md5(self.objects[key]).hexdigest()}"' |
| 120 | |
| 121 | def put_object(self, Bucket: str, Key: str, Body: bytes, IfNoneMatch: str | None = None, IfMatch: str | None = None) -> dict[str, object]: |
| 122 | self._maybe_fail() |
| 123 | self.calls.append(("put", Key)) |
| 124 | if IfNoneMatch == "*" and Key in self.objects: |
| 125 | raise self._err("PreconditionFailed") |
| 126 | if IfMatch is not None: |
| 127 | if Key not in self.objects: |
| 128 | raise self._err("NoSuchKey") |
| 129 | if IfMatch != self._etag(Key): |
| 130 | raise self._err("PreconditionFailed") |
| 131 | self.objects[Key] = Body |
| 132 | return {} |
| 133 | |
| 134 | def list_objects_v2(self, Bucket: str, Prefix: str = "", ContinuationToken: str | None = None) -> dict[str, object]: |
| 135 | self._maybe_fail() |
| 136 | keys = sorted(k for k in self.objects if k.startswith(Prefix)) |
| 137 | return {"Contents": [{"Key": k} for k in keys], "IsTruncated": False} |
| 138 | |
| 139 | def delete_object(self, Bucket: str, Key: str) -> dict[str, object]: |
| 140 | self.calls.append(("delete", Key)) |
| 141 | del self.objects[Key] |
| 142 | return {} |
| 143 | |
| 144 | |
| 145 | class TestS3Storage: |
| 146 | def test_create_only_put_is_conditional_and_refuses_overwrite(self) -> None: |
| 147 | s3 = _FakeS3() |
| 148 | store = S3Storage("b", client=s3) |
| 149 | store.put_new("muse-1.tar.gz", b"a") |
| 150 | with pytest.raises(ObjectExists): |
| 151 | store.put_new("muse-1.tar.gz", b"b") |
| 152 | assert s3.objects["muse-1.tar.gz"] == b"a" |
| 153 | |
| 154 | def test_exists_read_list(self) -> None: |
| 155 | store = S3Storage("b", client=_FakeS3()) |
| 156 | assert not store.exists("k") |
| 157 | store.put_new("k", b"v") |
| 158 | assert store.exists("k") and store.read("k") == b"v" and store.list_keys("k") == ["k"] |
| 159 | |
| 160 | def test_conditional_replace_sends_if_match_and_refuses_a_stale_etag(self) -> None: |
| 161 | s3 = _FakeS3() |
| 162 | store = S3Storage("b", client=s3) |
| 163 | store.put_new("pointers/staging.json", b"1") |
| 164 | _, etag = store.read_versioned("pointers/staging.json") |
| 165 | store.put_if_match("pointers/staging.json", b"2", etag) # first writer wins |
| 166 | with pytest.raises(StaleState): |
| 167 | store.put_if_match("pointers/staging.json", b"3", etag) # same observation, now stale |
| 168 | assert s3.objects["pointers/staging.json"] == b"2" |
| 169 | |
| 170 | def test_conditional_replace_never_recreates_a_vanished_key(self) -> None: |
| 171 | s3 = _FakeS3() |
| 172 | store = S3Storage("b", client=s3) |
| 173 | store.put_new("p", b"1") |
| 174 | _, etag = store.read_versioned("p") |
| 175 | del s3.objects["p"] |
| 176 | with pytest.raises(StaleState): |
| 177 | store.put_if_match("p", b"2", etag) |
| 178 | assert "p" not in s3.objects |
| 179 | |
| 180 | def test_missing_etag_means_no_conditional_write_is_possible(self) -> None: |
| 181 | class NoEtag(_FakeS3): |
| 182 | def get_object(self, Bucket: str, Key: str) -> dict[str, _Body]: # type: ignore[override] |
| 183 | return {"Body": _Body(self.objects[Key])} # type: ignore[return-value] |
| 184 | |
| 185 | s3 = NoEtag() |
| 186 | s3.objects["p"] = b"1" |
| 187 | with pytest.raises(StorageUnavailable, match="ETag"): |
| 188 | S3Storage("b", client=s3).read_versioned("p") |
| 189 | |
| 190 | def test_conditional_replace_of_a_missing_pointer_read_is_keyerror(self) -> None: |
| 191 | with pytest.raises(KeyError): |
| 192 | S3Storage("b", client=_FakeS3()).read_versioned("absent") |
| 193 | |
| 194 | @pytest.mark.parametrize("code", ["AccessDenied", "InternalError", "SlowDown"]) |
| 195 | def test_any_unexpected_backend_error_is_unavailable_never_missing(self, code: str) -> None: |
| 196 | s3 = _FakeS3() |
| 197 | s3.fail_with = code |
| 198 | store = S3Storage("b", client=s3) |
| 199 | with pytest.raises(StorageUnavailable): |
| 200 | store.exists("k") # an outage must not read as "object absent" |
| 201 | with pytest.raises(StorageUnavailable): |
| 202 | store.list_keys() |
| 203 | |
| 204 | |
| 205 | class TestDirStorage: |
| 206 | def test_create_only_and_replace(self, tmp_path: pathlib.Path) -> None: |
| 207 | store = DirStorage(tmp_path) |
| 208 | store.put_new("pointers/staging.json", b"1") |
| 209 | with pytest.raises(ObjectExists): |
| 210 | store.put_new("pointers/staging.json", b"2") |
| 211 | _, token = store.read_versioned("pointers/staging.json") |
| 212 | store.put_if_match("pointers/staging.json", b"3", token) |
| 213 | assert store.read("pointers/staging.json") == b"3" |
| 214 | assert store.list_keys("pointers/") == ["pointers/staging.json"] |
| 215 | with pytest.raises(StaleState): |
| 216 | store.put_if_match("pointers/staging.json", b"4", token) # token from before the replace |
| 217 | assert store.read("pointers/staging.json") == b"3" |
| 218 | store.delete("pointers/staging.json") |
| 219 | assert not store.exists("pointers/staging.json") |
File History
2 commits
sha256:675003834b19b109f8cb126e543a064a7bd6256930dd4a80ffcfe87c6eff3f17
fix(release): idempotent concurrent publish (D3) and compar…
Opus 5.5
minor
⚠
12 days ago
sha256:5c8cb67657bbffc367d7e4b74e9fd7dea32dada487ae1ef893d2d5abfa0447e1
feat(release): gate report bound to the exact artifact, gat…
Opus 5.5
patch
12 days ago