gunk-dev/the-valley

mirror-e2e.sh

source20b79cbd2afcmain's published tip history
6.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" ]; }
22only_main() { [ "$(heads)" = refs/heads/main ]; }
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"
32 ln -s "$dispatch" "$1/hooks/post-receive"
33 ln -s "$2" "$1/hooks/post-receive.d/valley-mirrors"
34}
35
36# The mirror as it stands today: every head and tag of the
37# primary, topic branches included. Seeded before the hook is
38# wired, so nothing sweeps it out from under the setup.
39git init --quiet --bare mirror-pilot.git
40git init --quiet --bare mirror.git
41git clone --quiet "$PWD/mirror-pilot.git" work
42git -C work commit --quiet --allow-empty -m one
43git -C work tag v1
44git -C work tag doomed
45for b in idea/one idea/two; do git -C work branch "$b"; done
46git -C work push --quiet origin \
47 '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*'
48git -C mirror-pilot.git push --quiet ../mirror.git \
49 '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*'
50[ "$(heads | wc -l)" -eq 3 ]
51wire mirror-pilot.git "$mhook"
52
53# A real push to the primary — a topic branch alongside main,
54# and a tag deleted. The hook replicates asynchronously, so
55# wait on main's new tip before judging the rest.
56git -C work commit --quiet --allow-empty -m two
57git -C work tag -d doomed >/dev/null
58git -C work branch idea/three
59git -C work push --quiet --follow-tags origin main idea/three
60git -C work push --quiet --delete origin doomed
61tip="$(git -C work rev-parse HEAD)"
62wait_for mirror_main_is
63
64# main and the tags are published; nothing else is, and the
65# topic branches that were on the mirror are gone. The sweep
66# is a second push, so it converges just after main lands.
67wait_for only_main
68[ "$(tags)" = refs/tags/v1 ]
69
70# A branch that appears on the mirror by any other route is
71# unpublished on the next push.
72git -C mirror.git branch sneaky main
73git -C work commit --quiet --allow-empty -m three
74git -C work push --quiet origin main
75tip="$(git -C work rev-parse HEAD)"
76wait_for mirror_main_is
77wait_for only_main
78
79# An unreachable mirror costs a log line, never the push: the
80# hook must return promptly and the push must succeed.
81git init --quiet --bare dead-mirror.git
82wire dead-mirror.git "$deadhook"
83git clone --quiet "$PWD/dead-mirror.git" deadwork
84git -C deadwork commit --quiet --allow-empty -m one
85timeout 60 git -C deadwork push --quiet origin main
86[ "$(git -C dead-mirror.git rev-parse main)" = "$(git -C deadwork rev-parse HEAD)" ]
87
88# Two publishers of one project. git reads the local refs before it
89# contacts the mirror, so a publisher that stalls between the two can
90# force-push the old main it read over a newer one. The race mirror is
91# an ssh URL, and this stand-in for ssh runs the remote git here. The
92# first connection made while `hold` exists takes it and waits for `go`:
93# that stalls one publisher in the window, every time.
94racehook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-race' "$initScriptPath" | head -n1)"
95pusher="$(grep -o '/nix/store/[^ ]*-valley-mirror-push-race' "$racehook" | head -n1)"
96test -x "$racehook" && test -x "$pusher"
97cat >fake-ssh <<EOF
98#!$(command -v sh)
99if mv "$PWD/hold" "$PWD/held" 2>/dev/null; then
100 while [ ! -e "$PWD/go" ]; do sleep 0.1; done
101fi
102exec sh -c "\$2"
103EOF
104chmod +x fake-ssh
105git init --quiet --bare race.git
106git init --quiet --bare race-mirror.git
107git -C race.git config core.sshCommand "$PWD/fake-ssh"
108git -C race.git config ssh.variant simple
109git init --quiet racework
110git -C racework commit --quiet --allow-empty -m old
111git -C racework commit --quiet --allow-empty -m new
112old="$(git -C racework rev-parse HEAD~1)"
113new="$(git -C racework rev-parse HEAD)"
114# A fetch runs no hook, so the commits arrive unpublished.
115git -C race.git fetch --quiet ../racework "+$new:refs/heads/main"
116
117set_main() { git -C race.git update-ref refs/heads/main "$1"; }
118race_main_is() { [ "$(git -C race-mirror.git rev-parse --verify --quiet main)" = "$1" ]; }
119race_reset() {
120 rm -f hold held go
121 git -C race-mirror.git update-ref -d refs/heads/main 2>/dev/null || true
122 set_main "$old"
123}
124# The pusher alone, as an unserialized publisher runs it.
125unlocked() { (cd race.git && "$pusher"); }
126# The real hook, as post-receive runs it: detached, under the lock.
127hooked() { (cd race.git && "$racehook" </dev/null 8>&-); }
128lock_free() { flock -n race.git/valley-publish.flock true; }
129
130# The negative control. Without the lock, the publisher that read the
131# old main stalls, the other pushes the new main, and the first then
132# rewinds the mirror to the old one.
133race_reset
134touch hold
135unlocked &
136stalled=$!
137wait_for test -e held
138set_main "$new"
139unlocked
140race_main_is "$new"
141touch go
142wait "$stalled"
143race_main_is "$old"
144
145# With the lock, the same ordering ends at the new main. The first
146# publisher stalls holding the lock; the second, started after main
147# moved, waits for it and pushes last.
148race_reset
149touch hold
150hooked
151wait_for test -e held
152set_main "$new"
153hooked
154sleep 1
155race_main_is ""
156touch go
157wait_for race_main_is "$new"
158wait_for lock_free
159race_main_is "$new"
160
161# A publisher takes the lock before it reads main. Hold the lock here,
162# start a publisher, and move main while it waits: it publishes the
163# main it finds once it holds the lock.
164race_reset
165exec 8>>race.git/valley-publish.flock
166flock 8
167hooked
168sleep 1
169race_main_is ""
170set_main "$new"
171flock -u 8
172exec 8>&-
173wait_for race_main_is "$new"
174wait_for lock_free
175race_main_is "$new"
176
177touch "$out"