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