| 1 | """Boot a staged vpac image and test rejection, install and upgrade using the guest binary.""" |
| 2 | import argparse |
| 3 | from filecmp import cmp |
| 4 | import hashlib |
| 5 | import os |
| 6 | import shutil |
| 7 | import subprocess |
| 8 | import sys |
| 9 | from pathlib import Path |
| 10 | |
| 11 | |
| 12 | def 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 = work / "update" |
| 60 | siblings = sorted(path for path in initial.glob("*.pkg.tar.zst") if path != old) |
| 61 | log("publish-update", ["feed", "publish", args.keys, update, arch, new, *siblings]) |
| 62 | delta = root / "var/lib/vinix/test-update" |
| 63 | delta.mkdir() |
| 64 | delta.chmod(0o755) |
| 65 | for base in ("vinix.db", "vinix.files", new.name): |
| 66 | for name in (base, base + ".sig"): |
| 67 | target = delta / name |
| 68 | shutil.copyfile(update / arch / name, target) |
| 69 | target.chmod(0o644) |
| 70 | |
| 71 | fixture = work / "fixture.sh" |
| 72 | fixture.write_bytes(f"VPAC_TEST_SCENARIO={args.scenario}\nVPAC_TEST_PACKAGE_COMMAND={args.package_command}\n".encode() |
| 73 | + (tests / "userland/vpac.sh").read_bytes()) |
| 74 | disk = work / "disk" |
| 75 | env = os.environ.copy() |
| 76 | env.setdefault("VINIX_QEMU_TIMEOUT", "720") |
| 77 | log("boot", [sys.executable, tests / "disk-root.py", arch, root, fixture, |
| 78 | args.iso, args.boot, disk], env=env) |
| 79 | files = sorted(work.glob("*.log")) + [fixture, disk / "guest/serial.log", disk / "receipts.sha256"] |
| 80 | with (work / "receipts.sha256").open("x") as report: |
| 81 | for file in files: |
| 82 | with file.open("rb") as stream: |
| 83 | report.write(f"{hashlib.file_digest(stream, 'sha256').hexdigest()} {file.relative_to(work)}\n") |
| 84 | |
| 85 | |
| 86 | if __name__ == "__main__": |
| 87 | parser = argparse.ArgumentParser(description=__doc__) |
| 88 | parser.add_argument("arch", choices=("x86_64", "aarch64")) |
| 89 | parser.add_argument("--scenario", choices=("all", "install", "upgrade"), default="all") |
| 90 | parser.add_argument("--package-command", choices=("vpac", "pkg"), default="vpac") |
| 91 | parser.add_argument("--staged-root", type=lambda path: Path(path).resolve(), |
| 92 | help="reuse a staged root from the same feed and build volume") |
| 93 | for option in ("repo", "keys", "fingerprint", "update_receipt", "auditor", "boot", "iso", "work"): |
| 94 | parser.add_argument(option, type=lambda path: Path(path).resolve()) |
| 95 | run(parser.parse_args()) |
| 96 | |