gunk-dev/the-valley

policy_test.go

source813c6679e1c2main's published tip history
4.3 KiB
1package main
2
3// The policy bridge, where this program is coupled to two others: the
4// deriver's output, and the window a policy writes down. Both are pinned
5// here rather than assumed.
6
7import (
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
12 "time"
13)
14
15func TestDeriverOutputIsRead(t *testing.T) {
16 out := `abc1234..def5678: 3 changed path(s)
17policy: instance + policy
18classes matched: code, prose
19
20required checks:
21 code-build mandatory code
22 prose-format default prose, knowledge
23
24unclassified: 1 path(s) matched no class
25 odd.txt
26`
27 d, err := parseDerived(out)
28 if err != nil {
29 t.Fatal(err)
30 }
31 if !d.classes["code"] || !d.classes["prose"] || len(d.classes) != 2 {
32 t.Fatalf("classes = %v", d.classes)
33 }
34 if got := strings.Join(d.requires["prose-format"], "+"); got != "prose+knowledge" {
35 t.Fatalf("prose-format required by %q", got)
36 }
37 if got := strings.Join(d.requires["code-build"], "+"); got != "code" {
38 t.Fatalf("code-build required by %q", got)
39 }
40 if len(d.requires) != 2 {
41 t.Fatalf("required = %v", d.requires)
42 }
43}
44
45func TestValidityWindowsAreReadAsThePolicyWritesThem(t *testing.T) {
46 for _, c := range []struct {
47 in string
48 want time.Duration
49 }{
50 {"", 0},
51 {"30s", 30 * time.Second},
52 {"15m", 15 * time.Minute},
53 {"6h", 6 * time.Hour},
54 {"2d", 48 * time.Hour},
55 } {
56 got, err := parseWindow(c.in)
57 if err != nil || got != c.want {
58 t.Errorf("parseWindow(%q) = %v, %v; want %v", c.in, got, err, c.want)
59 }
60 }
61}
62
63// Each layer's absence defaults in the safe direction of that layer's job,
64// and the two directions are opposite (dcr-f41f718). Both are pinned here,
65// because the value of the rule is that neither is the other.
66
67func TestATargetWithNoProjectLayerIsJudgedUnderTheFloorAlone(t *testing.T) {
68 tip := &worktree{dir: t.TempDir()}
69 p := policySource{projectDir: "policy/project"}
70 done, err := p.openProject(tip)
71 if err != nil {
72 t.Fatal(err)
73 }
74 defer done()
75 if p.note == "" {
76 t.Fatal("the pass said nothing about composing the floor alone")
77 }
78 // What the deriver and the composer are handed is a layer that adds
79 // nothing, not a directory that is not there.
80 docs, err := filepath.Glob(filepath.Join(p.project, "*.cue"))
81 if err != nil || len(docs) != 1 {
82 t.Fatalf("the resolved project layer holds %v (%v)", docs, err)
83 }
84 if body, err := os.ReadFile(docs[0]); err != nil || strings.TrimSpace(string(body)) != "package verification" {
85 t.Fatalf("the layer that adds nothing reads %q (%v)", body, err)
86 }
87 done()
88 if _, err := os.Stat(p.project); !os.IsNotExist(err) {
89 t.Fatalf("the pass left its scratch layer behind: %v", err)
90 }
91}
92
93func TestATargetsOwnProjectLayerIsUsedWhereItIs(t *testing.T) {
94 tip := &worktree{dir: t.TempDir()}
95 own := filepath.Join(tip.dir, "policy/project")
96 if err := os.MkdirAll(own, 0o755); err != nil {
97 t.Fatal(err)
98 }
99 if err := os.WriteFile(filepath.Join(own, "policy.cue"), []byte("package verification\n"), 0o644); err != nil {
100 t.Fatal(err)
101 }
102 p := policySource{projectDir: "policy/project"}
103 done, err := p.openProject(tip)
104 if err != nil {
105 t.Fatal(err)
106 }
107 defer done()
108 if p.project != own {
109 t.Fatalf("the project layer resolved to %s, not to %s", p.project, own)
110 }
111 if p.note != "" {
112 t.Fatalf("an ordinary composition said %q", p.note)
113 }
114}
115
116func TestAFloorThatIsNotThereIsRefused(t *testing.T) {
117 // The materialized source, which is the one a test can drive without a
118 // repository. The repository source refuses the same way, and the e2e
119 // observes it there.
120 _, done, err := instanceLayer{path: t.TempDir()}.open()
121 defer done()
122 if err == nil {
123 t.Fatal("a floor directory with no documents in it was accepted")
124 }
125 if !strings.Contains(err.Error(), floorless) {
126 t.Fatalf("the refusal reads %q", err)
127 }
128}
129
130func TestARefusalIsReadAsTheSentenceItIs(t *testing.T) {
131 // attest reports what it checked, then refuses, then elaborates. The
132 // sentence is the refusal; the elaboration is not.
133 out := `note /tmp/x.note
134text ok (411 bytes signed)
135attest: tree digest mismatch: the statement is about a different tree
136 recorded aaaa
137 found bbbb
138`
139 if got := refusalLine(out); got != "tree digest mismatch: the statement is about a different tree" {
140 t.Fatalf("refusalLine = %q", got)
141 }
142 if got := refusalLine("something with no prefix at all\n"); got != "something with no prefix at all" {
143 t.Fatalf("refusalLine fallback = %q", got)
144 }
145}