| 1 | """Check the file contract between the ports worker and Vinix's ISO builder.""" |
| 2 | |
| 3 | import argparse |
| 4 | import hashlib |
| 5 | from importlib import import_module |
| 6 | from pathlib import Path |
| 7 | import sys |
| 8 | import tempfile |
| 9 | import unittest |
| 10 | from unittest.mock import patch |
| 11 | |
| 12 | |
| 13 | sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tools")) |
| 14 | release_inputs = import_module("release-inputs") |
| 15 | |
| 16 | |
| 17 | class ReleaseInputs(unittest.TestCase): |
| 18 | def setUp(self): |
| 19 | temp = tempfile.TemporaryDirectory() |
| 20 | self.addCleanup(temp.cleanup) |
| 21 | root = Path(temp.name) |
| 22 | self.args = argparse.Namespace( |
| 23 | arch="aarch64", native=root / "native", output=root / "arm64", release="iso-fixture", |
| 24 | commit="a" * 40, repo=root / "repo", desktop=root / "desktop", |
| 25 | graphics=root / "graphics", auditor=root / "audit", fingerprint="a" * 40, |
| 26 | archive_coverage=root / "coverage.tsv") |
| 27 | for name in ("bin/vinix-desktop", "sbin/vinix-session", "sbin/vinix-authd", |
| 28 | "sbin/vinix-system", "sbin/vinix-account"): |
| 29 | path = self.args.native / "usr" / name |
| 30 | path.parent.mkdir(parents=True, exist_ok=True) |
| 31 | path.write_bytes(b"\x7fELF\x02\x01" + bytes(12) + (183).to_bytes(2, "little")) |
| 32 | path.chmod(0o755) |
| 33 | |
| 34 | def test_freezes_checked_inputs_and_passes_release_tag(self): |
| 35 | names = ["initramfs.tar", "vinix-x11-arm64.tar.gz", "vinix-x11-arm64.tar.gz.sha256", "build-info"] |
| 36 | |
| 37 | def stage(argv, check): |
| 38 | self.assertTrue(check) |
| 39 | if argv[1].name == "bundle-x11.py": |
| 40 | target = argv[-1] |
| 41 | target.mkdir() |
| 42 | sidecar = target / names[1] |
| 43 | sidecar.write_bytes(b"sidecar") |
| 44 | digest = hashlib.sha256(sidecar.read_bytes()).hexdigest() |
| 45 | (target / names[2]).write_text(f"{digest} {sidecar.name}\n") |
| 46 | else: |
| 47 | self.assertEqual(argv[1].name, "stage-image.py") |
| 48 | self.assertIn("--release=iso-fixture", argv) |
| 49 | self.assertEqual(argv[-4:], ["--seed-client", self.args.graphics, |
| 50 | "--archive-coverage", self.args.archive_coverage]) |
| 51 | argv[5].mkdir() |
| 52 | (argv[5] / names[0]).write_bytes(b"initramfs") |
| 53 | |
| 54 | with patch.object(release_inputs.subprocess, "run", side_effect=stage): |
| 55 | release_inputs.assemble(self.args) |
| 56 | output = self.args.output |
| 57 | self.assertEqual((output / "build-info").read_text(), |
| 58 | f"commit={self.args.commit}\nrelease=iso-fixture\narch=arm64\n") |
| 59 | self.assertEqual((output / "SHA256SUMS").read_text().splitlines(), [ |
| 60 | f"{hashlib.sha256((output / name).read_bytes()).hexdigest()} {name}" |
| 61 | for name in sorted(names) |
| 62 | ]) |
| 63 | |
| 64 | def test_rejects_native_program_for_the_other_cpu(self): |
| 65 | self.args.arch = "x86_64" |
| 66 | with self.assertRaisesRegex(ValueError, "wrong ELF architecture"): |
| 67 | release_inputs.assemble(self.args) |
| 68 | self.assertFalse(self.args.output.exists()) |
| 69 | |
| 70 | |
| 71 | if __name__ == "__main__": |
| 72 | unittest.main() |
| 73 | |