|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +package test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "regexp" |
| 10 | + "testing" |
| 11 | + |
| 12 | + tgithub "github.com/openshift-pipelines/pipelines-as-code/test/pkg/github" |
| 13 | + "github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload" |
| 14 | + twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait" |
| 15 | + "github.com/tektoncd/pipeline/pkg/names" |
| 16 | + "gotest.tools/v3/assert" |
| 17 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 18 | +) |
| 19 | + |
| 20 | +func TestGithubDeduplicatePipelineRuns(t *testing.T) { |
| 21 | + targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns") |
| 22 | + |
| 23 | + ctx := context.TODO() |
| 24 | + ctx, runcnx, opts, ghcnx, err := tgithub.Setup(ctx, false, false) |
| 25 | + assert.NilError(t, err) |
| 26 | + |
| 27 | + yamlEntries := map[string]string{} |
| 28 | + yamlEntries[".tekton/pipelinerun-match-all-branch.yaml"] = "testdata/pipelinerun.yaml" |
| 29 | + |
| 30 | + repoinfo, resp, err := ghcnx.Client().Repositories.Get(ctx, opts.Organization, opts.Repo) |
| 31 | + assert.NilError(t, err) |
| 32 | + if resp != nil && resp.StatusCode == http.StatusNotFound { |
| 33 | + t.Errorf("Repository %s not found in %s", opts.Organization, opts.Repo) |
| 34 | + } |
| 35 | + |
| 36 | + err = tgithub.CreateCRD(ctx, t, repoinfo, runcnx, opts, targetNS) |
| 37 | + assert.NilError(t, err) |
| 38 | + |
| 39 | + entries, err := payload.GetEntries(yamlEntries, targetNS, "*", "pull_request, push", map[string]string{}) |
| 40 | + assert.NilError(t, err) |
| 41 | + |
| 42 | + targetRefName := fmt.Sprintf("refs/heads/%s", |
| 43 | + names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-test")) |
| 44 | + |
| 45 | + sha, vref, err := tgithub.PushFilesToRef(ctx, ghcnx.Client(), "Test Github Deduplicate PipelineRuns", repoinfo.GetDefaultBranch(), targetRefName, |
| 46 | + opts.Organization, opts.Repo, entries) |
| 47 | + assert.NilError(t, err) |
| 48 | + |
| 49 | + runcnx.Clients.Log.Infof("Commit %s has been created and pushed to %s", sha, vref.GetURL()) |
| 50 | + number, err := tgithub.PRCreate(ctx, runcnx, ghcnx, opts.Organization, |
| 51 | + opts.Repo, targetRefName, repoinfo.GetDefaultBranch(), "Test Github Deduplicate PipelineRuns") |
| 52 | + assert.NilError(t, err) |
| 53 | + |
| 54 | + g := &tgithub.PRTest{ |
| 55 | + PRNumber: number, |
| 56 | + SHA: sha, |
| 57 | + TargetNamespace: targetNS, |
| 58 | + Logger: runcnx.Clients.Log, |
| 59 | + Provider: ghcnx, |
| 60 | + Options: opts, |
| 61 | + TargetRefName: targetRefName, |
| 62 | + Cnx: runcnx, |
| 63 | + } |
| 64 | + |
| 65 | + defer g.TearDown(ctx, t) |
| 66 | + |
| 67 | + runcnx.Clients.Log.Infof("Pull request %d has been created", number) |
| 68 | + err = twait.UntilPipelineRunCreated(ctx, runcnx.Clients, twait.Opts{ |
| 69 | + RepoName: targetNS, |
| 70 | + Namespace: targetNS, |
| 71 | + MinNumberStatus: 1, |
| 72 | + PollTimeout: twait.DefaultTimeout, |
| 73 | + TargetSHA: sha, |
| 74 | + }) |
| 75 | + assert.NilError(t, err) |
| 76 | + |
| 77 | + // but here we can't guarantee that for which event PipelineRun is created because we don't which |
| 78 | + // event is received first on controller either push or pull_request. |
| 79 | + prs, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{}) |
| 80 | + assert.NilError(t, err) |
| 81 | + assert.Assert(t, len(prs.Items) == 1) |
| 82 | + |
| 83 | + maxLines := int64(50) |
| 84 | + err = twait.RegexpMatchingInControllerLog(ctx, runcnx, *regexp.MustCompile("Skipping duplicate PipelineRun"), 10, "controller", &maxLines) |
| 85 | + assert.NilError(t, err) |
| 86 | +} |
0 commit comments