| 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. The mirror is |
| 6 | # missing at first. The landing's event has to reach the bus anyway, at |
| 7 | # once, and the landing has to stay queued through the failed mirror push |
| 8 | # until systemd's retry publishes it. |
| 9 | { |
| 10 | pkgs, |
| 11 | lib, |
| 12 | self, |
| 13 | packages, |
| 14 | ... |
| 15 | }: |
| 16 | let |
| 17 | # Throwaway keys, made once at build time, and the signers file the |
| 18 | # controller is given. They hold no authority anywhere: the machine that |
| 19 | # trusts them exists for one test run. |
| 20 | keys = |
| 21 | pkgs.runCommand "valley-publish-e2e-keys" |
| 22 | { |
| 23 | nativeBuildInputs = [ |
| 24 | pkgs.openssh |
| 25 | packages.attest |
| 26 | ]; |
| 27 | } |
| 28 | '' |
| 29 | mkdir $out |
| 30 | for who in integrator contributor; do |
| 31 | ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$out/$who" |
| 32 | done |
| 33 | attest key --key "$out/contributor" --name contributor/attestations > "$out/known_signers" |
| 34 | ''; |
| 35 | |
| 36 | declaration = pkgs.writeText "valley-publish-e2e.cue" '' |
| 37 | package valley |
| 38 | |
| 39 | projects: { |
| 40 | // The instance repository, which carries the floor. |
| 41 | "instance": protection: refs: ["refs/heads/main"] |
| 42 | "project": { |
| 43 | protection: {} |
| 44 | mirrors: ["/srv/mirror.git"] |
| 45 | } |
| 46 | } |
| 47 | ''; |
| 48 | |
| 49 | # A floor that requires nothing, so a change lands on the strength of its |
| 50 | # delta applying, and the test needs no evidence and no nix evaluation. |
| 51 | floor = pkgs.writeText "floor.cue" '' |
| 52 | package verification |
| 53 | |
| 54 | floor: { |
| 55 | checks: {} |
| 56 | classes: {} |
| 57 | unclassified: {} |
| 58 | } |
| 59 | ''; |
| 60 | in |
| 61 | { |
| 62 | publish-e2e = pkgs.testers.runNixOSTest { |
| 63 | name = "valley-publish-e2e"; |
| 64 | nodes.host = { |
| 65 | imports = [ self.nixosModules.default ]; |
| 66 | environment.systemPackages = [ |
| 67 | pkgs.git |
| 68 | pkgs.natscli |
| 69 | pkgs.jq |
| 70 | ]; |
| 71 | services.valley = { |
| 72 | enable = true; |
| 73 | config = declaration; |
| 74 | authorizedKeys = [ (lib.fileContents "${keys}/contributor.pub") ]; |
| 75 | bus.enable = true; |
| 76 | integrator = { |
| 77 | enable = true; |
| 78 | signingKeyFile = "${keys}/integrator"; |
| 79 | knownSignersFile = "${keys}/known_signers"; |
| 80 | instanceProject = "instance"; |
| 81 | interval = "2s"; |
| 82 | }; |
| 83 | }; |
| 84 | }; |
| 85 | |
| 86 | testScript = '' |
| 87 | import json |
| 88 | |
| 89 | host.wait_for_unit("multi-user.target") |
| 90 | host.wait_for_unit("valley-publish@project.path") |
| 91 | host.wait_until_succeeds("test -d /srv/git/project.git/valley-publish-queue") |
| 92 | |
| 93 | # The repositories belong to the git user, so git reads them as it. |
| 94 | git = "sudo -u git git" |
| 95 | queue = "/srv/git/project.git/valley-publish-queue" |
| 96 | |
| 97 | def unit(prop): |
| 98 | return host.succeed(f"systemctl show -p {prop} --value valley-publish@project").strip() |
| 99 | |
| 100 | # Every payload on the bus, as published. |
| 101 | def payloads(): |
| 102 | out = host.succeed( |
| 103 | "n=$(nats --server nats://127.0.0.1:4222 stream info valley --json | jq .state.messages);" |
| 104 | " for i in $(seq 1 $n); do" |
| 105 | " nats --server nats://127.0.0.1:4222 stream get valley $i --json | jq -r .data | base64 -d; echo;" |
| 106 | " done" |
| 107 | ) |
| 108 | return [line for line in out.splitlines() if line.strip()] |
| 109 | |
| 110 | def events(): |
| 111 | return [json.loads(line) for line in payloads()] |
| 112 | |
| 113 | # The mirror does not exist yet, so the first push to it fails. |
| 114 | |
| 115 | # Two commits: the floor, which is main on both repositories, and a |
| 116 | # change one commit ahead of it. |
| 117 | commit = "git -c user.name=valley-check -c user.email=valley-check@localhost commit --quiet" |
| 118 | host.succeed( |
| 119 | "mkdir -p /tmp/work/policy/instance", |
| 120 | "cp ${floor} /tmp/work/policy/instance/floor.cue", |
| 121 | f"cd /tmp/work && git init --quiet --initial-branch=main && git add -A && {commit} -m floor", |
| 122 | f"cd /tmp/work && echo change > change.txt && git add -A && {commit} -m change", |
| 123 | ) |
| 124 | base = host.succeed("git -C /tmp/work rev-parse HEAD~1").strip() |
| 125 | change = host.succeed("git -C /tmp/work rev-parse HEAD").strip() |
| 126 | host.succeed("chown -R git:git /tmp/work") |
| 127 | |
| 128 | # Seeded by fetching into the served repositories. A fetch runs no |
| 129 | # hook, so nothing here is published, and the mirror and the bus see |
| 130 | # only what the landing does. |
| 131 | def seed(repo, refspec): |
| 132 | host.succeed(f"{git} -C /srv/git/{repo}.git fetch --quiet /tmp/work {refspec}") |
| 133 | |
| 134 | seed("instance", f"{base}:refs/heads/main") |
| 135 | seed("project", f"{base}:refs/heads/main") |
| 136 | host.fail(f"{git} -C /srv/mirror.git rev-parse --verify --quiet refs/heads/main") |
| 137 | # The request ref alone asks for the change to land. |
| 138 | seed("project", f"{change}:refs/the-valley/integration-requests/main/one") |
| 139 | |
| 140 | with subtest("the landing moves main"): |
| 141 | host.wait_until_succeeds( |
| 142 | f"test \"$({git} -C /srv/git/project.git rev-parse refs/heads/main)\" = {change}", timeout=120 |
| 143 | ) |
| 144 | |
| 145 | with subtest("a failed mirror push keeps the move queued and fails the run"): |
| 146 | host.wait_until_succeeds( |
| 147 | "journalctl -t valley-mirror | grep -q 'project: push to /srv/mirror.git FAILED'", timeout=60 |
| 148 | ) |
| 149 | host.wait_until_succeeds("test \"$(systemctl show -p SubState --value valley-publish@project)\" = auto-restart") |
| 150 | host.succeed(f"test -n \"$(ls -A {queue})\"") |
| 151 | |
| 152 | # The integrator publishes the event itself, so the mirror being down |
| 153 | # does not hold it back. Its payload is byte for byte the one the host |
| 154 | # publishes for a push of the same move. |
| 155 | with subtest("the landing is on the bus while the mirror is down"): |
| 156 | host.wait_until_succeeds( |
| 157 | "nats --server nats://127.0.0.1:4222 stream get valley --last-for valley.git.project.ref-updated" |
| 158 | ) |
| 159 | host.succeed(f"test -n \"$(ls -A {queue})\"") |
| 160 | moved = [p for p in payloads() if '"event":"ref-updated"' in p] |
| 161 | assert moved == [ |
| 162 | f'{{"event":"ref-updated","repo":"project","ref":"refs/heads/main","old":"{base}","new":"{change}"}}' |
| 163 | ], moved |
| 164 | |
| 165 | with subtest("systemd retries the push after a delay, and the retry reaches the mirror"): |
| 166 | host.succeed( |
| 167 | "mkdir /srv/mirror.git && chown git:git /srv/mirror.git", |
| 168 | f"{git} init --quiet --bare /srv/mirror.git", |
| 169 | ) |
| 170 | host.wait_until_succeeds( |
| 171 | f"test \"$({git} -C /srv/mirror.git rev-parse refs/heads/main)\" = {change}", timeout=90 |
| 172 | ) |
| 173 | host.wait_until_succeeds("journalctl -t valley-mirror | grep -q 'project: pushed to /srv/mirror.git'") |
| 174 | host.wait_until_succeeds(f"test -z \"$(ls -A {queue})\"") |
| 175 | host.wait_until_succeeds("test \"$(systemctl show -p ActiveState --value valley-publish@project)\" = inactive") |
| 176 | # One failed run, then one retry 30 seconds later: no respin. |
| 177 | restarts = unit("NRestarts") |
| 178 | assert restarts == "1", restarts |
| 179 | |
| 180 | with subtest("a move that cannot be deleted fails the run, which waits to retry"): |
| 181 | # A directory in the queue is a move rm -f cannot delete. |
| 182 | host.succeed(f"sudo -u git mkdir {queue}/stuck", f"sudo -u git touch {queue}/stuck/x") |
| 183 | host.wait_until_succeeds( |
| 184 | "journalctl -u valley-publish@project | grep -q 'cannot remove .*stuck'", timeout=60 |
| 185 | ) |
| 186 | host.wait_until_succeeds("test \"$(systemctl show -p SubState --value valley-publish@project)\" = auto-restart") |
| 187 | restarts = unit("NRestarts") |
| 188 | host.sleep(10) |
| 189 | assert unit("NRestarts") == restarts, "the drain restarted without waiting" |
| 190 | assert unit("SubState") == "auto-restart" |
| 191 | host.succeed("systemctl is-active valley-publish@project.path") |
| 192 | host.succeed(f"rm -r {queue}/stuck") |
| 193 | host.wait_until_succeeds( |
| 194 | "test \"$(systemctl show -p ActiveState --value valley-publish@project)\" = inactive", timeout=300 |
| 195 | ) |
| 196 | host.succeed(f"test -z \"$(ls -A {queue})\"") |
| 197 | |
| 198 | with subtest("the landing is one ref-updated event on the bus"): |
| 199 | moved = [e for e in events() if e["event"] == "ref-updated"] |
| 200 | assert moved == [ |
| 201 | {"event": "ref-updated", "repo": "project", "ref": "refs/heads/main", "old": base, "new": change} |
| 202 | ], moved |
| 203 | landed = [e for e in events() if e["event"] == "integration-succeeded"] |
| 204 | assert len(landed) == 1 and landed[0]["new"] == change, landed |
| 205 | ''; |
| 206 | }; |
| 207 | } |