gunk-dev/the-valley

policy-deriver.sh

source8043b5e912d0main's published tip history
19.7 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export GIT_CONFIG_NOSYSTEM=1
6git config --global user.name valley-check
7git config --global user.email valley-check@localhost
8git config --global init.defaultBranch main
9
10policy="$TMPDIR/policy"
11mkdir -p "$policy/instance" "$policy/project"
12cp "$fixtures/floor.cue" "$policy/instance/floor.cue"
13cp "$fixtures/template.cue" "$policy/instance/template.cue"
14cp "$fixtures/project.cue" "$policy/project/policy.cue"
15
16repo="$TMPDIR/tree"
17git init --quiet "$repo"
18cd "$repo" || exit 1
19mkdir -p docs notes src/deep a/x/y
20echo moved > docs/moved.md
21for f in docs/kept.md docs/gone.md src/one.rs src/deep/two.rs \
22 a/b a/x/y/b Makefile; do
23 echo seed > "$f"
24done
25git add -A
26git commit --quiet -m base
27base="$(git rev-parse HEAD)"
28
29derive() {
30 valley checks --schema "$schema" \
31 --instance "$policy/instance" --project "$policy/project" \
32 "$@" "$base" HEAD
33}
34
35# The check names in the deriver's table, sorted, on one line.
36named() {
37 awk '/^required checks:$/ {f = 1; next}
38 /^$/ {f = 0}
39 f && /^ / {print $1}' <<<"$report" | sort | paste -sd' ' -
40}
41
42# Start a case from the base tree; assert its check table.
43# bash_derive SCRIPT SCHEMA INSTANCE PROJECT [--names] BASE HEAD: SCRIPT's
44# own deriver, derive_required, which approve runs, over two object IDs. It
45# prints the required checks as rows under `required checks:` and then the
46# carry mode, or with --names the names alone, so the cases below read it
47# as they read the report.
48bash_derive() {
49 bash -c '
50 source <(sed "/^main \"\\\$@\"\$/d" "$1")
51 trap cleanup_policy_tmp EXIT
52 schema="$2" instance="$3" project="$4" names=0
53 shift 4
54 [ "$1" != --names ] || { names=1; shift; }
55 derive_required "$schema" "$instance" "$project" "$1" "$2" || exit 1
56 if [ "$names" -eq 1 ]; then
57 [ "${#DERIVED_REQUIRED[@]}" -eq 0 ] || printf "%s\n" "${!DERIVED_REQUIRED[@]}"
58 exit 0
59 fi
60 echo "required checks:"
61 for check in "${!DERIVED_REQUIRED[@]}"; do printf " %s\n" "$check"; done
62 printf "\ncarry mode: %s\n" "$DERIVED_CARRY"
63 ' bash "$@"
64}
65
66case_start() { git checkout --quiet -B "$1" "$base"; }
67expect() {
68 local got
69 got="$(named)"
70 if [ "$got" != "$1" ]; then
71 echo "policy-deriver: $2" >&2
72 echo " expected: $1" >&2
73 echo " got: $got" >&2
74 echo "$report" >&2
75 exit 1
76 fi
77}
78
79# Assert the carry mode the report states. The floor gives flat
80# `same-class` and nested `exact`, and no other class a mode.
81expect_carry() {
82 local got
83 got="$(sed -n 's/^carry mode: //p' <<<"$report")"
84 if [ "$got" != "$1" ]; then
85 echo "policy-deriver: $2" >&2
86 echo " expected carry mode: $1" >&2
87 echo " got: $got" >&2
88 echo "$report" >&2
89 exit 1
90 fi
91}
92
93# `**` crosses segments, `*` does not. One file under src/
94# matches both patterns; one a segment deeper matches only
95# the `**` one.
96case_start flat
97echo edit > src/one.rs
98git commit --quiet -am flat
99report="$(derive)"
100expect "c-deep c-flat" "src/*.rs must match a file directly under src/"
101# Overlapping classes compose to the strictest mode: flat's same-class
102# over deep's same-paths.
103expect_carry same-class "a path in a same-class class and a same-paths one must be same-class"
104
105case_start deep
106echo edit > src/deep/two.rs
107git commit --quiet -am deep
108report="$(derive)"
109expect "c-deep" "src/*.rs must not match across a path separator"
110expect_carry same-paths "a class that asks for no mode must be same-paths"
111
112# A `**` between two segments matches no segment at all …
113case_start nested-none
114echo edit > a/b
115git commit --quiet -am nested-none
116report="$(derive)"
117expect "c-nested" "a/**/b must match a/b"
118expect_carry exact "a path in an exact class must be exact"
119
120# Paths in different classes compose the same way: exact over same-class.
121case_start strictest
122echo edit > a/b
123echo edit > src/one.rs
124git commit --quiet -am strictest
125report="$(derive)"
126expect "c-deep c-flat c-nested" "a change to two classes must require both"
127expect_carry exact "an exact path beside a same-class one must be exact"
128
129# … and any number of them.
130case_start nested-many
131echo edit > a/x/y/b
132git commit --quiet -am nested-many
133report="$(derive)"
134expect "c-nested" "a/**/b must match a/x/y/b"
135
136# A rename counts on both sides: the class covering the old
137# path is required as much as the one covering the new.
138case_start renamed
139git mv docs/moved.md notes/moved.md
140git commit --quiet -m renamed
141git diff --name-status -M "$base" HEAD | grep -q '^R' \
142 || { echo "policy-deriver: the rename case is not a rename" >&2; exit 1; }
143report="$(derive)"
144expect "c-docs c-notes c-tmpl" "a rename must count on both sides"
145
146# A deleted path is still a changed path.
147case_start deleted
148git rm --quiet docs/gone.md
149git commit --quiet -m deleted
150report="$(derive)"
151expect "c-docs c-tmpl" "a deletion must count"
152
153# The form of the value is the mandatory bit: the floor's
154# concrete `true` against the template's `bool | *true`.
155grep -qE '^ c-docs +mandatory +docs$' <<<"$report"
156grep -qE '^ c-tmpl +default +docs$' <<<"$report"
157
158# A check's cost is a declared property and changes nothing required. The
159# same diff under a project layer that marks c-docs slow requires the same
160# checks, for the same classes, with the same mandatory bits: every line above the
161# cost summary is the same. Only the summary moves c-docs.
162cp -R "$policy/project" "$policy/project-slow"
163cat > "$policy/project-slow/slow.cue" <<'EOF'
164package verification
165project: checks: "c-docs": {runner: "nix", attribute: "c-docs", cost: "slow"}
166EOF
167derive_slow() {
168 valley checks --schema "$schema" \
169 --instance "$policy/instance" --project "$policy/project-slow" \
170 "$@" "$base" HEAD
171}
172quick_report="$report"
173report="$(derive_slow)"
174expect "c-docs c-tmpl" "a slow check must be required as a quick one is"
175above_summary() { sed -e '/^quick:/,$d' -e '/^policy:/d' <<<"$1"; }
176[ "$(above_summary "$quick_report")" = "$(above_summary "$report")" ] || {
177 echo "policy-deriver: marking a check slow changed what is required" >&2
178 diff <(above_summary "$quick_report") <(above_summary "$report") >&2
179 exit 1
180}
181grep -qx 'quick: c-docs, c-tmpl' <<<"$quick_report"
182grep -qx 'slow: none' <<<"$quick_report"
183grep -qx 'quick: c-tmpl' <<<"$report"
184grep -qx 'slow: c-docs' <<<"$report"
185
186# The names alone are what an agent reads, so they are exactly the required
187# names, one per line, and --cost filters them.
188[ "$(derive_slow --names)" = "$(printf 'c-docs\nc-tmpl')" ]
189[ "$(derive_slow --names --cost quick)" = c-tmpl ]
190[ "$(derive_slow --names --cost slow)" = c-docs ]
191[ "$(derive --names --cost slow)" = "" ]
192if derive_slow --names --cost fast 2> "$TMPDIR/cost.err"; then
193 echo "policy-deriver: --cost fast was accepted" >&2
194 exit 1
195fi
196grep -q "cost takes quick or slow, not 'fast'" "$TMPDIR/cost.err"
197if derive_slow --cost quick 2> /dev/null; then
198 echo "policy-deriver: --cost without --names was accepted" >&2
199 exit 1
200fi
201
202# A change that requires nothing prints no names and no cost summary.
203report="$(valley checks --schema "$schema" \
204 --instance "$policy/instance" --project "$policy/project-slow" "$base" "$base")"
205grep -qx 'required checks: none' <<<"$report"
206expect_carry same-paths "a change that touches nothing must be same-paths"
207if grep -q '^quick:' <<<"$report"; then
208 echo "policy-deriver: a change that requires nothing printed a cost summary" >&2
209 exit 1
210fi
211[ "$(valley checks --schema "$schema" --names \
212 --instance "$policy/instance" --project "$policy/project-slow" "$base" "$base")" = "" ]
213
214# A schema that predates `cost` cannot render the composed policy. The
215# deriver refuses rather than reading the empty render as a policy that
216# requires nothing.
217grep -v '^ cost: \*"quick" | "slow"$' "$schema" > "$TMPDIR/costless.cue"
218if cmp -s "$schema" "$TMPDIR/costless.cue"; then
219 echo "policy-deriver: the costless schema still declares cost, so this case proves nothing" >&2
220 exit 1
221fi
222if valley checks --schema "$TMPDIR/costless.cue" \
223 --instance "$policy/instance" --project "$policy/project" \
224 "$base" HEAD > "$TMPDIR/costless.out" 2>&1; then
225 echo "policy-deriver: a schema the render cannot read derived an answer" >&2
226 cat "$TMPDIR/costless.out" >&2
227 exit 1
228fi
229grep -q '^valley: ' "$TMPDIR/costless.out"
230if bash_derive "$cliScript" "$TMPDIR/costless.cue" "$policy/instance" "$policy/project" \
231 "$base" "$(git rev-parse HEAD)" > "$TMPDIR/costless.out" 2>&1; then
232 echo "policy-deriver: bin/valley's deriver derived an answer under the costless schema" >&2
233 exit 1
234fi
235grep -q 'does not render' "$TMPDIR/costless.out"
236
237# The same for a schema that predates the carry modes: with no resolved
238# mode to render, the deriver refuses rather than reading every class as
239# same-paths.
240grep -v '^_mode: ' "$schema" > "$TMPDIR/modeless.cue"
241if cmp -s "$schema" "$TMPDIR/modeless.cue"; then
242 echo "policy-deriver: the modeless schema still resolves modes, so this case proves nothing" >&2
243 exit 1
244fi
245if valley checks --schema "$TMPDIR/modeless.cue" \
246 --instance "$policy/instance" --project "$policy/project" \
247 "$base" HEAD > "$TMPDIR/modeless.out" 2>&1; then
248 echo "policy-deriver: a schema with no carry modes derived an answer" >&2
249 cat "$TMPDIR/modeless.out" >&2
250 exit 1
251fi
252grep -q '^valley: ' "$TMPDIR/modeless.out"
253if bash_derive "$cliScript" "$TMPDIR/modeless.cue" "$policy/instance" "$policy/project" \
254 "$base" "$(git rev-parse HEAD)" > "$TMPDIR/modeless.out" 2>&1; then
255 echo "policy-deriver: bin/valley's deriver derived an answer under the modeless schema" >&2
256 exit 1
257fi
258grep -q 'does not render' "$TMPDIR/modeless.out"
259
260# A document may refine `policy` directly, and a mode written there counts.
261# The floor gives the deep class no mode, and a project document that
262# refines `policy` makes it exact.
263cp -R "$policy/project" "$policy/project-refined"
264cat > "$policy/project-refined/refined.cue" <<'EOF'
265package verification
266policy: classes: deep: carry: exact: true
267EOF
268git checkout --quiet deep
269report="$(valley checks --schema "$schema" \
270 --instance "$policy/instance" --project "$policy/project-refined" "$base" HEAD)"
271expect "c-deep" "refining policy's carry must not change what deep requires"
272expect_carry exact "a mode written by refining policy must count"
273
274# A policy string holding a newline cannot forge a line of the render. Each
275# forged layer adds a pattern that would end its own line and start another:
276# a looser carry mode for the nested class, which the floor makes exact, or
277# a second copy of a requirement. There are three walls, and each is tested
278# with the walls before it taken down. The schema refuses the pattern, so
279# the layers do not compose. Under a schema that admits it, the render
280# refuses the line. Under a copy of the script whose render admits it too,
281# the deriver refuses the second line.
282for forged in carry requires; do
283 mkdir -p "$policy/forged-$forged"
284 cp "$policy/project/policy.cue" "$policy/forged-$forged/policy.cue"
285done
286cat > "$policy/forged-carry/forged.cue" <<'EOF'
287package verification
288project: classes: forged: paths: "unused\ncarry\tnested\tsame-paths": true
289EOF
290cat > "$policy/forged-requires/forged.cue" <<'EOF'
291package verification
292project: classes: forged: paths: "unused\nrequires\tnested\tc-nested": true
293EOF
294
295# The schema with #Pattern admitting anything, and the script with its
296# render's line check removed. Each must differ from what it copies, or the
297# case below it proves nothing.
298[ "$(grep -c '^#Pattern: ' "$schema")" = 1 ] || {
299 echo "policy-deriver: #Pattern is not one line of the schema, so the loose schema proves nothing" >&2
300 exit 1
301}
302{ grep -v '^#Pattern: ' "$schema"; echo '#Pattern: string'; } > "$TMPDIR/loose.cue"
303sed -e 's/" & line}$/"}/' -e '/^let line = /d' "$cliScript" > "$TMPDIR/valley-unguarded"
304if ! grep -q '" & line}$' "$cliScript" || grep -q '& line}\|^let line' "$TMPDIR/valley-unguarded"; then
305 echo "policy-deriver: the unguarded script still checks its render's lines, so it proves nothing" >&2
306 exit 1
307fi
308
309# refused WHAT TEXT COMMAND…: COMMAND must fail, print nothing on stdout,
310# and say TEXT.
311refused() {
312 local what="$1" text="$2"
313 shift 2
314 if "$@" > "$TMPDIR/forged.out" 2> "$TMPDIR/forged.err"; then
315 echo "policy-deriver: $what derived an answer" >&2
316 cat "$TMPDIR/forged.out" >&2
317 exit 1
318 fi
319 [ ! -s "$TMPDIR/forged.out" ] || {
320 echo "policy-deriver: $what printed to stdout" >&2
321 exit 1
322 }
323 grep -qF -- "$text" "$TMPDIR/forged.err" || {
324 echo "policy-deriver: $what was refused without saying $text" >&2
325 cat "$TMPDIR/forged.err" >&2
326 exit 1
327 }
328}
329
330# forged_derive SCHEMA LAYER [SCRIPT]: derive the case at HEAD under SCHEMA
331# and the project layer LAYER, with valley checks or with SCRIPT's own
332# deriver. Only SCRIPT's deriver reads the policy as rendered lines, so
333# only it can be forged.
334forged_derive() {
335 if [ $# -lt 3 ]; then
336 valley checks --schema "$1" --instance "$policy/instance" \
337 --project "$policy/$2" "$base" HEAD
338 else
339 bash_derive "$3" "$1" "$policy/instance" "$policy/$2" "$base" "$(git rev-parse HEAD)"
340 fi
341}
342
343case_start forged
344echo edit > a/b
345git commit --quiet -am forged
346# Without the walls, an honest layer still derives exact here, so each
347# refusal below is the forgery's doing.
348report="$(forged_derive "$TMPDIR/loose.cue" project "$TMPDIR/valley-unguarded")"
349expect "c-nested" "the unguarded script must derive an honest layer as the real one does"
350expect_carry exact "the unguarded script must derive an honest layer's carry mode"
351
352refused "a forged carry line" "do not compose" \
353 forged_derive "$schema" forged-carry
354refused "a forged carry line under a schema that admits it" "does not render" \
355 forged_derive "$TMPDIR/loose.cue" forged-carry "$cliScript"
356refused "a forged carry line under a render that admits it" \
357 "rendered 'carry nested' twice" \
358 forged_derive "$TMPDIR/loose.cue" forged-carry "$TMPDIR/valley-unguarded"
359refused "a forged requirement line under a render that admits it" \
360 "rendered 'requires nested c-nested' twice" \
361 forged_derive "$TMPDIR/loose.cue" forged-requires "$TMPDIR/valley-unguarded"
362
363# A path no class covers takes the unclassified set, and says
364# so by name. It is same-paths.
365case_start uncovered
366echo edit > Makefile
367git commit --quiet -am uncovered
368report="$(derive)"
369expect "c-uncovered" "an uncovered path must take the unclassified set"
370grep -q '^classes matched: unclassified$' <<<"$report"
371grep -q '^unclassified: 1 path(s) matched no class$' <<<"$report"
372grep -q '^ Makefile$' <<<"$report"
373expect_carry same-paths "an unclassified path must be same-paths"
374
375# A replacement ref changes nothing the derivation reads. Here
376# refs/replace/<head> makes git read the uncovered case's head as
377# the flat case's, so a deriver that left replacement refs on
378# would derive the src/ checks for a change that touches only
379# the Makefile — and the caller asking for them is not listened
380# to either.
381git update-ref "refs/replace/$(git rev-parse uncovered)" "$(git rev-parse flat)"
382git diff --name-only "$base" uncovered | grep -x src/one.rs > /dev/null \
383 || { echo "policy-deriver: the replacement did not take, so this case proves nothing" >&2; exit 1; }
384git checkout --quiet uncovered
385report="$(GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" derive)"
386expect "c-uncovered" "a replacement ref must not change the derived checks"
387git update-ref -d "refs/replace/$(git rev-parse uncovered)"
388
389# ----------------------------------------------------------------------
390# Every producer fails closed. The deriver's calls of git, sort and cue
391# are failed one at a time, one per run, by fail-nth standing in for the
392# tool first on PATH. A producer read through a process substitution
393# loses its status, and its failure then reads as an empty list — no
394# paths, no catalogue, no classes — which derives a change that
395# requires nothing. So every failure must end the run non-zero, say so, and print
396# nothing on stdout: no report, and no names.
397#
398# This runs bin/valley itself, because the package's wrapper puts its own
399# git ahead of anything on PATH. The case touches a class, a second class
400# through `**`, the template's default, and an uncovered path, so every
401# producer has something to lose.
402case_start mixed
403echo edit > docs/kept.md
404echo edit > src/one.rs
405echo edit > Makefile
406git commit --quiet -am mixed
407# An unclassified path beside a same-class one makes the change no looser.
408report="$(derive)"
409expect_carry same-class "an unclassified path must not loosen a same-class change"
410
411shims="$TMPDIR/shims"
412mkdir -p "$shims"
413for tool in git sort cue; do ln -s "$failNth" "$shims/$tool"; done
414
415# shimmed TOOL N [option…]: derive the mixed case with TOOL's Nth call
416# failing (0 fails none). The count of TOOL's calls is left in
417# $TMPDIR/shim-count.
418shimmed() {
419 local tool="$1" at="$2" status=2
420 shift 2
421 [ "$tool" != git ] || status=128 # git's own status for a fatal error
422 local head
423 head="$(git rev-parse HEAD)"
424 echo 0 > "$TMPDIR/shim-count"
425 SHIM_REAL_PATH="$PATH" SHIM_TOOL="$tool" SHIM_FAIL_AT="$at" \
426 SHIM_STATUS="$status" SHIM_COUNT="$TMPDIR/shim-count" PATH="$shims:$PATH" \
427 bash_derive "$cliScript" "$schema" "$policy/instance" "$policy/project" \
428 "$@" "$base" "$head"
429}
430
431# The check names a run's stdout requires, sorted, on one line.
432required_in() {
433 if [ "$1" = names ]; then
434 sort "$2" | paste -sd' ' -
435 else
436 report="$(cat "$2")"
437 named
438 fi
439}
440
441shim_fail() {
442 echo "policy-deriver: $1" >&2
443 for f in shim.out shim.err; do
444 echo "--- $f" >&2
445 cat "$TMPDIR/$f" >&2
446 done
447 exit 1
448}
449
450for tool in git sort cue; do
451 for mode in report names; do
452 opts=()
453 [ "$mode" = report ] || opts=(--names)
454 shimmed "$tool" 0 "${opts[@]}" > "$TMPDIR/shim.want"
455 want="$(required_in "$mode" "$TMPDIR/shim.want")"
456 [ "$want" = "c-deep c-docs c-flat c-tmpl c-uncovered" ] || {
457 echo "policy-deriver: the mixed case requires $want" >&2
458 exit 1
459 }
460 calls="$(cat "$TMPDIR/shim-count")"
461 [ "$calls" -gt 0 ] || {
462 echo "policy-deriver: the $mode run never called $tool, so failing it proves nothing" >&2
463 exit 1
464 }
465 for ((at = 1; at <= calls; at++)); do
466 what="$mode run, $tool call $at of $calls"
467 if shimmed "$tool" "$at" "${opts[@]}" > "$TMPDIR/shim.out" 2> "$TMPDIR/shim.err"; then
468 # The one failure a run may survive is a cue probe after the
469 # composition and the render: it asks whether a requirement is
470 # mandatory, and a probe that fails reads as mandatory, the strict
471 # answer. What is required must not move.
472 [ "$tool" = cue ] && [ "$at" -gt 2 ] \
473 || shim_fail "the $what failed and the deriver still answered"
474 [ "$(required_in "$mode" "$TMPDIR/shim.out")" = "$want" ] \
475 || shim_fail "the $what failed and changed what is required"
476 continue
477 fi
478 [ ! -s "$TMPDIR/shim.out" ] \
479 || shim_fail "the $what failed and the deriver printed to stdout"
480 grep -q '^valley: ' "$TMPDIR/shim.err" \
481 || shim_fail "the $what failed and the deriver did not say so"
482 done
483 done
484done
485
486# A diff cut short mid-record is a failure too, even though git exited 0:
487# cut inside the first path, after one status, and inside the second
488# status, after one whole record. The last is the one a plain `while
489# read` ends on quietly, dropping the record it cut.
490for cut in 1 2; do
491 for opts in "" --names; do
492 # shellcheck disable=SC2086 # an empty $opts is no option at all
493 if SHIM_TRUNCATE="$cut" SHIM_REAL_PATH="$PATH" PATH="$shims:$PATH" \
494 bash_derive "$cliScript" "$schema" "$policy/instance" "$policy/project" $opts \
495 "$base" "$(git rev-parse HEAD)" > "$TMPDIR/shim.out" 2> "$TMPDIR/shim.err"; then
496 shim_fail "a diff cut after $cut field(s) derived an answer"
497 fi
498 [ ! -s "$TMPDIR/shim.out" ] \
499 || shim_fail "a diff cut after $cut field(s) printed to stdout"
500 grep -q 'truncated diff output' "$TMPDIR/shim.err" \
501 || shim_fail "a diff cut after $cut field(s) was not named as one"
502 done
503done
504
505touch "$out"