|
10 | 10 | import sys |
11 | 11 | import tempfile |
12 | 12 | import unittest |
| 13 | +from urllib.error import HTTPError, URLError |
13 | 14 | from unittest.mock import patch |
14 | 15 |
|
15 | 16 | from nullius import cli |
16 | | -from nullius.standard import cli as standard_cli, definitions |
| 17 | +from nullius.standard import cli as standard_cli, definitions, pins |
17 | 18 |
|
18 | 19 | APP = Path(__file__).resolve().parents[1] |
19 | 20 |
|
@@ -173,5 +174,98 @@ def test_new_creates_only_a_user_entry_and_unknown_version_has_a_clear_error(sel |
173 | 174 | standard_cli.new_version("missing", "2") |
174 | 175 |
|
175 | 176 |
|
| 177 | +class ImagePins(unittest.TestCase): |
| 178 | + OLD, NEW = "sha256:" + "a" * 64, "sha256:" + "b" * 64 |
| 179 | + REF = "docker.io/example/image@" + OLD |
| 180 | + |
| 181 | + def test_both_pin_sources_are_read_without_executing_or_writing(self): |
| 182 | + with tempfile.TemporaryDirectory() as folder: |
| 183 | + root = Path(folder) |
| 184 | + driver = root / "driver.py" |
| 185 | + driver.write_text(f'raise RuntimeError("must not execute")\nIMAGES = {{"task": "{self.REF}"}}\n') |
| 186 | + self.assertEqual(pins.references(folder), [(self.REF, self.REF)]) |
| 187 | + (root / "images.json").write_text(json.dumps({"task": self.REF})) |
| 188 | + self.assertEqual(pins.references(folder), [("task", self.REF)]) |
| 189 | + before = {p.name: p.read_bytes() for p in root.iterdir()} |
| 190 | + with patch.object(definitions, "get", return_value={"_here": folder}), \ |
| 191 | + patch.object(pins, "current", return_value=self.OLD), contextlib.redirect_stdout(io.StringIO()): |
| 192 | + self.assertEqual(standard_cli.main(["pins", "fixture", "--limit", "1"]), 0) |
| 193 | + self.assertEqual({p.name: p.read_bytes() for p in root.iterdir()}, before) |
| 194 | + |
| 195 | + def test_tags_registry_ports_and_docker_hub_shorthand(self): |
| 196 | + for reference, override, expected in ( |
| 197 | + ("ubuntu@" + self.OLD, None, ("registry-1.docker.io", "library/ubuntu", "latest", self.OLD)), |
| 198 | + ("registry.example:5443/team/image:stable@" + self.OLD, None, |
| 199 | + ("registry.example:5443", "team/image", "stable", self.OLD)), |
| 200 | + (self.REF, "v2", ("registry-1.docker.io", "example/image", "v2", self.OLD))): |
| 201 | + self.assertEqual(pins.target(reference, override), expected) |
| 202 | + for ref in ("not-pinned", "https://example/image@" + self.OLD, "user:password@host/image@" + self.OLD): |
| 203 | + with self.assertRaises(ValueError): |
| 204 | + pins.target(ref) |
| 205 | + |
| 206 | + @staticmethod |
| 207 | + def response(body=b"", **headers): |
| 208 | + response = io.BytesIO(body) |
| 209 | + response.headers = headers |
| 210 | + return response |
| 211 | + |
| 212 | + def test_registry_head_and_anonymous_bearer_challenge(self): |
| 213 | + challenge = HTTPError("https://registry.example/v2/example/image/manifests/latest", 401, "auth", { |
| 214 | + "WWW-Authenticate": 'Bearer realm="https://auth.example/token",service="registry.example",scope="repository:other:push"'}, None) |
| 215 | + with patch.object(pins, "build_opener") as opener: |
| 216 | + open_ = opener.return_value.open |
| 217 | + open_.side_effect = [challenge, self.response(b'{"token":"fixture-token"}'), |
| 218 | + self.response(**{"Docker-Content-Digest": self.OLD})] |
| 219 | + self.assertEqual(pins.current("registry.example", "example/image", "latest"), self.OLD) |
| 220 | + requests = [call.args[0] for call in open_.call_args_list] |
| 221 | + self.assertEqual([r.get_method() for r in requests], ["HEAD", "GET", "HEAD"]) |
| 222 | + self.assertIn("scope=repository%3Aexample%2Fimage%3Apull", requests[1].full_url) |
| 223 | + self.assertEqual(requests[2].get_header("Authorization"), "Bearer fixture-token") |
| 224 | + self.assertIn("application/vnd.oci.image.index.v1+json", requests[0].get_header("Accept")) |
| 225 | + |
| 226 | + def test_missing_manifest_is_distinct_from_auth_rate_limits_and_connection_failures(self): |
| 227 | + for failure, state in ((HTTPError("https://registry.example", 404, "gone", {}, None), "gone"), |
| 228 | + (HTTPError("https://registry.example", 429, "limited", {}, None), "not checked"), |
| 229 | + (HTTPError("https://registry.example", 403, "denied", {}, None), "not checked"), |
| 230 | + (URLError("unreachable"), "not checked"), (TimeoutError(), "not checked")): |
| 231 | + with self.subTest(failure=failure), patch.object(pins, "current", side_effect=failure): |
| 232 | + self.assertEqual(pins.check(self.REF)[0], state) |
| 233 | + challenge = 'Bearer realm="https://auth.example/token"' |
| 234 | + with patch.object(pins, "build_opener") as opener: |
| 235 | + opener.return_value.open.side_effect = [HTTPError("https://registry.example", 401, "auth", {"WWW-Authenticate": challenge}, None), |
| 236 | + HTTPError("https://auth.example/token", 404, "gone", {}, None)] |
| 237 | + state, detail = pins.check(self.REF) |
| 238 | + self.assertEqual(state, "not checked") |
| 239 | + self.assertIn("token service returned HTTP 404", detail) |
| 240 | + |
| 241 | + def test_missing_digest_and_unsafe_auth_are_not_used(self): |
| 242 | + with self.assertRaisesRegex(ValueError, "no valid sha256"): |
| 243 | + pins.current("registry.example", "example/image", "latest", open_=lambda *a, **k: self.response()) |
| 244 | + for challenge in ('Basic realm="private"', 'Bearer realm="http://auth.example/token"'): |
| 245 | + with self.assertRaisesRegex(ValueError, "unsupported authentication"): |
| 246 | + pins._token(challenge, "example/image", lambda *a, **k: self.fail("must not request")) |
| 247 | + |
| 248 | + def test_cli_limits_and_exit_precedence(self): |
| 249 | + refs = [("first", self.REF), ("second", self.REF)] |
| 250 | + with patch.object(definitions, "get", return_value={"_here": "unused"}), patch.object(pins, "references", return_value=refs), \ |
| 251 | + patch.object(pins, "current") as current: |
| 252 | + for results, expected in (([self.OLD, self.OLD], 0), ([self.OLD, self.NEW], 1), |
| 253 | + ([self.NEW, URLError("down")], 2)): |
| 254 | + current.side_effect = results |
| 255 | + out = io.StringIO() |
| 256 | + with contextlib.redirect_stdout(out): |
| 257 | + self.assertEqual(standard_cli.main(["pins", "fixture"]), expected) |
| 258 | + if expected == 1: |
| 259 | + self.assertIn(f"{self.OLD} -> {self.NEW}", out.getvalue()) |
| 260 | + current.reset_mock(side_effect=True) |
| 261 | + current.return_value = self.OLD |
| 262 | + with contextlib.redirect_stdout(io.StringIO()): |
| 263 | + self.assertEqual(standard_cli.main(["pins", "fixture", "--tag", "stable", "--limit", "1"]), 0) |
| 264 | + current.assert_called_once_with("registry-1.docker.io", "example/image", "stable") |
| 265 | + with contextlib.redirect_stderr(io.StringIO()): |
| 266 | + self.assertEqual(standard_cli.main(["pins", "fixture", "--limit", "0"]), 2) |
| 267 | + self.assertEqual(standard_cli.main(["pins", "fixture", "--tag", "bad/tag"]), 2) |
| 268 | + |
| 269 | + |
176 | 270 | if __name__ == "__main__": |
177 | 271 | unittest.main() |
0 commit comments