| 1 | package checksfix |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "the-valley/flow/internal/event" |
| 13 | "the-valley/flow/internal/restack" |
| 14 | "the-valley/flow/internal/valley" |
| 15 | ) |
| 16 | |
| 17 | // TestPassCountsAttempts drives passes over a change whose checks keep |
| 18 | // failing on each new head. Each head gets one run; a run whose next head |
| 19 | // still fails counts as a failed attempt, and after 3 klaus stops, until |
| 20 | // checks pass and the count starts over. A change another principal owns |
| 21 | // gets nothing. |
| 22 | func TestPassCountsAttempts(t *testing.T) { |
| 23 | dir := t.TempDir() |
| 24 | l, err := LoadLedger(dir) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | var launched []Launch |
| 29 | var stalls []map[string]interface{} |
| 30 | n := 0 |
| 31 | d := Deps{ |
| 32 | Emit: func(runID, typ string, data map[string]interface{}) { |
| 33 | if runID != "kxyz" || typ != event.ChangeChecksStalled { |
| 34 | t.Errorf("emitted %s under %s", typ, runID) |
| 35 | } |
| 36 | stalls = append(stalls, data) |
| 37 | }, |
| 38 | Principal: func(context.Context, string) string { return "operator" }, |
| 39 | NewRunID: func() (string, error) { n++; return fmt.Sprintf("run-%d", n), nil }, |
| 40 | Launch: func(_ context.Context, la Launch) error { launched = append(launched, la); return nil }, |
| 41 | Running: func(restack.Key, string) bool { return false }, |
| 42 | Now: time.Now, |
| 43 | } |
| 44 | repos := []restack.Repo{{Name: "widget", Dir: dir}} |
| 45 | listing := func(owner, head, state string) *valley.Changes { |
| 46 | v := valley.Version{N: 1, Head: head, Branch: "agent/x", Landing: valley.NotLanded, |
| 47 | Checks: &valley.Checks{State: state, Required: []valley.Check{{Name: "web-unit", State: state, Why: "boom"}}}} |
| 48 | return &valley.Changes{Repos: []valley.Repo{{Repo: "widget", Changes: []valley.Change{{ |
| 49 | ChangeID: "kxyz", Handle: "kxyz", Owner: owner, State: valley.StateReady, Active: 1, Versions: []valley.Version{v}}}}}} |
| 50 | } |
| 51 | pass := func(owner, head, state, want string) { |
| 52 | t.Helper() |
| 53 | views, err := Pass(context.Background(), d, l, repos, listing(owner, head, state), true) |
| 54 | if err != nil { |
| 55 | t.Fatalf("head %s %s: %v", head, state, err) |
| 56 | } |
| 57 | var lines []string |
| 58 | for _, v := range views { |
| 59 | lines = append(lines, v.String()) |
| 60 | } |
| 61 | if got := strings.Join(lines, "\n"); !strings.Contains(got, want) { |
| 62 | t.Errorf("head %s %s: pass said %q, want %q", head, state, got, want) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | pass("someone-else", "h0", valley.ChecksFailed, "") |
| 67 | if len(launched) != 0 { |
| 68 | t.Fatalf("another owner's change got a run: %+v", launched) |
| 69 | } |
| 70 | pass("operator", "h1", valley.ChecksFailed, "fix run run-1 dispatched (attempt 1 of 3)") |
| 71 | pass("operator", "h1", valley.ChecksFailed, "fix run run-1 dispatched (attempt 1 of 3)") |
| 72 | pass("operator", "h2", valley.ChecksFailed, "fix run run-2 dispatched (attempt 2 of 3)") |
| 73 | pass("operator", "h3", valley.ChecksFailed, "fix run run-3 dispatched (attempt 3 of 3)") |
| 74 | if len(stalls) != 0 { |
| 75 | t.Errorf("stalled while fix runs were left: %v", stalls) |
| 76 | } |
| 77 | pass("operator", "h4", valley.ChecksFailed, "stopped after 3 fix runs") |
| 78 | pass("operator", "h4", valley.ChecksFailed, "stopped after 3 fix runs") |
| 79 | if len(stalls) != 1 || stalls[0]["head"] != "h4" || stalls[0]["attempts"] != 3 || stalls[0]["reason"] != "stopped after 3 fix runs" { |
| 80 | t.Errorf("one stall for h4, want reported once: %v", stalls) |
| 81 | } |
| 82 | pass("operator", "h5", valley.ChecksFailed, "stopped after 3 fix runs") |
| 83 | if len(stalls) != 2 || stalls[1]["head"] != "h5" { |
| 84 | t.Errorf("a new failed head past the attempts stalls again: %v", stalls) |
| 85 | } |
| 86 | if len(launched) != 3 { |
| 87 | t.Fatalf("launched %d runs, want 3", len(launched)) |
| 88 | } |
| 89 | if la := launched[2]; la.Head != "h3" || la.Owner != "operator" || la.Branch != "agent/x" || la.ChangeID != "kxyz" { |
| 90 | t.Errorf("the third launch expects %+v", la) |
| 91 | } |
| 92 | |
| 93 | pass("operator", "h5", valley.ChecksPassed, "") |
| 94 | pass("operator", "h6", valley.ChecksFailed, "fix run run-4 dispatched (attempt 1 of 3)") |
| 95 | |
| 96 | // A restarted pass reads what the ledger recorded. |
| 97 | if l, err = LoadLedger(dir); err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | pass("operator", "h6", valley.ChecksFailed, "fix run run-4 dispatched (attempt 1 of 3)") |
| 101 | if len(launched) != 4 { |
| 102 | t.Errorf("launched %d runs, want 4", len(launched)) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // TestPassReportsAStallOnceSaved: a pass whose attempts are spent and whose |
| 107 | // ledger can't be saved ends with an error and reports nothing; once the |
| 108 | // ledger saves, the stall is reported, and a later pass, reading the |
| 109 | // ledger, reports it no more. |
| 110 | func TestPassReportsAStallOnceSaved(t *testing.T) { |
| 111 | dir := filepath.Join(t.TempDir(), "session") // not made yet: saving fails |
| 112 | stalls := 0 |
| 113 | d := Deps{ |
| 114 | Emit: func(string, string, map[string]interface{}) { stalls++ }, |
| 115 | Principal: func(context.Context, string) string { return "operator" }, |
| 116 | NewRunID: func() (string, error) { return "", fmt.Errorf("no run IDs") }, |
| 117 | Launch: func(context.Context, Launch) error { t.Error("launched past the attempts"); return nil }, |
| 118 | Running: func(restack.Key, string) bool { return false }, |
| 119 | Now: time.Now, |
| 120 | } |
| 121 | repos := []restack.Repo{{Name: "widget", Dir: dir}} |
| 122 | v := valley.Version{N: 4, Head: "h4", Branch: "agent/x", Landing: valley.NotLanded, |
| 123 | Checks: &valley.Checks{State: valley.ChecksFailed, Required: []valley.Check{{Name: "web-unit", State: valley.ChecksFailed}}}} |
| 124 | listed := &valley.Changes{Repos: []valley.Repo{{Repo: "widget", Changes: []valley.Change{{ |
| 125 | ChangeID: "kxyz", Handle: "kxyz", Owner: "operator", State: valley.StateReady, Active: 4, Versions: []valley.Version{v}}}}}} |
| 126 | spent := func() *Ledger { |
| 127 | l, err := LoadLedger(dir) |
| 128 | if err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | l.entries["widget/kxyz"] = &Entry{Repo: "widget", ChangeID: "kxyz", Attempts: MaxAttempts, Heads: []string{"h3"}} |
| 132 | return l |
| 133 | } |
| 134 | |
| 135 | if _, err := Pass(context.Background(), d, spent(), repos, listed, true); err == nil || stalls != 0 { |
| 136 | t.Fatalf("a ledger that can't be saved: err %v, %d stalls reported; want an error and none", err, stalls) |
| 137 | } |
| 138 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | if views, err := Pass(context.Background(), d, spent(), repos, listed, true); err != nil || stalls != 1 || len(views) != 1 || views[0].State != StateStopped { |
| 142 | t.Fatalf("once the ledger saves: %v, err %v, %d stalls reported; want stopped, reported once", views, err, stalls) |
| 143 | } |
| 144 | l, err := LoadLedger(dir) |
| 145 | if err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | if _, err := Pass(context.Background(), d, l, repos, listed, true); err != nil || stalls != 1 { |
| 149 | t.Errorf("a later pass: err %v, %d stalls reported; want still one", err, stalls) |
| 150 | } |
| 151 | } |