gunk-dev/the-valley

check_test.go

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