| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestJobReasons(t *testing.T) { |
| 9 | s := openTest(t) |
| 10 | ctx := context.Background() |
| 11 | tim, _ := s.UpsertUser(ctx, 1, "tim", "", false) |
| 12 | repo, err := s.CreateRepo(ctx, "user", tim.ID, "ci", "", "master", "private") |
| 13 | if err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | old, err := s.CreatePipeline(ctx, repo.ID, "abc", "refs/heads/master", "push", "tim", "x", []NewJob{ |
| 17 | {Name: "a", Stage: "test", Script: "true", Interruptible: true}, |
| 18 | {Name: "b", Stage: "test", Script: "true"}, |
| 19 | }) |
| 20 | if err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | newer, _ := s.CreatePipeline(ctx, repo.ID, "def", "refs/heads/master", "push", "tim", "y", []NewJob{{Name: "a", Stage: "test", Script: "true"}}) |
| 24 | if _, _, err := s.CancelInterruptible(ctx, repo.ID, "refs/heads/master", newer); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | jobs, _ := s.JobsByPipeline(ctx, old) |
| 28 | for _, j := range jobs { |
| 29 | if j.Name == "a" && (!j.Obsolete() || j.CancelReason() != "superseded") { |
| 30 | t.Fatalf("a: %+v", j.Reason) |
| 31 | } |
| 32 | if j.Name == "b" { |
| 33 | if ok, _ := s.CancelJob(ctx, j.ID); !ok { |
| 34 | t.Fatal("cancel b") |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | jobs, _ = s.JobsByPipeline(ctx, old) |
| 39 | if PipelineObsolete(jobs) { |
| 40 | t.Fatal("handmatig geannuleerde job: pipeline is niet obsolete") |
| 41 | } |
| 42 | for _, j := range jobs { |
| 43 | if j.Name == "b" && (j.Obsolete() || j.CancelReason() != "manual") { |
| 44 | t.Fatalf("b: %q", j.Reason) |
| 45 | } |
| 46 | } |
| 47 | if !PipelineObsolete([]Job{{Status: "canceled", Reason: ReasonSuperseded}, {Status: "success"}, {Status: "canceled", AllowFailure: true}}) { |
| 48 | t.Fatal("alle tellende geannuleerde jobs superseded") |
| 49 | } |
| 50 | if PipelineObsolete([]Job{{Status: "success"}}) { |
| 51 | t.Fatal("geen geannuleerde job") |
| 52 | } |
| 53 | // Timeout-reden en reset bij opnieuw starten. |
| 54 | nj, _ := s.JobsByPipeline(ctx, newer) |
| 55 | if err := s.FinishJobReason(ctx, nj[0].ID, "failed", "Job timed out after 1m\n", "job_execution_timeout"); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | nj, _ = s.JobsByPipeline(ctx, newer) |
| 59 | if nj[0].FailureReason() != "job_execution_timeout" || nj[0].CancelReason() != "" { |
| 60 | t.Fatalf("timeout: %q", nj[0].Reason) |
| 61 | } |
| 62 | if err := s.RequeueJob(ctx, nj[0].ID, 1, ""); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | nj, _ = s.JobsByPipeline(ctx, newer) |
| 66 | if nj[0].Reason != "" { |
| 67 | t.Fatal("reden blijft staan na requeue") |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestProtectedBlocker(t *testing.T) { |
| 72 | s := openTest(t) |
| 73 | ctx := context.Background() |
| 74 | tim, _ := s.UpsertUser(ctx, 1, "tim", "", false) |
| 75 | repo, _ := s.CreateRepo(ctx, "user", tim.ID, "ci", "", "master", "private") |
| 76 | ref := "refs/heads/master" |
| 77 | a, _ := s.CreatePipeline(ctx, repo.ID, "a", ref, "push", "tim", "a", []NewJob{{Name: "up", Stage: "d", Script: "true", DoNotInterrupt: true}}) |
| 78 | b, _ := s.CreatePipeline(ctx, repo.ID, "b", ref, "push", "tim", "b", []NewJob{{Name: "up", Stage: "d", Script: "true", DoNotInterrupt: true}}) |
| 79 | other, _ := s.CreatePipeline(ctx, repo.ID, "c", "refs/heads/feature", "push", "tim", "c", []NewJob{{Name: "x", Stage: "d", Script: "true"}}) |
| 80 | if _, ok := s.ProtectedBlocker(ctx, repo.ID, ref, b); ok { |
| 81 | t.Fatal("wachtende dni-job blokkeert niet") |
| 82 | } |
| 83 | ja, _ := s.JobsByPipeline(ctx, a) |
| 84 | if !ja[0].DoNotInterrupt { |
| 85 | t.Fatal("vlag niet bewaard") |
| 86 | } |
| 87 | _, _ = s.MarkJobRunning(ctx, ja[0].ID) |
| 88 | if j, ok := s.ProtectedBlocker(ctx, repo.ID, ref, b); !ok || j.ID != ja[0].ID { |
| 89 | t.Fatal("nieuwere pipeline hoort te wachten") |
| 90 | } |
| 91 | if _, ok := s.ProtectedBlocker(ctx, repo.ID, ref, a); ok { |
| 92 | t.Fatal("geen wachten op jezelf of nieuwere pipelines") |
| 93 | } |
| 94 | if _, ok := s.ProtectedBlocker(ctx, repo.ID, "refs/heads/feature", other); ok { |
| 95 | t.Fatal("andere ref wacht niet") |
| 96 | } |
| 97 | if prot, _ := s.RunningProtectedJobs(ctx, repo.ID, 0); len(prot) != 1 { |
| 98 | t.Fatalf("lopend: %d", len(prot)) |
| 99 | } |
| 100 | _ = s.FinishJobReason(ctx, ja[0].ID, "failed", "", "job_execution_timeout") |
| 101 | if _, ok := s.ProtectedBlocker(ctx, repo.ID, ref, b); ok { |
| 102 | t.Fatal("na een timeout gaat het wachten door") |
| 103 | } |
| 104 | } |