gunk-dev/the-valley

valley-walk.sh

source58e68be0d151main's published tip history
18.1 KiB
1# shellcheck shell=bash
2# valley review with no change, end to end and unmocked, over the
3# projects one test host serves: the walk through what needs the operator,
4# across projects, in landing order, through the packaged CLI. The host is
5# made by the integrator host's rendered valley-init, as valley-changes
6# makes it, so discovery reads the project list a host publishes and every
7# record and request is pushed through the hook that host installs.
8# Paths and fixtures come from the derivation environment.
9# shellcheck disable=SC2154
10set -euo pipefail
11
12w="$TMPDIR/valley-walk"
13mkdir -p "$w"
14export HOME="$w" XDG_CACHE_HOME="$w/cache" XDG_STATE_HOME="$w/state" TERM=xterm
15export GIT_CONFIG_NOSYSTEM=1
16# Nobody is anybody by default: a commit made where no identity is set
17# fails, so a review in a cache clone commits only with the identity the
18# walk lends it from the checkout.
19git config --global user.useConfigOnly true
20git config --global init.defaultBranch main
21git config --global advice.detachedHead false
22unset VALLEY_KNOWN_SIGNERS VALLEY_INTEGRATOR_SIGNER VISUAL || true
23
24fail() {
25 printf 'valley-walk: %s\n' "$*" >&2
26 for f in "$w"/out "$w"/err; do
27 if [ -s "$f" ]; then cat "$f" >&2; fi
28 done
29 exit 1
30}
31
32ok() {
33 "$@" > "$w/out" 2> "$w/err" || fail "$* failed"
34}
35
36line() {
37 grep -qE -- "$1" "$w/out" || fail "no line matches $1"
38}
39
40no_line() {
41 if grep -qE -- "$1" "$w/out"; then fail "a line matches $1"; fi
42}
43
44# before <ERE> <ERE>: the first line matching the first comes before the
45# first line matching the second.
46before() {
47 local a b
48 a="$(grep -nE -m 1 -- "$1" "$w/out" | cut -d: -f1 || true)"
49 b="$(grep -nE -m 1 -- "$2" "$w/out" | cut -d: -f1 || true)"
50 [ -n "$a" ] && [ -n "$b" ] && [ "$a" -lt "$b" ] || fail "$1 does not come before $2"
51}
52
53# walk <status> <answers> [<flag>...]: valley review with no change, from
54# the open checkout, answering from a pipe; it exits with <status>.
55walk() {
56 local want="$1" answers="$2" status=0
57 shift 2
58 printf '%b' "$answers" | valley review "$@" > "$w/out" 2> "$w/err" || status=$?
59 [ "$status" = "$want" ] || fail "the walk exited $status, not $want"
60}
61
62# wait_for <file> <ERE>: wait until the file shows a line matching it. The
63# walk's output is a file a child writes, which a shell has no event for,
64# so it is read again every tenth of a second, for a minute at most.
65wait_for() {
66 local i
67 for i in $(seq 600); do
68 if grep -qE -- "$2" "$1" 2> /dev/null; then return 0; fi
69 sleep 0.1
70 done
71 fail "$1 never showed $2"
72}
73
74# The host: guarded, open, released and sealed, with sealed the instance
75# repository, as valley-changes makes it. sealed's main carries the floor
76# approving derives the policy from.
77data="$w/srv/git"
78mkdir -p "$data"
79sed "s|/srv/git|$data|g" "$initScriptPath" > "$w/init.sh"
80bash "$w/init.sh" 2> "$w/init.err" || fail "valley-init failed: $(cat "$w/init.err")"
81instance="$data/sealed.git"
82for name in guarded open released sealed; do
83 git -C "$data/$name.git" config --unset core.sharedRepository || true
84done
85git init --quiet "$w/floor"
86mkdir -p "$w/floor/policy/instance"
87cp "$floor"/*.cue "$w/floor/policy/instance/"
88git -C "$w/floor" add policy
89git -C "$w/floor" -c user.name=integrator -c user.email=integrator@localhost commit --quiet -m floor
90git -C "$w/floor" push --quiet "$instance" HEAD:refs/heads/floor
91git -C "$instance" update-ref refs/heads/main "$(git -C "$w/floor" rev-parse HEAD)"
92
93id() { printf '%s%s' "$1" "$(printf 'k%.0s' $(seq $((32 - ${#1}))))"; }
94G1="$(id kmkm)" G2="$(id knkn)" O1="$(id popo)" S1="$(id sqsq)" S2="$(id srsr)"
95M1="$(id mlml)" M2="$(id mnmn)" M3="$(id mpmp)"
96
97# A checkout of a project whose main names sealed as its owning valley,
98# with alice's identity in its own configuration alone.
99export VALLEY_PRINCIPAL=alice
100checkout() {
101 local dir="$w/$1"
102 git clone --quiet "$data/$1.git" "$dir" 2> /dev/null
103 git -C "$dir" config valley.principal alice
104 git -C "$dir" config user.name alice
105 git -C "$dir" config user.email alice@example.org
106 mkdir -p "$dir/policy"
107 printf '%s\n' "$instance" > "$dir/policy/valley"
108 git -C "$dir" add policy/valley
109 git -C "$dir" commit --quiet -m base
110 VALLEY_PRINCIPAL=integrator git -C "$dir" push --quiet origin main
111}
112
113# branch <name> <from> <subject…>: a branch with a commit per subject,
114# pushed to origin.
115branch() {
116 local name="$1" from="$2" s
117 shift 2
118 git checkout --quiet -b "$name" "$from"
119 for s in "$@"; do
120 echo "$s" > "$s.txt"
121 git add "$s.txt"
122 git commit --quiet -m "$s"
123 done
124 git push --quiet origin "$name"
125}
126
127# ready <id> <branch>: the branch readied as the change, each a second
128# after the last, so the landing order is the order they are readied in.
129at=1700000000
130ready() {
131 at=$((at + 1))
132 GIT_COMMITTER_DATE="@$at +0000" ok valley ready --change "$1" "$2"
133}
134
135checkout open
136checkout guarded
137cd "$w/guarded"
138branch g-one main g-one
139ready "$G1" g-one
140branch g-two main g-two
141ready "$G2" g-two
142cd "$w/open"
143branch o-one main o-one
144ready "$O1" o-one
145
146# ----------------------------------------------------------------------
147# Three ready changes in two projects, guarded's read through the walk's
148# cache clone: the first approved and the second rejected there, the
149# third skipped. Each step is headed by its place, project, change and
150# checks, and the summary names every outcome, then what still needs the
151# operator: the rejected change is withdrawn, and pending lists it no more.
152walk 0 'a\nr\nyes\ns\n'
153line '^needs you \(3, in landing order\)$'
154before '^\[1/3\] guarded · kmkm v1 · 1 check pending$' '^\[2/3\] guarded · knkn v1 · 1 check pending$'
155before '^\[2/3\] guarded · knkn v1' '^\[3/3\] open · popo v1 · 1 check pending$'
156line 'rejected g-two: deleted from origin · knkn withdrawn$'
157line '^ ✓ guarded kmkm v1 approved$'
158line '^ ✓ guarded knkn v1 rejected · branch deleted$'
159line '^ ◇ open popo v1 skipped$'
160line '^still needs you \(1\): open popo$'
161no_line 'nothing needs you'
162[ "$(git -C "$data/guarded.git" rev-parse "refs/the-valley/integration-requests/main/c-$G1")" = \
163 "$(git -C "$w/guarded" rev-parse g-one)" ] || fail "the approval filed no request at kmkm's head"
164if git -C "$data/guarded.git" rev-parse --verify --quiet refs/heads/g-two > /dev/null; then
165 fail "the rejection left g-two on origin"
166fi
167ok valley pending --all
168no_line knkn
169clone="$(echo "$XDG_CACHE_HOME"/valley/repos/*-guarded)"
170[ -d "$clone" ] || fail "guarded was read through no cache clone"
171for key in valley.principal user.name user.email; do
172 if git -C "$clone" config --local --get "$key" > /dev/null; then fail "the walk wrote $key into the cache clone"; fi
173done
174
175# ----------------------------------------------------------------------
176# A stale stack in guarded, based from the walk with [b]ase onto main: the
177# rebase commits and writes records in the cache clone, as alice, with the
178# identity the walk lent it.
179cd "$w/guarded"
180git checkout --quiet main
181branch s-one main s-one
182ready "$S1" s-one
183branch s-two s-one s-two
184ready "$S2" s-two
185git checkout --quiet main
186echo moved > moved.txt
187git add moved.txt
188git commit --quiet -m "main moves"
189VALLEY_PRINCIPAL=integrator git push --quiet origin main
190cd "$w/open"
191walk 0 'b\nn\ns\n' --repo guarded
192line '^needs you \(1, in landing order\)$'
193line '^\[1/1\] guarded · srsr v1 · stack of 2 · 1 check pending$'
194line '^ ◇ guarded srsr v1 skipped$'
195tip="$(git -C "$data/guarded.git" rev-parse main)"
196for b in s-one s-two; do
197 git -C "$data/guarded.git" merge-base --is-ancestor "$tip" "$b" || fail "the base left $b off main"
198done
199cd "$w/guarded"
200ok valley resolve "$S2"
201[ "$(cut -f4 "$w/out")" = 2 ] || fail "the base wrote no second version of srsr: $(cat "$w/out")"
202
203# ----------------------------------------------------------------------
204# Changes that move after the walk read its queue: while popo's prompt
205# waits, mlml is unreadied, mnmn readied at a second version, and mpmp's
206# branch deleted. mlml and mpmp are not reviewed, and mnmn is said to have
207# moved, then reviewed and approved at its new version.
208cd "$w/open"
209branch m-one main m-one
210ready "$M1" m-one
211branch m-two main m-two
212ready "$M2" m-two
213branch m-three main m-three
214ready "$M3" m-three
215rm -f "$w/fifo"
216mkfifo "$w/fifo"
217valley review --repo open < "$w/fifo" > "$w/walk.out" 2> "$w/walk.err" &
218walker=$!
219exec 3> "$w/fifo"
220wait_for "$w/walk.out" '\[s\]kip'
221ok valley unready "$M1"
222git checkout --quiet m-two
223echo again > m-two.txt
224git commit --quiet -am "m-two again"
225git push --quiet origin m-two
226ready "$M2" m-two
227git push --quiet origin --delete m-three
228printf 's\n' >&3
229wait_for "$w/walk.out" 'moved to v2'
230printf 'a\n' >&3
231exec 3>&-
232status=0
233wait "$walker" || status=$?
234mv "$w/walk.out" "$w/out"
235mv "$w/walk.err" "$w/err"
236[ "$status" = 0 ] || fail "the walk with moving changes exited $status"
237line '^\[2/4\] open · mlml v1'
238line '^mlml is not reviewed: it is a draft$'
239line '^mnmn moved to v2 since the queue was read$'
240line '^ ⊘ open mlml v1 not reviewed · it is a draft$'
241line '^ ✓ open mnmn v1 approved v2$'
242line '^mpmp is not reviewed: its branch is gone from origin$'
243line '^ ⊘ open mpmp v1 not reviewed · its branch is gone from origin$'
244[ "$(git -C "$data/open.git" rev-parse "refs/the-valley/integration-requests/main/c-$M2")" = "$(git rev-parse m-two)" ] ||
245 fail "mnmn was not approved at its second version"
246
247# ----------------------------------------------------------------------
248# Ctrl-C at a step's prompt, on a terminal: the step is interrupted, the
249# summary so far is printed, and the walk exits 130. The diff is paged
250# first, by a less that quits since it fits on one screen.
251git config pager.diff 'less -FX'
252status=0
253# On a terminal the prompt's keys are styled, so its plain tail is waited
254# on. A walk that never gets there is ended rather than left waiting.
255{
256 wait_for "$w/session.log" '\]kip: '
257 printf '\003'
258 wait_for "$w/session.log" 'walked'
259} | timeout 300 script -qec "valley review --repo open" /dev/null > "$w/session.log" 2>&1 || status=$?
260sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' "$w/session.log" > "$w/out"
261[ "$status" = 130 ] || fail "the interrupted walk exited $status, not 130: $(cat "$w/out")"
262line '^walked \(1 of 2\)$'
263line '^ ◇ open popo v1 interrupted$'
264no_line 'still needs you|nothing needs you'
265git config --unset pager.diff
266
267# ----------------------------------------------------------------------
268# A project that does not read is said, and nothing says that nothing
269# needs the operator. An empty queue says that nothing does, with the
270# counts pending gives, and exits 0.
271mv "$data/released.git" "$data/released.moved"
272walk 1 '' --repo released
273line '^✗ released did not read: '
274no_line 'nothing needs you'
275mv "$data/released.moved" "$data/released.git"
276ok valley withdraw "$O1"
277ok valley withdraw "$M3"
278walk 0 '' --repo open
279line '^nothing needs you · 1 moving · 1 draft'
280
281# ----------------------------------------------------------------------
282# Two steps in different repositories, on a terminal, their answers read
283# from one file: an approval in guarded's cache clone, then a rejection
284# confirmed in the open checkout. Each step's pager runs in the step's
285# repository root and environment: the clone's is lent the checkout's
286# principal through GIT_CONFIG_COUNT, and the checkout's step carries
287# nothing of the clone's. A review that read ahead of its answer would
288# take the next step's.
289G3="$(id kvkv)" O2="$(id pqpq)" O3="$(id pxpx)" G4="$(id kxkx)" O4="$(id pzpz)" O5="$(id pwpw)"
290cd "$w/guarded"
291ok valley withdraw "$S2"
292ok valley withdraw "$S1"
293git checkout --quiet main
294branch v-one main v-one
295ready "$G3" v-one
296cd "$w/open"
297git checkout --quiet main
298branch q-one main q-one
299ready "$O2" q-one
300git checkout --quiet main
301printf '%s\n' '#!/bin/sh' \
302 'printf "%s %s %s\n" "$(pwd -P)" "$(git config --show-origin --get valley.principal | cut -f1)" "${GIT_CONFIG_COUNT:-none}" >> "$PAGER_LOG"' \
303 'exec cat' > "$w/pager.sh"
304chmod +x "$w/pager.sh"
305git config --global pager.diff "$w/pager.sh"
306printf 'a\nr\nyes\n' > "$w/answers"
307status=0
308{
309 wait_for "$w/session.log" 'nothing needs you|still needs you'
310} | PAGER_LOG="$w/pager.log" LESS=-FX timeout 300 \
311 script -qec "stty rows 200 cols 200; valley review < $w/answers" /dev/null > "$w/session.log" 2>&1 || status=$?
312sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' "$w/session.log" > "$w/out"
313git config --global --unset pager.diff
314[ "$status" = 0 ] || fail "the walk from one answers file exited $status"
315line '^ ✓ guarded kvkv v1 approved$'
316line '^ ✓ open pqpq v1 rejected · branch deleted$'
317[ "$(git -C "$data/guarded.git" rev-parse "refs/the-valley/integration-requests/main/c-$G3")" = \
318 "$(git -C "$w/guarded" rev-parse v-one)" ] || fail "the approval in the clone filed no request at kvkv's head"
319if git -C "$data/open.git" rev-parse --verify --quiet refs/heads/q-one > /dev/null; then
320 fail "the rejection left q-one on origin"
321fi
322printf '%s command line: 3\n%s file:.git/config none\n' "$(cd "$clone" && pwd -P)" "$(pwd -P)" > "$w/pager.want"
323cmp -s "$w/pager.log" "$w/pager.want" || fail "the steps' pagers ran as $(cat "$w/pager.log")"
324
325# ----------------------------------------------------------------------
326# A step that fails does not end the walk. While the first step's prompt
327# waits, the second step's branch is moved on guarded's origin and a stale
328# lock is left on the cache clone's remote-tracking ref for it. The step
329# passes the walk's valley check, and its review's fetch, the first thing
330# the review does, cannot update the ref and fails. The step ends in error
331# with git's message, and the third is reviewed.
332branch x-one main x-one
333ready "$O3" x-one
334cd "$w/guarded"
335git checkout --quiet main
336branch x-two main x-two
337ready "$G4" x-two
338cd "$w/open"
339branch z-one main z-one
340ready "$O4" z-one
341git checkout --quiet main
342rm -f "$w/fifo"
343mkfifo "$w/fifo"
344valley review < "$w/fifo" > "$w/walk.out" 2> "$w/walk.err" &
345walker=$!
346exec 3> "$w/fifo"
347wait_for "$w/walk.out" '\[s\]kip'
348git -C "$data/guarded.git" update-ref refs/heads/x-two refs/heads/main
349mkdir -p "$clone/.git/refs/remotes/origin"
350touch "$clone/.git/refs/remotes/origin/x-two.lock"
351printf 's\ns\n' >&3
352exec 3>&-
353status=0
354wait "$walker" || status=$?
355rm -f "$clone/.git/refs/remotes/origin/x-two.lock"
356mv "$w/walk.out" "$w/out"
357mv "$w/walk.err" "$w/err"
358[ "$status" = 1 ] || fail "the walk with a failing step exited $status, not 1"
359before '^\[2/3\] guarded · kxkx v1' '^\[3/3\] open · pzpz v1'
360line '^ ◇ open pxpx v1 skipped$'
361line '^ ✗ guarded kxkx v1 error'
362no_line 'could not read which valley'
363grep -q 'fetching ref refs/remotes/origin/x-two failed' "$w/err" || fail "the walk's stderr does not carry the review's fetch failure: $(cat "$w/err")"
364line '^ ◇ open pzpz v1 skipped$'
365
366# ----------------------------------------------------------------------
367# SIGTERM sent to the walk alone while a step's prompt waits ends the step
368# interrupted, and the walk as Ctrl-C does: the summary so far, exit 130,
369# and nothing of its own left in its TMPDIR.
370mkdir "$w/term-tmp"
371rm -f "$w/fifo"
372mkfifo "$w/fifo"
373TMPDIR="$w/term-tmp" valley review --repo open < "$w/fifo" > "$w/walk.out" 2> "$w/walk.err" &
374walker=$!
375exec 3> "$w/fifo"
376wait_for "$w/walk.out" '\[s\]kip'
377kill -TERM "$walker"
378status=0
379wait "$walker" || status=$?
380exec 3>&-
381mv "$w/walk.out" "$w/out"
382mv "$w/walk.err" "$w/err"
383[ "$status" = 130 ] || fail "the walk sent SIGTERM exited $status, not 130"
384line '^walked \(1 of 2\)$'
385line '^ ◇ open pxpx v1 interrupted$'
386no_line 'still needs you|nothing needs you'
387[ -z "$(ls -A "$w/term-tmp")" ] || fail "the walk left $(ls -A "$w/term-tmp") in its TMPDIR"
388
389# ----------------------------------------------------------------------
390# Ctrl-C while less pages a step's diff, on a terminal: less takes it and
391# quits (-K), and once it has, the step ends interrupted, the summary so
392# far is printed, the walk exits 130, and nothing of its own is left in
393# its TMPDIR.
394mkdir "$w/pager-tmp"
395status=0
396{
397 wait_for "$w/session.log" '\(END\)'
398 printf '\003'
399 wait_for "$w/session.log" 'walked'
400} | TMPDIR="$w/pager-tmp" LESS=-K timeout 300 script -qec "valley review --repo open" /dev/null > "$w/session.log" 2>&1 || status=$?
401sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' "$w/session.log" > "$w/out"
402[ "$status" = 130 ] || fail "the walk interrupted in its pager exited $status, not 130"
403line '^walked \(1 of 2\)$'
404line '^ ◇ open pxpx v1 interrupted$'
405no_line '\]kip: '
406[ -z "$(ls -A "$w/pager-tmp")" ] || fail "the walk left $(ls -A "$w/pager-tmp") in its TMPDIR"
407
408# ----------------------------------------------------------------------
409# Ctrl-C during a step's approval checks, sent to the walk's process group
410# as a terminal sends it: the checks stop, the step ends interrupted, the
411# walk exits 130, and nothing is left behind: no worktree, nothing in its
412# TMPDIR, no request. open's policy gates held/ on a check that holds until
413# it is stopped.
414ssh-keygen -q -t ed25519 -N "" -C valley-walk -f "$w/attest-key"
415mkdir -p policy/project
416printf '%s\n' 'package verification' '' 'project: {' ' checks: hold: {' ' runner: "command"' \
417 " command: \"echo started > $w/held && sleep 600\"" ' }' ' classes: held: {' \
418 ' paths: "held/**": true' ' requires: hold: true' ' }' '}' > policy/project/policy.cue
419git add policy/project/policy.cue
420git commit --quiet -m "a check that holds until it is stopped"
421VALLEY_PRINCIPAL=integrator git push --quiet origin main
422git checkout --quiet -b w-held main
423mkdir held
424echo held > held/readme.md
425git add held
426git commit --quiet -m "a change the held check gates"
427git push --quiet origin w-held
428ready "$O5" w-held
429git checkout --quiet main
430printf 's\ns\na\n' > "$w/answers"
431mkdir "$w/held-tmp"
432TMPDIR="$w/held-tmp" VALLEY_ATTEST_KEY="$w/attest-key" setsid valley review --repo open < "$w/answers" > "$w/out" 2> "$w/err" &
433walker=$!
434wait_for "$w/held" started
435kill -INT -- "-$walker"
436status=0
437wait "$walker" || status=$?
438[ "$status" = 130 ] || fail "the walk interrupted in its checks exited $status, not 130"
439line '^walked \(3 of 3\)$'
440line '^ ◇ open pwpw v1 interrupted$'
441[ -z "$(ls -A "$w/held-tmp")" ] || fail "the walk left $(ls -A "$w/held-tmp") in its TMPDIR"
442[ "$(git worktree list --porcelain | grep -c '^worktree ')" = 1 ] || fail "the checks' worktree outlived the walk"
443if git -C "$data/open.git" rev-parse --verify --quiet "refs/the-valley/integration-requests/main/c-$O5" > /dev/null; then
444 fail "an approval interrupted during its checks filed a request"
445fi
446
447touch "$out"