gabriel / musehub public
test_release_checks.py python
172 lines 7.0 KB
Raw
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 io
6 import pathlib
7 import tarfile
8 import textwrap
9
10 import pytest
11
12 from deploy import release_checks as rc
13 from deploy.release_safety import DirStorage, ObjectExists, S3Storage, StorageUnavailable
14
15
16 def sdist(tmp: pathlib.Path, version: str = "1.2.3", *, extras: tuple[str, ...] = ("all",), requires: bool = True,
17 contract: int = 1, info_version: str | None = None) -> pathlib.Path:
18 lines = [f"Metadata-Version: 2.4", "Name: muse", f"Version: {info_version or version}", "Requires-Python: >=3.14"]
19 if requires:
20 lines.append("Requires-Dist: click>=8")
21 lines += [f"Provides-Extra: {e}" for e in extras]
22 files = {
23 f"muse-{version}/PKG-INFO": "\n".join(lines) + "\n",
24 f"muse-{version}/muse/core/install_manifest.py": f"INSTALLER_CONTRACT = {contract}\n",
25 }
26 path = tmp / f"muse-{version}.tar.gz"
27 with tarfile.open(path, "w:gz") as tf:
28 for name, text in files.items():
29 raw = text.encode()
30 info = tarfile.TarInfo(name)
31 info.size = len(raw)
32 tf.addfile(info, io.BytesIO(raw))
33 return path
34
35
36 class TestPackagingChecks:
37 def test_good_sdist_passes_metadata_checks(self, tmp_path: pathlib.Path) -> None:
38 rc.dependency_metadata(sdist(tmp_path))
39
40 def test_missing_all_extra_fails(self, tmp_path: pathlib.Path) -> None:
41 with pytest.raises(rc.CheckFailed, match="all"):
42 rc.dependency_metadata(sdist(tmp_path, extras=()))
43
44 def test_no_requires_dist_fails(self, tmp_path: pathlib.Path) -> None:
45 with pytest.raises(rc.CheckFailed, match="Requires-Dist"):
46 rc.dependency_metadata(sdist(tmp_path, requires=False))
47
48 def test_version_must_agree_across_filename_pkginfo_and_pyproject(self, tmp_path: pathlib.Path) -> None:
49 repo = tmp_path / "repo"
50 repo.mkdir()
51 (repo / "pyproject.toml").write_text('[project]\nversion = "1.2.3"\n')
52 rc.version(sdist(tmp_path), repo)
53 with pytest.raises(rc.CheckFailed, match="disagreement"):
54 rc.version(sdist(tmp_path, info_version="1.2.4"), repo)
55 (repo / "pyproject.toml").write_text('[project]\nversion = "1.2.4"\n')
56 with pytest.raises(rc.CheckFailed, match="disagreement"):
57 rc.version(sdist(tmp_path), repo)
58
59 def test_non_canonical_version_fails(self, tmp_path: pathlib.Path) -> None:
60 repo = tmp_path / "repo"
61 repo.mkdir()
62 (repo / "pyproject.toml").write_text('[project]\nversion = "1.2.3-rc1"\n')
63 with pytest.raises(rc.CheckFailed, match="canonical"):
64 rc.version(sdist(tmp_path, "1.2.3-rc1"), repo)
65
66 def test_manifest_contract_mismatch_fails(self, tmp_path: pathlib.Path) -> None:
67 hub = tmp_path / "hub" / "musehub" / "api" / "routes" / "musehub"
68 hub.mkdir(parents=True)
69 (hub / "install.py").write_text(" installer_contract=1,\n")
70 rc.manifest_contract(sdist(tmp_path), tmp_path / "hub")
71 with pytest.raises(rc.CheckFailed, match="mismatch"):
72 rc.manifest_contract(sdist(tmp_path, contract=2), tmp_path / "hub")
73
74 def test_cli_exits_nonzero_on_failure(self, tmp_path: pathlib.Path) -> None:
75 assert rc.main(["dependency-metadata", str(sdist(tmp_path, extras=()))]) == 1
76 assert rc.main(["dependency-metadata", str(sdist(tmp_path))]) == 0
77 assert rc.main(["nonsense"]) == 2
78
79
80 class _Body:
81 def __init__(self, data: bytes) -> None:
82 self._d = data
83
84 def read(self) -> bytes:
85 return self._d
86
87
88 class _FakeS3:
89 """Just enough of boto3's S3 client; models If-None-Match: * and error codes."""
90
91 def __init__(self) -> None:
92 self.objects: dict[str, bytes] = {}
93 self.calls: list[tuple[str, str]] = []
94 self.fail_with: str | None = None
95
96 def _err(self, code: str) -> Exception:
97 from botocore.exceptions import ClientError
98
99 return ClientError({"Error": {"Code": code, "Message": code}}, "op")
100
101 def _maybe_fail(self) -> None:
102 if self.fail_with:
103 raise self._err(self.fail_with)
104
105 def head_object(self, Bucket: str, Key: str) -> dict[str, object]:
106 self._maybe_fail()
107 if Key not in self.objects:
108 raise self._err("404")
109 return {}
110
111 def get_object(self, Bucket: str, Key: str) -> dict[str, _Body]:
112 self._maybe_fail()
113 if Key not in self.objects:
114 raise self._err("NoSuchKey")
115 return {"Body": _Body(self.objects[Key])}
116
117 def put_object(self, Bucket: str, Key: str, Body: bytes, IfNoneMatch: str | None = None) -> dict[str, object]:
118 self._maybe_fail()
119 self.calls.append(("put", Key))
120 if IfNoneMatch == "*" and Key in self.objects:
121 raise self._err("PreconditionFailed")
122 self.objects[Key] = Body
123 return {}
124
125 def list_objects_v2(self, Bucket: str, Prefix: str = "", ContinuationToken: str | None = None) -> dict[str, object]:
126 self._maybe_fail()
127 keys = sorted(k for k in self.objects if k.startswith(Prefix))
128 return {"Contents": [{"Key": k} for k in keys], "IsTruncated": False}
129
130 def delete_object(self, Bucket: str, Key: str) -> dict[str, object]:
131 self.calls.append(("delete", Key))
132 del self.objects[Key]
133 return {}
134
135
136 class TestS3Storage:
137 def test_create_only_put_is_conditional_and_refuses_overwrite(self) -> None:
138 s3 = _FakeS3()
139 store = S3Storage("b", client=s3)
140 store.put_new("muse-1.tar.gz", b"a")
141 with pytest.raises(ObjectExists):
142 store.put_new("muse-1.tar.gz", b"b")
143 assert s3.objects["muse-1.tar.gz"] == b"a"
144
145 def test_exists_read_list(self) -> None:
146 store = S3Storage("b", client=_FakeS3())
147 assert not store.exists("k")
148 store.put_replace("k", b"v")
149 assert store.exists("k") and store.read("k") == b"v" and store.list_keys("k") == ["k"]
150
151 @pytest.mark.parametrize("code", ["AccessDenied", "InternalError", "SlowDown"])
152 def test_any_unexpected_backend_error_is_unavailable_never_missing(self, code: str) -> None:
153 s3 = _FakeS3()
154 s3.fail_with = code
155 store = S3Storage("b", client=s3)
156 with pytest.raises(StorageUnavailable):
157 store.exists("k") # an outage must not read as "object absent"
158 with pytest.raises(StorageUnavailable):
159 store.list_keys()
160
161
162 class TestDirStorage:
163 def test_create_only_and_replace(self, tmp_path: pathlib.Path) -> None:
164 store = DirStorage(tmp_path)
165 store.put_new("pointers/staging.json", b"1")
166 with pytest.raises(ObjectExists):
167 store.put_new("pointers/staging.json", b"2")
168 store.put_replace("pointers/staging.json", b"3")
169 assert store.read("pointers/staging.json") == b"3"
170 assert store.list_keys("pointers/") == ["pointers/staging.json"]
171 store.delete("pointers/staging.json")
172 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 13 days ago