import hashlib import json import subprocess import sys import tempfile import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[1] class ReleaseRecordTests(unittest.TestCase): def test_writes_versions_checksum_and_immutable_image_ids(self): with tempfile.TemporaryDirectory() as directory: artifact = Path(directory) / "GuestOps-0.2.0.tar.gz" output = Path(directory) / "release-record.json" artifact.write_bytes(b"reviewed image archive") subprocess.run( [ sys.executable, str(ROOT / "deploy" / "release_record.py"), "--artifact", str(artifact), "--commit", "a" * 40, "--api-image", "guestops-api:" + "a" * 40, "--api-id", "sha256:api", "--worker-image", "guestops-worker:" + "a" * 40, "--worker-id", "sha256:worker", "--output", str(output), ], check=True, ) record = json.loads(output.read_text(encoding="utf-8")) self.assertEqual(record["version"], "0.2.0") self.assertEqual(record["commit"], "a" * 40) self.assertEqual(record["images"]["api"]["id"], "sha256:api") self.assertEqual( record["artifact"]["sha256"], hashlib.sha256(artifact.read_bytes()).hexdigest(), ) def test_rejects_abbreviated_commit(self): with tempfile.TemporaryDirectory() as directory: artifact = Path(directory) / "images.tar.gz" artifact.write_bytes(b"fixture") result = subprocess.run( [ sys.executable, str(ROOT / "deploy" / "release_record.py"), "--artifact", str(artifact), "--commit", "abc123", "--api-image", "api:test", "--api-id", "sha256:api", "--worker-image", "worker:test", "--worker-id", "sha256:worker", "--output", str(Path(directory) / "record.json"), ], capture_output=True, text=True, ) self.assertNotEqual(result.returncode, 0) self.assertIn("full 40-character Git SHA", result.stderr) if __name__ == "__main__": unittest.main()