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