| 1 | package triage |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/wire" |
| 8 | "amadan.net/rastrillo/rastrillo/budget" |
| 9 | ) |
| 10 | |
| 11 | func TestMain(m *testing.M) { os.Exit(budget.Main(m)) } |
| 12 | |
| 13 | func stopped(specialist, reason string) Facts { |
| 14 | return Facts{NeedsYou: wire.NeedsRetry, Specialist: specialist, Reason: reason, Terminated: true} |
| 15 | } |
| 16 | |
| 17 | // TestClassify is spec §§2.1-2.3 and §4, one row per case. |
| 18 | func TestClassify(t *testing.T) { |
| 19 | for name, c := range map[string]struct { |
| 20 | f Facts |
| 21 | act, rec string |
| 22 | why string |
| 23 | }{ |
| 24 | "runner restarted": {stopped(wire.SpecialistEngineering, "runner_restarted"), ActRetry, "", ""}, |
| 25 | "interrupted review": {stopped(wire.SpecialistReview, "interrupted"), ActRetry, "", ""}, |
| 26 | "superseded checks": {stopped(wire.SpecialistChecks, "superseded"), ActRetry, "", ""}, |
| 27 | "step timeout": {stopped(wire.SpecialistEngineering, "step_timeout"), ActRetry, "", ""}, |
| 28 | "checks timeout": {stopped(wire.SpecialistChecks, "checks_timeout"), ActRetry, "", ""}, |
| 29 | "review blocked, passing": {Facts{NeedsYou: wire.NeedsReviewBlocked, Specialist: wire.SpecialistReview, Reason: "interrupted", Terminated: true}, ActRetry, "", ""}, |
| 30 | "not a passing stop": {stopped(wire.SpecialistEngineering, "provider_failed"), ActNone, wire.RecommendRetry, ""}, |
| 31 | "no termination proof": {Facts{NeedsYou: wire.NeedsRetry, Specialist: wire.SpecialistEngineering, Reason: "runner_restarted"}, ActNone, wire.RecommendRetry, ""}, |
| 32 | "already retried on its own": {func() Facts { f := stopped(wire.SpecialistEngineering, "runner_restarted"); f.Retried = true; return f }(), ActNone, wire.RecommendRetry, wire.TriageWhyTried}, |
| 33 | "a merge step, never": {stopped(wire.SpecialistMerge, "interrupted"), ActNone, "", ""}, |
| 34 | "on a worker, the person's": {func() Facts { f := stopped(wire.SpecialistChecks, "runner_restarted"); f.OnWorker = true; return f }(), ActNone, wire.RecommendRetry, ""}, |
| 35 | "conflict": {Facts{NeedsYou: wire.NeedsMergeConflict}, ActRebase, "", ""}, |
| 36 | "conflict, candidate rebased": {Facts{NeedsYou: wire.NeedsMergeConflict, Rebased: true}, ActNone, wire.RecommendRebase, wire.TriageWhyTried}, |
| 37 | "conflict, two rebases": {Facts{NeedsYou: wire.NeedsMergeConflict, Rebases: MaxRebases}, ActNone, wire.RecommendRebase, wire.TriageWhyTried}, |
| 38 | "stale network checks": {Facts{NeedsYou: wire.NeedsMerge, Checks: wire.ChecksCannotRun, ChecksReason: "needs_network", Stale: true}, ActRecheck, "", ""}, |
| 39 | "stale macOS checks": {Facts{NeedsYou: wire.NeedsMerge, Checks: wire.ChecksCannotRun, ChecksReason: "needs_macos", Stale: true}, ActRecheck, "", ""}, |
| 40 | "network, not stale": {Facts{NeedsYou: wire.NeedsMerge, Checks: wire.ChecksCannotRun, ChecksReason: "needs_network"}, ActNone, "", ""}, |
| 41 | "stale but re-checked": {Facts{NeedsYou: wire.NeedsMerge, Checks: wire.ChecksCannotRun, ChecksReason: "needs_network", Stale: true, Rechecked: true}, ActNone, "", ""}, |
| 42 | "stale, another reason": {Facts{NeedsYou: wire.NeedsMerge, Checks: wire.ChecksCannotRun, ChecksReason: "checks_unavailable", Stale: true}, ActNone, "", ""}, |
| 43 | "checks passed, asked": {Facts{NeedsYou: wire.NeedsMerge, Checks: wire.ChecksPassed}, ActNone, "", ""}, |
| 44 | "fix rounds": {Facts{NeedsYou: wire.NeedsFixRounds}, ActNone, wire.RecommendRound, ""}, |
| 45 | "branch moved": {Facts{NeedsYou: wire.NeedsBranchMoved}, ActNone, "", ""}, |
| 46 | "machine removed": {Facts{NeedsYou: wire.NeedsMachineRemoved}, ActNone, "", ""}, |
| 47 | "nothing needed": {Facts{}, ActNone, "", ""}, |
| 48 | } { |
| 49 | v := Classify(c.f) |
| 50 | if v.Action != c.act || v.Recommend != c.rec || v.Why != c.why { |
| 51 | t.Errorf("%s: got %+v, want act %q recommend %q why %q", name, v, c.act, c.rec, c.why) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // TestClassifyNeverTouches: an ended, cancel-requested, folded or merging |
| 57 | // task is never acted on, whatever it needs (a held one is |
| 58 | // TestClassifyHeldStillRetries). |
| 59 | func TestClassifyNeverTouches(t *testing.T) { |
| 60 | base := stopped(wire.SpecialistEngineering, "runner_restarted") |
| 61 | if v := Classify(base); v.Action != ActRetry { |
| 62 | t.Fatalf("base %+v", v) |
| 63 | } |
| 64 | for name, bend := range map[string]func(*Facts){ |
| 65 | "ended": func(f *Facts) { f.Ended = true }, |
| 66 | "cancel requested": func(f *Facts) { f.CancelRequested = true }, |
| 67 | "folded": func(f *Facts) { f.Folded = true }, |
| 68 | "merging": func(f *Facts) { f.Merging = true }, |
| 69 | } { |
| 70 | f := base |
| 71 | bend(&f) |
| 72 | if v := Classify(f); v != (Verdict{}) { |
| 73 | t.Errorf("%s: %+v", name, v) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // TestPassing: the passing stops, and only those. |
| 79 | func TestPassing(t *testing.T) { |
| 80 | for _, r := range []string{"runner_restarted", "interrupted", "superseded", "step_timeout", "checks_timeout"} { |
| 81 | if !Passing(r) { |
| 82 | t.Errorf("%s is a passing stop", r) |
| 83 | } |
| 84 | } |
| 85 | for _, r := range []string{"", "provider_failed", "no_changes", "push_failed", "merge_conflict", "cancelled", "policy_rejected"} { |
| 86 | if Passing(r) { |
| 87 | t.Errorf("%s is not a passing stop", r) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // A merge the hub refused because the repository's default branch is |
| 93 | // unborn and the hub cannot make the first merge yet waits on the |
| 94 | // platform: no action (never retried on its own), no recommendation for |
| 95 | // the person, and the platform named as the reason. Any other merge |
| 96 | // refusal, and the same reason on another specialist, are not this. |
| 97 | func TestClassifyWaitsOnThePlatform(t *testing.T) { |
| 98 | f := Facts{NeedsYou: wire.NeedsMerge, Specialist: wire.SpecialistMerge, Reason: wire.ReasonMergeUnbornDefault, Terminated: true, Ready: true} |
| 99 | if v := Classify(f); v.Action != ActNone || v.Recommend != "" || v.Waiting != wire.ReasonMergeUnbornDefault { |
| 100 | t.Fatalf("unborn default: %+v", v) |
| 101 | } |
| 102 | for _, g := range []Facts{ |
| 103 | {NeedsYou: wire.NeedsMerge, Specialist: wire.SpecialistMerge, Reason: "merge_failed", Terminated: true, Ready: true}, |
| 104 | {NeedsYou: wire.NeedsRetry, Specialist: wire.SpecialistEngineering, Reason: wire.ReasonMergeUnbornDefault, Terminated: true}, |
| 105 | {NeedsYou: wire.NeedsMerge, Specialist: wire.SpecialistMerge, Reason: wire.ReasonMergeUnbornDefault, Terminated: true, Ended: true}, |
| 106 | } { |
| 107 | if v := Classify(g); v.Waiting != "" { |
| 108 | t.Fatalf("%+v waits: %+v", g, v) |
| 109 | } |
| 110 | } |
| 111 | if Passing(wire.ReasonMergeUnbornDefault) { |
| 112 | t.Fatal("a platform wait is not a passing reason") |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // TestClassifyHeldStillRetries: the coordinator's merge hold stops Equipo |
| 117 | // merging on its own, nothing else. A held task's passing stop is still |
| 118 | // retried; its merge-side actions (a rebase, a re-check before the merge |
| 119 | // card) still wait for the person. |
| 120 | func TestClassifyHeldStillRetries(t *testing.T) { |
| 121 | held := func(f Facts) Facts { f.Held = true; return f } |
| 122 | for name, c := range map[string]struct { |
| 123 | f Facts |
| 124 | act, rec string |
| 125 | }{ |
| 126 | "step timeout": {held(stopped(wire.SpecialistEngineering, "step_timeout")), ActRetry, ""}, |
| 127 | "runner restarted": {held(stopped(wire.SpecialistEngineering, "runner_restarted")), ActRetry, ""}, |
| 128 | "checks timeout": {held(stopped(wire.SpecialistChecks, "checks_timeout")), ActRetry, ""}, |
| 129 | "not a passing stop": {held(stopped(wire.SpecialistEngineering, "provider_failed")), ActNone, wire.RecommendRetry}, |
| 130 | "a merge step, never": {held(stopped(wire.SpecialistMerge, "interrupted")), ActNone, ""}, |
| 131 | "fix rounds": {held(Facts{NeedsYou: wire.NeedsFixRounds}), ActNone, wire.RecommendRound}, |
| 132 | "conflict": {held(Facts{NeedsYou: wire.NeedsMergeConflict}), ActNone, ""}, |
| 133 | "stale checks": {held(Facts{NeedsYou: wire.NeedsMerge, Checks: wire.ChecksCannotRun, ChecksReason: "needs_network", Stale: true}), ActNone, ""}, |
| 134 | "unborn default": {held(Facts{NeedsYou: wire.NeedsMerge, Specialist: wire.SpecialistMerge, Reason: wire.ReasonMergeUnbornDefault, Terminated: true}), ActNone, ""}, |
| 135 | } { |
| 136 | v := Classify(c.f) |
| 137 | if v.Action != c.act || v.Recommend != c.rec || v.Waiting != "" { |
| 138 | t.Errorf("held, %s: got %+v, want act %q recommend %q", name, v, c.act, c.rec) |
| 139 | } |
| 140 | f := c.f |
| 141 | f.Held = false |
| 142 | if name != "conflict" && name != "stale checks" && name != "unborn default" { |
| 143 | if got := Classify(f); got != v { |
| 144 | t.Errorf("%s: held %+v, not held %+v", name, v, got) |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |