Files
kiri/scripts/ci/test_release_gate_mac.py
T
Precious Okwu 3db7378e5b CI & release: enforce executable regression checks, PR gates, Windows unit tests, and Mac release gate (#1128)
* CI & release: enforce executable regression checks, PR gates, Windows unit tests, and Mac release gate

* Compose: keep empty environment variables distinct instead of gluing onto preceding entry
2026-12-02 02:41:04 +01:00

237 lines
8.4 KiB
Python

#!/usr/bin/env python3
"""Unit tests for scripts/release-gate-mac.py."""
import importlib.util
import json
from pathlib import Path
import stat
import struct
import subprocess
import tempfile
import unittest
REPO = Path(__file__).resolve().parents[2]
def _load_module():
path = REPO / "scripts/release-gate-mac.py"
spec = importlib.util.spec_from_file_location("release_gate_mac", path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
GATE = _load_module()
def _make_cpio(entries: list[tuple[str, int, bytes]], *, include_trailer: bool = True) -> bytes:
buf = bytearray()
ino = 0
def add(name: str, mode: int, payload: bytes) -> None:
nonlocal ino
ino += 1
name_bytes = name.encode("utf-8") + b"\x00"
fields = [
ino,
mode,
0,
0,
1,
0,
len(payload),
0,
0,
0,
0,
len(name_bytes),
0,
]
buf.extend(b"070701")
for val in fields:
buf.extend(f"{val:08x}".encode("ascii"))
buf.extend(name_bytes)
while len(buf) % 4:
buf.append(0)
buf.extend(payload)
while len(buf) % 4:
buf.append(0)
for name, mode, payload in entries:
add(name, mode, payload)
if include_trailer:
ino = -1
add("TRAILER!!!", 0, b"")
return bytes(buf)
def _write_ext4_image(path: Path, *, size: int, magic: int = 0xEF53, compat: int = 0x003C) -> None:
with path.open("wb") as fh:
fh.truncate(size)
sb = bytearray(1024)
struct.pack_into("<H", sb, 0x38, magic)
struct.pack_into("<I", sb, 0x5C, compat)
fh.seek(1024)
fh.write(sb)
class ReleaseGateMacTests(unittest.TestCase):
def test_initramfs_accepts_required_regular_files_and_symlinks(self):
with tempfile.TemporaryDirectory() as tmp:
cpio = Path(tmp) / "initramfs.cpio"
cpio.write_bytes(
_make_cpio([
("./init", stat.S_IFREG | 0o755, b"\x7fELF-kiri-init"),
("sbin/e2fsck", stat.S_IFREG | 0o755, b"\x7fELF-e2fsck"),
("sbin/resize2fs", stat.S_IFLNK | 0o777, b"e2fsck"),
])
)
GATE.verify_initramfs(cpio)
def test_initramfs_rejects_missing_or_empty_tools(self):
with tempfile.TemporaryDirectory() as tmp:
cpio = Path(tmp) / "initramfs.cpio"
# Missing sbin/resize2fs.
cpio.write_bytes(
_make_cpio([
("init", stat.S_IFREG | 0o755, b"init"),
("sbin/e2fsck", stat.S_IFREG | 0o755, b"fsck"),
])
)
with self.assertRaisesRegex(GATE.GateError, "sbin/resize2fs"):
GATE.verify_initramfs(cpio)
# Empty sbin/e2fsck stub.
cpio.write_bytes(
_make_cpio([
("init", stat.S_IFREG | 0o755, b"init"),
("sbin/e2fsck", stat.S_IFREG | 0o755, b""),
("sbin/resize2fs", stat.S_IFREG | 0o755, b"resize"),
])
)
with self.assertRaisesRegex(GATE.GateError, "empty"):
GATE.verify_initramfs(cpio)
# Directory instead of executable binary.
cpio.write_bytes(
_make_cpio([
("init", stat.S_IFREG | 0o755, b"init"),
("sbin/e2fsck", stat.S_IFDIR | 0o755, b""),
("sbin/resize2fs", stat.S_IFREG | 0o755, b"resize"),
])
)
with self.assertRaisesRegex(GATE.GateError, "not a regular file"):
GATE.verify_initramfs(cpio)
# Truncated archive without TRAILER!!!
cpio.write_bytes(
_make_cpio(
[
("init", stat.S_IFREG | 0o755, b"init"),
("sbin/e2fsck", stat.S_IFREG | 0o755, b"fsck"),
("sbin/resize2fs", stat.S_IFREG | 0o755, b"resize"),
],
include_trailer=False,
)
)
with self.assertRaisesRegex(GATE.GateError, "TRAILER"):
GATE.verify_initramfs(cpio)
def test_ext4_superblock_checks_magic_size_and_sparse_super2(self):
with tempfile.TemporaryDirectory() as tmp:
img = Path(tmp) / "data.img"
_write_ext4_image(img, size=64 << 20, magic=0xEF53, compat=0x003C)
GATE.verify_ext4_superblock(img, expected_size=64 << 20)
# Wrong size fails.
with self.assertRaisesRegex(GATE.GateError, "expected"):
GATE.verify_ext4_superblock(img, expected_size=64 << 30)
# Bad magic fails.
_write_ext4_image(img, size=64 << 20, magic=0x1234, compat=0x003C)
with self.assertRaisesRegex(GATE.GateError, "magic"):
GATE.verify_ext4_superblock(img, expected_size=64 << 20)
# sparse_super2 (0x0200) set fails.
_write_ext4_image(img, size=64 << 20, magic=0xEF53, compat=0x003C | 0x0200)
with self.assertRaisesRegex(GATE.GateError, "sparse_super2"):
GATE.verify_ext4_superblock(img, expected_size=64 << 20)
def test_unpack_and_verify_guest_stages_complete_bundle(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
app = root / "Kiri.app"
macos = app / "Contents/MacOS"
guest = app / "Contents/Resources/guest"
macos.mkdir(parents=True)
guest.mkdir(parents=True)
exe = macos / "kiri"
exe.write_bytes(b"#!/bin/sh\nexit 0\n")
exe.chmod(0o755)
(guest / "Image").write_bytes(b"kernel-image")
(guest / "initramfs.cpio").write_bytes(
_make_cpio([
("init", stat.S_IFREG | 0o755, b"init"),
("sbin/e2fsck", stat.S_IFREG | 0o755, b"fsck"),
("sbin/resize2fs", stat.S_IFREG | 0o755, b"resize"),
])
)
raw_dir = root / "raw"
raw_dir.mkdir()
_write_ext4_image(raw_dir / "data.img", size=64 << 30, magic=0xEF53, compat=0x003C)
subprocess.run(
["tar", "-cSzf", str(guest / "data.img.tgz"), "-C", str(raw_dir), "data.img"],
check=True,
)
staged = root / "staged"
GATE.unpack_and_verify_guest(app, staged)
self.assertTrue((staged / "Image").is_file())
self.assertTrue((staged / "initramfs.cpio").is_file())
self.assertEqual((staged / "data.img").stat().st_size, 64 << 30)
def test_bundle_sha256_and_receipt_are_deterministic(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
app = root / "Kiri.app"
(app / "Contents/MacOS").mkdir(parents=True)
exe = app / "Contents/MacOS/kiri"
exe.write_bytes(b"binary-v1")
exe.chmod(0o755)
(app / "Contents/Info.plist").write_text("<plist/>", encoding="utf-8")
digest_a = GATE.bundle_sha256(app)
digest_b = GATE.bundle_sha256(app)
self.assertEqual(digest_a, digest_b)
exe.chmod(0o644)
self.assertNotEqual(digest_a, GATE.bundle_sha256(app))
exe.chmod(0o755)
self.assertEqual(digest_a, GATE.bundle_sha256(app))
exe.write_bytes(b"binary-v2")
digest_c = GATE.bundle_sha256(app)
self.assertNotEqual(digest_a, digest_c)
receipt_file = root / "receipt.json"
written = GATE.write_receipt(
receipt_file,
app,
["guest_assets_present", "packaged_vm_disk_grow"],
head="a" * 40,
app_digest=digest_c,
timestamp="2026-12-02T00:00:00Z",
)
loaded = json.loads(receipt_file.read_text(encoding="utf-8"))
self.assertEqual(loaded, written)
self.assertEqual(loaded["git_head"], "a" * 40)
self.assertEqual(loaded["app_sha256"], digest_c)
self.assertEqual(loaded["timestamp"], "2026-12-02T00:00:00Z")
self.assertEqual(loaded["checks_passed"], ["guest_assets_present", "packaged_vm_disk_grow"])
if __name__ == "__main__":
unittest.main()