gunk-dev/the-valley

policy-deriver.sh

source08dfaa862db7main's published tip history
13.8 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 path no class covers takes the unclassified set, and says
226# so by name. It is same-paths.
227case_start uncovered
228echo edit > Makefile
229git commit --quiet -am uncovered
230report="$(derive)"
231expect "c-uncovered" "an uncovered path must take the unclassified set"
232grep -q '^classes matched: unclassified$' <<<"$report"
233grep -q '^unclassified: 1 path(s) matched no class$' <<<"$report"
234grep -q '^ Makefile$' <<<"$report"
235expect_carry same-paths "an unclassified path must be same-paths"
236
237# A replacement ref changes nothing the derivation reads. Here
238# refs/replace/<head> makes git read the uncovered case's head as
239# the flat case's, so a deriver that left replacement refs on
240# would derive the src/ checks for a change that touches only
241# the Makefile — and the caller asking for them is not listened
242# to either.
243git update-ref "refs/replace/$(git rev-parse uncovered)" "$(git rev-parse flat)"
244git diff --name-only "$base" uncovered | grep -x src/one.rs > /dev/null \
245 || { echo "policy-deriver: the replacement did not take, so this case proves nothing" >&2; exit 1; }
246git checkout --quiet uncovered
247report="$(GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" derive)"
248expect "c-uncovered" "a replacement ref must not change the derived checks"
249git update-ref -d "refs/replace/$(git rev-parse uncovered)"
250
251# ----------------------------------------------------------------------
252# Every producer fails closed. The deriver's calls of git, sort and cue
253# are failed one at a time, one per run, by fail-nth standing in for the
254# tool first on PATH. A producer read through a process substitution
255# loses its status, and its failure then reads as an empty list — no
256# paths, no catalogue, no classes — which derives a change that
257# requires nothing. So every failure must end the run non-zero, say so, and print
258# nothing on stdout: no report, and no names.
259#
260# This runs bin/valley itself, because the package's wrapper puts its own
261# git ahead of anything on PATH. The case touches a class, a second class
262# through `**`, the template's default, and an uncovered path, so every
263# producer has something to lose.
264case_start mixed
265echo edit > docs/kept.md
266echo edit > src/one.rs
267echo edit > Makefile
268git commit --quiet -am mixed
269# An unclassified path beside a same-class one makes the change no looser.
270report="$(derive)"
271expect_carry same-class "an unclassified path must not loosen a same-class change"
272
273shims="$TMPDIR/shims"
274mkdir -p "$shims"
275for tool in git sort cue; do ln -s "$failNth" "$shims/$tool"; done
276
277# shimmed TOOL N [option…]: derive the mixed case with TOOL's Nth call
278# failing (0 fails none). The count of TOOL's calls is left in
279# $TMPDIR/shim-count.
280shimmed() {
281 local tool="$1" at="$2" status=2
282 shift 2
283 [ "$tool" != git ] || status=128 # git's own status for a fatal error
284 echo 0 > "$TMPDIR/shim-count"
285 SHIM_REAL_PATH="$PATH" SHIM_TOOL="$tool" SHIM_FAIL_AT="$at" \
286 SHIM_STATUS="$status" SHIM_COUNT="$TMPDIR/shim-count" PATH="$shims:$PATH" \
287 bash "$cliScript" checks --schema "$schema" \
288 --instance "$policy/instance" --project "$policy/project" \
289 "$@" "$base" HEAD
290}
291
292# The check names a run's stdout requires, sorted, on one line.
293required_in() {
294 if [ "$1" = names ]; then
295 sort "$2" | paste -sd' ' -
296 else
297 report="$(cat "$2")"
298 named
299 fi
300}
301
302shim_fail() {
303 echo "policy-deriver: $1" >&2
304 for f in shim.out shim.err; do
305 echo "--- $f" >&2
306 cat "$TMPDIR/$f" >&2
307 done
308 exit 1
309}
310
311for tool in git sort cue; do
312 for mode in report names; do
313 opts=()
314 [ "$mode" = report ] || opts=(--names)
315 shimmed "$tool" 0 "${opts[@]}" > "$TMPDIR/shim.want"
316 want="$(required_in "$mode" "$TMPDIR/shim.want")"
317 [ "$want" = "c-deep c-docs c-flat c-tmpl c-uncovered" ] || {
318 echo "policy-deriver: the mixed case requires $want" >&2
319 exit 1
320 }
321 calls="$(cat "$TMPDIR/shim-count")"
322 [ "$calls" -gt 0 ] || {
323 echo "policy-deriver: the $mode run never called $tool, so failing it proves nothing" >&2
324 exit 1
325 }
326 for ((at = 1; at <= calls; at++)); do
327 what="$mode run, $tool call $at of $calls"
328 if shimmed "$tool" "$at" "${opts[@]}" > "$TMPDIR/shim.out" 2> "$TMPDIR/shim.err"; then
329 # The one failure a run may survive is a cue probe after the
330 # composition and the render: it asks whether a requirement is
331 # mandatory, and a probe that fails reads as mandatory, the strict
332 # answer. What is required must not move.
333 [ "$tool" = cue ] && [ "$at" -gt 2 ] \
334 || shim_fail "the $what failed and the deriver still answered"
335 [ "$(required_in "$mode" "$TMPDIR/shim.out")" = "$want" ] \
336 || shim_fail "the $what failed and changed what is required"
337 continue
338 fi
339 [ ! -s "$TMPDIR/shim.out" ] \
340 || shim_fail "the $what failed and the deriver printed to stdout"
341 grep -q '^valley: ' "$TMPDIR/shim.err" \
342 || shim_fail "the $what failed and the deriver did not say so"
343 done
344 done
345done
346
347# A diff cut short mid-record is a failure too, even though git exited 0:
348# cut inside the first path, after one status, and inside the second
349# status, after one whole record. The last is the one a plain `while
350# read` ends on quietly, dropping the record it cut.
351for cut in 1 2; do
352 for opts in "" --names; do
353 # shellcheck disable=SC2086 # an empty $opts is no option at all
354 if SHIM_TRUNCATE="$cut" SHIM_REAL_PATH="$PATH" PATH="$shims:$PATH" \
355 bash "$cliScript" checks --schema "$schema" \
356 --instance "$policy/instance" --project "$policy/project" $opts \
357 "$base" HEAD > "$TMPDIR/shim.out" 2> "$TMPDIR/shim.err"; then
358 shim_fail "a diff cut after $cut field(s) derived an answer"
359 fi
360 [ ! -s "$TMPDIR/shim.out" ] \
361 || shim_fail "a diff cut after $cut field(s) printed to stdout"
362 grep -q 'truncated diff output' "$TMPDIR/shim.err" \
363 || shim_fail "a diff cut after $cut field(s) was not named as one"
364 done
365done
366
367touch "$out"