gunk-dev/the-valley

restack-e2e.sh

source8d0e7f14a4f9main's published tip history
26.9 KiB
1# shellcheck shell=bash
2# valley-restacker once --report-only, end to end over a real bare
3# repository wired with the fixture 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 and restack to the principal requester, which
6# files the requests, and restack alone to the principal restacker, which
7# is the principal the restacker counts.
8#
9# In the planner's scenarios, outcome records are injected on the server
10# side with update-ref, because no push may write one: only the
11# integrator writes them. Each injection is marked where it is made. The
12# fixtures after them run the real integrator.
13# Paths and fixtures come from the derivation environment.
14# shellcheck disable=SC2154
15set -euo pipefail
16
17w="$TMPDIR/restack-e2e"
18mkdir -p "$w"
19export HOME="$w"
20export GIT_CONFIG_NOSYSTEM=1
21git config --global user.name restack-e2e
22git config --global user.email restack-e2e@localhost
23git config --global init.defaultBranch main
24git config --global advice.detachedHead false
25
26fail() {
27 printf 'restack-e2e: %s\n' "$*" >&2
28 for f in "$w"/out "$w"/err; do
29 if [ -s "$f" ]; then cat "$f" >&2; fi
30 done
31 exit 1
32}
33
34origin="$w/project.git"
35hook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$initScriptPath" | head -n1)"
36test -x "$hook"
37git init --quiet --bare "$origin"
38cp "$hook" "$origin/hooks/pre-receive"
39chmod +x "$origin/hooks/pre-receive"
40
41repo="$w/project"
42git init --quiet "$repo"
43cd "$repo"
44git remote add origin "$origin"
45# The project layer caps the restacker at two automatic versions a change.
46mkdir -p policy/project
47printf 'package verification\n\nproject: restack: maxAutomaticVersions: 2\n' > policy/project/policy.cue
48echo base > readme
49git add readme policy
50git commit --quiet -m base
51env -u VALLEY_PRINCIPAL git push --quiet origin main
52
53as() {
54 local who="$1"
55 shift
56 git config valley.principal "$who"
57 VALLEY_PRINCIPAL="$who" "$@"
58}
59ok() { "$@" > "$w/out" 2> "$w/err" || fail "$* failed"; }
60
61# change <owner> <branch> <base>: a commit on base, pushed and readied by
62# its owner; prints the change ID.
63change() {
64 local who="$1" branch="$2" base="$3"
65 git checkout --quiet -B "$branch" "$base"
66 echo "$branch" > "$branch"
67 git add "$branch"
68 git commit --quiet -m "$branch"
69 as "$who" git push --quiet --force origin "$branch"
70 ok as "$who" valley ready
71 sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out"
72}
73# branch <name> <base>: a plain branch, which no record is on.
74branch() {
75 git checkout --quiet -B "$1" "$2"
76 echo "$1" > "$1"
77 git add "$1"
78 git commit --quiet -m "$1"
79 as alice git push --quiet origin "$1"
80}
81
82base="$(git rev-parse main)"
83a="$(change alice a "$base")"
84b1="$(change bob b1 "$base")"
85b2="$(change bob b2 b1)"
86c1="$(change alice c1 "$base")"
87c2="$(change alice c2 c1)"
88c3="$(change bob c3 c1)"
89d="$(change alice d "$base")"
90ok as alice valley unready "$d"
91wd="$(change bob wd "$base")"
92ok as bob valley withdraw "$wd"
93branch plain "$base"
94e="$(change alice e "$base")"
95branch e-plain e
96g1="$(change alice g1 "$base")"
97g2="$(change alice g2 g1)"
98ok as alice valley unready "$g1"
99u="$(change bob u "$base")"
100# k gains two versions from the restacker principal. A version the restack
101# grant adds keeps the active version's title, so each commit's subject is
102# k's title; one under any other title is refused, and its branch is put
103# back.
104k="$(change alice k "$base")"
105git checkout --quiet k
106for n in 2 3; do
107 echo "$n" >> k
108 git commit --quiet -am k
109 as restacker git push --quiet origin k
110 ok as restacker valley ready --change "$k" k
111done
112echo forged >> k
113git commit --quiet -am 'a title of its own'
114as restacker git push --quiet origin k
115if as restacker valley ready --change "$k" k > "$w/out" 2> "$w/err"; then
116 fail 'the restacker readied a version under a title of its own'
117fi
118grep -q 'under the title of the active one' "$w/err" || fail 'the refusal does not name the title'
119git reset --quiet --hard HEAD~1
120as restacker git push --quiet --force origin k
121# s lands by fast-forward before main moves, so it is submitted.
122s="$(change alice s "$base")"
123env -u VALLEY_PRINCIPAL git push --quiet origin s:main
124git checkout --quiet main
125git reset --quiet --hard origin/main
126echo moved > moved
127git add moved
128git commit --quiet -m moved
129env -u VALLEY_PRINCIPAL git push --quiet origin main
130tip="$(git rev-parse main)"
131
132# Server-side injection of an outcome record under a request name.
133evidence() {
134 git -C "$origin" for-each-ref --format='%(refname) %(objectname)' refs/the-valley/attestations/ \
135 | LC_ALL=C sort | sha256sum | cut -c1-16
136}
137inject() {
138 local name="$1" line="$2" blob
139 blob="$(printf '%s\n' "$line" | git -C "$origin" hash-object -w --stdin)"
140 git -C "$origin" update-ref "refs/the-valley/integration-outcomes/main/$name" "$blob"
141}
142# u's request was judged failed, and no request is left: its landing is
143# uncertain.
144inject "c-$u" "$(git rev-parse u) $base $(evidence) failed 2026-10-18T00:00:00Z"
145
146# request <name> <head>: requester files an integration request.
147request() { as requester git push --quiet origin "$2:refs/the-valley/integration-requests/main/$1"; }
148
149refs() { git -C "$origin" for-each-ref --format='%(refname) %(objectname)' | LC_ALL=C sort; }
150plan() {
151 local before
152 before="$(refs)"
153 valley-restacker once --report-only --json --repo "$origin" --state "$w/state" \
154 --principal restacker --instance "$smokePolicy/instance" "$@" > "$w/plan.json" 2> "$w/err" \
155 || fail "the planner failed"
156 [ "$(refs)" = "$before" ] || fail 'a --report-only pass changed a ref on origin'
157}
158# family <id>: the family plan a change is a layer of.
159family() { jq -c --arg id "$1" '.families[] | select(any(.layers[]; .id == $id))' "$w/plan.json"; }
160action() {
161 local got
162 got="$(family "$1" | jq -r .action)"
163 [ "$got" = "$2" ] || fail "the family of $1 is $got, not $2: $(family "$1")"
164}
165says() { family "$1" | jq -e --arg s "$2" 'any(.reasons[], .notes[]; contains($s))' > /dev/null \
166 || fail "the family of $1 does not say: $2: $(family "$1")"; }
167
168# ----------------------------------------------------------------------
169# Selection and families.
170plan
171jq -e --arg t "$tip" '.tip == $t and .maxAutomaticVersions == 2' "$w/plan.json" > /dev/null \
172 || fail "the plan does not read main's tip or the project's cap"
173action "$a" restack
174action "$b1" restack
175[ "$(family "$b1" | jq -c '[.layers[].id]')" = "[\"$b1\",\"$b2\"]" ] || fail 'b1 and b2 are not one family'
176[ "$(family "$c1" | jq '.layers | length')" = 3 ] || fail 'c1, c2 and c3 are not one family'
177[ "$(family "$c2" | jq -r '.layers[0].id')" = "$c1" ] || fail 'the shared lower layer is not the bottom'
178action "$c3" restack
179action "$d" leave
180action "$wd" leave
181action "$u" leave
182action "$e" hold
183says "$e" 'e-plain is a plain branch'
184action "$g2" hold
185says "$g2" 'is a draft'
186[ -z "$(family "$s")" ] || fail 'a submitted change was planned'
187jq -e '[.families[].layers[] | select(.name == "plain")] | length == 0' "$w/plan.json" > /dev/null \
188 || fail 'a lone plain branch was planned'
189
190# Capacity: k has two versions uploaded by the restacker principal, and a
191# third is over the project's cap of 2.
192action "$k" hold
193says "$k" 'over the cap of 2'
194family "$k" | jq -e '.layers[0].capacity.uploads == 3' > /dev/null || fail 'k does not count three uploads'
195
196# ----------------------------------------------------------------------
197# Request holds. A request by name with no outcome holds.
198request a a
199plan
200action "$a" hold
201says "$a" 'no outcome yet'
202family "$a" | jq -e '.requests[0].by == ["name", "head"]' > /dev/null || fail 'request a was not related by name'
203# A current stale releases.
204inject a "$(git rev-parse a) $tip $(evidence) stale"
205plan
206action "$a" restack
207# A mismatched head, tip or fingerprint holds.
208inject a "$base $tip $(evidence) stale"
209plan
210action "$a" hold
211says "$a" 'is about head'
212inject a "$(git rev-parse a) $base $(evidence) stale"
213plan
214says "$a" 'judged on main at'
215inject a "$(git rev-parse a) $tip 0000000000000000 stale"
216plan
217says "$a" 'judged on evidence'
218# A malformed outcome holds.
219inject a "not an outcome"
220plan
221says "$a" 'does not read'
222# A current reject releases, with a note.
223inject a "$(git rev-parse a) $tip $(evidence) reject"
224plan
225action "$a" restack
226says "$a" 'was rejected'
227
228# By an exact head, under a name nothing else relates.
229request zz-head "$(git rev-parse b2)"
230# By range: a commit on c2 no branch holds, so only the request names it
231# and the planner has to fetch it before reading its ancestry.
232git checkout --quiet -B scratch c2
233echo over > over
234git add over
235git commit --quiet -m over
236request zz-range "$(git rev-parse HEAD)"
237git checkout --quiet main
238git branch --quiet -D scratch
239plan
240action "$b1" hold
241family "$b1" | jq -e '.requests[] | select(.name == "zz-head") | .by == ["head"]' > /dev/null \
242 || fail 'zz-head was not related by head'
243action "$c1" hold
244family "$c1" | jq -e '.requests[] | select(.name == "zz-range") | .by == ["range"]' > /dev/null \
245 || fail 'zz-range was not related by range'
246action "$c3" hold
247
248# A request whose head is no commit cannot be related or not, so every
249# family it might be about holds. Injected on the server side, since the
250# hook takes no such request.
251git -C "$origin" update-ref refs/the-valley/integration-requests/main/zz-blob \
252 "$(echo blob | git -C "$origin" hash-object -w --stdin)"
253plan
254action "$a" hold
255says "$a" 'cannot be determined'
256
257# The human summary reads the same plan.
258valley-restacker once --report-only --repo "$origin" --state "$w/state" --principal restacker \
259 --instance "$smokePolicy/instance" > "$w/out" 2> "$w/err" || fail 'the summary failed'
260grep -qE '^[k-z]+: hold$' "$w/out" || fail 'the summary holds no family'
261# And without --report-only, nothing runs.
262if valley-restacker once --repo "$origin" --state "$w/state" --principal restacker \
263 --instance "$smokePolicy/instance" > "$w/out" 2> "$w/err"; then
264 fail 'once ran without --report-only'
265fi
266
267# ----------------------------------------------------------------------
268# A cap larger than any int. The schema admits it, so it composes, and the
269# planner counts it as the largest int: no practical cap. k, held above by
270# the cap of 2, is not held by this one. The request whose head is no
271# commit would hold k too, so it is removed first, on the server side
272# where it was injected.
273git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/zz-blob
274git checkout --quiet main
275printf 'package verification\n\nproject: restack: maxAutomaticVersions: 9223372036854775808\n' \
276 > policy/project/policy.cue
277git commit --quiet -am 'a cap larger than any int'
278env -u VALLEY_PRINCIPAL git push --quiet origin main
279plan
280grep -q '"maxAutomaticVersions": 9223372036854775807,' "$w/plan.json" \
281 || fail "the plan does not count the cap as the largest int: $(cat "$w/plan.json")"
282action "$k" restack
283
284# ----------------------------------------------------------------------
285# The writes a pass would make, built and not pushed: k's branch push, its
286# fourth version and its report, as complete, and e's report alone, as
287# blocked, since its family is held. plan has held every ref on origin
288# still across every pass.
289family "$k" | jq -e --arg id "$k" '.writes.result == "complete"
290 and (.writes.branches | map(.branch)) == ["k"]
291 and (.writes.versions | map([.change_id, .version])) == [[$id, 4]]
292 and (.writes.reports | map([.change_id, .result])) == [[$id, "complete"]]' > /dev/null \
293 || fail "k's writes are not a complete restack: $(family "$k")"
294family "$e" | jq -e --arg id "$e" '.writes.result == "blocked" and .writes.branches == [] and .writes.versions == []
295 and (.writes.reports | map([.change_id, .result])) == [[$id, "blocked"]]' > /dev/null \
296 || fail "e's writes are not a blocked report: $(family "$e")"
297
298# A request for an older version of a change holds its family, whatever
299# its name: by that version's exact head, and by a range that holds it and
300# not the active head. k's first version is no layer's head now.
301k1="$(git -C "$origin" cat-file blob "refs/the-valley/changes/main/$k:record.json" | jq -r '.versions[0].head | ltrimstr("git-sha1:")')"
302[ "$k1" != "$(git rev-parse k)" ] || fail "k's first version is its active head"
303request zz-old "$k1"
304plan
305action "$k" hold
306family "$k" | jq -e '.requests[] | select(.name == "zz-old") | .by == ["head"]' > /dev/null \
307 || fail "a request at k's first version head was not related by head: $(family "$k")"
308git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/zz-old
309git checkout --quiet -B scratch "$k1"
310echo "on k's first version" > on-k1
311git add on-k1
312git commit --quiet -m 'on k v1'
313request zz-old-range "$(git rev-parse HEAD)"
314git checkout --quiet main
315git branch --quiet -D scratch
316plan
317action "$k" hold
318family "$k" | jq -e '.requests[] | select(.name == "zz-old-range") | .by == ["range"]' > /dev/null \
319 || fail "a request on k's first version was not related by range: $(family "$k")"
320git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/zz-old-range
321
322# No git variable that points git elsewhere, or gives it configuration,
323# reaches the restacker. With each pointing at a decoy, the pass plans and
324# builds what it does without, and the decoy is untouched.
325shape() { jq -S 'del(.operation) | .families |= map(.writes |= (if . then .result else . end))' "$w/plan.json"; }
326plan
327shape > "$w/without"
328decoy="$w/decoy"
329git init --quiet --bare "$decoy/repo.git"
330mkdir -p "$decoy/wt" "$decoy/objects/info" "$decoy/objects/pack"
331digest() { find "$decoy" -type f -exec sha256sum {} + | LC_ALL=C sort | sha256sum; }
332before_decoy="$(digest)"
333before="$(refs)"
334env GIT_DIR="$decoy/repo.git" GIT_WORK_TREE="$decoy/wt" GIT_INDEX_FILE="$decoy/index" \
335 GIT_OBJECT_DIRECTORY="$decoy/objects" GIT_ALTERNATE_OBJECT_DIRECTORIES="$decoy/no-objects" \
336 GIT_COMMON_DIR="$decoy/repo.git" GIT_NAMESPACE=decoy GIT_DEFAULT_HASH=sha256 \
337 GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="url.$decoy/repo.git.insteadOf" GIT_CONFIG_VALUE_0="$origin" \
338 GIT_CONFIG_PARAMETERS="'url.$decoy/repo.git.insteadof'='$origin'" \
339 valley-restacker once --report-only --json --repo "$origin" --state "$w/state" \
340 --principal restacker --instance "$smokePolicy/instance" > "$w/plan.json" 2> "$w/err" \
341 || fail "the planner failed with git variables inherited"
342[ "$(refs)" = "$before" ] || fail 'a --report-only pass changed a ref on origin'
343shape > "$w/with"
344diff "$w/without" "$w/with" > /dev/null || fail "with git variables inherited, the plan differs: $(diff "$w/without" "$w/with")"
345[ "$(digest)" = "$before_decoy" ] || fail 'with git variables inherited, the pass changed the decoy'
346! grep -qF "$decoy" "$w/err" || fail "with git variables inherited, the pass printed: $(cat "$w/err")"
347
348# A pass whose stderr nobody reads still ends, with the error it could not
349# print. Its stderr is a fifo that is held open, filled, and never read.
350mkfifo "$w/unread-err"
351exec 8<> "$w/unread-err"
352dd if=/dev/zero of="$w/unread-err" bs=4096 count=1024 oflag=nonblock 2> /dev/null || true
353dd if=/dev/zero of="$w/unread-err" bs=1 count=4096 oflag=nonblock 2> /dev/null || true
354status=0
355start=$SECONDS
356timeout -k 5 60 valley-restacker once --report-only --no-such-flag 2> "$w/unread-err" > /dev/null || status=$?
357took=$((SECONDS - start))
358exec 8<&-
359[ "$status" = 1 ] || fail "a pass whose stderr nobody read ended with $status, not 1"
360[ "$took" -lt 30 ] || fail "a pass whose stderr nobody read took ${took}s to end"
361
362
363# ======================================================================
364# The fixtures the publishing slice's tests stand on, each proved here.
365
366# One verifier file, which the rendered hook checks pushed evidence
367# against, and the integrator and the restacker read too.
368signers="$keys/known_signers"
369grep -qF -- "--known-signers $signers" "$hook" || fail 'the rendered hook does not read the shared verifier keys'
370# ssh-keygen signs only with a key nobody else can read.
371mkdir -p "$w/keys"
372for who in contributor integrator; do install -m 600 "$keys/$who" "$w/keys/$who"; done
373contributor=contributor.valley.invalid/attestations
374integrator_name=integrator.valley.invalid/attestations
375
376# The transport (restack/e2e/transport): git reaches origin through it as
377# ssh, as the principal the URL names, and the real receive-pack runs the
378# rendered hook. A barrier is armed by writing it to $VALLEY_BARRIER/hold.
379export GIT_SSH_COMMAND=valley-restack-transport GIT_SSH_VARIANT=simple
380export VALLEY_BARRIER="$w/barrier"
381mkdir -p "$VALLEY_BARRIER"
382url() { printf 'ssh://%s@valley%s' "$1" "$2"; }
383disarm() { rm -f "${VALLEY_BARRIER:?}"/*; }
384arm() {
385 disarm
386 echo "$*" > "$VALLEY_BARRIER/hold"
387}
388arrived() {
389 local i=0
390 until [ -e "$VALLEY_BARRIER/arrived" ]; do
391 i=$((i + 1))
392 [ "$i" -lt 1200 ] || fail 'nothing reached the barrier'
393 sleep 0.05
394 done
395}
396release() { touch "$VALLEY_BARRIER/release"; }
397at() { git -C "$1" rev-parse --verify --quiet "$2" || true; }
398
399# ----------------------------------------------------------------------
400# The restacker principal holds restack and not request, so it cannot file
401# a request, which requester can.
402cd "$repo"
403git checkout --quiet -B fixture main
404if git push --quiet "$(url restacker "$origin")" fixture:refs/the-valley/integration-requests/main/zz-by-restacker \
405 2> "$w/err"; then
406 fail 'the restacker principal filed a request'
407fi
408git push --quiet "$(url requester "$origin")" fixture:refs/the-valley/integration-requests/main/zz-by-requester \
409 2> "$w/err" || fail 'requester could not file a request through the transport'
410git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/zz-by-requester
411
412# A barrier holds a push before origin sees it, and releases it.
413echo held > held
414git add held
415git commit --quiet -m held
416arm receive connect
417git push --quiet "$(url alice "$origin")" fixture:refs/heads/held 2> "$w/err" &
418pid=$!
419arrived
420[ -z "$(at "$origin" refs/heads/held)" ] || fail 'a push the barrier held reached origin'
421kill -0 "$pid" 2> /dev/null || fail 'a push the barrier held ended'
422release
423wait "$pid" || fail 'a released push failed'
424[ "$(at "$origin" refs/heads/held)" = "$(git rev-parse fixture)" ] || fail 'a released push did not reach origin'
425
426# A push held there loses its lease to one made while it waits.
427was="$(git rev-parse fixture)"
428git commit --quiet --allow-empty -m mine
429arm receive connect
430git push --quiet --force-with-lease="refs/heads/held:$was" "$(url restacker "$origin")" fixture:refs/heads/held \
431 2> "$w/err" &
432pid=$!
433arrived
434git checkout --quiet -B author "$was"
435git commit --quiet --allow-empty -m "the author's"
436as alice git push --quiet origin author:held
437release
438if wait "$pid"; then fail 'a push held across another kept its lease'; fi
439grep -q 'stale info' "$w/err" || fail "the held push was not refused for its lease: $(cat "$w/err")"
440[ "$(at "$origin" refs/heads/held)" = "$(git rev-parse author)" ] || fail "the author's push did not stand"
441
442# A barrier holds a push that origin has taken, before its client hears.
443git checkout --quiet fixture
444arm receive taken refs/heads/taken
445git push --quiet "$(url alice "$origin")" fixture:refs/heads/taken 2> "$w/err" &
446pid=$!
447arrived
448[ "$(at "$origin" refs/heads/taken)" = "$(git rev-parse fixture)" ] || fail 'origin had not taken the held push'
449kill -0 "$pid" 2> /dev/null || fail 'the client heard before the release'
450release
451wait "$pid" || fail 'a push held after origin took it failed'
452
453# A dropped answer: origin takes the push, and its client reports that
454# origin never said so.
455arm receive drop refs/heads/dropped
456if git push --porcelain "$(url alice "$origin")" fixture:refs/heads/dropped > "$w/out" 2> "$w/err"; then
457 fail 'a push whose answer was lost succeeded'
458fi
459cat "$w/out" "$w/err" | grep -q 'remote failed to report status' || fail 'the client did not lose the answer'
460[ "$(at "$origin" refs/heads/dropped)" = "$(git rev-parse fixture)" ] || fail 'origin did not take the push whose answer was lost'
461disarm
462
463# ----------------------------------------------------------------------
464# A repository served with the integrator's evidence fixtures: the nix
465# stand-in (integrator/e2e), and a policy whose checks are pure, under a
466# floor that requires nothing.
467cp -r "$integratorE2e" "$w/stand-ins"
468chmod -R +w "$w/stand-ins"
469patchShebangs "$w/stand-ins" > /dev/null
470export PATH="$w/stand-ins:$PATH" VALLEY_FAKE_STORE="$w/store"
471mkdir -p "$w/floor"
472printf 'package verification\n\nfloor: {\n\tchecks: {}\n\tclasses: {}\n\tunclassified: {}\n}\n' > "$w/floor/floor.cue"
473
474served="$w/served.git"
475git init --quiet --bare "$served"
476cp "$hook" "$served/hooks/pre-receive"
477chmod +x "$served/hooks/pre-receive"
478git init --quiet "$w/fixture"
479cd "$w/fixture"
480git remote add origin "$served"
481mkdir -p docs src checks policy/project
482echo "the readme" > docs/readme.md
483echo "the source" > src/main.txt
484echo docs > checks/prose-format.inputs
485echo src > checks/code-build.inputs
486cat > policy/project/policy.cue << 'CUE'
487package verification
488
489project: {
490 checks: {
491 "prose-format": {runner: "nix", attribute: "prose-format"}
492 "code-build": {runner: "nix", attribute: "code-build"}
493 }
494 classes: {
495 prose: {paths: "docs/**": true, requires: "prose-format": true}
496 code: {paths: "src/**": true, requires: "code-build": true}
497 }
498 unclassified: {}
499}
500CUE
501git add -A
502git commit --quiet -m 'seed the project'
503env -u VALLEY_PRINCIPAL git push --quiet origin main
504
505# attested <branch> <attest run flags…>: alice's change, its evidence signed
506# by the contributor's key and pushed with it through the hook, readied;
507# prints the change ID.
508attested() {
509 local branch="$1"
510 shift
511 as alice attest run --repo . --key "$w/keys/contributor" --name "$contributor" \
512 --push origin --branch "$branch" "$@" > "$w/out" 2> "$w/err" || fail "attest run on $branch failed"
513 ok as alice valley ready
514 sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out"
515}
516git checkout --quiet -b lands main
517echo "a second source line" >> src/main.txt
518git commit --quiet -am 'a change the integrator lands'
519lands="$(attested lands --check code-build)"
520# Its evidence claims an effectful check where the policy runs a pure one.
521git checkout --quiet -b lies main
522echo "a readme whose evidence lies" > docs/readme.md
523git commit --quiet -am 'a change whose evidence lies about its kind'
524lies="$(attested lies --command prose-format=true)"
525git checkout --quiet main
526echo moved > moved.txt
527git add moved.txt
528git commit --quiet -m 'main moves on'
529env -u VALLEY_PRINCIPAL git push --quiet origin main
530
531integrate() {
532 integrator reconcile --repo "$served" --key "$w/keys/integrator" --name "$integrator_name" \
533 --known-signers "$signers" --instance "$w/floor" --schema "$SCHEMA_VERIFICATION" \
534 --attest-schema "$SCHEMA_ATTESTATION" --event-schema "$SCHEMA_EVENTS" > "$w/integrator.out" 2>&1 \
535 || fail "the integrator failed: $(cat "$w/integrator.out")"
536}
537judged() {
538 grep -q "^c-$1 -> refs/heads/main .*: $2\$" "$w/integrator.out" \
539 || fail "the integrator did not $2 c-$1: $(cat "$w/integrator.out")"
540}
541# requester files each request through the transport, one pass each, so
542# the reject is judged on the tip the landing left.
543git push --quiet "$(url requester "$served")" "lands:refs/the-valley/integration-requests/main/c-$lands"
544integrate
545judged "$lands" land
546git push --quiet "$(url requester "$served")" "lies:refs/the-valley/integration-requests/main/c-$lies"
547integrate
548judged "$lies" reject
549grep -q 'requires a pure check and the attestation claims an effectful check' "$w/integrator.out" \
550 || fail "the reject is not for the evidence's kind: $(cat "$w/integrator.out")"
551
552# The planner, reading the shared verifier keys, sees the landed change
553# submitted: it is no layer. Without the keys, the same landing is only
554# uncertain. The rejected change's family is released by the real
555# outcome, current at its head, tip and evidence, and the reject is noted.
556served_plan() {
557 valley-restacker once --report-only --json --repo "$(url restacker "$served")" --state "$w/served-state" \
558 --principal restacker --instance "$w/floor" "$@" > "$w/plan.json" 2> "$w/err" || fail 'the planner failed'
559}
560served_plan --known-signers "$signers" --integrator-name "$integrator_name"
561[ -z "$(family "$lands")" ] || fail "the change the integrator landed was planned: $(family "$lands")"
562action "$lies" restack
563says "$lies" 'was rejected'
564family "$lies" | jq -e --arg n "c-$lies" '.requests[] | select(.name == $n) | .decision == "reject" and .current' \
565 > /dev/null || fail "the reject was not read as current: $(family "$lies")"
566served_plan
567action "$lands" leave
568family "$lands" | jq -e '.layers[0].state == "uncertain"' > /dev/null \
569 || fail "without the keys, the landing is not uncertain: $(family "$lands")"
570
571# ----------------------------------------------------------------------
572# A pass locks its state directory: a second pass fails while a barrier
573# holds the first one's read of origin, and the first goes on once
574# released. The directory belongs to the restacker principal, so a pass
575# for another is refused it.
576pass() {
577 valley-restacker once --report-only --repo "$(url restacker "$served")" --state "$w/served-state" \
578 --principal restacker --instance "$w/floor" "$@"
579}
580arm upload connect
581pass > "$w/first.out" 2> "$w/first.err" &
582pid=$!
583arrived
584if pass > "$w/out" 2> "$w/err"; then fail 'a second pass ran while the first held the state'; fi
585grep -q 'another pass is using the state directory' "$w/err" || fail "the second pass did not say why: $(cat "$w/err")"
586release
587wait "$pid" || fail "the first pass failed once released: $(cat "$w/first.err")"
588if valley-restacker once --report-only --repo "$(url restacker "$served")" --state "$w/served-state" \
589 --principal requester --instance "$w/floor" > "$w/out" 2> "$w/err"; then
590 fail 'a pass for another principal used the state directory'
591fi
592grep -q 'give each its own --state' "$w/err" || fail "the refusal does not say why: $(cat "$w/err")"
593
594# A pass whose read of origin never returns ends at its --timeout.
595arm upload connect
596if pass --timeout 2s > "$w/out" 2> "$w/err"; then fail 'a pass held at the barrier finished'; fi
597grep -q 'ran past its --timeout of 2s' "$w/err" || fail "the pass did not end at its timeout: $(cat "$w/err")"
598disarm
599
600# A pass whose output nobody reads ends at its --timeout too, and lets go
601# of the lock on its state, so the next pass takes it. Its stdout is a
602# fifo that is held open, filled, and never read, so the pass's first
603# write to it blocks. The outer timeout only bounds this check: it kills
604# a pass that outlives the stop it sends.
605mkfifo "$w/unread"
606exec 7<> "$w/unread"
607dd if=/dev/zero of="$w/unread" bs=4096 count=1024 oflag=nonblock 2> /dev/null || true
608dd if=/dev/zero of="$w/unread" bs=1 count=4096 oflag=nonblock 2> /dev/null || true
609status=0
610start=$SECONDS
611timeout -k 5 60 valley-restacker once --report-only --repo "$(url restacker "$served")" --state "$w/served-state" \
612 --principal restacker --instance "$w/floor" --timeout 15s > "$w/unread" 2> "$w/err" || status=$?
613took=$((SECONDS - start))
614exec 7<&-
615case "$status" in
616 0) fail 'a pass whose output nobody read finished' ;;
617 124 | 137) fail 'a pass whose output nobody read did not end at its timeout' ;;
618esac
619[ "$took" -lt 45 ] || fail "a pass with a timeout of 15s took ${took}s"
620grep -q 'ran past its --timeout of 15s: the plan was not written' "$w/err" \
621 || fail "the pass did not end at its timeout while writing: $(cat "$w/err")"
622pass > "$w/out" 2> "$w/err" || fail "the next pass did not take the lock: $(cat "$w/err")"
623
624touch "$out"