gunk-dev/the-valley

cue-vet.sh

source08dfaa862db7main's published tip history
5.1 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4cue vet -c "$schema" "$example"
5cue export "$schema" "$example" > example.json
6grep -q 'gunk-dev/the-valley' example.json
7grep -q 'restic-sftp' example.json
8
9# A declaration with no optional field set must stay valid —
10# consumers written before backup or protection existed keep
11# vetting.
12cue vet -c "$schema" "$hosts/no-backup.cue"
13
14# What the installer renders a hook from. A project that
15# leaves the set out gets the default, a project that names
16# patterns gets them in order, and a project that declares no
17# protection exports none at all — which is how the installer
18# tells a protected project from an open one.
19protected=("$schema" "$hosts/protected.cue")
20[ "$(cue export -e 'projects.guarded.protection.refs[0]' "${protected[@]}")" \
21 = '"refs/heads/main"' ]
22cue export -e 'projects.released.protection.refs' "${protected[@]}" > released.json
23grep -q '"refs/heads/release/\*"' released.json
24if cue export -e 'projects.open.protection' "${protected[@]}" > open.err 2>&1; then
25 echo "cue-vet: a project declaring no protection exported one" >&2
26 exit 1
27fi
28
29# The norm: protection that names no writer. It exports an
30# empty list rather than nothing, so the installer renders a
31# hook whose exception list is empty rather than missing.
32[ "$(cue export -e 'projects.sealed.protection.writers' "${protected[@]}")" = '[]' ]
33
34# The same for grants: none by default, so the hook reads a
35# project that opens nothing as exactly that, whether or not it
36# declares protection. A grant exports its patterns and its
37# writers as declared.
38[ "$(cue export -e 'projects.sealed.grants' "${protected[@]}")" = '{}' ]
39[ "$(cue export -e 'projects.open.grants' "${protected[@]}")" = '{}' ]
40cue export -e 'projects.released.grants' "${protected[@]}" > grants.json
41grep -q '"refs/tags/release/\*"' grants.json
42
43# The event schema accepts what the publisher hook emits …
44cue vet -d '#RefUpdated' "$eventSchema" "$events/ref-updated.json"
45# … and stays closed: a field outside the git-derivable
46# identity — wall-clock time, a hostname — must be rejected,
47# or replay determinism silently dies. An abbreviated object
48# id is rejected for the same reason.
49for bad in "$events"/rejected/*.json; do
50 if cue vet -d '#RefUpdated' "$eventSchema" "$bad"; then
51 echo "cue-vet: expected $(basename "$bad") to be rejected" >&2
52 exit 1
53 fi
54done
55
56# The rejected declarations, and the words each rejection must name.
57while IFS=$'\t' read -r name names; do
58 case "$name" in ''|'#'*) continue ;; esac
59 if cue vet -c "$schema" "$hosts/rejected/$name.cue" > "$name.err" 2>&1; then
60 echo "cue-vet: expected invalid declaration '$name' to be rejected" >&2
61 exit 1
62 fi
63 if [ -n "$names" ] && ! grep -qF -- "$names" "$name.err"; then
64 echo "cue-vet: '$name' was rejected without naming $names" >&2
65 cat "$name.err" >&2
66 exit 1
67 fi
68done < "$rejectedCases"
69
70# The worked policy example: its instance floor and templates composed with
71# its project layer, as schema/verification.cue's header shows. The
72# deriver's own fixtures are policy-deriver's.
73cue vet -c "$verificationSchema" "$policies"/instance/*.cue \
74 "$policies"/project/*.cue
75
76# The carry modes, case by case. Each fixture under examples/policy/carry/
77# composes a floor, a template and a project in one document. Each must vet,
78# and each class must resolve to the mode the layers mean: the strictest
79# mode whose key is true, and same-paths when none is.
80carry="$policies/carry"
81while read -r fixture class mode; do
82 cue vet -c "$verificationSchema" "$carry/$fixture.cue"
83 got="$(cue eval --out text -e "_mode.$class" "$verificationSchema" "$carry/$fixture.cue")"
84 [ "$got" = "$mode" ] || {
85 echo "cue-vet: carry/$fixture.cue resolves $class to '$got', not $mode" >&2
86 exit 1
87 }
88done <<'EOF'
89floor-pins-exact guarded exact
90template-default-declined guarded same-paths
91template-default-declined kept same-class
92no-carry guarded same-paths
93project-adds-exact guarded exact
94EOF
95
96# The restacker's cap is 64 unless a layer sets it.
97[ "$(cue export -e policy.restack.maxAutomaticVersions "$verificationSchema" \
98 "$carry/no-carry.cue")" = 64 ]
99[ "$(cue export -e policy.restack.maxAutomaticVersions "$verificationSchema" \
100 "$carry/project-adds-exact.cue")" = 16 ]
101
102# The compositions the schema must reject, and the field each rejection must
103# name: a project dropping the floor's mode, a carry key that is not a
104# mode, a cap below 1, and a cap outside every layer.
105while read -r fixture names; do
106 if cue vet -c "$verificationSchema" "$carry/rejected/$fixture.cue" \
107 > "$fixture.err" 2>&1; then
108 echo "cue-vet: expected carry/rejected/$fixture.cue to be rejected" >&2
109 exit 1
110 fi
111 grep -qF -- "$names" "$fixture.err" || {
112 echo "cue-vet: carry/rejected/$fixture.cue was rejected without naming $names" >&2
113 cat "$fixture.err" >&2
114 exit 1
115 }
116done <<'EOF'
117project-drops-floor-exact policy.classes.guarded.carry.exact
118not-a-mode policy.classes.guarded.carry."same-file"
119cap-below-one policy.restack.maxAutomaticVersions
120restack-outside-project restack: conflicting values "INVALID: unknown top-level field
121EOF
122touch "$out"