gunk-dev/the-valley

restack-e2e.sh

source29699b69fbf8main's published tip history
9.9 KiB
1# shellcheck shell=bash
2# valley-restacker once --report-only, end to end over a real bare
3# repository wired with the protected host's rendered pre-receive hook.
4# Every change is readied with the repository's own valley as its owner;
5# the hook grants request to the principal requester, which files the
6# requests and is the principal the restacker counts.
7#
8# Outcome records are injected on the server side with update-ref,
9# because no push may write one: only the integrator writes them. Each
10# injection is marked where it is made. No scenario here runs the real
11# integrator.
12# Paths and fixtures come from the derivation environment.
13# shellcheck disable=SC2154
14set -euo pipefail
15
16w="$TMPDIR/restack-e2e"
17mkdir -p "$w"
18export HOME="$w"
19export GIT_CONFIG_NOSYSTEM=1
20git config --global user.name restack-e2e
21git config --global user.email restack-e2e@localhost
22git config --global init.defaultBranch main
23git config --global advice.detachedHead false
24
25fail() {
26 printf 'restack-e2e: %s\n' "$*" >&2
27 for f in "$w"/out "$w"/err; do
28 if [ -s "$f" ]; then cat "$f" >&2; fi
29 done
30 exit 1
31}
32
33origin="$w/project.git"
34hook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$initScriptPath" | head -n1)"
35test -x "$hook"
36git init --quiet --bare "$origin"
37cp "$hook" "$origin/hooks/pre-receive"
38chmod +x "$origin/hooks/pre-receive"
39
40repo="$w/project"
41git init --quiet "$repo"
42cd "$repo"
43git remote add origin "$origin"
44# The project layer caps the restacker at two automatic versions a change.
45mkdir -p policy/project
46printf 'package verification\n\nproject: restack: maxAutomaticVersions: 2\n' > policy/project/policy.cue
47echo base > readme
48git add readme policy
49git commit --quiet -m base
50env -u VALLEY_PRINCIPAL git push --quiet origin main
51
52as() {
53 local who="$1"
54 shift
55 git config valley.principal "$who"
56 VALLEY_PRINCIPAL="$who" "$@"
57}
58ok() { "$@" > "$w/out" 2> "$w/err" || fail "$* failed"; }
59
60# change <owner> <branch> <base>: a commit on base, pushed and readied by
61# its owner; prints the change ID.
62change() {
63 local who="$1" branch="$2" base="$3"
64 git checkout --quiet -B "$branch" "$base"
65 echo "$branch" > "$branch"
66 git add "$branch"
67 git commit --quiet -m "$branch"
68 as "$who" git push --quiet --force origin "$branch"
69 ok as "$who" valley ready
70 sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out"
71}
72# branch <name> <base>: a plain branch, which no record is on.
73branch() {
74 git checkout --quiet -B "$1" "$2"
75 echo "$1" > "$1"
76 git add "$1"
77 git commit --quiet -m "$1"
78 as alice git push --quiet origin "$1"
79}
80
81base="$(git rev-parse main)"
82a="$(change alice a "$base")"
83b1="$(change bob b1 "$base")"
84b2="$(change bob b2 b1)"
85c1="$(change alice c1 "$base")"
86c2="$(change alice c2 c1)"
87c3="$(change bob c3 c1)"
88d="$(change alice d "$base")"
89ok as alice valley unready "$d"
90wd="$(change bob wd "$base")"
91ok as bob valley withdraw "$wd"
92branch plain "$base"
93e="$(change alice e "$base")"
94branch e-plain e
95g1="$(change alice g1 "$base")"
96g2="$(change alice g2 g1)"
97ok as alice valley unready "$g1"
98u="$(change bob u "$base")"
99k="$(change requester k "$base")"
100git checkout --quiet k
101echo again >> k
102git commit --quiet -am again
103as requester git push --quiet origin k
104ok as requester valley ready --change "$k" k
105# s lands by fast-forward before main moves, so it is submitted.
106s="$(change alice s "$base")"
107env -u VALLEY_PRINCIPAL git push --quiet origin s:main
108git checkout --quiet main
109git reset --quiet --hard origin/main
110echo moved > moved
111git add moved
112git commit --quiet -m moved
113env -u VALLEY_PRINCIPAL git push --quiet origin main
114tip="$(git rev-parse main)"
115
116# Server-side injection of an outcome record under a request name.
117evidence() {
118 git -C "$origin" for-each-ref --format='%(refname) %(objectname)' refs/the-valley/attestations/ \
119 | LC_ALL=C sort | sha256sum | cut -c1-16
120}
121inject() {
122 local name="$1" line="$2" blob
123 blob="$(printf '%s\n' "$line" | git -C "$origin" hash-object -w --stdin)"
124 git -C "$origin" update-ref "refs/the-valley/integration-outcomes/main/$name" "$blob"
125}
126# u's request was judged failed, and no request is left: its landing is
127# uncertain.
128inject "c-$u" "$(git rev-parse u) $base $(evidence) failed 2026-10-18T00:00:00Z"
129
130# request <name> <head>: requester files an integration request.
131request() { as requester git push --quiet origin "$2:refs/the-valley/integration-requests/main/$1"; }
132
133refs() { git -C "$origin" for-each-ref --format='%(refname) %(objectname)' | LC_ALL=C sort; }
134plan() {
135 local before
136 before="$(refs)"
137 valley-restacker once --report-only --json --repo "$origin" --state "$w/state" \
138 --principal requester --instance "$smokePolicy/instance" "$@" > "$w/plan.json" 2> "$w/err" \
139 || fail "the planner failed"
140 [ "$(refs)" = "$before" ] || fail 'a --report-only pass changed a ref on origin'
141}
142# family <id>: the family plan a change is a layer of.
143family() { jq -c --arg id "$1" '.families[] | select(any(.layers[]; .id == $id))' "$w/plan.json"; }
144action() {
145 local got
146 got="$(family "$1" | jq -r .action)"
147 [ "$got" = "$2" ] || fail "the family of $1 is $got, not $2: $(family "$1")"
148}
149says() { family "$1" | jq -e --arg s "$2" 'any(.reasons[], .notes[]; contains($s))' > /dev/null \
150 || fail "the family of $1 does not say: $2: $(family "$1")"; }
151
152# ----------------------------------------------------------------------
153# Selection and families.
154plan
155jq -e --arg t "$tip" '.tip == $t and .maxAutomaticVersions == 2' "$w/plan.json" > /dev/null \
156 || fail "the plan does not read main's tip or the project's cap"
157action "$a" restack
158action "$b1" restack
159[ "$(family "$b1" | jq -c '[.layers[].id]')" = "[\"$b1\",\"$b2\"]" ] || fail 'b1 and b2 are not one family'
160[ "$(family "$c1" | jq '.layers | length')" = 3 ] || fail 'c1, c2 and c3 are not one family'
161[ "$(family "$c2" | jq -r '.layers[0].id')" = "$c1" ] || fail 'the shared lower layer is not the bottom'
162action "$c3" restack
163action "$d" leave
164action "$wd" leave
165action "$u" leave
166action "$e" hold
167says "$e" 'e-plain is a plain branch'
168action "$g2" hold
169says "$g2" 'is a draft'
170[ -z "$(family "$s")" ] || fail 'a submitted change was planned'
171jq -e '[.families[].layers[] | select(.name == "plain")] | length == 0' "$w/plan.json" > /dev/null \
172 || fail 'a lone plain branch was planned'
173
174# Capacity: k has two versions uploaded by requester, and a third is over
175# the project's cap of 2.
176action "$k" hold
177says "$k" 'over the cap of 2'
178family "$k" | jq -e '.layers[0].capacity.uploads == 3' > /dev/null || fail 'k does not count three uploads'
179
180# ----------------------------------------------------------------------
181# Request holds. A request by name with no outcome holds.
182request a a
183plan
184action "$a" hold
185says "$a" 'no outcome yet'
186family "$a" | jq -e '.requests[0].by == ["name", "head"]' > /dev/null || fail 'request a was not related by name'
187# A current stale releases.
188inject a "$(git rev-parse a) $tip $(evidence) stale"
189plan
190action "$a" restack
191# A mismatched head, tip or fingerprint holds.
192inject a "$base $tip $(evidence) stale"
193plan
194action "$a" hold
195says "$a" 'is about head'
196inject a "$(git rev-parse a) $base $(evidence) stale"
197plan
198says "$a" 'judged on main at'
199inject a "$(git rev-parse a) $tip 0000000000000000 stale"
200plan
201says "$a" 'judged on evidence'
202# A malformed outcome holds.
203inject a "not an outcome"
204plan
205says "$a" 'does not read'
206# A current reject releases, with a note.
207inject a "$(git rev-parse a) $tip $(evidence) reject"
208plan
209action "$a" restack
210says "$a" 'was rejected'
211
212# By an exact head, under a name nothing else relates.
213request zz-head "$(git rev-parse b2)"
214# By range: a commit on c2 no branch holds, so only the request names it
215# and the planner has to fetch it before reading its ancestry.
216git checkout --quiet -B scratch c2
217echo over > over
218git add over
219git commit --quiet -m over
220request zz-range "$(git rev-parse HEAD)"
221git checkout --quiet main
222git branch --quiet -D scratch
223plan
224action "$b1" hold
225family "$b1" | jq -e '.requests[] | select(.name == "zz-head") | .by == ["head"]' > /dev/null \
226 || fail 'zz-head was not related by head'
227action "$c1" hold
228family "$c1" | jq -e '.requests[] | select(.name == "zz-range") | .by == ["range"]' > /dev/null \
229 || fail 'zz-range was not related by range'
230action "$c3" hold
231
232# A request whose head is no commit cannot be related or not, so every
233# family it might be about holds. Injected on the server side, since the
234# hook takes no such request.
235git -C "$origin" update-ref refs/the-valley/integration-requests/main/zz-blob \
236 "$(echo blob | git -C "$origin" hash-object -w --stdin)"
237plan
238action "$a" hold
239says "$a" 'cannot be determined'
240
241# The human summary reads the same plan.
242valley-restacker once --report-only --repo "$origin" --state "$w/state" --principal requester \
243 --instance "$smokePolicy/instance" > "$w/out" 2> "$w/err" || fail 'the summary failed'
244grep -qE '^[k-z]+: hold$' "$w/out" || fail 'the summary holds no family'
245# And without --report-only, nothing runs.
246if valley-restacker once --repo "$origin" --state "$w/state" --principal requester \
247 --instance "$smokePolicy/instance" > "$w/out" 2> "$w/err"; then
248 fail 'once ran without --report-only'
249fi
250
251# ----------------------------------------------------------------------
252# A cap larger than any int. The schema admits it, so it composes, and the
253# planner counts it as the largest int: no practical cap. k, held above by
254# the cap of 2, is not held by this one. The request whose head is no
255# commit would hold k too, so it is removed first, on the server side
256# where it was injected.
257git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/zz-blob
258git checkout --quiet main
259printf 'package verification\n\nproject: restack: maxAutomaticVersions: 9223372036854775808\n' \
260 > policy/project/policy.cue
261git commit --quiet -am 'a cap larger than any int'
262env -u VALLEY_PRINCIPAL git push --quiet origin main
263plan
264grep -q '"maxAutomaticVersions": 9223372036854775807,' "$w/plan.json" \
265 || fail "the plan does not count the cap as the largest int: $(cat "$w/plan.json")"
266action "$k" restack
267
268touch "$out"