gunk-dev/the-valley

valley-changes.sh

source81eeeb107e7cmain's published tip history
16.0 KiB
1# shellcheck shell=bash
2# valley changes and valley pending, end to end and unmocked, over the
3# repositories one test host serves. The host is made by the integrator
4# host's rendered valley-init, relocated as init-e2e relocates it, so the
5# project list discovery reads is the one a host publishes, and every
6# record is pushed through the hook that host installs. What the
7# integrator writes on disk when it lands a change — main, the outcome
8# record, the transfer statement — is written here the same way, with a
9# key generated for the run.
10# Paths and fixtures come from the derivation environment.
11# shellcheck disable=SC2154
12set -euo pipefail
13
14w="$TMPDIR/valley-changes"
15mkdir -p "$w"
16export HOME="$w"
17export XDG_CACHE_HOME="$w/cache"
18export GIT_CONFIG_NOSYSTEM=1
19git config --global user.name valley-changes-check
20git config --global user.email valley-changes-check@localhost
21git config --global init.defaultBranch main
22git config --global advice.detachedHead false
23unset VALLEY_KNOWN_SIGNERS VALLEY_INTEGRATOR_SIGNER
24
25fail() {
26 printf 'valley-changes: %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
33# ok <command…>: the command succeeds, and its output is kept.
34ok() {
35 "$@" > "$w/out" 2> "$w/err" || fail "$* failed"
36}
37
38# holds <jq condition…>: the last JSON printed holds each.
39holds() {
40 local cond
41 for cond in "$@"; do
42 jq -e "$cond" "$w/out" > /dev/null || fail "$cond does not hold"
43 done
44}
45
46# line <ERE>: the last output has a line matching it.
47line() {
48 grep -qE -- "$1" "$w/out" || fail "no line matches $1"
49}
50
51no_line() {
52 if grep -qE -- "$1" "$w/out"; then fail "a line matches $1"; fi
53}
54
55# The host: guarded, open, released and sealed, with sealed the instance
56# repository. valley-init creates them, wires their hooks, and publishes
57# the project list in sealed.
58data="$w/srv/git"
59mkdir -p "$data"
60sed "s|/srv/git|$data|g" "$initScriptPath" > "$w/init.sh"
61bash "$w/init.sh" 2> "$w/init.err" || fail "valley-init failed: $(cat "$w/init.err")"
62instance="$data/sealed.git"
63# The integrator's repositories are group-shared, and git then sets the
64# setgid bit on every directory it makes, which this sandbox refuses. The
65# setting is dropped so the pushes below can write objects; it decides
66# nothing this check reads.
67for name in guarded open released sealed; do
68 git -C "$data/$name.git" config --unset core.sharedRepository || true
69done
70[ "$(git -C "$instance" cat-file blob refs/the-valley/projects)" = "$(printf '%s\n' guarded open released sealed)" ] ||
71 fail "valley-init published another project list"
72
73# A change's ID, by the letter it repeats. Each is 32 letters from k to z.
74id() { printf '%s%s' "$1" "$(printf 'k%.0s' $(seq $((32 - ${#1}))))"; }
75A="$(id nxvpwlkm)" B="$(id mmmm)" C="$(id pppp)" D="$(id qqqq)" E="$(id rrrr)"
76W="$(id ssss)" F="$(id tttt)" G="$(id uuuu)" U="$(id vvvv)" H="$(id wwww)"
77R="$(id nxvpwlkz)" D2="$(id xxxx)" E2="$(id yyyy)"
78
79# A checkout of a project, whose main names sealed as its owning valley.
80# Every push carries the principal, as the key sshd authenticated would.
81export VALLEY_PRINCIPAL=klaus-env
82checkout() {
83 local name="$1" dir="$w/$1"
84 git clone --quiet "$data/$name.git" "$dir" 2> /dev/null
85 git -C "$dir" config valley.principal klaus-env
86 mkdir -p "$dir/policy"
87 printf '# the owning valley\n%s\n' "$instance" > "$dir/policy/valley"
88 git -C "$dir" add policy/valley
89 git -C "$dir" commit --quiet -m base
90 VALLEY_PRINCIPAL=integrator git -C "$dir" push --quiet origin main
91}
92
93# branch <name> <from> <subject…>: a branch from a commit, with a commit
94# per subject, each adding a file of its own, pushed to origin.
95branch() {
96 local name="$1" from="$2" s
97 shift 2
98 git checkout --quiet -b "$name" "$from"
99 for s in "$@"; do
100 echo "$s" > "$s.txt"
101 git add "$s.txt"
102 git commit --quiet -m "$s"
103 done
104 git push --quiet origin "$name"
105}
106
107# ready <id> <branch>: the branch's head readied as the change.
108ready() {
109 ok valley ready --change "$1" "$2"
110}
111
112# ----------------------------------------------------------------------
113# open: a stack of three, a draft, a withdrawn change with a ready change
114# on top of it, a change landed by fast-forward, one landed as a new
115# commit with its transfer statement, one whose request was consumed with
116# no evidence stored, one whose transfer statement another key signed,
117# and a branch no record is on.
118checkout open
119cd "$w/open"
120base="$(git rev-parse HEAD)"
121branch stack-a "$base" stack-a
122ready "$A" stack-a
123branch stack-b stack-a stack-b
124ready "$B" stack-b
125branch stack-c stack-b stack-c
126ready "$C" stack-c
127branch paused "$base" paused
128ready "$D" paused
129ok valley unready "$D"
130branch gone "$base" gone
131ready "$E" gone
132ok valley withdraw "$E"
133branch atop-gone gone atop-gone
134ready "$W" atop-gone
135branch ff "$base" ff
136ready "$F" ff
137branch reparent "$base" reparent
138ready "$G" reparent
139branch lost "$base" lost
140ready "$U" lost
141branch forged "$base" forged
142ready "$H" forged
143branch loose "$base" loose
144
145# Before anything lands, ff names its change.
146ok valley-go resolve ff
147line "^change"$'\t'"$F"$'\t'
148
149# The integrator's keys: the integrator's own, and another a known
150# signer holds, which may sign evidence and not a landing.
151ssh-keygen -q -t ed25519 -N '' -C integrator -f "$w/integrator.key"
152ssh-keygen -q -t ed25519 -N '' -C mallory -f "$w/mallory.key"
153{
154 attest key --key "$w/integrator.key" --name integrator
155 attest key --key "$w/mallory.key" --name mallory
156} > "$w/known-signers"
157
158# F lands by fast-forward.
159VALLEY_PRINCIPAL=integrator git push --quiet origin ff:main
160fhead="$(git rev-parse ff)"
161
162# G lands as a new commit on main, and the integrator records it: main
163# moves, the outcome record says land, the request is consumed, and the
164# transfer statement is stored under the landed tree.
165git checkout --quiet -b landing ff
166git cherry-pick reparent > /dev/null
167landed="$(git rev-parse HEAD)"
168VALLEY_PRINCIPAL=integrator git push --quiet origin landing:main
169tree_digest() { attest digest --repo "$w/open" --rev "$1" | cut -d: -f2; }
170
171# memo <name> <head> <decision…>: the integrator's outcome record.
172memo() {
173 local name="$1" head="$2" blob
174 shift 2
175 blob="$(printf '%s %s %s %s\n' "$head" "$fhead" 0123456789abcdef "$*" | git -C "$data/open.git" hash-object -w --stdin)"
176 git -C "$data/open.git" update-ref "refs/the-valley/integration-outcomes/main/$name" "$blob"
177}
178
179# transfer <change> <head> <landed> <key> <name>: a transfer statement,
180# as the integrator composes, signs and stores one.
181transfer() {
182 local change="$1" head="$2" at="$3" key="$4" name="$5" ldigest blob tree
183 ldigest="$(tree_digest "$at")"
184 jq -n --arg change "$change" --arg base "$(tree_digest "$head")" --arg landed "$ldigest" \
185 --arg commit "$at" --arg tip "$fhead" '{
186 statementType: "the-valley/attestation/v1",
187 subject: {primary: "valley-tree-v1", digest: {"valley-tree-v1": $landed, "git-sha1": $commit}},
188 predicateType: "the-valley/integration/transfer/v1",
189 predicate: {change: $change, target: "refs/heads/main", base: {"valley-tree-v1": $base},
190 policy: {sha256: ("ab" * 32), "git-sha1": $tip}, required: "none"}
191 }' > "$w/statement.json"
192 attest render "$w/statement.json" | attest sign --key "$key" --name "$name" > "$w/transfer.note"
193 blob="$(git -C "$data/open.git" hash-object -w "$w/transfer.note")"
194 tree="$(printf '100644 blob %s\ttransfer.note\n' "$blob" | git -C "$data/open.git" mktree)"
195 git -C "$data/open.git" update-ref "refs/the-valley/attestations/$ldigest/$(printf '%08x' "${#name}")" "$tree"
196}
197memo reparent "$(git rev-parse reparent)" land
198transfer reparent "$(git rev-parse reparent)" "$landed" "$w/integrator.key" integrator
199
200# U's request was consumed, and no evidence was stored.
201memo lost "$(git rev-parse lost)" land
202# H's request was consumed too, and its transfer statement is signed by a
203# known key that is not the integrator's: it is no record of a landing.
204memo forged "$(git rev-parse forged)" land
205git checkout --quiet -b landing-h landing
206git cherry-pick forged > /dev/null
207VALLEY_PRINCIPAL=integrator git push --quiet origin landing-h:main
208transfer forged "$(git rev-parse forged)" "$(git rev-parse HEAD)" "$w/mallory.key" mallory
209# A's request is pending.
210git -C "$data/open.git" update-ref refs/the-valley/integration-requests/main/stack-a "$(git rev-parse stack-a)"
211
212# ----------------------------------------------------------------------
213# guarded: a ready change whose ID shares seven letters with A's, a draft
214# and a withdrawn change.
215checkout guarded
216cd "$w/guarded"
217gbase="$(git rev-parse HEAD)"
218branch g-ready "$gbase" g-ready
219ready "$R" g-ready
220branch g-draft "$gbase" g-draft
221ready "$D2" g-draft
222ok valley unready "$D2"
223branch g-gone "$gbase" g-gone
224ready "$E2" g-gone
225ok valley withdraw "$E2"
226
227# released is listed and cannot be read.
228mv "$data/released.git" "$data/released.moved"
229
230export VALLEY_KNOWN_SIGNERS="$w/known-signers" VALLEY_INTEGRATOR_SIGNER=integrator
231
232# ----------------------------------------------------------------------
233# One repository: every record in every state, as valley-changes/v1.
234cd "$w/open"
235ok valley changes --json
236[ "$(wc -l < "$w/out")" = 1 ] || fail "changes printed more than one line"
237holds '.schema == "valley-changes/v1"' '.host == "localhost"' '(.repos | length) == 1' \
238 '.repos[0].repo == "open"' ".repos[0].url == \"$data/open.git\"" '.repos[0].error == null' \
239 '(.repos[0].changes | length) == 10'
240c() { printf '.repos[0].changes[] | select(.change_id == "%s")' "$1"; }
241holds "$(c "$A") | .handle == \"nxvp\" and .state == \"ready\" and .owner == \"klaus-env\" and .target == \"main\" and .active == 1 and .seq == 1" \
242 "$(c "$A") | .record == \"$(git -C "$data/open.git" rev-parse "refs/the-valley/changes/main/$A")\"" \
243 "$(c "$A") | .versions[0] | .n == 1 and .title == \"stack-a\" and .branch == \"stack-a\" and .uploader == \"klaus-env\"" \
244 "$(c "$A") | .versions[0] | .head == \"$(git rev-parse stack-a)\" and .landing == \"not-landed\" and .landed_commit == null" \
245 "$(c "$A") | .versions[0].request == {ref: \"refs/the-valley/integration-requests/main/stack-a\", commit: \"$(git rev-parse stack-a)\"}" \
246 "$(c "$A") | .versions[0].outcome == null" \
247 "$(c "$D") | .state == \"draft\" and .seq == 2" \
248 "$(c "$E") | .state == \"withdrawn\""
249# Landing evidence, one version each way.
250holds "$(c "$F") | .versions[0] | .landing == \"landed\" and .landed_commit == \"$fhead\"" \
251 "$(c "$G") | .versions[0] | .landing == \"landed-recorded\" and .landed_commit == \"$landed\"" \
252 "$(c "$G") | .versions[0].outcome == {decision: \"land\", head: \"$(git rev-parse reparent)\", tip: \"$fhead\"}" \
253 "$(c "$G") | .versions[0].request == null" \
254 "$(c "$U") | .versions[0] | .landing == \"uncertain\" and .landed_commit == null" \
255 "$(c "$H") | .versions[0].landing == \"uncertain\""
256# includes is ancestry, nearest only.
257holds "$(c "$A") | .includes == []" "$(c "$B") | .includes == [\"$A\"]" \
258 "$(c "$C") | .includes == [\"$B\"]" "$(c "$W") | .includes == [\"$E\"]"
259# The one branch no record is on, and no branch main reaches.
260holds '.repos[0].branches == [{branch: "loose", head: "'"$(git rev-parse loose)"'"}]'
261
262# Without the registry's keys, the transfer statement is not believed, and
263# that is said.
264env -u VALLEY_KNOWN_SIGNERS valley changes --json > "$w/out" 2> "$w/err" || fail "changes failed without the keys"
265holds "$(c "$G") | .versions[0].landing == \"uncertain\""
266grep -q 'could not be verified' "$w/err" || fail "an unverified transfer statement was not reported"
267
268# ----------------------------------------------------------------------
269# Every project the host publishes. Handles are unique across both
270# repositories, the repository that cannot be read carries its error, and
271# the others are listed all the same.
272ok valley changes --json --all
273holds '[.repos[].repo] == ["guarded", "open", "released", "sealed"]' \
274 '.repos[] | select(.repo == "released") | .error != null and .changes == [] and .url == "'"$data/released.git"'"' \
275 '.repos[] | select(.repo != "released") | .error == null' \
276 '.repos[] | select(.repo == "sealed") | .changes == []' \
277 '[.repos[].changes[].handle] | length == (unique | length)' \
278 "[.repos[].changes[] | select(.change_id == \"$A\" or .change_id == \"$R\") | .handle] == [\"nxvpwlkz\", \"nxvpwlkm\"]" \
279 '.repos[] | select(.repo == "guarded") | [.changes[].state] == ["ready", "draft", "withdrawn"]'
280# --repo limits the listing to one repository, read through its clone.
281ok valley changes --json --repo guarded
282holds '[.repos[].repo] == ["guarded"]' "[.repos[0].changes[].change_id] == [\"$R\", \"$D2\", \"$E2\"]" \
283 '.repos[0].changes[0].handle == "nxvp"'
284cd "$w/guarded"
285ok valley changes --json
286holds '[.repos[].repo] == ["guarded"]' '.repos[0].changes[0].handle == "nxvp"'
287if valley changes --json --repo nowhere > "$w/out" 2> "$w/err"; then fail "--repo took a project the host does not list"; fi
288grep -q 'lists no project nowhere' "$w/err" || fail "--repo nowhere was refused without saying why"
289cd "$w/open"
290
291# A repository whose main names no owning valley lists itself alone, and
292# says discovery is unavailable.
293git init --quiet --bare "$w/solo.git"
294git clone --quiet "$w/solo.git" "$w/solo" 2> /dev/null
295git -C "$w/solo" commit --quiet --allow-empty -m solo
296git -C "$w/solo" push --quiet origin main
297(cd "$w/solo" && valley changes --json --all) > "$w/out" 2> "$w/err" || fail "changes --all failed without discovery"
298holds '[.repos[].repo] == ["solo"]'
299grep -q 'discovery is unavailable' "$w/err" || fail "changes --all did not say discovery is unavailable"
300
301# ----------------------------------------------------------------------
302# pending: the ready changes that have not landed, by handle, version and
303# title, then the branches no record is on, then the drafts.
304ok valley pending
305line '^nxvp v1 \| stack-a$'
306line '^mmmm v1 \| stack-b \| includes nxvp$'
307line '^pppp v1 \| stack-c \| includes mmmm$'
308line '^ssss v1 \| atop-gone \| includes rrrr$'
309line '^vvvv v1 \| lost \| uncertain$'
310line '^wwww v1 \| forged \| uncertain$'
311no_line '^(tttt|uuuu|rrrr|qqqq) v1 \|'
312line '^branches$'
313line '^loose \| loose \| .* \| ahead 1 \| 1 file changed'
314line '^drafts$'
315line '^qqqq v1 on paused \| paused \| .* \| ahead 1 \| '
316[ "$(grep -n '^branches$' "$w/out" | cut -d: -f1)" -lt "$(grep -n '^drafts$' "$w/out" | cut -d: -f1)" ] ||
317 fail "pending listed the drafts before the branches"
318# --json is the same listing in valley-changes/v1's shape.
319ok valley pending --json
320holds '.schema == "valley-pending/v1"' \
321 '[.repos[0].changes[].handle] == ["mmmm", "nxvp", "pppp", "qqqq", "ssss", "vvvv", "wwww"]' \
322 '.repos[0].branches == [{branch: "loose", head: "'"$(git rev-parse loose)"'"}]'
323# Across the host, each repository is headed by its name.
324ok valley pending --all
325line '^guarded$'
326line '^nxvpwlkz v1 \| g-ready$'
327line '^guarded drafts$'
328line '^open$'
329line '^nxvpwlkm v1 \| stack-a$'
330line '^released: could not read '
331
332# --owes ends each change's line with the checks it owes, by cost, as
333# valley checks derives them, here and in a repository read through its
334# clone. The instance gets a floor for that, written to its main on disk
335# as the integrator would.
336git init --quiet "$w/floor"
337mkdir -p "$w/floor/policy/instance"
338cp "$floor"/*.cue "$w/floor/policy/instance/"
339git -C "$w/floor" add policy
340git -C "$w/floor" commit --quiet -m floor
341git -C "$w/floor" push --quiet "$instance" HEAD:refs/heads/floor
342git -C "$instance" update-ref refs/heads/main "$(git -C "$w/floor" rev-parse HEAD)"
343ok valley pending --owes --all
344line '^nxvpwlkm v1 \| stack-a \| owes 1 quick, 0 slow$'
345line '^mmmm v1 \| stack-b \| includes nxvpwlkm \| owes 1 quick, 0 slow$'
346line '^nxvpwlkz v1 \| g-ready \| owes 1 quick, 0 slow$'
347
348# ----------------------------------------------------------------------
349# A branch name is used again once its change has landed: ff now names no
350# change, and reparent none either, once its transfer statement verifies.
351ok valley-go resolve ff
352line "^branch"$'\t'"ff$"
353ok valley-go resolve reparent
354line "^branch"$'\t'"reparent$"
355env -u VALLEY_KNOWN_SIGNERS valley-go resolve reparent > "$w/out" 2> "$w/err" || fail "resolve failed"
356line "^change"$'\t'"$G"$'\t'
357ok valley-go resolve stack-a
358line "^change"$'\t'"$A"$'\t'
359
360# valley help names the verbs klaus looks for.
361valley help > "$w/out"
362line '^ ready '
363line '^ changes '
364
365touch "$out"