gunk-dev/the-valley

mirror-e2e.sh

sourcedd20c0d5ac09main's published tip history
7.6 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export GIT_CONFIG_NOSYSTEM=1
6git config --global user.name valley-check
7git config --global user.email valley-check@localhost
8git config --global init.defaultBranch main
9cd "$TMPDIR" || exit 1
10
11wait_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}
19heads() { git -C mirror.git for-each-ref --format='%(refname)' refs/heads; }
20tags() { git -C mirror.git for-each-ref --format='%(refname)' refs/tags; }
21mirror_main_is() { [ "$(git -C mirror.git rev-parse main)" = "$tip" ]; }
22only_main() { [ "$(heads)" = refs/heads/main ]; }
23queued() { 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.
27dispatch="$(grep -o '/nix/store/[^ ]*-valley-post-receive' "$initScriptPath" | head -n1)"
28mhook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-mirror-pilot' "$initScriptPath" | head -n1)"
29deadhook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-dead-mirror' "$initScriptPath" | head -n1)"
30test -x "$dispatch" && test -x "$mhook" && test -x "$deadhook"
31wire() {
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.
39drains="$(sed -n 's|^ExecStart=\(.*\)/%i$|\1|p' "$publishUnitPath")"
40test -x "$drains/mirror-pilot" && test -x "$drains/dead-mirror" && test -x "$drains/race"
41drain() { (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.
46git init --quiet --bare mirror-pilot.git
47git init --quiet --bare mirror.git
48git clone --quiet "$PWD/mirror-pilot.git" work
49git -C work commit --quiet --allow-empty -m one
50git -C work tag v1
51git -C work tag doomed
52for b in idea/one idea/two; do git -C work branch "$b"; done
53git -C work push --quiet origin \
54 '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*'
55git -C mirror-pilot.git push --quiet ../mirror.git \
56 '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*'
57[ "$(heads | wc -l)" -eq 3 ]
58wire 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.
63git -C work commit --quiet --allow-empty -m two
64git -C work tag -d doomed >/dev/null
65git -C work branch idea/three
66git -C work push --quiet --follow-tags origin main idea/three
67git -C work push --quiet --delete origin doomed
68tip="$(git -C work rev-parse HEAD)"
69[ "$(queued mirror-pilot.git)" -eq 2 ]
70[ "$(git -C mirror.git rev-parse main)" != "$tip" ]
71drain 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.
76mirror_main_is
77only_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.
82git -C mirror.git branch sneaky main
83git -C work commit --quiet --allow-empty -m three
84git -C work push --quiet origin main
85tip="$(git -C work rev-parse HEAD)"
86drain mirror-pilot
87mirror_main_is
88only_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.
94mkdir mirror-pilot.git/valley-publish-queue/stuck
95touch mirror-pilot.git/valley-publish-queue/stuck/x
96if drain mirror-pilot; then
97 echo "mirror-e2e: a drain that could not delete a move succeeded" >&2
98 exit 1
99fi
100[ "$(queued mirror-pilot.git)" -eq 1 ]
101rm -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.
106git init --quiet --bare dead-mirror.git
107wire dead-mirror.git "$deadhook"
108git clone --quiet "$PWD/dead-mirror.git" deadwork
109git -C deadwork commit --quiet --allow-empty -m one
110timeout 60 git -C deadwork push --quiet origin main
111[ "$(git -C dead-mirror.git rev-parse main)" = "$(git -C deadwork rev-parse HEAD)" ]
112if drain dead-mirror; then
113 echo "mirror-e2e: a drain whose mirror push failed succeeded" >&2
114 exit 1
115fi
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.
125racehook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-race' "$initScriptPath" | head -n1)"
126pusher="$(grep -o '/nix/store/[^ ]*-valley-mirror-push-race' "$drains/race" | head -n1)"
127test -x "$racehook" && test -x "$pusher"
128cat >fake-ssh <<EOF
129#!$(command -v sh)
130if mv "$PWD/hold" "$PWD/held" 2>/dev/null; then
131 while [ ! -e "$PWD/go" ]; do sleep 0.1; done
132fi
133exec sh -c "\$2"
134EOF
135chmod +x fake-ssh
136git init --quiet --bare race.git
137mkdir race.git/valley-publish-queue
138git init --quiet --bare race-mirror.git
139git -C race.git config core.sshCommand "$PWD/fake-ssh"
140git -C race.git config ssh.variant simple
141git init --quiet racework
142git -C racework commit --quiet --allow-empty -m old
143git -C racework commit --quiet --allow-empty -m new
144old="$(git -C racework rev-parse HEAD~1)"
145new="$(git -C racework rev-parse HEAD)"
146zero="$(printf '%040d' 0)"
147# A fetch runs no hook, so the commits arrive unpublished.
148git -C race.git fetch --quiet ../racework "+$new:refs/heads/main"
149
150# Moving main the way a push does: the ref, then the hook.
151move_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}
155race_main_is() { [ "$(git -C race-mirror.git rev-parse --verify --quiet main)" = "$1" ]; }
156race_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.
162pusher() { (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.
167race_reset
168touch hold
169pusher &
170stalled=$!
171wait_for test -e held
172git -C race.git update-ref refs/heads/main "$new"
173pusher
174race_main_is "$new"
175touch go
176wait "$stalled"
177race_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.
185race_reset
186move_main "$zero" "$old"
187touch hold
188drain race &
189stalled=$!
190wait_for test -e held
191move_main "$old" "$new"
192touch go
193wait "$stalled"
194race_main_is "$old"
195[ "$(queued race.git)" -eq 1 ]
196drain race
197race_main_is "$new"
198[ "$(queued race.git)" -eq 0 ]
199
200touch "$out"