vinix-ports / tests / desktop-guest.py
98 lines · 84 sloc · 4.08 KB
Raw
1"""Exercise the packaged release PID 1 with the native Vinix first-boot desktop."""
2import argparse
3import hashlib
4import os
5import shutil
6import subprocess
7import tarfile
8from pathlib import Path
9
10
11# Published November 28 release, checked against each architecture's ISO.
12INITRAMFS = {
13 "x86_64": "8be0f76685936c66d40609f62ad7ad5d2b231e6778678618a445e7fe83ce54da",
14 "aarch64": "a32afaa82a33096256280086bacbc39873e36a2e3ecd81c5504960b9034ec4f9",
15}
16PROGRAMS = ("usr/bin/vinix-desktop", "usr/sbin/vinix-session", "usr/sbin/vinix-account",
17 "usr/sbin/vinix-authd", "usr/sbin/vinix-system")
18
19
20def digest(path):
21 with path.open("rb") as stream:
22 return hashlib.file_digest(stream, "sha256").hexdigest()
23
24
25def native_overlay(archive, arch, root):
26 if digest(archive) != INITRAMFS[arch]:
27 raise ValueError("release initramfs checksum mismatch")
28 copies = set()
29 with tarfile.open(archive, "r|gz") as members:
30 for item in members:
31 name = item.name.removeprefix("./")
32 if name not in PROGRAMS:
33 continue
34 if not item.isfile() or not item.mode & 0o111:
35 raise ValueError(f"expected a native executable at {name}")
36 target = root / name
37 with members.extractfile(item) as source, target.open("xb") as output:
38 shutil.copyfileobj(source, output)
39 target.chmod(0o755)
40 copies.add(name)
41 if copies != set(PROGRAMS):
42 raise ValueError(f"missing release programs: {set(PROGRAMS) - copies}")
43 record_native_programs(root)
44
45
46def record_native_programs(root):
47 watchdog = "usr/libexec/vinix-desktop-watchdog"
48 checksummed = ["sbin/init", *PROGRAMS]
49 if (root / watchdog).is_file():
50 checksummed.append(watchdog)
51 for name in checksummed:
52 program = root / name
53 if program.is_symlink() or not program.is_file() or not os.access(program, os.X_OK):
54 raise ValueError(f"expected an installed native executable at /{name}")
55 manifest = root / "desktop-programs.sha256"
56 with manifest.open("x") as output:
57 for name in checksummed:
58 output.write(f"{digest(root / name)} /{name}\n")
59
60
61def run(args):
62 work = args.work
63 work.mkdir()
64 client = work / "client"
65 tests = Path(__file__).resolve().parent
66
67 def log(name, argv, **kwargs):
68 with (work / (name + ".log")).open("x") as output:
69 subprocess.run(argv, stdout=output, stderr=subprocess.STDOUT, check=True, **kwargs)
70
71 log("install", ["feed", "install", args.keys, args.repo, args.arch, client, "--fakeroot",
72 "vinix-desktop-init", "vinix-accounts", "vinix-release", "netbase"])
73 log("audit", [args.auditor, args.arch, client])
74 root = work / "root"
75 shutil.copytree(client / "root", root, symlinks=True)
76 native_overlay(args.initramfs, args.arch, root)
77 shutil.copyfile(root / "desktop-programs.sha256", work / "desktop-programs.sha256")
78 log("compile", ["vinix-cc", "-Wall", "-Wextra", "-Werror", "-O2", tests / "userland/framebuffer.c",
79 "-o", root / "framebuffer-check"],
80 env={**os.environ, "VPAC_MAKEPKG_ROOT": str(args.sdk)})
81 helper = root / "usr/libexec/vinix-version-check"
82 helper.parent.mkdir(exist_ok=True)
83 with helper.open("x") as output:
84 output.write("#!/bin/sh\nexec /bin/sh /userland-check.sh\n")
85 helper.chmod(0o755)
86 mode = "--aarch64-fixture" if args.arch == "aarch64" else "--fixture"
87 log("boot", [args.boot, root, args.iso, work / "guest", mode, tests / "userland/desktop.sh"])
88 with (work / "receipts.sha256").open("x") as output:
89 for file in sorted([*work.glob("*.log"), work / "guest/serial.log", work / "desktop-programs.sha256"]):
90 output.write(f"{digest(file)} {file.relative_to(work)}\n")
91
92
93if __name__ == "__main__":
94 parser = argparse.ArgumentParser(description=__doc__)
95 parser.add_argument("arch", choices=INITRAMFS)
96 for name in ("keys", "repo", "auditor", "sdk", "initramfs", "boot", "iso", "work"):
97 parser.add_argument(name, type=lambda path: Path(path).resolve())
98 run(parser.parse_args())
99