diff options
| author | Dave Hibberd <hibby@debian.org> | 2026-10-20 22:35:06 +0100 |
|---|---|---|
| committer | Dave Hibberd <hibby@debian.org> | 2026-10-20 22:35:06 +0100 |
| commit | 82ebb1ed1af30cee4dfbb1c628431d32885a00e0 (patch) | |
| tree | 1a1f782d3e576b758be0bc61171574a7cd8f2376 /.github/scripts/tests/test_release_linux_images.py | |
| parent | 061658210acb6425517733ea012dda803a7f72a1 (diff) | |
| parent | 61d684922b0f1c8455cc60eb3d123df058c9e1f5 (diff) | |
Merge tag 'debian/3.3.0_beta2+dfsg-1' into hibbian/trixie-backports
tagging wsjtx 3.3.0~beta2+dfsg-1
Diffstat (limited to '.github/scripts/tests/test_release_linux_images.py')
| -rw-r--r-- | .github/scripts/tests/test_release_linux_images.py | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/.github/scripts/tests/test_release_linux_images.py b/.github/scripts/tests/test_release_linux_images.py new file mode 100644 index 00000000..10a3fd0f --- /dev/null +++ b/.github/scripts/tests/test_release_linux_images.py @@ -0,0 +1,158 @@ +#!/usr/bin/env python3 +"""Release dependency selection and registry-copy regression checks.""" +import hashlib +import importlib.util +import json +from pathlib import Path +import subprocess +import tempfile +import unittest +from unittest.mock import patch + +SPEC = importlib.util.spec_from_file_location("release_linux_images", Path(__file__).parents[1] / "release-linux-images.py") +images = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(images) +GENERATION = "validated-build-20260925-123-1" +DIGEST = "sha256:" + "a" * 64 +RECIPE = "b" * 64 + + +def selection(): + return {"schema": 1, "generation": GENERATION, "images": { + key: {"reference": f"{images.PUBLIC}/{spec[0]}@{DIGEST}", "recipe_sha256": RECIPE} + for key, spec in images.IMAGES.items() + }} + + +def manifests(): + common = {"generation": GENERATION.removeprefix("validated-"), "recipe_sha256": RECIPE, + "toolchain_id": "gcc13-armhf-abc", "runtime_sha256": "c" * 64} + runtime = dict(common) + runtime.update({"cross_" + key: common[key] for key in ("generation", "recipe_sha256", "toolchain_id")}) + return [dict(common), dict(common), dict(common), runtime] + + +class SelectionTests(unittest.TestCase): + def setUp(self): + self.fingerprint = patch.object(images, "fingerprint", return_value=RECIPE) + self.fingerprint.start() + self.addCleanup(self.fingerprint.stop) + + def test_complete_selection_and_anonymous_reads(self): + with patch.object(images, "anonymous_digest", return_value=DIGEST) as remote: + self.assertEqual(images.validate(selection(), True), selection()) + self.assertEqual(remote.call_count, 4) + + def test_missing_malformed_and_untrusted_selection(self): + cases = [] + lock = selection(); del lock["images"]["armhf_cross"]; cases.append(lock) + lock = selection(); lock["images"]["x86_64"] = None; cases.append(lock) + lock = selection(); lock["generation"] = "stable"; cases.append(lock) + lock = selection(); lock["images"]["x86_64"]["reference"] = f"{images.PRIVATE}/linux-noble@{DIGEST}"; cases.append(lock) + lock = selection(); lock["images"]["x86_64"]["reference"] = f"{images.PUBLIC}/linux-noble:stable"; cases.append(lock) + lock = selection(); lock["images"]["aarch64"]["recipe_sha256"] = "c" * 64; cases.append(lock) + for lock in cases: + with self.subTest(lock=lock), self.assertRaises(ValueError): + images.validate(lock) + + def test_remote_digest_mismatch(self): + with patch.object(images, "anonymous_digest", return_value="sha256:" + "c" * 64), self.assertRaisesRegex(ValueError, "digest mismatch"): + images.validate(selection(), True) + + def test_provenance_binds_exact_lock_bytes_and_four_builders(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "lock.json" + path.write_text(json.dumps(selection())) + first = images.provenance(path) + self.assertEqual(first["builders"], dict.fromkeys(images.IMAGES, DIGEST)) + path.write_text(json.dumps(selection(), indent=2)) + self.assertNotEqual(first["lock_sha256"], images.provenance(path)["lock_sha256"]) + + def test_candidate_provenance_mismatch_fails(self): + with tempfile.TemporaryDirectory() as directory: + lock = Path(directory) / "lock.json" + record = Path(directory) / "provenance.json" + lock.write_text(json.dumps(selection())) + expected = images.provenance(lock) + record.write_text(json.dumps({"linux_images": expected})) + with patch("sys.argv", ["helper", "verify-provenance", str(lock), str(record)]): + images.main() + expected["builders"]["armhf_cross"] = "sha256:" + "c" * 64 + record.write_text(json.dumps({"linux_images": expected})) + with self.assertRaisesRegex(ValueError, "provenance does not match"): + images.main() + + def test_existing_output_is_never_reused(self): + with tempfile.TemporaryDirectory() as directory: + with patch("sys.argv", ["helper", "prepare", GENERATION, "--output-directory", directory]), \ + patch.object(images, "prepare") as prepare, \ + self.assertRaisesRegex(ValueError, "must not already exist"): + images.main() + prepare.assert_not_called() + + def prepare(self, existing=None, manifest_values=None): + def inspect(reference, anonymous=False, missing_ok=False): + return existing if missing_ok else DIGEST + with patch.object(images, "inspect", side_effect=inspect), \ + patch.object(images, "image_manifest", side_effect=manifest_values or manifests()), \ + patch.object(images, "anonymous_digest", return_value=DIGEST), \ + patch.object(images, "run") as command: + result = images.prepare(GENERATION) + return result, command.call_args_list + + def test_copy_preserves_all_manifests_and_digest(self): + (lock, _), commands = self.prepare() + self.assertEqual(lock, selection()) + self.assertEqual(len(commands), 4) + for call in commands: + args = call.args[0] + self.assertEqual(args[:4], ["skopeo", "copy", "--all", "--preserve-digests"]) + self.assertIn("@" + DIGEST, args[4]) + self.assertTrue(args[5].endswith(":release-deps-" + GENERATION)) + + def test_identical_rerun_skips_copy(self): + _, commands = self.prepare(existing=DIGEST) + self.assertEqual(commands, []) + + def test_conflicting_retained_tag_fails(self): + with self.assertRaisesRegex(ValueError, "Conflicting retained"): + self.prepare(existing="sha256:" + "c" * 64) + + def test_mixed_armhf_pair_fails_before_copy(self): + for field in ("cross_generation", "cross_recipe_sha256", "cross_toolchain_id", "runtime_sha256"): + values = manifests() + values[-1][field] = "wrong" + with self.subTest(field=field), self.assertRaisesRegex(ValueError, "pair mismatch"): + self.prepare(manifest_values=values) + + def test_partial_copy_failure_emits_no_selection(self): + with tempfile.TemporaryDirectory() as directory: + output = Path(directory) / "output" + with patch("sys.argv", ["helper", "prepare", GENERATION, "--output-directory", str(output)]), \ + patch.object(images, "inspect", side_effect=lambda ref, anonymous=False, missing_ok=False: None if missing_ok else DIGEST), \ + patch.object(images, "image_manifest", side_effect=manifests()), \ + patch.object(images, "run", side_effect=["", subprocess.CalledProcessError(1, "copy", stderr="denied")]), \ + self.assertRaises(subprocess.CalledProcessError): + images.main() + self.assertFalse(output.exists()) + + def test_registry_inspection_hashes_exact_manifest_without_platform_selection(self): + raw = b'{"mediaType":"application/vnd.oci.image.index.v1+json","manifests":[]}\n' + result = subprocess.CompletedProcess([], 0, stdout=raw) + with patch.object(images.subprocess, "run", return_value=result) as command: + digest = images.inspect("ghcr.io/wsjtx/wsjtx/linux-armv7-bookworm:test", anonymous=True) + self.assertEqual(digest, "sha256:" + hashlib.sha256(raw).hexdigest()) + self.assertIn("--raw", command.call_args.args[0]) + self.assertIn("--no-creds", command.call_args.args[0]) + + def test_authentication_error_is_not_missing_tag(self): + with patch.object(images.subprocess, "run", side_effect=subprocess.CalledProcessError(1, "inspect", stderr=b"unauthorized")), self.assertRaises(subprocess.CalledProcessError): + images.inspect("test", missing_ok=True) + + def test_only_manifest_unknown_allows_first_copy(self): + with patch.object(images.subprocess, "run", side_effect=subprocess.CalledProcessError(1, "inspect", stderr=b"manifest unknown")): + self.assertIsNone(images.inspect("test", missing_ok=True)) + + +if __name__ == "__main__": + unittest.main() |
