gunk-dev/the-valley

policy-deriver.sh

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