vinix-ports / tests / vpac-guest.py
76 lines · 68 sloc · 3.54 KB · f98dcf28a458c2e1c6e7a36657c62fe25a86cd41
Raw
1"""Boot a staged vpac image and test rejection, install and upgrade using the guest binary."""
2import argparse
3import hashlib
4import os
5import shutil
6import subprocess
7import sys
8from pathlib import Path
9
10
11def run(args):
12 work = args.work
13 work.mkdir()
14 arch = args.arch
15 tests = Path(__file__).resolve().parent
16 pin = args.fingerprint.read_text().strip()
17 trust = work / "client-keys"
18 repo = args.repo
19 client = work / "client"
20 image = work / "image"
21
22 def log(name, argv, **options):
23 with (work / (name + ".log")).open("x") as output:
24 subprocess.run(argv, stdout=output, stderr=subprocess.STDOUT, check=True, **options)
25
26 log("trust", ["feed", "trust", trust, repo / "test-signer.asc", pin])
27 log("install", ["feed", "install", trust, repo, arch, client, "--fakeroot",
28 "vpac", "busybox-init", "vinix-keyring", "vinix-accounts", "vinix-filesystem"])
29 log("stage", [sys.executable, tests.parent / "tools/stage-image.py", arch, client, repo,
30 image, args.auditor, pin])
31
32 # The updater receives a second complete signed index. Only its changed files
33 # have to be added to the guest image alongside the first verified snapshot.
34 root = image / "root"
35 initial = root / "var/lib/vinix/repository" / arch
36 old = initial / "ca-certificates-bundle-20261020-2-any.pkg.tar.zst"
37 if not old.is_file() or not list(initial.glob("netbase-*.pkg.tar.zst")):
38 raise ValueError("initial feed needs CA bundle release 2 and netbase")
39 digest, filename = args.update_receipt.read_text().strip().split()
40 if filename != "ca-certificates-bundle-20261020-3-any.pkg.tar.zst":
41 raise ValueError("expected CA bundle release 3 in the update receipt")
42 new = args.update_receipt.parent / filename
43 with new.open("rb") as stream:
44 if hashlib.file_digest(stream, "sha256").hexdigest() != digest:
45 raise ValueError("update archive differs from its build receipt")
46 update = work / "update"
47 siblings = sorted(path for path in initial.glob("*.pkg.tar.zst") if path != old)
48 log("publish-update", ["feed", "publish", args.keys, update, arch, new, *siblings])
49 delta = root / "var/lib/vinix/test-update"
50 delta.mkdir()
51 for base in ("vinix.db", "vinix.files", new.name):
52 for name in (base, base + ".sig"):
53 shutil.copyfile(update / arch / name, delta / name)
54
55 fixture = work / "fixture.sh"
56 fixture.write_bytes(f"VPAC_TEST_SCENARIO={args.scenario}\n".encode()
57 + (tests / "userland/vpac.sh").read_bytes())
58 disk = work / "disk"
59 env = os.environ.copy()
60 env.setdefault("VINIX_QEMU_TIMEOUT", "720")
61 log("boot", [sys.executable, tests / "disk-root.py", arch, root, fixture,
62 args.iso, args.boot, disk], env=env)
63 files = sorted(work.glob("*.log")) + [fixture, disk / "guest/serial.log", disk / "receipts.sha256"]
64 with (work / "receipts.sha256").open("x") as report:
65 for file in files:
66 with file.open("rb") as stream:
67 report.write(f"{hashlib.file_digest(stream, 'sha256').hexdigest()} {file.relative_to(work)}\n")
68
69
70if __name__ == "__main__":
71 parser = argparse.ArgumentParser(description=__doc__)
72 parser.add_argument("arch", choices=("x86_64", "aarch64"))
73 parser.add_argument("--scenario", choices=("all", "install", "upgrade"), default="all")
74 for option in ("repo", "keys", "fingerprint", "update_receipt", "auditor", "boot", "iso", "work"):
75 parser.add_argument(option, type=lambda path: Path(path).resolve())
76 run(parser.parse_args())
77