gunk-dev/the-valley

mirror-e2e.sh

source60ad80d5a5a5main's published tip history
10.3 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" ]; }
22queued() { 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.
26dispatch="$(grep -o '/nix/store/[^ ]*-valley-post-receive' "$initScriptPath" | head -n1)"
27mhook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-mirror-pilot' "$initScriptPath" | head -n1)"
28deadhook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-dead-mirror' "$initScriptPath" | head -n1)"
29test -x "$dispatch" && test -x "$mhook" && test -x "$deadhook"
30wire() {
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.
38drains="$(sed -n 's|^ExecStart=\(.*\)/%i$|\1|p' "$publishUnitPath")"
39test -x "$drains/mirror-pilot" && test -x "$drains/dead-mirror" && test -x "$drains/race"
40drain() { (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.
45git init --quiet --bare mirror-pilot.git
46git init --quiet --bare mirror.git
47git clone --quiet "$PWD/mirror-pilot.git" work
48git -C work commit --quiet --allow-empty -m one
49git -C work tag v1
50git -C work tag doomed
51for b in idea/one idea/two; do git -C work branch "$b"; done
52git -C work push --quiet origin \
53 '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*'
54git -C mirror-pilot.git push --quiet ../mirror.git \
55 '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*'
56[ "$(heads | wc -l)" -eq 3 ]
57wire 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.
62git -C work commit --quiet --allow-empty -m two
63git -C work tag -d doomed >/dev/null
64git -C work branch idea/three
65git -C work push --quiet --follow-tags origin main idea/three
66git -C work push --quiet --delete origin doomed
67tip="$(git -C work rev-parse HEAD)"
68[ "$(queued mirror-pilot.git)" -eq 2 ]
69[ "$(git -C mirror.git rev-parse main)" != "$tip" ]
70drain 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.
77mirror_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.
84prev="$tip"
85git -C mirror.git branch sneaky "$prev"
86git -C mirror.git update-ref refs/other/data "$prev"
87git -C work commit --quiet --allow-empty -m three
88git -C work push --quiet origin main
89tip="$(git -C work rev-parse HEAD)"
90drain mirror-pilot
91mirror_main_is
92[ "$(git -C mirror.git rev-parse --verify --quiet sneaky)" = "$prev" ]
93[ "$(git -C mirror.git rev-parse --verify --quiet refs/other/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.
99mkdir mirror-pilot.git/valley-publish-queue/stuck
100touch mirror-pilot.git/valley-publish-queue/stuck/x
101if drain mirror-pilot; then
102 echo "mirror-e2e: a drain that could not delete a move succeeded" >&2
103 exit 1
104fi
105[ "$(queued mirror-pilot.git)" -eq 1 ]
106rm -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.
111git init --quiet --bare dead-mirror.git
112wire dead-mirror.git "$deadhook"
113git clone --quiet "$PWD/dead-mirror.git" deadwork
114git -C deadwork commit --quiet --allow-empty -m one
115timeout 60 git -C deadwork push --quiet origin main
116[ "$(git -C dead-mirror.git rev-parse main)" = "$(git -C deadwork rev-parse HEAD)" ]
117if drain dead-mirror; then
118 echo "mirror-e2e: a drain whose mirror push failed succeeded" >&2
119 exit 1
120fi
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.
130racehook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-race' "$initScriptPath" | head -n1)"
131pusher="$(grep -o '/nix/store/[^ ]*-valley-mirror-push-race' "$drains/race" | head -n1)"
132test -x "$racehook" && test -x "$pusher"
133cat >fake-ssh <<EOF
134#!$(command -v sh)
135if mv "$PWD/hold" "$PWD/held" 2>/dev/null; then
136 while [ ! -e "$PWD/go" ]; do sleep 0.1; done
137fi
138exec sh -c "\$2"
139EOF
140chmod +x fake-ssh
141git init --quiet --bare race.git
142mkdir race.git/valley-publish-queue
143git init --quiet --bare race-mirror.git
144git -C race.git config core.sshCommand "$PWD/fake-ssh"
145git -C race.git config ssh.variant simple
146git init --quiet racework
147git -C racework commit --quiet --allow-empty -m old
148git -C racework commit --quiet --allow-empty -m new
149old="$(git -C racework rev-parse HEAD~1)"
150new="$(git -C racework rev-parse HEAD)"
151zero="$(printf '%040d' 0)"
152# A fetch runs no hook, so the commits arrive unpublished.
153git -C race.git fetch --quiet ../racework "+$new:refs/heads/main"
154
155# Moving main the way a push does: the ref, then the hook.
156move_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}
160race_main_is() { [ "$(git -C race-mirror.git rev-parse --verify --quiet main)" = "$1" ]; }
161race_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.
167pusher() { (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.
172race_reset
173touch hold
174pusher &
175stalled=$!
176wait_for test -e held
177git -C race.git update-ref refs/heads/main "$new"
178pusher
179race_main_is "$new"
180touch go
181wait "$stalled"
182race_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.
190race_reset
191move_main "$zero" "$old"
192touch hold
193drain race &
194stalled=$!
195wait_for test -e held
196move_main "$old" "$new"
197touch go
198wait "$stalled"
199race_main_is "$old"
200[ "$(queued race.git)" -eq 1 ]
201drain race
202race_main_is "$new"
203[ "$(queued race.git)" -eq 0 ]
204
205# A mirror declared on a project that already has a main. The rendered
206# valley-init script runs as the host runs it, with the data directory
207# moved into the sandbox. It queues one push, and the drain publishes
208# main with no move of the primary in between. The projects init
209# creates have no main to publish, and nothing is queued for them.
210data="$TMPDIR/srv/git"
211mkdir -p "$data"
212sed "s|/srv/git|$data|g" "$initScriptPath" >init.sh
213git init --quiet --bare "$data/mirror-pilot.git"
214git init --quiet --bare "$data/mirror.git"
215git -C work push --quiet "$data/mirror-pilot.git" main
216tip="$(git -C work rev-parse HEAD)"
217bash init.sh
218[ "$(queued "$data/mirror-pilot.git")" -eq 1 ]
219[ "$(queued "$data/dead-mirror.git")" -eq 0 ]
220
221# The mirror refuses the push, at length: its reason is the last of a
222# thousand lines, one long line and one that would erase a terminal's
223# line, and names a URL with a credential in its userinfo and another in
224# its query. The drain fails and the push stays queued for the unit's
225# retry. The drain's output, which is the unit's, gives the mirror's
226# reason in one bounded line, the URL's word dropped, with no credential
227# and no control character.
228printf '%s\n' "#!$(command -v sh)" 'seq 1000' 'head -c 3000 /dev/zero | tr "\0" x; echo' \
229 "printf '\\033[2Kerased\\n'" \
230 'echo "main is protected: see https://operator:s3cret-token@example.org/rules?access_token=s3cret-token"' \
231 'exit 1' >"$data/mirror.git/hooks/pre-receive"
232chmod +x "$data/mirror.git/hooks/pre-receive"
233if (cd "$data" && drain mirror-pilot) 2>refused.err; then
234 echo "mirror-e2e: a drain whose mirror refused the push succeeded" >&2
235 exit 1
236fi
237cat refused.err
238[ "$(queued "$data/mirror-pilot.git")" -eq 1 ]
239refusal="$(grep -F 'main is protected: see […]' refused.err)" || {
240 echo "mirror-e2e: the refusal's reason is not in the drain's output" >&2
241 exit 1
242}
243if grep -q -e s3cret-token -e "$(printf '\033')" refused.err; then
244 echo "mirror-e2e: the drain's output carries a credential or a control character" >&2
245 exit 1
246fi
247[ "$(printf '%s\n' "$refusal" | wc -l)" -eq 1 ]
248[ "${#refusal}" -lt 1000 ]
249rm "$data/mirror.git/hooks/pre-receive"
250(cd "$data" && drain mirror-pilot)
251[ "$(git -C "$data/mirror.git" rev-parse main)" = "$tip" ]
252[ "$(queued "$data/mirror-pilot.git")" -eq 0 ]
253
254touch "$out"