gunk-dev/the-valley

checksfix_test.go

source672eff5ffaecmain's published tip history
3.1 KiB
1package checksfix
2
3import (
4 "context"
5 "fmt"
6 "strings"
7 "testing"
8 "time"
9
10 "the-valley/flow/internal/restack"
11 "the-valley/flow/internal/valley"
12)
13
14// TestPassCountsAttempts drives passes over a change whose checks keep
15// failing on each new head. Each head gets one run; a run whose next head
16// still fails counts as a failed attempt, and after 3 klaus stops, until
17// checks pass and the count starts over. A change another principal owns
18// gets nothing.
19func TestPassCountsAttempts(t *testing.T) {
20 dir := t.TempDir()
21 l, err := LoadLedger(dir)
22 if err != nil {
23 t.Fatal(err)
24 }
25 var launched []Launch
26 n := 0
27 d := Deps{
28 Principal: func(context.Context, string) string { return "operator" },
29 NewRunID: func() (string, error) { n++; return fmt.Sprintf("run-%d", n), nil },
30 Launch: func(_ context.Context, la Launch) error { launched = append(launched, la); return nil },
31 Running: func(restack.Key, string) bool { return false },
32 Now: time.Now,
33 }
34 repos := []restack.Repo{{Name: "widget", Dir: dir}}
35 listing := func(owner, head, state string) *valley.Changes {
36 v := valley.Version{N: 1, Head: head, Branch: "agent/x", Landing: valley.NotLanded,
37 Checks: &valley.Checks{State: state, Required: []valley.Check{{Name: "web-unit", State: state, Why: "boom"}}}}
38 return &valley.Changes{Repos: []valley.Repo{{Repo: "widget", Changes: []valley.Change{{
39 ChangeID: "kxyz", Handle: "kxyz", Owner: owner, State: valley.StateReady, Active: 1, Versions: []valley.Version{v}}}}}}
40 }
41 pass := func(owner, head, state, want string) {
42 t.Helper()
43 lines := Pass(context.Background(), d, l, repos, listing(owner, head, state), true)
44 if got := strings.Join(lines, "\n"); !strings.Contains(got, want) {
45 t.Errorf("head %s %s: pass said %q, want %q", head, state, got, want)
46 }
47 }
48
49 pass("someone-else", "h0", valley.ChecksFailed, "")
50 if len(launched) != 0 {
51 t.Fatalf("another owner's change got a run: %+v", launched)
52 }
53 pass("operator", "h1", valley.ChecksFailed, "fix run run-1 dispatched (attempt 1 of 3)")
54 pass("operator", "h1", valley.ChecksFailed, "fix run run-1 dispatched (attempt 1 of 3)")
55 pass("operator", "h2", valley.ChecksFailed, "fix run run-2 dispatched (attempt 2 of 3)")
56 pass("operator", "h3", valley.ChecksFailed, "fix run run-3 dispatched (attempt 3 of 3)")
57 pass("operator", "h4", valley.ChecksFailed, "stopped after 3 fix runs")
58 pass("operator", "h5", valley.ChecksFailed, "stopped after 3 fix runs")
59 if len(launched) != 3 {
60 t.Fatalf("launched %d runs, want 3", len(launched))
61 }
62 if la := launched[2]; la.Head != "h3" || la.Owner != "operator" || la.Branch != "agent/x" || la.ChangeID != "kxyz" {
63 t.Errorf("the third launch expects %+v", la)
64 }
65
66 pass("operator", "h5", valley.ChecksPassed, "")
67 pass("operator", "h6", valley.ChecksFailed, "fix run run-4 dispatched (attempt 1 of 3)")
68
69 // A restarted pass reads what the ledger recorded.
70 if l, err = LoadLedger(dir); err != nil {
71 t.Fatal(err)
72 }
73 pass("operator", "h6", valley.ChecksFailed, "fix run run-4 dispatched (attempt 1 of 3)")
74 if len(launched) != 4 {
75 t.Errorf("launched %d runs, want 4", len(launched))
76 }
77}