gunk-dev/the-valley

mainstate_test.go

source60ad80d5a5a5main's published tip history
6.9 KiB
1package main
2
3import (
4 "reflect"
5 "strings"
6 "testing"
7)
8
9// Main reads clear, clearing or muddy from the walk and the worker's runs:
10// a failure, in a build or an evaluation, counts only when it is newer
11// than the check's last pass. A landing is named by its change's handle,
12// and a main commit without a change ID by its commit ID.
13func TestMainLines(t *testing.T) {
14 c3, c2, c1 := "c3000000000", "c2000000000", "c1000000000"
15 walk := func(checks ...followCheck) mainWalk {
16 return mainWalk{checks: checks, walked: []string{c3, c2, c1}, tip: "t3",
17 trees: map[string]string{"t3": c3, "t2": c2, "t1": c1}, ids: map[string]string{c3: "kkkk", c2: ""}}
18 }
19 h := handleMap{{"app", recordTarget, "kkkk"}: "kk"}
20 e2e := nixCheck{"worker-e2e", "worker-e2e"}
21 due := followCheck{nixCheck: e2e, due: true, covers: []string{c3, c2}, passed: c1}
22 failedAt := func(tree string) []checkBuild {
23 return []checkBuild{{project: "app", check: "worker-e2e", tree: tree, result: "failed"}}
24 }
25 // A check that did not evaluate records no build time (attest/check.go).
26 page, _, err := parsePage(strings.NewReader(`{"kind":"record","type":"check.result","project":"app","tree":"t2",` +
27 `"check":"worker-e2e","data":{"result":"error","eval_ms":900}}` + "\n" + `{"kind":"end","cursor":""}` + "\n"))
28 if err != nil || len(page.builds) != 1 {
29 t.Fatalf("the page of a check that did not evaluate read as %v, %v", page.builds, err)
30 }
31 unevaluated := []checkBuild{page.builds[0].checkBuild}
32 running := []mainRun{{project: "app", tree: "t3", run: "r", checks: []string{"worker-e2e"}}}
33 term := terminal{width: 80}
34 for _, tc := range []struct {
35 name string
36 walk mainWalk
37 builds []checkBuild
38 runs []mainRun
39 want []string
40 }{
41 {"passed", walk(followCheck{nixCheck: e2e, passed: c2}), nil, nil,
42 []string{"main ✓ clear · checks passed at c200000"}},
43 {"due", walk(due), nil, nil,
44 []string{"main ◐ clearing · worker-e2e due · covers kk, c200000"}},
45 {"running", walk(due), nil, running,
46 []string{"main ◐ clearing · worker-e2e running · covers kk, c200000"}},
47 {"failed since its pass", walk(due), failedAt("t2"), nil,
48 []string{"main ✗ muddy · worker-e2e failed at c200000 · covers kk, c200000",
49 " see: valley telemetry read --source worker"}},
50 {"did not evaluate", walk(due), unevaluated, nil,
51 []string{"main ✗ muddy · worker-e2e failed at c200000 · covers kk, c200000",
52 " see: valley telemetry read --source worker"}},
53 {"failed before its pass", walk(due), failedAt("t1"), nil,
54 []string{"main ◐ clearing · worker-e2e due · covers kk, c200000"}},
55 } {
56 if got := term.mainLines(tc.walk, "main ", "app", h, tc.builds, tc.runs); !reflect.DeepEqual(got, tc.want) {
57 t.Errorf("%s: got %q, want %q", tc.name, got, tc.want)
58 }
59 }
60}
61
62// A check request's batch carries no change, as a follow run's does, and
63// its start is not a run on main. A check's result counts by the tree it
64// is about: at a tree the walk read on main it is a run on main whoever
65// asked for it, and at any other it is not, even when the records naming
66// its batch's class were not read and it is the check's newest result.
67// Each tree is judged by its own newest result, so a pass at an older
68// commit's tree hides no failure at a newer one's. A
69// worker that names no class ran no request, so its batches with no
70// change read as follow runs, as they always did.
71func TestARequestBatchIsNotARunOnMain(t *testing.T) {
72 c2, c1 := "c2000000000", "c1000000000"
73 walk := mainWalk{walked: []string{c2, c1}, tip: "t2", trees: map[string]string{"t2": c2, "t1": c1},
74 checks: []followCheck{{nixCheck: nixCheck{"worker-e2e", "worker-e2e"}, due: true, passed: c1}}}
75 term := terminal{width: 80}
76 read := func(lines ...string) workerPage {
77 t.Helper()
78 page, _, err := parsePage(strings.NewReader(strings.Join(append(lines, `{"kind":"end","cursor":""}`), "\n") + "\n"))
79 if err != nil {
80 t.Fatal(err)
81 }
82 return page
83 }
84 batch := func(typ, run, class, tree string) string {
85 data := `"attest_run":"` + run + `","checks":["worker-e2e"]`
86 if class != "" {
87 data += `,"class":"` + class + `"`
88 }
89 return `{"kind":"record","type":"` + typ + `","project":"app","tree":"` + tree + `","data":{` + data + `}}`
90 }
91 result := func(run, tree, result string) string {
92 return `{"kind":"record","type":"check.result","run":"` + run + `","project":"app","tree":"` + tree +
93 `","check":"worker-e2e","data":{"result":"` + result + `","build_ms":5000}}`
94 }
95 for _, tc := range []struct {
96 name string
97 lines []string
98 want string
99 }{
100 {"a request running at main's tip", []string{batch("batch.start", "q", "request", "t2")},
101 "main ◐ clearing · worker-e2e due"},
102 {"a request that failed at a main tree", []string{
103 batch("batch.end", "q", "request", "t2"), result("q", "t2", "failed"), batch("batch.start", "q", "request", "t2")},
104 "main ✗ muddy · worker-e2e failed at c200000"},
105 {"a request that failed at another tree, its batch unread", []string{result("q", "t9", "failed")},
106 "main ◐ clearing · worker-e2e due"},
107 {"a request's newest result at another tree, its batch unread, since a follow run failed", []string{
108 result("q", "t9", "passed"),
109 batch("batch.end", "f", "follow", "t2"), result("f", "t2", "failed"), batch("batch.start", "f", "follow", "t2")},
110 "main ✗ muddy · worker-e2e failed at c200000"},
111 {"a request that passed since a follow run failed", []string{
112 batch("batch.end", "q", "request", "t9"), result("q", "t9", "passed"), batch("batch.start", "q", "request", "t9"),
113 batch("batch.end", "f", "follow", "t2"), result("f", "t2", "failed"), batch("batch.start", "f", "follow", "t2")},
114 "main ✗ muddy · worker-e2e failed at c200000"},
115 {"a request's pass at an older commit's tree since the tip's run failed", []string{
116 result("q", "t1", "passed"),
117 batch("batch.end", "f", "follow", "t2"), result("f", "t2", "failed"), batch("batch.start", "f", "follow", "t2")},
118 "main ✗ muddy · worker-e2e failed at c200000"},
119 {"a follow run running", []string{batch("batch.start", "f", "follow", "t2")},
120 "main ◐ clearing · worker-e2e running"},
121 {"a run that names no class", []string{batch("batch.start", "o", "", "t2")},
122 "main ◐ clearing · worker-e2e running"},
123 {"a run that names no class and failed", []string{
124 batch("batch.end", "o", "", "t2"), result("o", "t2", "failed"), batch("batch.start", "o", "", "t2")},
125 "main ✗ muddy · worker-e2e failed at c200000"},
126 } {
127 page := read(tc.lines...)
128 var builds []checkBuild
129 for _, b := range page.builds {
130 builds = append(builds, b.checkBuild)
131 }
132 if got := term.mainLines(walk, "main ", "app", nil, builds, page.mains); got[0] != tc.want {
133 t.Errorf("%s: got %q, want %q", tc.name, got, tc.want)
134 }
135 }
136 ready := read(`{"kind":"record","type":"batch.start","project":"app","change":"kkkk","tree":"t2","data":{"attest_run":"g","class":"ready"}}`)
137 if len(ready.mains) != 0 {
138 t.Errorf("a ready change's batch read as %v", ready.mains)
139 }
140}