| 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, so the landing also has to survive a failed mirror push |
| 7 | # and be published by the retry, which waits for the project's publish lock. |
| 8 | { |
| 9 | pkgs, |
| 10 | lib, |
| 11 | self, |
| 12 | packages, |
| 13 | ... |
| 14 | }: |
| 15 | let |
| 16 | # Throwaway keys, made once at build time, and the signers file the |
| 17 | # controller is given. They hold no authority anywhere: the machine that |
| 18 | # trusts them exists for one test run. |
| 19 | keys = |
| 20 | pkgs.runCommand "valley-publish-e2e-keys" |
| 21 | { |
| 22 | nativeBuildInputs = [ |
| 23 | pkgs.openssh |
| 24 | packages.attest |
| 25 | ]; |
| 26 | } |
| 27 | '' |
| 28 | mkdir $out |
| 29 | for who in integrator contributor; do |
| 30 | ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$out/$who" |
| 31 | done |
| 32 | attest key --key "$out/contributor" --name contributor/attestations > "$out/known_signers" |
| 33 | ''; |
| 34 | |
| 35 | declaration = pkgs.writeText "valley-publish-e2e.cue" '' |
| 36 | package valley |
| 37 | |
| 38 | projects: { |
| 39 | // The instance repository, which carries the floor. |
| 40 | "instance": protection: refs: ["refs/heads/main"] |
| 41 | "project": { |
| 42 | protection: {} |
| 43 | mirrors: ["/srv/mirror.git"] |
| 44 | } |
| 45 | } |
| 46 | ''; |
| 47 | |
| 48 | # A floor that requires nothing, so a change lands on the strength of its |
| 49 | # delta applying, and the test needs no evidence and no nix evaluation. |
| 50 | floor = pkgs.writeText "floor.cue" '' |
| 51 | package verification |
| 52 | |
| 53 | floor: { |
| 54 | checks: {} |
| 55 | classes: {} |
| 56 | unclassified: {} |
| 57 | } |
| 58 | ''; |
| 59 | in |
| 60 | { |
| 61 | publish-e2e = pkgs.testers.runNixOSTest { |
| 62 | name = "valley-publish-e2e"; |
| 63 | nodes.host = { |
| 64 | imports = [ self.nixosModules.default ]; |
| 65 | environment.systemPackages = [ |
| 66 | pkgs.git |
| 67 | pkgs.natscli |
| 68 | pkgs.jq |
| 69 | ]; |
| 70 | services.valley = { |
| 71 | enable = true; |
| 72 | config = declaration; |
| 73 | authorizedKeys = [ (lib.fileContents "${keys}/contributor.pub") ]; |
| 74 | bus.enable = true; |
| 75 | integrator = { |
| 76 | enable = true; |
| 77 | signingKeyFile = "${keys}/integrator"; |
| 78 | knownSignersFile = "${keys}/known_signers"; |
| 79 | instanceProject = "instance"; |
| 80 | interval = "2s"; |
| 81 | }; |
| 82 | }; |
| 83 | }; |
| 84 | |
| 85 | testScript = '' |
| 86 | import json |
| 87 | |
| 88 | host.wait_for_unit("multi-user.target") |
| 89 | host.wait_for_unit("valley-publish@project.path") |
| 90 | host.wait_until_succeeds("test -d /srv/git/project.git/valley-publish-queue") |
| 91 | |
| 92 | # The repositories belong to the git user, so git reads them as it. |
| 93 | git = "sudo -u git git" |
| 94 | queue = "/srv/git/project.git/valley-publish-queue" |
| 95 | lock = "/srv/git/project.git/valley-publish.flock" |
| 96 | |
| 97 | # The mirror does not exist yet, so the first push to it fails. |
| 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("a failed mirror push keeps the move queued and fails the run"): |
| 130 | host.wait_until_succeeds( |
| 131 | "journalctl -t valley-mirror | grep -q 'project: push to /srv/mirror.git FAILED'", timeout=60 |
| 132 | ) |
| 133 | # The move went to the bus, so it is marked sent, and it stays. |
| 134 | host.succeed(f"ls {queue} | grep -q '[.]sent$'") |
| 135 | |
| 136 | with subtest("the retry waits for the publish lock"): |
| 137 | # Held here, as the git user, between two runs: while the unit waits |
| 138 | # to restart, the lock is free. |
| 139 | host.succeed( |
| 140 | "systemd-run --unit=hold-publish-lock -p User=git" |
| 141 | f" ${pkgs.util-linux}/bin/flock {lock} ${pkgs.runtimeShell}" |
| 142 | " -c '${pkgs.coreutils}/bin/touch /tmp/publish-lock-held; exec ${pkgs.coreutils}/bin/sleep infinity'" |
| 143 | ) |
| 144 | host.wait_for_file("/tmp/publish-lock-held", timeout=30) |
| 145 | host.succeed( |
| 146 | "mkdir /srv/mirror.git && chown git:git /srv/mirror.git", |
| 147 | f"{git} init --quiet --bare /srv/mirror.git", |
| 148 | ) |
| 149 | # systemd, not the path unit, starts the retry, after its delay. |
| 150 | host.wait_until_succeeds( |
| 151 | "test \"$(systemctl show -p SubState --value valley-publish@project)\" = start", timeout=90 |
| 152 | ) |
| 153 | restarts = host.succeed("systemctl show -p NRestarts --value valley-publish@project").strip() |
| 154 | assert restarts == "1", restarts |
| 155 | host.sleep(3) |
| 156 | host.fail(f"{git} -C /srv/mirror.git rev-parse --verify --quiet refs/heads/main") |
| 157 | host.succeed(f"test -n \"$(ls -A {queue})\"") |
| 158 | host.succeed("systemctl stop hold-publish-lock") |
| 159 | |
| 160 | with subtest("the retry reaches the mirror, pushed as the git user"): |
| 161 | host.wait_until_succeeds( |
| 162 | f"test \"$({git} -C /srv/mirror.git rev-parse refs/heads/main)\" = {change}", timeout=60 |
| 163 | ) |
| 164 | host.wait_until_succeeds("journalctl -t valley-mirror | grep -q 'project: pushed to /srv/mirror.git'") |
| 165 | host.wait_until_succeeds(f"test -z \"$(ls -A {queue})\"") |
| 166 | host.wait_until_succeeds("test \"$(systemctl show -p ActiveState --value valley-publish@project)\" = inactive") |
| 167 | |
| 168 | def events(): |
| 169 | out = host.succeed( |
| 170 | "n=$(nats --server nats://127.0.0.1:4222 stream info valley --json | jq .state.messages);" |
| 171 | " for i in $(seq 1 $n); do" |
| 172 | " nats --server nats://127.0.0.1:4222 stream get valley $i --json | jq -r .data | base64 -d; echo;" |
| 173 | " done" |
| 174 | ) |
| 175 | return [json.loads(line) for line in out.splitlines() if line.strip()] |
| 176 | |
| 177 | # Once, though the drain ran twice: a retry does not resend to the bus. |
| 178 | with subtest("the landing is one ref-updated event on the bus"): |
| 179 | host.wait_until_succeeds( |
| 180 | "nats --server nats://127.0.0.1:4222 stream get valley --last-for valley.git.project.ref-updated" |
| 181 | ) |
| 182 | moved = [e for e in events() if e["event"] == "ref-updated"] |
| 183 | assert moved == [ |
| 184 | {"event": "ref-updated", "repo": "project", "ref": "refs/heads/main", "old": base, "new": change} |
| 185 | ], moved |
| 186 | landed = [e for e in events() if e["event"] == "integration-succeeded"] |
| 187 | assert len(landed) == 1 and landed[0]["new"] == change, landed |
| 188 | ''; |
| 189 | }; |
| 190 | } |