gunk-dev/the-valley

model_test.go

sourced67699705b80main's published tip history
8.1 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// TestDeriveOverAModel holds Derive's own rules, over a policy written
93// here rather than composed: the strictest carry mode among the matched
94// classes, a pin that is mandatory where any requirement of it is, a
95// class named unclassified kept apart from the unclassified set, and a
96// requirement of a check the policy does not define refused.
97func TestDeriveOverAModel(t *testing.T) {
98 nix := composed.Check{Runner: "nix", Cost: "quick"}
99 m := &composed.Model{
100 Policy: composed.Policy{Checks: map[string]composed.Check{"a": nix, "b": nix, "u": nix}},
101 Classes: map[string]composed.Class{
102 "loose": {Paths: []string{"loose/**"}, Requires: map[string]composed.Pin{"a": composed.Default}, Carry: "same-paths"},
103 "strict": {Paths: []string{"strict/*"}, Requires: map[string]composed.Pin{"a": composed.Mandatory, "b": composed.Default}, Carry: "same-class"},
104 "unclassified": {Paths: []string{"odd"}, Requires: map[string]composed.Pin{"b": composed.Default}, Carry: "exact"},
105 },
106 Unclassified: map[string]composed.Pin{"u": composed.Mandatory},
107 }
108 res, err := Derive(m, []string{"strict/x", "loose/y/z", "strict/x", "elsewhere"})
109 if err != nil {
110 t.Fatal(err)
111 }
112 want := &Result{
113 Paths: []string{"elsewhere", "loose/y/z", "strict/x"},
114 Classes: []string{"loose", "strict"},
115 Unclassified: []string{"elsewhere"},
116 Carry: "same-class",
117 Required: []Required{
118 {Name: "a", Classes: []string{"loose", "strict"}, Pin: composed.Mandatory, Check: nix},
119 {Name: "b", Classes: []string{"strict"}, Pin: composed.Default, Check: nix},
120 {Name: "u", Classes: []string{}, Unclassified: true, Pin: composed.Mandatory, Check: nix},
121 },
122 }
123 if !reflect.DeepEqual(res, want) {
124 t.Errorf("Derive = %+v\nwant %+v", res, want)
125 }
126 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 {
127 t.Errorf("the class named unclassified derives %+v, %v", res, err)
128 }
129 if res, err = Derive(m, nil); err != nil || res.Carry != "same-paths" || len(res.Required) != 0 {
130 t.Errorf("no paths derive %+v, %v", res, err)
131 }
132 m.Unclassified["ghost"] = composed.Mandatory
133 if _, err := Derive(m, []string{"elsewhere"}); err == nil || !strings.Contains(err.Error(), "defines no check") {
134 t.Errorf("a requirement of an undefined check derived: %v", err)
135 }
136}
137
138// TestChangedRefusesWhatGitDoesNotWrite holds Changed to a record cut
139// anywhere, and to the records git never writes.
140func TestChangedRefusesWhatGitDoesNotWrite(t *testing.T) {
141 got, err := Changed([]byte("R087\x00old\x00new\x00D\x00gone\nwith a newline\x00M\x00caf\xe9\x00"))
142 if want := []string{"old", "new", "gone\nwith a newline", "caf\xe9"}; err != nil || !reflect.DeepEqual(got, want) {
143 t.Errorf("Changed = %q, %v", got, err)
144 }
145 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"} {
146 if got, err := Changed([]byte(out)); err == nil {
147 t.Errorf("Changed(%q) = %q", out, got)
148 }
149 }
150}
151
152// TestMatch is the glob language by example.
153func TestMatch(t *testing.T) {
154 for _, c := range []struct {
155 glob, path string
156 want bool
157 }{
158 {"src/*.rs", "src/one.rs", true},
159 {"src/*.rs", "src/deep/two.rs", false},
160 {"src/**/*.rs", "src/one.rs", true},
161 {"src/**/*.rs", "src/deep/two.rs", true},
162 {"a/**/b", "a/b", true},
163 {"a/**/b", "a/x/y/b", true},
164 {"a/**/b", "ab", false},
165 {"docs/**", "docs/x/y", true},
166 {"docs/**", "docs", false},
167 {"**/*.md", "README.md", true},
168 {"**/*.md", "a/b/c.md", true},
169 {"**", "", true},
170 {"a**b", "a/b", false},
171 {"a**b", "axxb", true},
172 {"?", "é", false}, // two bytes
173 {"??", "é", true},
174 {"?", "/", false},
175 {"*", "a\nb", true},
176 {"c.(x)", "c.(x)", true},
177 {"c.(x)", "cx(x)", false},
178 {"docs/", "docs", true}, // one trailing slash is dropped
179 {"docs//", "docs/", true},
180 } {
181 if got := Match(c.glob, c.path); got != c.want {
182 t.Errorf("Match(%q, %q) = %v", c.glob, c.path, got)
183 }
184 }
185}