| 1 | """Upgrade a signed Vinix account seed over locally edited, locked test accounts.""" |
| 2 | import argparse |
| 3 | from pathlib import Path |
| 4 | import shutil |
| 5 | import stat |
| 6 | import subprocess |
| 7 | |
| 8 | |
| 9 | def command(*args): |
| 10 | subprocess.run(args, check=True) |
| 11 | |
| 12 | |
| 13 | def test(args): |
| 14 | args.work.mkdir() |
| 15 | src = args.work / "vinix-accounts" |
| 16 | src.mkdir() |
| 17 | recipe = src / "PKGBUILD.vile" |
| 18 | shutil.copyfile(args.recipe, recipe) |
| 19 | manifest = args.work / "manifest" |
| 20 | command("vpac", "--native-build", recipe, manifest) |
| 21 | old, = manifest.read_text().splitlines() |
| 22 | old_feed = args.work / "old-feed" |
| 23 | new_feed = args.work / "new-feed" |
| 24 | command("feed", "publish", args.keys, old_feed, args.arch, old) |
| 25 | client = args.work / "client" |
| 26 | command("feed", "install", args.keys, old_feed, args.arch, client, "--fakeroot", "vinix-accounts") |
| 27 | etc = client / "root/etc" |
| 28 | edited = {} |
| 29 | for name, entry in [("passwd", "fixture:x:45000:45000:Test:/nonexistent:/bin/false"), |
| 30 | ("group", "fixture:x:45000:"), ("shadow", "fixture:!:0:0:99999:7:::")]: |
| 31 | path = etc / name |
| 32 | with path.open("a") as output: |
| 33 | output.write(entry + "\n") |
| 34 | edited[name] = path.read_bytes() |
| 35 | # Only the private test snapshot changes the packaged default group. |
| 36 | text = recipe.read_text() |
| 37 | start = " #:package, fn() {" |
| 38 | if text.count(start) != 1 or text.count("#:pkgrel, 1,") != 1: |
| 39 | raise ValueError("update fixture must match the original recipe") |
| 40 | text = text.replace("#:pkgrel, 1,", "#:pkgrel, 2,") |
| 41 | recipe.write_text(text.replace(start, start + '\n run("sed", "-i", "$a vendor:x:45001:", "group")')) |
| 42 | command("vpac", "--native-build", recipe, manifest) |
| 43 | new, = manifest.read_text().splitlines() |
| 44 | command("feed", "publish", args.keys, new_feed, args.arch, new) |
| 45 | config = (client / "config.vile").read_text() |
| 46 | if config.count("/old-feed/") != 1: |
| 47 | raise ValueError("expected exactly one initial feed in client configuration") |
| 48 | upgrade_config = client / "upgrade.vile" |
| 49 | upgrade_config.write_text(config.replace("/old-feed/", "/new-feed/")) |
| 50 | command("fakeroot", "--", "vpac", "--config", upgrade_config, "u", "--sync", "--noconfirm") |
| 51 | for name, value in edited.items(): |
| 52 | if (etc / name).read_bytes() != value: |
| 53 | raise RuntimeError(f"local {name} entries changed on upgrade") |
| 54 | if stat.S_IMODE((etc / "shadow").stat().st_mode) != 0o600: |
| 55 | raise RuntimeError("installed shadow permissions changed") |
| 56 | if "vendor:x:45001:" not in (etc / "group.pacnew").read_text().splitlines(): |
| 57 | raise RuntimeError("new packaged group defaults were not preserved for review") |
| 58 | db = client / "db/local" |
| 59 | if not (db / "vinix-accounts-0.1.0-2").is_dir() or (db / "vinix-accounts-0.1.0-1").exists(): |
| 60 | raise RuntimeError("account seed version did not upgrade") |
| 61 | print("Account seed upgrade preserved local passwd, group, private shadow and the new group defaults.") |
| 62 | |
| 63 | |
| 64 | if __name__ == "__main__": |
| 65 | parser = argparse.ArgumentParser(description=__doc__) |
| 66 | for name in ["recipe", "keys", "work"]: |
| 67 | parser.add_argument(name, type=lambda path: Path(path).resolve()) |
| 68 | parser.add_argument("arch", choices=["x86_64", "aarch64"]) |
| 69 | test(parser.parse_args()) |
| 70 | |