gunk-dev/the-valley

check_test.go

source6bf8ca46b311main's published tip history
5.2 KiB
1package main
2
3import (
4 "errors"
5 "os"
6 "path/filepath"
7 "reflect"
8 "sync"
9 "testing"
10 "time"
11
12 "the-valley/note/statement"
13)
14
15// buildChecks builds checks side by side and decides each one from its own
16// derivation's build. Here it is driven by a stand-in for nix build that
17// holds every build until the test ends it. So how many builds run at
18// once, and the order they end in, are the test's to choose, whatever the
19// machine. Each derivation's result is injected: a pass, a failure with a
20// log, a failure with nothing on standard error, and a build with two
21// outputs. Two checks share one derivation, and one check never
22// evaluated.
23func TestEachBuildDecidesOnlyItsOwnChecks(t *testing.T) {
24 built := t.TempDir()
25 if err := os.WriteFile(filepath.Join(built, "result"), []byte("built\n"), 0o644); err != nil {
26 t.Fatal(err)
27 }
28 type result struct {
29 paths, log string
30 err error
31 }
32 results := map[string]result{
33 "a.drv": {paths: built + "\n"},
34 "b.drv": {log: "b: the builder failed\n", err: errors.New("exit status 1")},
35 "c.drv": {err: errors.New("signal: killed")},
36 "d.drv": {paths: built + "\n" + built + "-dev\n"},
37 }
38 names := []string{"first", "broken", "silent", "split", "twin", "unevaluated"}
39 drvs := []string{"a.drv", "b.drv", "c.drv", "d.drv", "a.drv", ""}
40 runs := make([]ranCheck, len(names))
41 for i, name := range names {
42 runs[i] = ranCheck{out: checkOutcome{spec: checkSpec{name: name, attribute: name}}}
43 }
44 unevaluated := errors.New("evaluating unevaluated: no such check")
45 runs[5].err = unevaluated
46
47 var mu sync.Mutex
48 var events []string
49 running, most := 0, 0
50 note := func(event string, change int) {
51 mu.Lock()
52 defer mu.Unlock()
53 running += change
54 most = max(most, running)
55 events = append(events, event)
56 }
57 started, ended := make(chan string, len(results)), make(chan string, len(results))
58 release := map[string]chan struct{}{}
59 for drv := range results {
60 release[drv] = make(chan struct{})
61 }
62 build := func(drv string) (string, string, error) {
63 note("start "+drv, 1)
64 started <- drv
65 <-release[drv]
66 note("end "+drv, -1)
67 ended <- drv
68 r := results[drv]
69 return r.paths, r.log, r.err
70 }
71 finished := make(chan struct{})
72 go func() {
73 defer close(finished)
74 buildChecks(runs, drvs, 2, build, func(r ranCheck) { note("report "+r.out.spec.name, 0) }, nil)
75 }()
76 await := func(c chan string, what string) string {
77 t.Helper()
78 select {
79 case drv := <-c:
80 return drv
81 case <-time.After(10 * time.Second):
82 t.Fatalf("no build %s", what)
83 return ""
84 }
85 }
86
87 // Two builds at once, started in the order of the checks. A third
88 // waits for one of them to end.
89 if first, second := await(started, "started"), await(started, "started"); first+second != "a.drvb.drv" && first+second != "b.drva.drv" {
90 t.Fatalf("%s and %s started first, want a.drv and b.drv", first, second)
91 }
92 if len(started) != 0 {
93 t.Fatalf("a third build started beside two")
94 }
95 // The builds end in another order than the checks': b, c and d before
96 // a. Each one that ends lets the next start.
97 for _, next := range [][2]string{{"b.drv", "c.drv"}, {"c.drv", "d.drv"}, {"d.drv", ""}} {
98 close(release[next[0]])
99 if drv := await(ended, "ended"); drv != next[0] {
100 t.Fatalf("%s ended, want %s", drv, next[0])
101 }
102 if next[1] != "" {
103 if drv := await(started, "started"); drv != next[1] {
104 t.Fatalf("%s started, want %s", drv, next[1])
105 }
106 }
107 }
108 close(release["a.drv"])
109 await(ended, "ended")
110 select {
111 case <-finished:
112 case <-time.After(10 * time.Second):
113 t.Fatal("the builds ended and the checks were never all reported")
114 }
115
116 // Nothing is reported before the first check's build ends, and then
117 // every check is reported in order. Each derivation was built once.
118 want := []string{
119 "start a.drv", "start b.drv", "end b.drv", "start c.drv", "end c.drv", "start d.drv", "end d.drv", "end a.drv",
120 "report first", "report broken", "report silent", "report split", "report twin", "report unevaluated",
121 }
122 if events[0] == "start b.drv" {
123 events[0], events[1] = events[1], events[0]
124 }
125 if !reflect.DeepEqual(events, want) {
126 t.Errorf("the builds and reports went\n %q\nwant\n %q", events, want)
127 }
128 if most != 2 {
129 t.Errorf("%d builds ran at once, with room for 2", most)
130 }
131
132 // Each check has its own derivation's outcome and log, and no other's.
133 entries, err := fsEntries(built)
134 if err != nil {
135 t.Fatal(err)
136 }
137 output := statement.TreeDigest(entries)
138 for i, check := range []struct {
139 passed bool
140 log string
141 err string
142 }{
143 {passed: true},
144 {log: "b: the builder failed"},
145 {log: "signal: killed"},
146 {err: "split produced 2 outputs; a check attribute must produce exactly one"},
147 {passed: true},
148 {err: unevaluated.Error()},
149 } {
150 r := runs[i]
151 gotErr := ""
152 if r.err != nil {
153 gotErr = r.err.Error()
154 }
155 if r.out.passed != check.passed || r.out.log != check.log || gotErr != check.err {
156 t.Errorf("%s: passed %v, log %q, error %q; want passed %v, log %q, error %q",
157 names[i], r.out.passed, r.out.log, gotErr, check.passed, check.log, check.err)
158 }
159 if check.passed && r.out.output != output {
160 t.Errorf("%s: output %s, want %s", names[i], r.out.output, output)
161 }
162 if !check.passed && r.out.output != "" {
163 t.Errorf("%s did not pass and still has an output digest", names[i])
164 }
165 }
166}