| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" |
| 5 | export GIT_CONFIG_NOSYSTEM=1 |
| 6 | git config --global user.name valley-check |
| 7 | git config --global user.email valley-check@localhost |
| 8 | git config --global init.defaultBranch main |
| 9 | cd "$TMPDIR" || exit 1 |
| 10 | |
| 11 | wait_for() { |
| 12 | for _ in $(seq 1 150); do |
| 13 | "$@" >/dev/null 2>&1 && return 0 |
| 14 | sleep 0.2 |
| 15 | done |
| 16 | echo "mirror-e2e: timed out waiting for: $*" >&2 |
| 17 | return 1 |
| 18 | } |
| 19 | heads() { git -C mirror.git for-each-ref --format='%(refname)' refs/heads; } |
| 20 | tags() { git -C mirror.git for-each-ref --format='%(refname)' refs/tags; } |
| 21 | mirror_main_is() { [ "$(git -C mirror.git rev-parse main)" = "$tip" ]; } |
| 22 | only_main() { [ "$(heads)" = refs/heads/main ]; } |
| 23 | queued() { find "$1/valley-publish-queue" -mindepth 1 -maxdepth 1 | wc -l; } |
| 24 | |
| 25 | # A primary repo wired exactly as valley-init wires it, with |
| 26 | # the real store paths followed from the rendered init script. |
| 27 | dispatch="$(grep -o '/nix/store/[^ ]*-valley-post-receive' "$initScriptPath" | head -n1)" |
| 28 | mhook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-mirror-pilot' "$initScriptPath" | head -n1)" |
| 29 | deadhook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-dead-mirror' "$initScriptPath" | head -n1)" |
| 30 | test -x "$dispatch" && test -x "$mhook" && test -x "$deadhook" |
| 31 | wire() { |
| 32 | mkdir -p "$1/hooks/post-receive.d" "$1/valley-publish-queue" |
| 33 | ln -s "$dispatch" "$1/hooks/post-receive" |
| 34 | ln -s "$2" "$1/hooks/post-receive.d/valley-mirrors" |
| 35 | } |
| 36 | |
| 37 | # The drains, followed from the rendered publish unit, and one run |
| 38 | # of a project's drain as the unit runs it: in the repository. |
| 39 | drains="$(sed -n 's|^ExecStart=\(.*\)/%i$|\1|p' "$publishUnitPath")" |
| 40 | test -x "$drains/mirror-pilot" && test -x "$drains/dead-mirror" && test -x "$drains/race" |
| 41 | drain() { (cd "$1.git" && "$drains/$1"); } |
| 42 | |
| 43 | # The mirror as it stands today: every head and tag of the |
| 44 | # primary, topic branches included. Seeded before the hook is |
| 45 | # wired, so nothing sweeps it out from under the setup. |
| 46 | git init --quiet --bare mirror-pilot.git |
| 47 | git init --quiet --bare mirror.git |
| 48 | git clone --quiet "$PWD/mirror-pilot.git" work |
| 49 | git -C work commit --quiet --allow-empty -m one |
| 50 | git -C work tag v1 |
| 51 | git -C work tag doomed |
| 52 | for b in idea/one idea/two; do git -C work branch "$b"; done |
| 53 | git -C work push --quiet origin \ |
| 54 | '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*' |
| 55 | git -C mirror-pilot.git push --quiet ../mirror.git \ |
| 56 | '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*' |
| 57 | [ "$(heads | wc -l)" -eq 3 ] |
| 58 | wire mirror-pilot.git "$mhook" |
| 59 | |
| 60 | # A real push to the primary — a topic branch alongside main, |
| 61 | # and a tag deleted. The hook queues each push and pushes |
| 62 | # nothing; the drain then publishes everything queued at once. |
| 63 | git -C work commit --quiet --allow-empty -m two |
| 64 | git -C work tag -d doomed >/dev/null |
| 65 | git -C work branch idea/three |
| 66 | git -C work push --quiet --follow-tags origin main idea/three |
| 67 | git -C work push --quiet --delete origin doomed |
| 68 | tip="$(git -C work rev-parse HEAD)" |
| 69 | [ "$(queued mirror-pilot.git)" -eq 2 ] |
| 70 | [ "$(git -C mirror.git rev-parse main)" != "$tip" ] |
| 71 | drain mirror-pilot |
| 72 | [ "$(queued mirror-pilot.git)" -eq 0 ] |
| 73 | |
| 74 | # main and the tags are published; nothing else is, and the |
| 75 | # topic branches that were on the mirror are gone. |
| 76 | mirror_main_is |
| 77 | only_main |
| 78 | [ "$(tags)" = refs/tags/v1 ] |
| 79 | |
| 80 | # A branch that appears on the mirror by any other route is |
| 81 | # unpublished on the next push. |
| 82 | git -C mirror.git branch sneaky main |
| 83 | git -C work commit --quiet --allow-empty -m three |
| 84 | git -C work push --quiet origin main |
| 85 | tip="$(git -C work rev-parse HEAD)" |
| 86 | drain mirror-pilot |
| 87 | mirror_main_is |
| 88 | only_main |
| 89 | |
| 90 | # A move that cannot be deleted fails the drain, though the mirror |
| 91 | # push succeeded, so systemd retries it after a delay rather than |
| 92 | # the path unit restarting it at once. A directory in the queue is |
| 93 | # one rm -f cannot delete, whoever runs the check. |
| 94 | mkdir mirror-pilot.git/valley-publish-queue/stuck |
| 95 | touch mirror-pilot.git/valley-publish-queue/stuck/x |
| 96 | if drain mirror-pilot; then |
| 97 | echo "mirror-e2e: a drain that could not delete a move succeeded" >&2 |
| 98 | exit 1 |
| 99 | fi |
| 100 | [ "$(queued mirror-pilot.git)" -eq 1 ] |
| 101 | rm -r mirror-pilot.git/valley-publish-queue/stuck |
| 102 | |
| 103 | # An unreachable mirror costs the push nothing: the hook only |
| 104 | # queues, so the push returns promptly and succeeds. The drain |
| 105 | # fails, and the move stays queued for its retry. |
| 106 | git init --quiet --bare dead-mirror.git |
| 107 | wire dead-mirror.git "$deadhook" |
| 108 | git clone --quiet "$PWD/dead-mirror.git" deadwork |
| 109 | git -C deadwork commit --quiet --allow-empty -m one |
| 110 | timeout 60 git -C deadwork push --quiet origin main |
| 111 | [ "$(git -C dead-mirror.git rev-parse main)" = "$(git -C deadwork rev-parse HEAD)" ] |
| 112 | if drain dead-mirror; then |
| 113 | echo "mirror-e2e: a drain whose mirror push failed succeeded" >&2 |
| 114 | exit 1 |
| 115 | fi |
| 116 | [ "$(queued dead-mirror.git)" -eq 1 ] |
| 117 | |
| 118 | # Two moves of one project, published while one push stalls. git |
| 119 | # reads the local refs before it contacts the mirror, so a push |
| 120 | # that stalls between the two can force-push the old main it read |
| 121 | # over a newer one. The race mirror is an ssh URL, and this |
| 122 | # stand-in for ssh runs the remote git here. The first connection |
| 123 | # made while `hold` exists takes it and waits for `go`: that stalls |
| 124 | # one push in the window, every time. |
| 125 | racehook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-race' "$initScriptPath" | head -n1)" |
| 126 | pusher="$(grep -o '/nix/store/[^ ]*-valley-mirror-push-race' "$drains/race" | head -n1)" |
| 127 | test -x "$racehook" && test -x "$pusher" |
| 128 | cat >fake-ssh <<EOF |
| 129 | #!$(command -v sh) |
| 130 | if mv "$PWD/hold" "$PWD/held" 2>/dev/null; then |
| 131 | while [ ! -e "$PWD/go" ]; do sleep 0.1; done |
| 132 | fi |
| 133 | exec sh -c "\$2" |
| 134 | EOF |
| 135 | chmod +x fake-ssh |
| 136 | git init --quiet --bare race.git |
| 137 | mkdir race.git/valley-publish-queue |
| 138 | git init --quiet --bare race-mirror.git |
| 139 | git -C race.git config core.sshCommand "$PWD/fake-ssh" |
| 140 | git -C race.git config ssh.variant simple |
| 141 | git init --quiet racework |
| 142 | git -C racework commit --quiet --allow-empty -m old |
| 143 | git -C racework commit --quiet --allow-empty -m new |
| 144 | old="$(git -C racework rev-parse HEAD~1)" |
| 145 | new="$(git -C racework rev-parse HEAD)" |
| 146 | zero="$(printf '%040d' 0)" |
| 147 | # A fetch runs no hook, so the commits arrive unpublished. |
| 148 | git -C race.git fetch --quiet ../racework "+$new:refs/heads/main" |
| 149 | |
| 150 | # Moving main the way a push does: the ref, then the hook. |
| 151 | move_main() { |
| 152 | git -C race.git update-ref refs/heads/main "$2" |
| 153 | (cd race.git && printf '%s %s refs/heads/main\n' "$1" "$2" | "$racehook") |
| 154 | } |
| 155 | race_main_is() { [ "$(git -C race-mirror.git rev-parse --verify --quiet main)" = "$1" ]; } |
| 156 | race_reset() { |
| 157 | rm -f hold held go race.git/valley-publish-queue/* |
| 158 | git -C race-mirror.git update-ref -d refs/heads/main 2>/dev/null || true |
| 159 | git -C race.git update-ref refs/heads/main "$old" |
| 160 | } |
| 161 | # The pusher alone, as one of two publishers would run it. |
| 162 | pusher() { (cd race.git && "$pusher"); } |
| 163 | |
| 164 | # The negative control: two pushers at once. The one that read |
| 165 | # the old main stalls, the other pushes the new main, and the |
| 166 | # first then rewinds the mirror to the old one. |
| 167 | race_reset |
| 168 | touch hold |
| 169 | pusher & |
| 170 | stalled=$! |
| 171 | wait_for test -e held |
| 172 | git -C race.git update-ref refs/heads/main "$new" |
| 173 | pusher |
| 174 | race_main_is "$new" |
| 175 | touch go |
| 176 | wait "$stalled" |
| 177 | race_main_is "$old" |
| 178 | |
| 179 | # The single publisher, in the same ordering. The drain lists the |
| 180 | # queue, git reads the old main, and the push stalls. main moves |
| 181 | # on and its move is queued. The drain pushes the old main and |
| 182 | # deletes only the move it listed, so the newer move stays queued. |
| 183 | # On a host the path unit starts the next run as this one exits, |
| 184 | # never during it, and that run publishes the new main. |
| 185 | race_reset |
| 186 | move_main "$zero" "$old" |
| 187 | touch hold |
| 188 | drain race & |
| 189 | stalled=$! |
| 190 | wait_for test -e held |
| 191 | move_main "$old" "$new" |
| 192 | touch go |
| 193 | wait "$stalled" |
| 194 | race_main_is "$old" |
| 195 | [ "$(queued race.git)" -eq 1 ] |
| 196 | drain race |
| 197 | race_main_is "$new" |
| 198 | [ "$(queued race.git)" -eq 0 ] |
| 199 | |
| 200 | touch "$out" |