gunk-dev/the-valley

model_test.go

source1c34f8c82a43main's published tip history
9.9 KiB
1package derive
2
3import (
4 "reflect"
5 "strings"
6 "testing"
7
8 "the-valley/composed"
9)
10
11// TestReadReadsTheWholePolicy holds composed.Read to what a policy says:
12// each check's runner, attribute or command, validity and cost; each
13// class's paths, requirements, pins and carry mode, with what a project
14// turned off left out; and the digest Compose gives the same layers.
15func TestReadReadsTheWholePolicy(t *testing.T) {
16 r := newRig(t)
17 l := newLayers(t, r.glob(t, "examples/policy/instance/*.cue"), r.glob(t, "examples/policy/project/*.cue"), map[string]string{
18 "live.cue": `package verification
19project: checks: "smoke-live": {runner: "command", command: "curl -fsS https://example.org/health", validity: "15m", cost: "slow"}
20project: classes: live: {paths: "deploy/**": true, requires: "smoke-live": true, carry: exact: true}
21`})
22 m, err := r.read(l)
23 if err != nil {
24 t.Fatal(err)
25 }
26 checks := map[string]composed.Check{
27 "prose-format": {Runner: "nix", Attribute: "prose-format", Cost: "quick"},
28 "link-check": {Runner: "nix", Attribute: "link-check", Cost: "quick"},
29 "knowledge-lint": {Runner: "nix", Attribute: "knowledge-lint", Cost: "quick"},
30 "shellcheck": {Runner: "nix", Attribute: "shellcheck", Cost: "quick"},
31 "install-vm": {Runner: "nix", Attribute: "install-vm", Cost: "slow"},
32 "smoke-live": {Runner: "command", Command: "curl -fsS https://example.org/health", Validity: "15m", Cost: "slow"},
33 }
34 if !reflect.DeepEqual(m.Checks, checks) {
35 t.Errorf("checks = %+v", m.Checks)
36 }
37 classes := map[string]composed.Class{
38 "prose": {Paths: []string{"**/*.md"}, Requires: map[string]composed.Pin{"prose-format": composed.Mandatory, "link-check": composed.Mandatory}, Carry: "same-paths"},
39 // The template offers the lint as a default and the project turns
40 // it off, so the class requires nothing.
41 "knowledge": {Paths: []string{".the-valley/**"}, Requires: map[string]composed.Pin{}, Carry: "same-paths"},
42 "scripts": {Paths: []string{"bin/**"}, Requires: map[string]composed.Pin{"shellcheck": composed.Mandatory, "install-vm": composed.Mandatory}, Carry: "same-paths"},
43 "live": {Paths: []string{"deploy/**"}, Requires: map[string]composed.Pin{"smoke-live": composed.Mandatory}, Carry: "exact"},
44 }
45 if !reflect.DeepEqual(m.Classes, classes) {
46 t.Errorf("classes = %+v", m.Classes)
47 }
48 if want := map[string]composed.Pin{"prose-format": composed.Mandatory}; !reflect.DeepEqual(m.Unclassified, want) {
49 t.Errorf("unclassified = %+v", m.Unclassified)
50 }
51 pol, err := composed.Compose(t.Context(), r.cue, r.schema, l.instance, l.project)
52 if err != nil {
53 t.Fatal(err)
54 }
55 if m.Digest != pol.Digest || len(m.Digest) != 64 {
56 t.Errorf("Read's digest %q is not Compose's %q", m.Digest, pol.Digest)
57 }
58
59 // The template's default, left alone, is a default.
60 tmpl := newLayers(t, r.glob(t, "examples/policy/deriver/[ft]*.cue"), []string{r.path("examples/policy/deriver/project.cue")}, nil)
61 if m, err = r.read(tmpl); err != nil {
62 t.Fatal(err)
63 }
64 if got := m.Classes["docs"].Requires; got["c-tmpl"] != composed.Default || got["c-docs"] != composed.Mandatory {
65 t.Errorf("docs requires %v", got)
66 }
67}
68
69// TestReadRefuses holds composed.Read to refusing what it cannot read
70// whole: no schema, a layer with no documents, and a layer that does not
71// compose.
72func TestReadRefuses(t *testing.T) {
73 r := newRig(t)
74 good := newLayers(t, r.glob(t, "examples/policy/smoke/instance/*.cue"), nil, nil)
75 empty := layers{instance: t.TempDir(), project: good.project}
76 broken := newLayers(t, r.glob(t, "examples/policy/smoke/instance/*.cue"), nil, map[string]string{"broken.cue": "package verification\nproject: classes: x: requires: nothing: true\n"})
77 for _, c := range []struct {
78 name, schema string
79 l layers
80 says string
81 }{
82 {"no schema", r.path("schema/absent.cue"), good, "no policy schema at"},
83 {"an empty floor", r.schema, empty, "no policy layer at"},
84 {"a layer that does not compose", r.schema, broken, "do not compose"},
85 } {
86 if _, err := composed.Read(t.Context(), r.cue, c.schema, c.l.instance, c.l.project); err == nil || !strings.Contains(err.Error(), c.says) {
87 t.Errorf("%s: %v, not %q", c.name, err, c.says)
88 }
89 }
90}
91
92// TestTheHardwareRequirementIsRead holds Compose and Read to
93// approval.hardware as cue composes it, over the fixtures under
94// examples/policy/approval/. A floor's `true` and a project's `true` both
95// require a hardware key. A template default the project turned off and a
96// policy that never mentions approval do not. A project's `false` against
97// the floor's `true` is refused, rather than read as either.
98func TestTheHardwareRequirementIsRead(t *testing.T) {
99 r := newRig(t)
100 fixture := func(name string) layers {
101 return newLayers(t, []string{r.path("examples/policy/approval/" + name + ".cue")}, nil, nil)
102 }
103 for _, c := range []struct {
104 fixture string
105 want bool
106 }{
107 {"floor-requires-hardware", true},
108 {"project-requires-hardware", true},
109 {"template-default-declined", false},
110 {"no-requirement", false},
111 } {
112 l := fixture(c.fixture)
113 pol, err := composed.Compose(t.Context(), r.cue, r.schema, l.instance, l.project)
114 if err != nil {
115 t.Fatalf("%s: %v", c.fixture, err)
116 }
117 m, err := r.read(l)
118 if err != nil {
119 t.Fatalf("%s: %v", c.fixture, err)
120 }
121 if pol.HardwareApproval != c.want || m.HardwareApproval != c.want {
122 t.Errorf("%s: Compose reads HardwareApproval %v and Read %v, not %v", c.fixture, pol.HardwareApproval, m.HardwareApproval, c.want)
123 }
124 }
125
126 l := fixture("rejected/project-drops-floor-hardware")
127 if pol, err := composed.Compose(t.Context(), r.cue, r.schema, l.instance, l.project); err == nil || !strings.Contains(err.Error(), "policy.approval.hardware") {
128 t.Errorf("a project dropping the floor's hardware requirement composed to %+v, %v", pol, err)
129 }
130 if m, err := r.read(l); err == nil || !strings.Contains(err.Error(), "policy.approval.hardware") {
131 t.Errorf("a project dropping the floor's hardware requirement was read as %+v, %v", m, err)
132 }
133}
134
135// TestDeriveOverAModel holds Derive's own rules, over a policy written
136// here rather than composed: the strictest carry mode among the matched
137// classes, a pin that is mandatory where any requirement of it is, a
138// class named unclassified kept apart from the unclassified set, and a
139// requirement of a check the policy does not define refused.
140func TestDeriveOverAModel(t *testing.T) {
141 nix := composed.Check{Runner: "nix", Cost: "quick"}
142 m := &composed.Model{
143 Policy: composed.Policy{Checks: map[string]composed.Check{"a": nix, "b": nix, "u": nix}},
144 Classes: map[string]composed.Class{
145 "loose": {Paths: []string{"loose/**"}, Requires: map[string]composed.Pin{"a": composed.Default}, Carry: "same-paths"},
146 "strict": {Paths: []string{"strict/*"}, Requires: map[string]composed.Pin{"a": composed.Mandatory, "b": composed.Default}, Carry: "same-class"},
147 "unclassified": {Paths: []string{"odd"}, Requires: map[string]composed.Pin{"b": composed.Default}, Carry: "exact"},
148 },
149 Unclassified: map[string]composed.Pin{"u": composed.Mandatory},
150 }
151 res, err := Derive(m, []string{"strict/x", "loose/y/z", "strict/x", "elsewhere"})
152 if err != nil {
153 t.Fatal(err)
154 }
155 want := &Result{
156 Paths: []string{"elsewhere", "loose/y/z", "strict/x"},
157 Classes: []string{"loose", "strict"},
158 Unclassified: []string{"elsewhere"},
159 Carry: "same-class",
160 Required: []Required{
161 {Name: "a", Classes: []string{"loose", "strict"}, Pin: composed.Mandatory, Check: nix},
162 {Name: "b", Classes: []string{"strict"}, Pin: composed.Default, Check: nix},
163 {Name: "u", Classes: []string{}, Unclassified: true, Pin: composed.Mandatory, Check: nix},
164 },
165 }
166 if !reflect.DeepEqual(res, want) {
167 t.Errorf("Derive = %+v\nwant %+v", res, want)
168 }
169 if res, err = Derive(m, []string{"odd"}); err != nil || res.Carry != "exact" || len(res.Required) != 1 || res.Required[0].Name != "b" || res.Required[0].Unclassified {
170 t.Errorf("the class named unclassified derives %+v, %v", res, err)
171 }
172 if res, err = Derive(m, nil); err != nil || res.Carry != "same-paths" || len(res.Required) != 0 {
173 t.Errorf("no paths derive %+v, %v", res, err)
174 }
175 m.Unclassified["ghost"] = composed.Mandatory
176 if _, err := Derive(m, []string{"elsewhere"}); err == nil || !strings.Contains(err.Error(), "defines no check") {
177 t.Errorf("a requirement of an undefined check derived: %v", err)
178 }
179}
180
181// TestChangedRefusesWhatGitDoesNotWrite holds Changed to a record cut
182// anywhere, and to the records git never writes.
183func TestChangedRefusesWhatGitDoesNotWrite(t *testing.T) {
184 got, err := Changed([]byte("R087\x00old\x00new\x00D\x00gone\nwith a newline\x00M\x00caf\xe9\x00"))
185 if want := []string{"old", "new", "gone\nwith a newline", "caf\xe9"}; err != nil || !reflect.DeepEqual(got, want) {
186 t.Errorf("Changed = %q, %v", got, err)
187 }
188 for _, out := range []string{"M", "M\x00", "M\x00a", "R100\x00old\x00", "M\x00a\x00R1", "Z\x00a\x00", "M\x00\x00", "\x00a\x00", "R1x\x00a\x00b\x00"} {
189 if got, err := Changed([]byte(out)); err == nil {
190 t.Errorf("Changed(%q) = %q", out, got)
191 }
192 }
193}
194
195// TestMatch is the glob language by example.
196func TestMatch(t *testing.T) {
197 for _, c := range []struct {
198 glob, path string
199 want bool
200 }{
201 {"src/*.rs", "src/one.rs", true},
202 {"src/*.rs", "src/deep/two.rs", false},
203 {"src/**/*.rs", "src/one.rs", true},
204 {"src/**/*.rs", "src/deep/two.rs", true},
205 {"a/**/b", "a/b", true},
206 {"a/**/b", "a/x/y/b", true},
207 {"a/**/b", "ab", false},
208 {"docs/**", "docs/x/y", true},
209 {"docs/**", "docs", false},
210 {"**/*.md", "README.md", true},
211 {"**/*.md", "a/b/c.md", true},
212 {"**", "", true},
213 {"a**b", "a/b", false},
214 {"a**b", "axxb", true},
215 {"?", "é", false}, // two bytes
216 {"??", "é", true},
217 {"?", "/", false},
218 {"*", "a\nb", true},
219 {"c.(x)", "c.(x)", true},
220 {"c.(x)", "cx(x)", false},
221 {"docs/", "docs", true}, // one trailing slash is dropped
222 {"docs//", "docs/", true},
223 } {
224 if got := Match(c.glob, c.path); got != c.want {
225 t.Errorf("Match(%q, %q) = %v", c.glob, c.path, got)
226 }
227 }
228}