vinix-ports / tests / vpac-guest.py
107 lines · 99 sloc · 5.52 KB · c7be15b1dad9105fc7c7414ec161b2c90b4a17f6
Raw
1"""Boot a staged vpac image and test rejection, install and upgrade using the guest binary."""
2import argparse
3from filecmp import cmp
4import hashlib
5import os
6import shutil
7import subprocess
8import sys
9from pathlib import Path
10
11
12def run(args):
13 work = args.work
14 if args.staged_root and work.is_relative_to(args.staged_root):
15 raise ValueError("put test output outside the staged root")
16 work.mkdir()
17 arch = args.arch
18 tests = Path(__file__).resolve().parent
19 pin = args.fingerprint.read_text().strip()
20 trust = work / "client-keys"
21 repo = args.repo
22 client = work / "client"
23 image = work / "image"
24 root = image / "root"
25
26 def log(name, argv, **options):
27 with (work / (name + ".log")).open("x") as output:
28 subprocess.run(argv, stdout=output, stderr=subprocess.STDOUT, check=True, **options)
29
30 log("trust", ["feed", "trust", trust, repo / "test-signer.asc", pin])
31 if args.staged_root:
32 seed = args.staged_root / "var/lib/vinix/repository" / arch
33 for file in ("vinix.db", "vinix.db.sig"):
34 if not cmp(seed / file, repo / arch / file, shallow=False):
35 raise ValueError(f"staged root has a different seed {file}")
36 log("staged-input", ["sha256sum", seed / "vinix.db", seed / "vinix.db.sig"])
37 # The disk runner copies this tree before the guest writes to it.
38 shutil.copytree(args.staged_root, root, symlinks=True, copy_function=os.link)
39 else:
40 package = "vinix-pkg" if args.package_command == "pkg" else "vpac"
41 log("install", ["feed", "install", trust, repo, arch, client, "--fakeroot",
42 package, "busybox-init", "vinix-keyring", "vinix-accounts", "vinix-filesystem"])
43 log("stage", [sys.executable, tests.parent / "tools/stage-image.py", arch, client, repo,
44 image, args.auditor, pin])
45
46 # The updater receives a second complete signed index. Only its changed files
47 # have to be added to the guest image alongside the first verified snapshot.
48 initial = root / "var/lib/vinix/repository" / arch
49 old = initial / "ca-certificates-bundle-20261020-2-any.pkg.tar.zst"
50 if not old.is_file() or not list(initial.glob("netbase-*.pkg.tar.zst")):
51 raise ValueError("initial feed needs CA bundle release 2 and netbase")
52 digest, filename = args.update_receipt.read_text().strip().split()
53 if filename != "ca-certificates-bundle-20261020-3-any.pkg.tar.zst":
54 raise ValueError("expected CA bundle release 3 in the update receipt")
55 new = args.update_receipt.parent / filename
56 with new.open("rb") as stream:
57 if hashlib.file_digest(stream, "sha256").hexdigest() != digest:
58 raise ValueError("update archive differs from its build receipt")
59 update = args.signed_update or work / "update"
60 if args.signed_update:
61 with (update / arch / filename).open("rb") as stream:
62 if hashlib.file_digest(stream, "sha256").hexdigest() != digest:
63 raise ValueError("reused signed update differs from its build receipt")
64 else:
65 siblings = sorted(path for path in initial.glob("*.pkg.tar.zst") if path != old)
66 log("publish-update", ["feed", "publish", args.keys, update, arch, new, *siblings])
67 delta = root / "var/lib/vinix/test-update"
68 delta.mkdir()
69 delta.chmod(0o755)
70 for base in ("vinix.db", "vinix.files", new.name):
71 for name in (base, base + ".sig"):
72 target = delta / name
73 shutil.copyfile(update / arch / name, target)
74 target.chmod(0o644)
75
76 fixture = work / "fixture.sh"
77 settings = (f"VPAC_TEST_SCENARIO={args.scenario}\nVPAC_TEST_PACKAGE_COMMAND={args.package_command}\n"
78 f"VPAC_TEST_UPGRADE_HTTPS={int(args.upgrade_https)}\n")
79 fixture.write_bytes(settings.encode() + (tests / "userland/vpac.sh").read_bytes())
80 disk = work / "disk"
81 env = os.environ.copy()
82 env.setdefault("VINIX_QEMU_TIMEOUT", "720")
83 log("boot", [sys.executable, tests / "disk-root.py", arch, root, fixture,
84 args.iso, args.boot, disk, "--link-inputs"], env=env)
85 files = sorted(work.glob("*.log")) + [fixture, disk / "guest/serial.log", disk / "receipts.sha256"]
86 with (work / "receipts.sha256").open("x") as report:
87 for file in files:
88 with file.open("rb") as stream:
89 report.write(f"{hashlib.file_digest(stream, 'sha256').hexdigest()} {file.relative_to(work)}\n")
90
91
92if __name__ == "__main__":
93 parser = argparse.ArgumentParser(description=__doc__)
94 parser.add_argument("arch", choices=("x86_64", "aarch64"))
95 parser.add_argument("--scenario", choices=("all", "install", "upgrade"), default="all")
96 parser.add_argument("--package-command", choices=("vpac", "pkg"), default="vpac")
97 parser.add_argument("--upgrade-https", action="store_true", help="serve the signed upgrade over guest loopback TLS")
98 parser.add_argument("--signed-update", type=lambda path: Path(path).resolve(),
99 help="reuse an existing signed update repository with the exact upgrade archive")
100 parser.add_argument("--staged-root", type=lambda path: Path(path).resolve(),
101 help="reuse a staged root from the same feed and build volume")
102 for option in ("repo", "keys", "fingerprint", "update_receipt", "auditor", "boot", "iso", "work"):
103 parser.add_argument(option, type=lambda path: Path(path).resolve())
104 args = parser.parse_args()
105 if args.upgrade_https and args.scenario == "install":
106 parser.error("--upgrade-https requires an upgrade scenario")
107 run(args)
108