gunk-dev/the-valley

restack-e2e.sh

source499d9e61dfeemain's published tip history
33.3 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
262# ----------------------------------------------------------------------
263# A cap larger than any int. The schema admits it, so it composes, and the
264# planner counts it as the largest int: no practical cap. k, held above by
265# the cap of 2, is not held by this one. The request whose head is no
266# commit would hold k too, so it is removed first, on the server side
267# where it was injected.
268git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/zz-blob
269git checkout --quiet main
270printf 'package verification\n\nproject: restack: maxAutomaticVersions: 9223372036854775808\n' \
271 > policy/project/policy.cue
272git commit --quiet -am 'a cap larger than any int'
273env -u VALLEY_PRINCIPAL git push --quiet origin main
274plan
275grep -q '"maxAutomaticVersions": 9223372036854775807,' "$w/plan.json" \
276 || fail "the plan does not count the cap as the largest int: $(cat "$w/plan.json")"
277action "$k" restack
278
279# ----------------------------------------------------------------------
280# The writes a pass would make, built and not pushed: k's branch push, its
281# fourth version and its report, as complete, and e's report alone, as
282# blocked, since its family is held. plan has held every ref on origin
283# still across every pass.
284family "$k" | jq -e --arg id "$k" '.writes.result == "complete"
285 and (.writes.branches | map(.branch)) == ["k"]
286 and (.writes.versions | map([.change_id, .version])) == [[$id, 4]]
287 and (.writes.reports | map([.change_id, .result])) == [[$id, "complete"]]' > /dev/null \
288 || fail "k's writes are not a complete restack: $(family "$k")"
289family "$e" | jq -e --arg id "$e" '.writes.result == "blocked" and .writes.branches == [] and .writes.versions == []
290 and (.writes.reports | map([.change_id, .result])) == [[$id, "blocked"]]' > /dev/null \
291 || fail "e's writes are not a blocked report: $(family "$e")"
292
293# A request for an older version of a change holds its family, whatever
294# its name: by that version's exact head, and by a range that holds it and
295# not the active head. k's first version is no layer's head now.
296k1="$(git -C "$origin" cat-file blob "refs/the-valley/changes/main/$k:record.json" | jq -r '.versions[0].head | ltrimstr("git-sha1:")')"
297[ "$k1" != "$(git rev-parse k)" ] || fail "k's first version is its active head"
298request zz-old "$k1"
299plan
300action "$k" hold
301family "$k" | jq -e '.requests[] | select(.name == "zz-old") | .by == ["head"]' > /dev/null \
302 || fail "a request at k's first version head was not related by head: $(family "$k")"
303git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/zz-old
304git checkout --quiet -B scratch "$k1"
305echo "on k's first version" > on-k1
306git add on-k1
307git commit --quiet -m 'on k v1'
308request zz-old-range "$(git rev-parse HEAD)"
309git checkout --quiet main
310git branch --quiet -D scratch
311plan
312action "$k" hold
313family "$k" | jq -e '.requests[] | select(.name == "zz-old-range") | .by == ["range"]' > /dev/null \
314 || fail "a request on k's first version was not related by range: $(family "$k")"
315git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/zz-old-range
316
317# No git variable that points git elsewhere, or gives it configuration,
318# reaches the restacker. With each pointing at a decoy, the pass plans and
319# builds what it does without, and the decoy is untouched.
320shape() { jq -S 'del(.operation) | .families |= map(.writes |= (if . then .result else . end))' "$w/plan.json"; }
321plan
322shape > "$w/without"
323decoy="$w/decoy"
324git init --quiet --bare "$decoy/repo.git"
325mkdir -p "$decoy/wt" "$decoy/objects/info" "$decoy/objects/pack"
326digest() { find "$decoy" -type f -exec sha256sum {} + | LC_ALL=C sort | sha256sum; }
327before_decoy="$(digest)"
328before="$(refs)"
329env GIT_DIR="$decoy/repo.git" GIT_WORK_TREE="$decoy/wt" GIT_INDEX_FILE="$decoy/index" \
330 GIT_OBJECT_DIRECTORY="$decoy/objects" GIT_ALTERNATE_OBJECT_DIRECTORIES="$decoy/no-objects" \
331 GIT_COMMON_DIR="$decoy/repo.git" GIT_NAMESPACE=decoy GIT_DEFAULT_HASH=sha256 \
332 GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="url.$decoy/repo.git.insteadOf" GIT_CONFIG_VALUE_0="$origin" \
333 GIT_CONFIG_PARAMETERS="'url.$decoy/repo.git.insteadof'='$origin'" \
334 valley-restacker once --report-only --json --repo "$origin" --state "$w/state" \
335 --principal restacker --instance "$smokePolicy/instance" > "$w/plan.json" 2> "$w/err" \
336 || fail "the planner failed with git variables inherited"
337[ "$(refs)" = "$before" ] || fail 'a --report-only pass changed a ref on origin'
338shape > "$w/with"
339diff "$w/without" "$w/with" > /dev/null || fail "with git variables inherited, the plan differs: $(diff "$w/without" "$w/with")"
340[ "$(digest)" = "$before_decoy" ] || fail 'with git variables inherited, the pass changed the decoy'
341! grep -qF "$decoy" "$w/err" || fail "with git variables inherited, the pass printed: $(cat "$w/err")"
342
343# A pass whose stderr nobody reads still ends, with the error it could not
344# print. Its stderr is a fifo that is held open, filled, and never read.
345mkfifo "$w/unread-err"
346exec 8<> "$w/unread-err"
347dd if=/dev/zero of="$w/unread-err" bs=4096 count=1024 oflag=nonblock 2> /dev/null || true
348dd if=/dev/zero of="$w/unread-err" bs=1 count=4096 oflag=nonblock 2> /dev/null || true
349status=0
350start=$SECONDS
351timeout -k 5 60 valley-restacker once --report-only --no-such-flag 2> "$w/unread-err" > /dev/null || status=$?
352took=$((SECONDS - start))
353exec 8<&-
354[ "$status" = 1 ] || fail "a pass whose stderr nobody read ended with $status, not 1"
355[ "$took" -lt 30 ] || fail "a pass whose stderr nobody read took ${took}s to end"
356
357
358# ======================================================================
359# The fixtures the publishing slice's tests stand on, each proved here.
360
361# One verifier file, which the rendered hook checks pushed evidence
362# against, and the integrator and the restacker read too.
363signers="$keys/known_signers"
364grep -qF -- "--known-signers $signers" "$hook" || fail 'the rendered hook does not read the shared verifier keys'
365# ssh-keygen signs only with a key nobody else can read.
366mkdir -p "$w/keys"
367for who in contributor integrator; do install -m 600 "$keys/$who" "$w/keys/$who"; done
368contributor=contributor.valley.invalid/attestations
369integrator_name=integrator.valley.invalid/attestations
370
371# The transport (restack/e2e/transport): git reaches origin through it as
372# ssh, as the principal the URL names, and the real receive-pack runs the
373# rendered hook. A barrier is armed by writing it to $VALLEY_BARRIER/hold.
374export GIT_SSH_COMMAND=valley-restack-transport GIT_SSH_VARIANT=simple
375export VALLEY_BARRIER="$w/barrier"
376mkdir -p "$VALLEY_BARRIER"
377url() { printf 'ssh://%s@valley%s' "$1" "$2"; }
378disarm() { rm -f "${VALLEY_BARRIER:?}"/*; }
379arm() {
380 disarm
381 echo "$*" > "$VALLEY_BARRIER/hold"
382}
383arrived() {
384 local i=0
385 until [ -e "$VALLEY_BARRIER/arrived" ]; do
386 i=$((i + 1))
387 [ "$i" -lt 1200 ] || fail 'nothing reached the barrier'
388 sleep 0.05
389 done
390}
391release() { touch "$VALLEY_BARRIER/release"; }
392at() { git -C "$1" rev-parse --verify --quiet "$2" || true; }
393
394# ----------------------------------------------------------------------
395# The restacker principal holds restack and not request, so it cannot file
396# a request, which requester can.
397cd "$repo"
398git checkout --quiet -B fixture main
399if git push --quiet "$(url restacker "$origin")" fixture:refs/the-valley/integration-requests/main/zz-by-restacker \
400 2> "$w/err"; then
401 fail 'the restacker principal filed a request'
402fi
403git push --quiet "$(url requester "$origin")" fixture:refs/the-valley/integration-requests/main/zz-by-requester \
404 2> "$w/err" || fail 'requester could not file a request through the transport'
405git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/zz-by-requester
406
407# A barrier holds a push before origin sees it, and releases it.
408echo held > held
409git add held
410git commit --quiet -m held
411arm receive connect
412git push --quiet "$(url alice "$origin")" fixture:refs/heads/held 2> "$w/err" &
413pid=$!
414arrived
415[ -z "$(at "$origin" refs/heads/held)" ] || fail 'a push the barrier held reached origin'
416kill -0 "$pid" 2> /dev/null || fail 'a push the barrier held ended'
417release
418wait "$pid" || fail 'a released push failed'
419[ "$(at "$origin" refs/heads/held)" = "$(git rev-parse fixture)" ] || fail 'a released push did not reach origin'
420
421# A push held there loses its lease to one made while it waits.
422was="$(git rev-parse fixture)"
423git commit --quiet --allow-empty -m mine
424arm receive connect
425git push --quiet --force-with-lease="refs/heads/held:$was" "$(url restacker "$origin")" fixture:refs/heads/held \
426 2> "$w/err" &
427pid=$!
428arrived
429git checkout --quiet -B author "$was"
430git commit --quiet --allow-empty -m "the author's"
431as alice git push --quiet origin author:held
432release
433if wait "$pid"; then fail 'a push held across another kept its lease'; fi
434grep -q 'stale info' "$w/err" || fail "the held push was not refused for its lease: $(cat "$w/err")"
435[ "$(at "$origin" refs/heads/held)" = "$(git rev-parse author)" ] || fail "the author's push did not stand"
436
437# A barrier holds a push that origin has taken, before its client hears.
438git checkout --quiet fixture
439arm receive taken refs/heads/taken
440git push --quiet "$(url alice "$origin")" fixture:refs/heads/taken 2> "$w/err" &
441pid=$!
442arrived
443[ "$(at "$origin" refs/heads/taken)" = "$(git rev-parse fixture)" ] || fail 'origin had not taken the held push'
444kill -0 "$pid" 2> /dev/null || fail 'the client heard before the release'
445release
446wait "$pid" || fail 'a push held after origin took it failed'
447
448# A dropped answer: origin takes the push, and its client reports that
449# origin never said so.
450arm receive drop refs/heads/dropped
451if git push --porcelain "$(url alice "$origin")" fixture:refs/heads/dropped > "$w/out" 2> "$w/err"; then
452 fail 'a push whose answer was lost succeeded'
453fi
454cat "$w/out" "$w/err" | grep -q 'remote failed to report status' || fail 'the client did not lose the answer'
455[ "$(at "$origin" refs/heads/dropped)" = "$(git rev-parse fixture)" ] || fail 'origin did not take the push whose answer was lost'
456disarm
457
458# ----------------------------------------------------------------------
459# A repository served with the integrator's evidence fixtures: the nix
460# stand-in (integrator/e2e), and a policy whose checks are pure, under a
461# floor that requires nothing.
462cp -r "$integratorE2e" "$w/stand-ins"
463chmod -R +w "$w/stand-ins"
464patchShebangs "$w/stand-ins" > /dev/null
465export PATH="$w/stand-ins:$PATH" VALLEY_FAKE_STORE="$w/store"
466mkdir -p "$w/floor"
467printf 'package verification\n\nfloor: {\n\tchecks: {}\n\tclasses: {}\n\tunclassified: {}\n}\n' > "$w/floor/floor.cue"
468
469served="$w/served.git"
470git init --quiet --bare "$served"
471cp "$hook" "$served/hooks/pre-receive"
472chmod +x "$served/hooks/pre-receive"
473git init --quiet "$w/fixture"
474cd "$w/fixture"
475git remote add origin "$served"
476mkdir -p docs src checks policy/project
477echo "the readme" > docs/readme.md
478echo "the source" > src/main.txt
479echo docs > checks/prose-format.inputs
480echo src > checks/code-build.inputs
481cat > policy/project/policy.cue << 'CUE'
482package verification
483
484project: {
485 checks: {
486 "prose-format": {runner: "nix", attribute: "prose-format"}
487 "code-build": {runner: "nix", attribute: "code-build"}
488 }
489 classes: {
490 prose: {paths: "docs/**": true, requires: "prose-format": true}
491 code: {paths: "src/**": true, requires: "code-build": true}
492 }
493 unclassified: {}
494}
495CUE
496git add -A
497git commit --quiet -m 'seed the project'
498env -u VALLEY_PRINCIPAL git push --quiet origin main
499
500# attested <branch> <attest run flags…>: alice's change, its evidence signed
501# by the contributor's key and pushed with it through the hook, readied;
502# prints the change ID.
503attested() {
504 local branch="$1"
505 shift
506 as alice attest run --repo . --key "$w/keys/contributor" --name "$contributor" \
507 --push origin --branch "$branch" "$@" > "$w/out" 2> "$w/err" || fail "attest run on $branch failed"
508 ok as alice valley ready
509 sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out"
510}
511git checkout --quiet -b lands main
512echo "a second source line" >> src/main.txt
513git commit --quiet -am 'a change the integrator lands'
514lands="$(attested lands --check code-build)"
515# Its evidence is edited after it was signed, so the signature no longer
516# verifies. The integrator rejects a tampered note. (A note of the wrong
517# kind, an unknown signer's note or a failing note is no evidence, and the
518# request is stale.)
519git checkout --quiet -b lies main
520echo "a readme whose evidence lies" > docs/readme.md
521git commit --quiet -am 'a change whose evidence lies about its signed result'
522lies="$(attested lies --check prose-format)"
523lies_ref="$(git -C "$served" for-each-ref --format='%(refname)' "refs/the-valley/attestations/$(attest digest --repo . --rev lies | cut -d: -f2)" | head -n1)"
524[ -n "$lies_ref" ] || fail "lies has no evidence on the served repository"
525git -C "$served" cat-file blob "$lies_ref:prose-format/statement.note" \
526 | sed 's/^predicate.result passed$/predicate.result failed/' > "$w/lies.note"
527cmp -s "$w/lies.note" <(git -C "$served" cat-file blob "$lies_ref:prose-format/statement.note") \
528 && fail 'the tamper changed nothing'
529lies_blob="$(git -C "$served" hash-object -w --stdin < "$w/lies.note")"
530lies_sub="$(printf '100644 blob %s\tstatement.note\n' "$lies_blob" | git -C "$served" mktree)"
531git -C "$served" update-ref "$lies_ref" "$(printf '040000 tree %s\tprose-format\n' "$lies_sub" | git -C "$served" mktree)"
532git checkout --quiet main
533echo moved > moved.txt
534git add moved.txt
535git commit --quiet -m 'main moves on'
536env -u VALLEY_PRINCIPAL git push --quiet origin main
537
538integrate() {
539 integrator reconcile --repo "$served" --key "$w/keys/integrator" --name "$integrator_name" \
540 --known-signers "$signers" --instance "$w/floor" --schema "$SCHEMA_VERIFICATION" \
541 --attest-schema "$SCHEMA_ATTESTATION" --event-schema "$SCHEMA_EVENTS" > "$w/integrator.out" 2>&1 \
542 || fail "the integrator failed: $(cat "$w/integrator.out")"
543}
544judged() {
545 grep -q "^c-$1 -> refs/heads/main .*: $2\$" "$w/integrator.out" \
546 || fail "the integrator did not $2 c-$1: $(cat "$w/integrator.out")"
547}
548# requester files each request through the transport, one pass each, so
549# the reject is judged on the tip the landing left.
550git push --quiet "$(url requester "$served")" "lands:refs/the-valley/integration-requests/main/c-$lands"
551integrate
552judged "$lands" land
553git push --quiet "$(url requester "$served")" "lies:refs/the-valley/integration-requests/main/c-$lies"
554integrate
555judged "$lies" reject
556grep -q 'does not verify as the statement it claims to be' "$w/integrator.out" \
557 || fail "the reject is not for the tampered evidence: $(cat "$w/integrator.out")"
558
559# The planner, reading the shared verifier keys, sees the landed change
560# submitted: it is no layer. Without the keys, the same landing is only
561# uncertain. The rejected change's family is released by the real
562# outcome, current at its head, tip and evidence, and the reject is noted.
563served_plan() {
564 valley-restacker once --report-only --json --repo "$(url restacker "$served")" --state "$w/served-state" \
565 --principal restacker --instance "$w/floor" "$@" > "$w/plan.json" 2> "$w/err" || fail 'the planner failed'
566}
567served_plan --known-signers "$signers" --integrator-name "$integrator_name"
568[ -z "$(family "$lands")" ] || fail "the change the integrator landed was planned: $(family "$lands")"
569action "$lies" restack
570says "$lies" 'was rejected'
571family "$lies" | jq -e --arg n "c-$lies" '.requests[] | select(.name == $n) | .decision == "reject" and .current' \
572 > /dev/null || fail "the reject was not read as current: $(family "$lies")"
573served_plan
574action "$lands" leave
575family "$lands" | jq -e '.layers[0].state == "uncertain"' > /dev/null \
576 || fail "without the keys, the landing is not uncertain: $(family "$lands")"
577# A signer projects file that restricts the integrator name says that
578# name is not the integrator's, so with the keys the same landing is again
579# only uncertain. A file that does not read
580# stops the pass.
581printf '%s served\n' "$integrator_name" > "$w/restricts-the-integrator"
582served_plan --known-signers "$signers" --integrator-name "$integrator_name" \
583 --signer-projects "$w/restricts-the-integrator"
584action "$lands" leave
585family "$lands" | jq -e '.layers[0].state == "uncertain"' > /dev/null \
586 || fail "with the integrator name restricted, the landing is not uncertain: $(family "$lands")"
587printf '%s served\n%s served\n' "$integrator_name" "$integrator_name" > "$w/named-twice"
588if valley-restacker once --report-only --json --repo "$(url restacker "$served")" --state "$w/served-state" \
589 --principal restacker --instance "$w/floor" --known-signers "$signers" --integrator-name "$integrator_name" \
590 --signer-projects "$w/named-twice" > "$w/plan.json" 2> "$w/err"; then
591 fail 'a pass ran with a signer projects file that does not read'
592fi
593grep -q -- "--signer-projects $w/named-twice does not read" "$w/err" || fail "the pass did not say why: $(cat "$w/err")"
594
595# ----------------------------------------------------------------------
596# A pass locks its state directory: a second pass fails while a barrier
597# holds the first one's read of origin, and the first goes on once
598# released. The directory belongs to the restacker principal, so a pass
599# for another is refused it.
600pass() {
601 valley-restacker once --report-only --repo "$(url restacker "$served")" --state "$w/served-state" \
602 --principal restacker --instance "$w/floor" "$@"
603}
604arm upload connect
605pass > "$w/first.out" 2> "$w/first.err" &
606pid=$!
607arrived
608if pass > "$w/out" 2> "$w/err"; then fail 'a second pass ran while the first held the state'; fi
609grep -q 'another pass is using the state directory' "$w/err" || fail "the second pass did not say why: $(cat "$w/err")"
610release
611wait "$pid" || fail "the first pass failed once released: $(cat "$w/first.err")"
612if valley-restacker once --report-only --repo "$(url restacker "$served")" --state "$w/served-state" \
613 --principal requester --instance "$w/floor" > "$w/out" 2> "$w/err"; then
614 fail 'a pass for another principal used the state directory'
615fi
616grep -q 'give each its own --state' "$w/err" || fail "the refusal does not say why: $(cat "$w/err")"
617
618# A pass whose read of origin never returns ends at its --timeout.
619arm upload connect
620if pass --timeout 2s > "$w/out" 2> "$w/err"; then fail 'a pass held at the barrier finished'; fi
621grep -q 'ran past its --timeout of 2s' "$w/err" || fail "the pass did not end at its timeout: $(cat "$w/err")"
622disarm
623
624# A pass whose output nobody reads ends at its --timeout too, and lets go
625# of the lock on its state, so the next pass takes it. Its stdout is a
626# fifo that is held open, filled, and never read, so the pass's first
627# write to it blocks. The outer timeout only bounds this check: it kills
628# a pass that outlives the stop it sends.
629mkfifo "$w/unread"
630exec 7<> "$w/unread"
631dd if=/dev/zero of="$w/unread" bs=4096 count=1024 oflag=nonblock 2> /dev/null || true
632dd if=/dev/zero of="$w/unread" bs=1 count=4096 oflag=nonblock 2> /dev/null || true
633status=0
634start=$SECONDS
635timeout -k 5 60 valley-restacker once --report-only --repo "$(url restacker "$served")" --state "$w/served-state" \
636 --principal restacker --instance "$w/floor" --timeout 15s > "$w/unread" 2> "$w/err" || status=$?
637took=$((SECONDS - start))
638exec 7<&-
639case "$status" in
640 0) fail 'a pass whose output nobody read finished' ;;
641 124 | 137) fail 'a pass whose output nobody read did not end at its timeout' ;;
642esac
643[ "$took" -lt 45 ] || fail "a pass with a timeout of 15s took ${took}s"
644grep -q 'ran past its --timeout of 15s: the plan was not written' "$w/err" \
645 || fail "the pass did not end at its timeout while writing: $(cat "$w/err")"
646pass > "$w/out" 2> "$w/err" || fail "the next pass did not take the lock: $(cat "$w/err")"
647
648# ----------------------------------------------------------------------
649# Write passes through the transport, as the restacker principal, on the
650# repository the real integrator lands in. bob's change, readied on the
651# seed, and lies, which its current reject releases, are each restacked
652# after the real landing, with no bus: each branch is rebased onto main's
653# tip, each record gains one version the restacker uploaded under its
654# owner, and each report says complete. lies's request still names its
655# old head. The answer to the first record push is dropped: origin takes
656# it and the client hears nothing, so the pass's next read finds the
657# version on origin, and adds it once. A second pass pushes nothing.
658seed="$(git -C "$served" rev-list --max-parents=0 main)"
659bobs="$(change bob bobs "$seed")"
660lies_was="$(at "$served" refs/heads/lies)"
661write_pass() {
662 valley-restacker once --json --repo "$(url restacker "$served")" --state "$w/served-state" \
663 --principal restacker --instance "$w/floor" --known-signers "$signers" --integrator-name "$integrator_name" \
664 > "$w/plan.json" 2> "$w/err" || fail "the write pass failed: $(cat "$w/err")"
665}
666arm receive drop refs/the-valley/changes/
667write_pass
668disarm
669main_tip="$(at "$served" refs/heads/main)"
670for pair in "$lies:lies:alice" "$bobs:bobs:bob"; do
671 IFS=: read -r id branch owner <<< "$pair"
672 head="$(at "$served" "refs/heads/$branch")"
673 [ "$(git -C "$served" rev-parse "$head^")" = "$main_tip" ] || fail "$branch was not rebased onto main's tip"
674 record="$(git -C "$served" cat-file blob "refs/the-valley/changes/main/$id:record.json")"
675 jq -e --arg h "git-sha1:$head" --arg o "$owner" '.seq == 2 and .active == 2 and (.versions | length) == 2
676 and .versions[1].head == $h and .versions[1].uploader == "restacker" and .owner == $o' <<< "$record" > /dev/null \
677 || fail "$branch's record is not restacked once: $record"
678 git -C "$served" cat-file blob "refs/the-valley/restack/main/$id:report.json" | jq -e '.result == "complete"' > /dev/null \
679 || fail "$branch's report does not say complete"
680done
681jq -e '[.actions[] | select(.write == "version")] | length == 2 and any(.[]; .result == "unknown")' "$w/plan.json" > /dev/null \
682 || fail "the dropped answer was not a push of unknown result: $(jq -c .actions "$w/plan.json")"
683[ "$(at "$served" "refs/the-valley/integration-requests/main/c-$lies")" = "$lies_was" ] || fail "lies's request was moved"
684before="$(git -C "$served" for-each-ref --format='%(refname) %(objectname)')"
685write_pass
686jq -e '.actions == []' "$w/plan.json" > /dev/null || fail "a second write pass pushed: $(jq -c .actions "$w/plan.json")"
687[ "$(git -C "$served" for-each-ref --format='%(refname) %(objectname)')" = "$before" ] || fail 'a second write pass moved a ref'
688
689# ----------------------------------------------------------------------
690# main -> under-p -> under-a -> under-b, with under-p a plain branch. The
691# real integrator lands under-a as one new commit, so main reaches neither
692# under-a's commits nor under-p. The planner reads under-p as no layer:
693# under-b's family is under-b alone, and it is restacked.
694git checkout --quiet -B under-p "$(at "$served" refs/heads/main)"
695echo "a plain branch below a landed change" > under-p.txt
696git add under-p.txt
697git commit --quiet -m 'a plain branch below a landed change'
698as alice git push --quiet origin under-p
699git checkout --quiet -B under-a
700echo "a line the landed change adds" >> src/main.txt
701git commit --quiet -am 'a change the integrator lands on the plain branch'
702under_a="$(attested under-a --check code-build)"
703git checkout --quiet -B under-b
704echo "a change above the landed one" > under-b.txt
705git add under-b.txt
706git commit --quiet -m 'a change above the landed one'
707as alice git push --quiet origin under-b
708ok as alice valley ready
709under_b="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")"
710git push --quiet "$(url requester "$served")" "under-a:refs/the-valley/integration-requests/main/c-$under_a"
711integrate
712judged "$under_a" land
713git -C "$served" merge-base --is-ancestor "$(git rev-parse under-p)" main \
714 && fail 'main reaches under-p; the fixture proves nothing'
715served_plan --known-signers "$signers" --integrator-name "$integrator_name"
716[ "$(family "$under_b" | jq -c '[.layers[].id]')" = "[\"$under_b\"]" ] \
717 || fail "under-b's family is not under-b alone: $(family "$under_b")"
718action "$under_b" restack
719jq -e '[.families[].layers[] | select(.name == "under-p")] | length == 0' "$w/plan.json" > /dev/null \
720 || fail "the plain branch below the landed change was planned: $(jq -c .families "$w/plan.json")"
721
722touch "$out"