gunk-dev/the-valley

closures_test.go

source0820acba4decmain's published tip history
7.6 KiB
1package main
2
3import (
4 "the-valley/integrator/verdict"
5
6 "fmt"
7 "os"
8 "path/filepath"
9 "sort"
10 "strings"
11 "testing"
12)
13
14// inputsLine is the line attest inputs prints for a check, with digests
15// made from its name so every check's are its own.
16func inputsLine(name string) string {
17 return fmt.Sprintf("%s valley-inputs-v1:%s sha256:%s\n", name, digestOf(name, "i"), digestOf(name, "d"))
18}
19
20func digestOf(name, kind string) string {
21 return sha256hex([]byte(kind + name))
22}
23
24func TestParseInputsTakesExactlyTheChecksAskedFor(t *testing.T) {
25 asked := []string{"a", "b"}
26 a, b := inputsLine("a"), inputsLine("b")
27 got, err := parseInputs(b+a, asked)
28 if err != nil {
29 t.Fatalf("a well-formed answer in another order was refused: %v", err)
30 }
31 if got["a"].Inputs != digestOf("a", "i") || got["a"].Derivation != digestOf("a", "d") || got["b"].Inputs != digestOf("b", "i") || len(got) != 2 {
32 t.Errorf("closures were not matched to their checks by name: %v", got)
33 }
34
35 hex := digestOf("a", "i")
36 for why, out := range map[string]string{
37 "nothing": "",
38 "a check missing": a,
39 "a check not asked for": a + b + inputsLine("c"),
40 "a check twice": a + b + a,
41 "two fields": a + "b valley-inputs-v1:" + hex + "\n",
42 "four fields": a + strings.TrimSuffix(b, "\n") + " extra\n",
43 "a doubled space": a + strings.Replace(b, " ", " ", 1),
44 "a blank line": a + "\n" + b,
45 "another inputs scheme": a + "b valley-inputs-v2:" + hex + " sha256:" + hex + "\n",
46 "another derivation hash": a + "b valley-inputs-v1:" + hex + " sha512:" + hex + "\n",
47 "an uppercase digest": a + "b valley-inputs-v1:" + strings.ToUpper(hex) + " sha256:" + hex + "\n",
48 "a short digest": a + "b valley-inputs-v1:" + hex[1:] + " sha256:" + hex + "\n",
49 "a digest that is not hex": a + "b valley-inputs-v1:" + hex + " sha256:" + hex[1:] + "g\n",
50 } {
51 if got, err := parseInputs(out, asked); err == nil {
52 t.Errorf("an answer with %s was taken: %v", why, got)
53 }
54 }
55}
56
57// fakeAttest is an attest that answers `attest inputs` from files in its
58// directory and logs the checks of every call, one call per line. Asked
59// for several checks, it prints batch.out and batch.err and exits with
60// batch.status. Asked for one, it fails with <name>.err when that exists,
61// and otherwise prints the check's line.
62func fakeAttest(t *testing.T) (*integrator, string) {
63 t.Helper()
64 dir := t.TempDir()
65 script := `#!/bin/sh
66dir="$(dirname "$0")"
67names=""
68while [ $# -gt 0 ]; do
69 case "$1" in --check) names="$names ${2%%=*}"; shift 2 ;; *) shift ;; esac
70done
71echo $names >> "$dir/calls"
72set -- $names
73if [ $# -gt 1 ]; then
74 cat "$dir/batch.out" 2> /dev/null
75 cat "$dir/batch.err" >&2 2> /dev/null
76 exit "$(cat "$dir/batch.status")"
77fi
78if [ -f "$dir/$1.err" ]; then cat "$dir/$1.err" >&2; exit 1; fi
79cat "$dir/$1.line"
80`
81 if err := os.WriteFile(filepath.Join(dir, "attest"), []byte(script), 0o755); err != nil {
82 t.Fatal(err)
83 }
84 for _, name := range []string{"a", "b", "c"} {
85 write(t, dir, name+".line", inputsLine(name))
86 }
87 return &integrator{attest: filepath.Join(dir, "attest")}, dir
88}
89
90func write(t *testing.T, dir, name, content string) {
91 t.Helper()
92 if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil {
93 t.Fatal(err)
94 }
95}
96
97// fakeCalls is the checks of each call the fake attest was given.
98func fakeCalls(t *testing.T, dir string) []string {
99 t.Helper()
100 b, err := os.ReadFile(filepath.Join(dir, "calls"))
101 if os.IsNotExist(err) {
102 return nil
103 }
104 if err != nil {
105 t.Fatal(err)
106 }
107 return strings.Split(strings.TrimSpace(string(b)), "\n")
108}
109
110// partitioned fails the test unless every check asked for is in exactly
111// one of the two maps, and nothing else is in either.
112func partitioned(t *testing.T, names []string, got map[string]verdict.Closure, bad map[string]string) {
113 t.Helper()
114 var all []string
115 for name := range got {
116 all = append(all, name)
117 if _, both := bad[name]; both {
118 t.Errorf("%s has both a closure and an error", name)
119 }
120 }
121 for name, why := range bad {
122 all = append(all, name)
123 if why == "" {
124 t.Errorf("%s has an empty error", name)
125 }
126 }
127 sort.Strings(all)
128 if strings.Join(all, " ") != strings.Join(names, " ") {
129 t.Errorf("the checks accounted for are %v, not %v", all, names)
130 }
131}
132
133func pure(names ...string) []verdict.Required {
134 var required []verdict.Required
135 for _, name := range names {
136 required = append(required, verdict.Required{Name: name, Runner: "nix", Attribute: name})
137 }
138 return required
139}
140
141func TestClosuresAsksOnceForEveryPureCheck(t *testing.T) {
142 in, dir := fakeAttest(t)
143 wt := &worktree{dir: t.TempDir()}
144
145 got, bad := in.closures(wt, "HEAD", []verdict.Required{{Name: "reachable", Runner: "command"}})
146 if calls := fakeCalls(t, dir); len(calls) != 0 || len(got)+len(bad) != 0 {
147 t.Errorf("no pure check, and attest was run %d times: %v %v", len(calls), got, bad)
148 }
149
150 write(t, dir, "batch.out", inputsLine("c")+inputsLine("a")+inputsLine("b"))
151 write(t, dir, "batch.err", "warning: nix said something on standard error\n")
152 write(t, dir, "batch.status", "0")
153 got, bad = in.closures(wt, "HEAD", append(pure("c", "a", "b"), verdict.Required{Name: "reachable", Runner: "command"}))
154 if calls := fakeCalls(t, dir); len(calls) != 1 || calls[0] != "a b c" {
155 t.Errorf("three pure checks were asked for in %q, not once in sorted order", calls)
156 }
157 partitioned(t, []string{"a", "b", "c"}, got, bad)
158 if len(bad) != 0 || got["b"].Inputs != digestOf("b", "i") {
159 t.Errorf("a good answer was not taken whole: %v %v", got, bad)
160 }
161}
162
163func TestClosuresAsksAgainOneAtATime(t *testing.T) {
164 for _, c := range []struct {
165 why, out, status string
166 }{
167 {"failed", inputsLine("a"), "1"},
168 {"left a check out", inputsLine("a") + inputsLine("c"), "0"},
169 {"printed a malformed line", inputsLine("a") + "b valley-inputs-v1:x sha256:y\n" + inputsLine("c"), "0"},
170 } {
171 t.Run(c.why, func(t *testing.T) {
172 in, dir := fakeAttest(t)
173 write(t, dir, "batch.out", c.out)
174 write(t, dir, "batch.err", "attest: evaluating b: the batch went wrong\n")
175 write(t, dir, "batch.status", c.status)
176 write(t, dir, "b.err", "attest: evaluating b: exit status 1: error: b does not evaluate\nmore detail\n")
177 got, bad := in.closures(&worktree{dir: t.TempDir()}, "HEAD", pure("a", "b", "c"))
178 if calls := fakeCalls(t, dir); strings.Join(calls, ",") != "a b c,a,b,c" {
179 t.Errorf("attest was asked for %q, not the batch and then each check", calls)
180 }
181 partitioned(t, []string{"a", "b", "c"}, got, bad)
182 if bad["b"] != "attest: evaluating b: exit status 1: error: b does not evaluate" {
183 t.Errorf("b's error is %q, not its own first line", bad["b"])
184 }
185 if got["a"].Inputs != digestOf("a", "i") || got["c"].Derivation != digestOf("c", "d") {
186 t.Errorf("the checks that evaluate did not keep their closures: %v", got)
187 }
188 })
189 }
190}
191
192func TestClosuresAsksOneCheckOnce(t *testing.T) {
193 in, dir := fakeAttest(t)
194 write(t, dir, "a.err", "attest: evaluating a: no such attribute\n")
195 got, bad := in.closures(&worktree{dir: t.TempDir()}, "HEAD", pure("a"))
196 if calls := fakeCalls(t, dir); len(calls) != 1 {
197 t.Errorf("one failing check was asked for %d times", len(calls))
198 }
199 partitioned(t, []string{"a"}, got, bad)
200 if bad["a"] != "attest: evaluating a: no such attribute" {
201 t.Errorf("a's error is %q", bad["a"])
202 }
203}
204
205func TestClosuresReportsAnAttestThatDoesNotStart(t *testing.T) {
206 in := &integrator{attest: filepath.Join(t.TempDir(), "absent")}
207 got, bad := in.closures(&worktree{dir: t.TempDir()}, "HEAD", pure("a", "b"))
208 partitioned(t, []string{"a", "b"}, got, bad)
209 if !strings.HasPrefix(bad["a"], "attest inputs: ") {
210 t.Errorf("an attest that did not start is reported as %q", bad["a"])
211 }
212}