| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | func staleSetup(t *testing.T) (*Store, int64) { |
| 10 | t.Helper() |
| 11 | s := openTest(t) |
| 12 | ctx := context.Background() |
| 13 | tim, err := s.UpsertUser(ctx, 1, "tim", "", false) |
| 14 | if err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | repo, err := s.CreateRepo(ctx, "user", tim.ID, "pp", "", "master", "private") |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | return s, repo.ID |
| 22 | } |
| 23 | |
| 24 | func relPipeline(t *testing.T, s *Store, repoID int64, sha, ref string) (int64, map[string]Job) { |
| 25 | t.Helper() |
| 26 | ctx := context.Background() |
| 27 | id, err := s.CreatePipeline(ctx, repoID, sha, ref, "push", "tim", "rel", []NewJob{ |
| 28 | {Name: "build", Stage: "build", Script: "true"}, |
| 29 | {Name: "release", Stage: "release", Script: "true", When: "manual"}, |
| 30 | {Name: "publish", Stage: "publish", Script: "true", Needs: []string{"release"}, NeedsSet: true}, |
| 31 | }) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | return id, staleJobs(t, s, id) |
| 36 | } |
| 37 | |
| 38 | func staleJobs(t *testing.T, s *Store, id int64) map[string]Job { |
| 39 | jobs, _ := s.JobsByPipeline(context.Background(), id) |
| 40 | m := map[string]Job{} |
| 41 | for _, j := range jobs { |
| 42 | m[j.Name] = j |
| 43 | } |
| 44 | return m |
| 45 | } |
| 46 | |
| 47 | func TestStaleManualSuperseded(t *testing.T) { |
| 48 | s, repo := staleSetup(t) |
| 49 | ctx := context.Background() |
| 50 | old, oj := relPipeline(t, s, repo, "a1", "refs/heads/master") |
| 51 | _ = s.FinishJob(ctx, oj["build"].ID, "success", "") |
| 52 | _, _ = s.RefreshPipelineStatus(ctx, old) |
| 53 | // Andere ref blijft ongemoeid. |
| 54 | other, _ := relPipeline(t, s, repo, "b1", "refs/heads/stage") |
| 55 | // Nieuwere pipeline op master: build loopt nog. |
| 56 | nw, _ := relPipeline(t, s, repo, "a2", "refs/heads/master") |
| 57 | out, err := s.ExpireSupersededManual(ctx, repo, "refs/heads/master", nw) |
| 58 | if err != nil || len(out) != 1 || out[0].PipelineID != old || out[0].Jobs != 2 { |
| 59 | t.Fatalf("out %+v %v", out, err) |
| 60 | } |
| 61 | j := staleJobs(t, s, old) |
| 62 | if j["release"].Status != "canceled" || !j["release"].Obsolete() || j["release"].CancelReason() != "superseded" { |
| 63 | t.Fatalf("release %+v", j["release"]) |
| 64 | } |
| 65 | if j["publish"].Status != "canceled" || j["build"].Status != "success" { |
| 66 | t.Fatalf("publish %s build %s", j["publish"].Status, j["build"].Status) |
| 67 | } |
| 68 | p, _ := s.PipelineByID(ctx, repo, old) |
| 69 | if p.Status != "canceled" || !PipelineObsolete(func() []Job { js, _ := s.JobsByPipeline(ctx, old); return js }()) { |
| 70 | t.Fatalf("pipeline %s", p.Status) |
| 71 | } |
| 72 | // Nieuwste blijft startbaar, andere ref ook. |
| 73 | if staleJobs(t, s, nw)["release"].Status != "manual" || staleJobs(t, s, other)["release"].Status != "manual" { |
| 74 | t.Fatal("nieuwste of andere ref verlopen") |
| 75 | } |
| 76 | if ok, _ := s.QueueJob(ctx, staleJobs(t, s, nw)["release"].ID); !ok { |
| 77 | t.Fatal("nieuwste niet startbaar") |
| 78 | } |
| 79 | // Verlopen job kan niet via QueueJob (status canceled). |
| 80 | if ok, _ := s.QueueJob(ctx, j["release"].ID); ok { |
| 81 | t.Fatal("verlopen job startte") |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestStaleManualRunningJobsStay(t *testing.T) { |
| 86 | s, repo := staleSetup(t) |
| 87 | ctx := context.Background() |
| 88 | old, oj := relPipeline(t, s, repo, "a1", "refs/heads/master") |
| 89 | // build staat nog in de wachtrij: alleen manual (en wat daarop wacht) verloopt. |
| 90 | nw, _ := relPipeline(t, s, repo, "a2", "refs/heads/master") |
| 91 | if _, err := s.ExpireSupersededManual(ctx, repo, "refs/heads/master", nw); err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | j := staleJobs(t, s, old) |
| 95 | if j["build"].Status != oj["build"].Status || j["release"].Status != "canceled" { |
| 96 | t.Fatalf("build %s release %s", j["build"].Status, j["release"].Status) |
| 97 | } |
| 98 | if st, _ := s.RefreshPipelineStatus(ctx, old); st != "running" { |
| 99 | t.Fatalf("pipeline met open build: %s", st) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestStaleManualConfigFailureDoesNotSupersede(t *testing.T) { |
| 104 | s, repo := staleSetup(t) |
| 105 | ctx := context.Background() |
| 106 | old, _ := relPipeline(t, s, repo, "a1", "refs/heads/master") |
| 107 | if _, _, err := s.RecordConfigFailure(ctx, repo, "a2", "refs/heads/master", "push", "kapot", 0, ""); err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | out, err := s.SweepStaleManual(ctx, time.Now()) |
| 111 | if err != nil || len(out) != 0 || staleJobs(t, s, old)["release"].Status != "manual" { |
| 112 | t.Fatalf("%+v %v", out, err) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func TestStaleManualAge(t *testing.T) { |
| 117 | s, repo := staleSetup(t) |
| 118 | ctx := context.Background() |
| 119 | id, j := relPipeline(t, s, repo, "a1", "refs/heads/master") |
| 120 | _ = s.FinishJob(ctx, j["build"].ID, "success", "") |
| 121 | if st, _ := s.RefreshPipelineStatus(ctx, id); st != "manual" { |
| 122 | t.Fatalf("opzet %s", st) |
| 123 | } |
| 124 | if d, _ := s.StaleManualDays(ctx); d != 7 { |
| 125 | t.Fatalf("standaard %d", d) |
| 126 | } |
| 127 | // 6 dagen: nog niet. |
| 128 | out, _ := s.SweepStaleManual(ctx, time.Now().Add(6*24*time.Hour)) |
| 129 | if len(out) != 0 { |
| 130 | t.Fatalf("te vroeg: %+v", out) |
| 131 | } |
| 132 | // Instance 0 = uit. |
| 133 | _ = s.SetStaleManualDays(ctx, 0) |
| 134 | if out, _ := s.SweepStaleManual(ctx, time.Now().Add(30*24*time.Hour)); len(out) != 0 { |
| 135 | t.Fatalf("0 = uit: %+v", out) |
| 136 | } |
| 137 | // Project overschrijft: 2 dagen. |
| 138 | _ = s.SetRepoStaleManualDays(ctx, repo, 2) |
| 139 | out, err := s.SweepStaleManual(ctx, time.Now().Add(3*24*time.Hour)) |
| 140 | if err != nil || len(out) != 1 || out[0].Reason != "stale" || out[0].Jobs != 2 { |
| 141 | t.Fatalf("%+v %v", out, err) |
| 142 | } |
| 143 | jj := staleJobs(t, s, id) |
| 144 | if jj["release"].CancelReason() != "stale" || !jj["release"].Obsolete() { |
| 145 | t.Fatalf("%+v", jj["release"]) |
| 146 | } |
| 147 | p, _ := s.PipelineByID(ctx, repo, id) |
| 148 | if p.Status != "canceled" { |
| 149 | t.Fatalf("pipeline %s", p.Status) |
| 150 | } |
| 151 | // Tweede keer: niets meer. |
| 152 | if out, _ := s.SweepStaleManual(ctx, time.Now().Add(3*24*time.Hour)); len(out) != 0 { |
| 153 | t.Fatalf("dubbel: %+v", out) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | func TestStaleManualRepoZeroOverridesInstance(t *testing.T) { |
| 158 | s, repo := staleSetup(t) |
| 159 | ctx := context.Background() |
| 160 | id, j := relPipeline(t, s, repo, "a1", "refs/heads/master") |
| 161 | _ = s.FinishJob(ctx, j["build"].ID, "success", "") |
| 162 | _, _ = s.RefreshPipelineStatus(ctx, id) |
| 163 | _ = s.SetRepoStaleManualDays(ctx, repo, 0) |
| 164 | if out, _ := s.SweepStaleManual(ctx, time.Now().Add(100*24*time.Hour)); len(out) != 0 { |
| 165 | t.Fatalf("project 0 = uit: %+v", out) |
| 166 | } |
| 167 | if err := s.SetStaleManualDays(ctx, -3); err == nil { |
| 168 | t.Fatal("negatief toegestaan") |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | func TestStaleManualAllowFailureSuccess(t *testing.T) { |
| 173 | s, repo := staleSetup(t) |
| 174 | ctx := context.Background() |
| 175 | id, err := s.CreatePipeline(ctx, repo, "a1", "refs/heads/master", "push", "tim", "x", []NewJob{ |
| 176 | {Name: "build", Stage: "build", Script: "true"}, |
| 177 | {Name: "deploy", Stage: "deploy", Script: "true", When: "manual", AllowFailure: true}, |
| 178 | }) |
| 179 | if err != nil { |
| 180 | t.Fatal(err) |
| 181 | } |
| 182 | _ = s.FinishJob(ctx, staleJobs(t, s, id)["build"].ID, "success", "") |
| 183 | _, _ = s.RefreshPipelineStatus(ctx, id) |
| 184 | nw, _ := relPipeline(t, s, repo, "a2", "refs/heads/master") |
| 185 | if _, err := s.ExpireSupersededManual(ctx, repo, "refs/heads/master", nw); err != nil { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | p, _ := s.PipelineByID(ctx, repo, id) |
| 189 | if p.Status != "success" { |
| 190 | t.Fatalf("allow_failure manual verlopen: %s", p.Status) |
| 191 | } |
| 192 | } |