gunk-dev/the-valley

publish-e2e.nix

source25d41084d6famain's published tip history
5.8 KiB
1# An integrator landing, published: one NixOS machine running the module
2# with a controller, the bus and a push mirror, and a change that lands by
3# integration rather than by push. The landing has to reach the mirror and
4# the bus exactly as a push does, though the integrator moves the ref with
5# update-ref under its own user, and post-receive never runs.
6{
7 pkgs,
8 lib,
9 self,
10 packages,
11 ...
12}:
13let
14 # Throwaway keys, made once at build time, and the signers file the
15 # controller is given. They hold no authority anywhere: the machine that
16 # trusts them exists for one test run.
17 keys =
18 pkgs.runCommand "valley-publish-e2e-keys"
19 {
20 nativeBuildInputs = [
21 pkgs.openssh
22 packages.attest
23 ];
24 }
25 ''
26 mkdir $out
27 for who in integrator contributor; do
28 ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$out/$who"
29 done
30 attest key --key "$out/contributor" --name contributor/attestations > "$out/known_signers"
31 '';
32
33 declaration = pkgs.writeText "valley-publish-e2e.cue" ''
34 package valley
35
36 projects: {
37 // The instance repository, which carries the floor.
38 "instance": protection: refs: ["refs/heads/main"]
39 "project": {
40 protection: {}
41 mirrors: ["/srv/mirror.git"]
42 }
43 }
44 '';
45
46 # A floor that requires nothing, so a change lands on the strength of its
47 # delta applying, and the test needs no evidence and no nix evaluation.
48 floor = pkgs.writeText "floor.cue" ''
49 package verification
50
51 floor: {
52 checks: {}
53 classes: {}
54 unclassified: {}
55 }
56 '';
57in
58{
59 publish-e2e = pkgs.testers.runNixOSTest {
60 name = "valley-publish-e2e";
61 nodes.host = {
62 imports = [ self.nixosModules.default ];
63 environment.systemPackages = [
64 pkgs.git
65 pkgs.natscli
66 pkgs.jq
67 ];
68 services.valley = {
69 enable = true;
70 config = declaration;
71 authorizedKeys = [ (lib.fileContents "${keys}/contributor.pub") ];
72 bus.enable = true;
73 integrator = {
74 enable = true;
75 signingKeyFile = "${keys}/integrator";
76 knownSignersFile = "${keys}/known_signers";
77 instanceProject = "instance";
78 interval = "2s";
79 };
80 };
81 };
82
83 testScript = ''
84 import json
85
86 host.wait_for_unit("multi-user.target")
87 host.wait_for_unit("valley-publish@project.path")
88 host.wait_until_succeeds("test -d /srv/git/project.git/valley-publish-queue")
89
90 # The repositories belong to the git user, so git reads them as it.
91 git = "sudo -u git git"
92
93 # The mirror, empty.
94 host.succeed(
95 "mkdir /srv/mirror.git && chown git:git /srv/mirror.git",
96 f"{git} init --quiet --bare /srv/mirror.git",
97 )
98
99 # Two commits: the floor, which is main on both repositories, and a
100 # change one commit ahead of it.
101 commit = "git -c user.name=valley-check -c user.email=valley-check@localhost commit --quiet"
102 host.succeed(
103 "mkdir -p /tmp/work/policy/instance",
104 "cp ${floor} /tmp/work/policy/instance/floor.cue",
105 f"cd /tmp/work && git init --quiet --initial-branch=main && git add -A && {commit} -m floor",
106 f"cd /tmp/work && echo change > change.txt && git add -A && {commit} -m change",
107 )
108 base = host.succeed("git -C /tmp/work rev-parse HEAD~1").strip()
109 change = host.succeed("git -C /tmp/work rev-parse HEAD").strip()
110 host.succeed("chown -R git:git /tmp/work")
111
112 # Seeded by fetching into the served repositories. A fetch runs no
113 # hook, so nothing here is published, and the mirror and the bus see
114 # only what the landing does.
115 def seed(repo, refspec):
116 host.succeed(f"{git} -C /srv/git/{repo}.git fetch --quiet /tmp/work {refspec}")
117
118 seed("instance", f"{base}:refs/heads/main")
119 seed("project", f"{base}:refs/heads/main")
120 host.fail(f"{git} -C /srv/mirror.git rev-parse --verify --quiet refs/heads/main")
121 # The request ref alone asks for the change to land.
122 seed("project", f"{change}:refs/the-valley/integration-requests/main/one")
123
124 with subtest("the landing moves main"):
125 host.wait_until_succeeds(
126 f"test \"$({git} -C /srv/git/project.git rev-parse refs/heads/main)\" = {change}", timeout=120
127 )
128
129 with subtest("the landing reaches the mirror, pushed as the git user"):
130 host.wait_until_succeeds(
131 f"test \"$({git} -C /srv/mirror.git rev-parse refs/heads/main)\" = {change}", timeout=60
132 )
133 host.wait_until_succeeds("journalctl -t valley-mirror | grep -q 'project: pushed to /srv/mirror.git'")
134 host.wait_until_succeeds("test -z \"$(ls -A /srv/git/project.git/valley-publish-queue)\"")
135
136 def events():
137 out = host.succeed(
138 "n=$(nats --server nats://127.0.0.1:4222 stream info valley --json | jq .state.messages);"
139 " for i in $(seq 1 $n); do"
140 " nats --server nats://127.0.0.1:4222 stream get valley $i --json | jq -r .data | base64 -d; echo;"
141 " done"
142 )
143 return [json.loads(line) for line in out.splitlines() if line.strip()]
144
145 with subtest("the landing is one ref-updated event on the bus"):
146 host.wait_until_succeeds(
147 "nats --server nats://127.0.0.1:4222 stream get valley --last-for valley.git.project.ref-updated"
148 )
149 moved = [e for e in events() if e["event"] == "ref-updated"]
150 assert moved == [
151 {"event": "ref-updated", "repo": "project", "ref": "refs/heads/main", "old": base, "new": change}
152 ], moved
153 landed = [e for e in events() if e["event"] == "integration-succeeded"]
154 assert len(landed) == 1 and landed[0]["new"] == change, landed
155 '';
156 };
157}