| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | func TestArtifactForPublishHonorsPipelineAndExpiry(t *testing.T) { |
| 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, "ci", "", "master", "private") |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | sha := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 22 | jobs := []NewJob{ |
| 23 | {Name: "build", Stage: "build", Image: "alpine:3.20", Script: "true"}, |
| 24 | {Name: "fail", Stage: "build", Image: "alpine:3.20", Script: "false"}, |
| 25 | } |
| 26 | pid, err := s.CreatePipeline(ctx, repo.ID, sha, "refs/tags/v1", "push", "tim", "rel", jobs) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | other, err := s.CreatePipeline(ctx, repo.ID, sha, "refs/tags/v1", "push", "tim", "rel", jobs[:1]) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | got, err := s.JobsByPipeline(ctx, pid) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | var build, fail Job |
| 39 | for _, j := range got { |
| 40 | switch j.Name { |
| 41 | case "build": |
| 42 | build = j |
| 43 | case "fail": |
| 44 | fail = j |
| 45 | } |
| 46 | } |
| 47 | if err := s.FinishJob(ctx, build.ID, "success", ""); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | if err := s.FinishJob(ctx, fail.ID, "failed", ""); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | later := time.Now().Add(time.Hour) |
| 54 | past := time.Now().Add(-time.Hour) |
| 55 | if err := s.AddArtifact(ctx, build.ID, "dist/app", "/data/app", 3, &later); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | if err := s.AddArtifact(ctx, build.ID, "dist/old", "/data/old", 3, &past); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | if err := s.AddArtifact(ctx, fail.ID, "dist/bad", "/data/bad", 3, nil); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | otherJobs, err := s.JobsByPipeline(ctx, other) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | if err := s.FinishJob(ctx, otherJobs[0].ID, "success", ""); err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | if err := s.AddArtifact(ctx, otherJobs[0].ID, "dist/other", "/data/other", 3, nil); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | path, err := s.ArtifactForPublish(ctx, pid, "build", "dist/app") |
| 75 | if err != nil || path != "/data/app" { |
| 76 | t.Fatalf("geldig: %q %v", path, err) |
| 77 | } |
| 78 | if _, err := s.ArtifactForPublish(ctx, pid, "build", "dist/old"); !errors.Is(err, ErrArtifactExpired) { |
| 79 | t.Fatalf("verlopen: %v", err) |
| 80 | } |
| 81 | if _, err := s.ArtifactForPublish(ctx, pid, "fail", "dist/bad"); !IsNotFound(err) { |
| 82 | t.Fatalf("mislukt: %v", err) |
| 83 | } |
| 84 | if _, err := s.ArtifactForPublish(ctx, pid, "build", "dist/other"); !IsNotFound(err) { |
| 85 | t.Fatalf("andere pipeline: %v", err) |
| 86 | } |
| 87 | if _, err := s.ArtifactPath(ctx, pid, "build", "dist/old"); err != nil { |
| 88 | t.Fatalf("ArtifactPath blijft verlopen bestanden teruggeven: %v", err) |
| 89 | } |
| 90 | } |