|
| 1 | +//go:build e2e |
| 2 | +// +build e2e |
| 3 | + |
| 4 | +package test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/google/go-github/v56/github" |
| 16 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/random" |
| 17 | + tgithub "github.com/openshift-pipelines/pipelines-as-code/test/pkg/github" |
| 18 | + "github.com/openshift-pipelines/pipelines-as-code/test/pkg/options" |
| 19 | + "github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload" |
| 20 | + "github.com/openshift-pipelines/pipelines-as-code/test/pkg/repository" |
| 21 | + tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" |
| 22 | + "github.com/tektoncd/pipeline/pkg/names" |
| 23 | + "gotest.tools/v3/assert" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | +) |
| 26 | + |
| 27 | +// TestGithubSecondPullRequestConcurrencyMultiplePR concurrency for the same Repository over multiples PR including a /retest |
| 28 | +// and a max-keep-run, may be a bit slow (180s at least) but it's worth it. |
| 29 | +func TestGithubSecondPullRequestConcurrencyMultiplePR(t *testing.T) { |
| 30 | + ctx := context.Background() |
| 31 | + label := "Github Multiple PullRequest Concurrency-1 MaxKeepRun-1 Multiple" |
| 32 | + numberOfPullRequest := 3 |
| 33 | + numberOfPipelineRuns := 3 |
| 34 | + numberOfRetests := 1 |
| 35 | + maxNumberOfConcurrentPipelineRuns := 1 |
| 36 | + maxKeepRun := 1 |
| 37 | + allPipelinesRunsCnt := (numberOfPullRequest * numberOfPipelineRuns) + (numberOfPullRequest * numberOfRetests * numberOfPipelineRuns) |
| 38 | + allPipelinesRunAfterCleanUp := allPipelinesRunsCnt / (maxKeepRun + 1) |
| 39 | + loopMax := 35 |
| 40 | + |
| 41 | + targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns") |
| 42 | + _, runcnx, opts, ghcnx, err := tgithub.Setup(ctx, true, false) |
| 43 | + assert.NilError(t, err) |
| 44 | + |
| 45 | + runcnx.Clients.Log.Infof("Starting %d pipelineruns, (numberOfPullRequest=%d*numberOfPipelineRuns=%d) + (numberOfPullRequest=%d*numberOfRetests=%d*numberOfPipelineRuns=%d) Should end after clean up (maxKeepRun=%d) with %d", |
| 46 | + allPipelinesRunsCnt, numberOfPullRequest, numberOfPipelineRuns, numberOfPullRequest, numberOfRetests, numberOfPipelineRuns, maxKeepRun, allPipelinesRunAfterCleanUp) |
| 47 | + |
| 48 | + repoinfo, resp, err := ghcnx.Client.Repositories.Get(ctx, opts.Organization, opts.Repo) |
| 49 | + assert.NilError(t, err) |
| 50 | + if resp != nil && resp.StatusCode == http.StatusNotFound { |
| 51 | + t.Errorf("Repository %s not found in %s", opts.Organization, opts.Repo) |
| 52 | + } |
| 53 | + // set concurrency |
| 54 | + opts.Concurrency = maxNumberOfConcurrentPipelineRuns |
| 55 | + err = tgithub.CreateCRD(ctx, t, repoinfo, runcnx, opts, targetNS) |
| 56 | + assert.NilError(t, err) |
| 57 | + |
| 58 | + allPullRequests := []int{} |
| 59 | + for prc := 0; prc < numberOfPullRequest; prc++ { |
| 60 | + branchName := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("branch") |
| 61 | + logmsg := fmt.Sprintf("Testing %s with Github APPS integration branch %s namespace %s", label, branchName, targetNS) |
| 62 | + yamlFiles := map[string]string{} |
| 63 | + randomAlphaString := strings.ToLower(random.AlphaString(4)) |
| 64 | + for i := 1; i <= numberOfPipelineRuns; i++ { |
| 65 | + yamlFiles[fmt.Sprintf(".tekton/prlongrunnning-%s-%d.yaml", randomAlphaString, i)] = "testdata/pipelinerun_long_running_maxkeep_run.yaml" |
| 66 | + } |
| 67 | + |
| 68 | + entries, err := payload.GetEntries(yamlFiles, targetNS, options.MainBranch, "pull_request", map[string]string{ |
| 69 | + "MaxKeepRun": fmt.Sprint(maxKeepRun), |
| 70 | + }) |
| 71 | + assert.NilError(t, err) |
| 72 | + |
| 73 | + targetRefName := fmt.Sprintf("refs/heads/%s", |
| 74 | + names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-test")) |
| 75 | + |
| 76 | + sha, vref, err := tgithub.PushFilesToRef(ctx, ghcnx.Client, logmsg, repoinfo.GetDefaultBranch(), targetRefName, |
| 77 | + opts.Organization, opts.Repo, entries) |
| 78 | + assert.NilError(t, err) |
| 79 | + runcnx.Clients.Log.Infof("Commit %s has been created and pushed to %s", sha, vref.GetURL()) |
| 80 | + |
| 81 | + prNumber, err := tgithub.PRCreate(ctx, runcnx, ghcnx, opts.Organization, opts.Repo, targetRefName, repoinfo.GetDefaultBranch(), logmsg) |
| 82 | + assert.NilError(t, err) |
| 83 | + |
| 84 | + defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) |
| 85 | + allPullRequests = append(allPullRequests, prNumber) |
| 86 | + } |
| 87 | + |
| 88 | + // send some retest to spice things up on concurrency and test the maxKeepRun |
| 89 | + for i := 0; i < numberOfRetests; i++ { |
| 90 | + for _, prNumber := range allPullRequests { |
| 91 | + _, _, err := ghcnx.Client.Issues.CreateComment(ctx, |
| 92 | + opts.Organization, |
| 93 | + opts.Repo, prNumber, |
| 94 | + &github.IssueComment{Body: github.String("/retest")}) |
| 95 | + assert.NilError(t, err) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + finished := false |
| 100 | + for i := 0; i < loopMax; i++ { |
| 101 | + unsuccessful := 0 |
| 102 | + prs, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{}) |
| 103 | + assert.NilError(t, err) |
| 104 | + for _, pr := range prs.Items { |
| 105 | + if pr.Status.GetConditions() == nil { |
| 106 | + unsuccessful++ |
| 107 | + continue |
| 108 | + } |
| 109 | + for _, condition := range pr.Status.GetConditions() { |
| 110 | + if condition.Status == "Unknown" || condition.GetReason() == tektonv1.PipelineRunSpecStatusPending { |
| 111 | + unsuccessful++ |
| 112 | + continue |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + if unsuccessful == 0 { |
| 117 | + finished = true |
| 118 | + break |
| 119 | + } |
| 120 | + runcnx.Clients.Log.Infof("number of unsuccessful PR %d out of %d, waiting 10s more, %d/%d", unsuccessful, allPipelinesRunsCnt, i, loopMax) |
| 121 | + // it's high because it takes time to process on kind |
| 122 | + time.Sleep(10 * time.Second) |
| 123 | + } |
| 124 | + if !finished { |
| 125 | + t.Errorf("we didn't get %d pipelineruns as successful, some of them are still pending or it's abnormally slow to process the Q", allPipelinesRunsCnt) |
| 126 | + } |
| 127 | + |
| 128 | + prs, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{}) |
| 129 | + assert.NilError(t, err) |
| 130 | + assert.Equal(t, len(prs.Items), allPipelinesRunAfterCleanUp, "we should have had %d kept after cleanup, we got %d", allPipelinesRunAfterCleanUp, len(prs.Items)) |
| 131 | + |
| 132 | + runcnx.Clients.Log.Infof("success: number of cleaned PR is %d we expected to have %d after the cleanup", len(prs.Items), allPipelinesRunAfterCleanUp) |
| 133 | + |
| 134 | + if os.Getenv("TEST_NOCLEANUP") != "true" { |
| 135 | + repository.NSTearDown(ctx, t, runcnx, targetNS) |
| 136 | + return |
| 137 | + } |
| 138 | +} |
0 commit comments