From b30bd22e2c43c2726feb1c44cf8d157c11161432 Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Thu, 8 Feb 2024 11:59:23 +0100 Subject: [PATCH] Reorganize the GitHub test we now use a struct and function attached to it instead of passing everything around all the times this will be more readable and easier to maintain Signed-off-by: Chmouel Boudjnah --- pkg/opscomments/comments_test.go | 2 +- test/gitea_access_control_test.go | 3 +- test/gitea_params_test.go | 6 +- test/gitea_test.go | 8 +- test/github_config_maxkeepruns_test.go | 32 ++--- test/github_incoming_test.go | 12 +- test/github_pullrequest_concurrency_test.go | 12 +- test/github_pullrequest_oktotest_test.go | 37 ++--- ...thub_pullrequest_privaterepository_test.go | 29 ++-- test/github_pullrequest_rerequest_test.go | 55 +++---- test/github_pullrequest_retest_test.go | 81 ++++++----- test/github_pullrequest_test.go | 57 +++++--- test/github_pullrequest_test_comment_test.go | 32 +++-- test/github_push_retest_test.go | 56 ++++---- test/github_push_test.go | 42 ++++-- ...en_to_list_of_private_public_repos_test.go | 15 +- test/github_tkn_pac_cli_test.go | 14 +- test/pkg/github/pr.go | 134 +++++++++++++----- test/pkg/github/setup.go | 25 ---- test/pkg/wait/check.go | 13 +- test/pkg/wait/wait.go | 15 +- 21 files changed, 414 insertions(+), 266 deletions(-) diff --git a/pkg/opscomments/comments_test.go b/pkg/opscomments/comments_test.go index 011db1cc5..80b89a771 100644 --- a/pkg/opscomments/comments_test.go +++ b/pkg/opscomments/comments_test.go @@ -145,7 +145,7 @@ func TestSetEventTypeTestPipelineRun(t *testing.T) { wantTestPr: "prname", }, { - name: "no-ops-commenttest event type", + name: "test single event type", comment: "/test prname", wantType: TestSingleCommentEventType.String(), wantTestPr: "prname", diff --git a/test/gitea_access_control_test.go b/test/gitea_access_control_test.go index d3d90eb4c..dc17939e5 100644 --- a/test/gitea_access_control_test.go +++ b/test/gitea_access_control_test.go @@ -424,7 +424,8 @@ func TestGiteaPolicyAllowedOwnerFiles(t *testing.T) { PollTimeout: twait.DefaultTimeout, TargetSHA: npr.Head.Sha, } - assert.NilError(t, twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)) + _, err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) + assert.NilError(t, err) time.Sleep(5 * time.Second) // “Evil does not sleep. It waits.” - Galadriel prs, err := topts.ParamsRun.Clients.Tekton.TektonV1().PipelineRuns(topts.TargetNS).List(context.Background(), metav1.ListOptions{}) diff --git a/test/gitea_params_test.go b/test/gitea_params_test.go index 8fa2ec801..3cedfa917 100644 --- a/test/gitea_params_test.go +++ b/test/gitea_params_test.go @@ -249,7 +249,7 @@ my email is a true beauty and like groot, I AM pac` PollTimeout: twait.DefaultTimeout, TargetSHA: topts.PullRequest.Head.Sha, } - err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) + _, err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) assert.NilError(t, err) time.Sleep(5 * time.Second) @@ -338,7 +338,7 @@ func TestGiteaParamsChangedFilesCEL(t *testing.T) { PollTimeout: twait.DefaultTimeout, TargetSHA: topts.PullRequest.Head.Sha, } - err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) + _, err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) assert.NilError(t, err) time.Sleep(5 * time.Second) @@ -385,7 +385,7 @@ func TestGiteaParamsChangedFilesCEL(t *testing.T) { PollTimeout: twait.DefaultTimeout, TargetSHA: topts.PullRequest.Head.Sha, } - err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) + _, err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) assert.NilError(t, err) time.Sleep(5 * time.Second) diff --git a/test/gitea_test.go b/test/gitea_test.go index 86f1842b1..41fd25186 100644 --- a/test/gitea_test.go +++ b/test/gitea_test.go @@ -295,7 +295,7 @@ func TestGiteaConfigMaxKeepRun(t *testing.T) { PollTimeout: twait.DefaultTimeout, TargetSHA: topts.PullRequest.Head.Sha, } - err := twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) + _, err := twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) assert.NilError(t, err) time.Sleep(15 * time.Second) // “Evil does not sleep. It waits.” - Galadriel @@ -511,7 +511,7 @@ func TestGiteaCancelRun(t *testing.T) { PollTimeout: twait.DefaultTimeout, TargetSHA: topts.PullRequest.Head.Sha, } - err := twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) + _, err := twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) assert.Error(t, err, "pipelinerun has failed") tgitea.CheckIfPipelineRunsCancelled(t, topts) @@ -694,7 +694,7 @@ func TestGiteaPushToTagGreedy(t *testing.T) { MinNumberStatus: 0, PollTimeout: twait.DefaultTimeout, } - err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) + _, err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) assert.NilError(t, err) } @@ -750,7 +750,7 @@ func TestGiteaClusterTasks(t *testing.T) { PollTimeout: twait.DefaultTimeout, TargetSHA: topts.PullRequest.Head.Sha, } - err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) + _, err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts) assert.NilError(t, err) topts.CheckForStatus = "success" diff --git a/test/github_config_maxkeepruns_test.go b/test/github_config_maxkeepruns_test.go index 7275ac93d..323f3e1d1 100644 --- a/test/github_config_maxkeepruns_test.go +++ b/test/github_config_maxkeepruns_test.go @@ -19,32 +19,32 @@ import ( func TestGithubMaxKeepRuns(t *testing.T) { ctx := context.TODO() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, sha := tgithub.RunPullRequest(ctx, t, - "Github MaxKeepRun config", - []string{"testdata/pipelinerun-max-keep-run-1.yaml"}, false, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github MaxKeepRun config", + YamlFiles: []string{"testdata/pipelinerun-max-keep-run-1.yaml"}, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) - runcnx.Clients.Log.Infof("Creating /retest in PullRequest") - _, _, err := ghcnx.Client.Issues.CreateComment(ctx, - opts.Organization, - opts.Repo, prNumber, + g.Cnx.Clients.Log.Infof("Creating /retest in PullRequest") + _, _, err := g.Provider.Client.Issues.CreateComment(ctx, g.Options.Organization, g.Options.Repo, g.PRNumber, &ghlib.IssueComment{Body: ghlib.String("/retest")}) assert.NilError(t, err) - runcnx.Clients.Log.Infof("Wait for the second repository update to be updated") + g.Cnx.Clients.Log.Infof("Wait for the second repository update to be updated") waitOpts := twait.Opts{ - RepoName: targetNS, - Namespace: targetNS, + RepoName: g.TargetNamespace, + Namespace: g.TargetNamespace, MinNumberStatus: 2, PollTimeout: twait.DefaultTimeout, - TargetSHA: sha, + TargetSHA: g.SHA, } - err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts) + _, err = twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, waitOpts) assert.NilError(t, err) count := 0 for { - prs, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{}) + prs, err := g.Cnx.Clients.Tekton.TektonV1().PipelineRuns(g.TargetNamespace).List(ctx, metav1.ListOptions{}) if err == nil && len(prs.Items) == 1 { if prs.Items[0].GetStatusCondition().GetCondition(apis.ConditionSucceeded).GetReason() == "Running" { t.Logf("skipping %s since currently running", prs.Items[0].GetName()) @@ -52,7 +52,7 @@ func TestGithubMaxKeepRuns(t *testing.T) { } // making sure secret is not deleted for existing pipelinerun if secretName, ok := prs.Items[0].GetAnnotations()[keys.GitAuthSecret]; ok { - sData, err := runcnx.Clients.Kube.CoreV1().Secrets(targetNS).Get(ctx, secretName, metav1.GetOptions{}) + sData, err := g.Cnx.Clients.Kube.CoreV1().Secrets(g.TargetNamespace).Get(ctx, secretName, metav1.GetOptions{}) assert.NilError(t, err, "Secret should not have been deleted while running pipelinerun") assert.Assert(t, sData.Name != "") } @@ -60,7 +60,7 @@ func TestGithubMaxKeepRuns(t *testing.T) { } time.Sleep(10 * time.Second) if count > 10 { - t.Fatalf("PipelineRun cleanups has not been done, we found %d in %s", len(prs.Items), targetNS) + t.Fatalf("PipelineRun cleanups has not been done, we found %d in %s", len(prs.Items), g.TargetNamespace) } count++ } diff --git a/test/github_incoming_test.go b/test/github_incoming_test.go index 22db1f31e..eac9cafe5 100644 --- a/test/github_incoming_test.go +++ b/test/github_incoming_test.go @@ -109,7 +109,17 @@ func verifyIncomingWebhook(t *testing.T, randomedString string, entries map[stri runcnx.Clients.Log.Infof("Kicked off on incoming URL: %s", url) assert.Assert(t, httpResp.StatusCode >= 200 && httpResp.StatusCode < 300) // to re enable after debugging... - defer tgithub.TearDown(ctx, t, runcnx, ghprovider, -1, targetRefName, randomedString, opts) + g := tgithub.PRTest{ + Cnx: runcnx, + Options: opts, + Provider: ghprovider, + TargetNamespace: randomedString, + TargetRefName: targetRefName, + PRNumber: -1, + SHA: sha, + Logger: runcnx.Clients.Log, + } + defer g.TearDown(ctx, t) sopt := wait.SuccessOpt{ Title: title, diff --git a/test/github_pullrequest_concurrency_test.go b/test/github_pullrequest_concurrency_test.go index 844a006f7..da5aef6d3 100644 --- a/test/github_pullrequest_concurrency_test.go +++ b/test/github_pullrequest_concurrency_test.go @@ -78,7 +78,17 @@ func TestGithubPullRequestConcurrency(t *testing.T) { prNumber, err := tgithub.PRCreate(ctx, runcnx, ghcnx, opts.Organization, opts.Repo, targetRefName, repoinfo.GetDefaultBranch(), logmsg) assert.NilError(t, err) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := tgithub.PRTest{ + Cnx: runcnx, + Options: opts, + Provider: ghcnx, + TargetNamespace: targetNS, + TargetRefName: targetRefName, + PRNumber: prNumber, + SHA: sha, + Logger: runcnx.Clients.Log, + } + defer g.TearDown(ctx, t) runcnx.Clients.Log.Info("waiting to let controller process the event") time.Sleep(5 * time.Second) diff --git a/test/github_pullrequest_oktotest_test.go b/test/github_pullrequest_oktotest_test.go index d021292ab..e68629fd7 100644 --- a/test/github_pullrequest_oktotest_test.go +++ b/test/github_pullrequest_oktotest_test.go @@ -26,22 +26,25 @@ func TestGithubPullRequestOkToTest(t *testing.T) { t.Skip("Skipping test since only enabled for nightly") } ctx := context.TODO() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, sha := tgithub.RunPullRequest(ctx, t, - "Github OkToTest comment", []string{"testdata/pipelinerun.yaml"}, false, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github OkToTest comment", + YamlFiles: []string{"testdata/pipelinerun.yaml"}, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) - repoinfo, resp, err := ghcnx.Client.Repositories.Get(ctx, opts.Organization, opts.Repo) + repoinfo, resp, err := g.Provider.Client.Repositories.Get(ctx, g.Options.Organization, g.Options.Repo) assert.NilError(t, err) if resp != nil && resp.StatusCode == http.StatusNotFound { - t.Errorf("Repository %s not found in %s", opts.Organization, opts.Repo) + t.Errorf("Repository %s not found in %s", g.Options.Organization, g.Options.Repo) } runevent := info.Event{ DefaultBranch: repoinfo.GetDefaultBranch(), - Organization: opts.Organization, - Repository: opts.Repo, + Organization: g.Options.Organization, + Repository: g.Options.Repo, URL: repoinfo.GetHTMLURL(), - Sender: opts.Organization, + Sender: g.Options.Organization, } installID, err := strconv.ParseInt(os.Getenv("TEST_GITHUB_REPO_INSTALLATION_ID"), 10, 64) @@ -59,7 +62,7 @@ func TestGithubPullRequestOkToTest(t *testing.T) { PullRequestLinks: &github.PullRequestLinks{ HTMLURL: github.String(fmt.Sprintf("%s/%s/pull/%d", os.Getenv("TEST_GITHUB_API_URL"), - os.Getenv("TEST_GITHUB_REPO_OWNER"), prNumber)), + os.Getenv("TEST_GITHUB_REPO_OWNER"), g.PRNumber)), }, }, Repo: &github.Repository{ @@ -74,7 +77,7 @@ func TestGithubPullRequestOkToTest(t *testing.T) { } err = payload.Send(ctx, - runcnx, + g.Cnx, os.Getenv("TEST_EL_URL"), os.Getenv("TEST_EL_WEBHOOK_SECRET"), os.Getenv("TEST_GITHUB_API_URL"), @@ -84,19 +87,19 @@ func TestGithubPullRequestOkToTest(t *testing.T) { ) assert.NilError(t, err) - runcnx.Clients.Log.Infof("Wait for the second repository update to be updated") + g.Cnx.Clients.Log.Infof("Wait for the second repository update to be updated") waitOpts := twait.Opts{ - RepoName: targetNS, - Namespace: targetNS, + RepoName: g.TargetNamespace, + Namespace: g.TargetNamespace, MinNumberStatus: 1, PollTimeout: twait.DefaultTimeout, - TargetSHA: sha, + TargetSHA: g.SHA, } - err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts) + _, err = twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, waitOpts) assert.NilError(t, err) - runcnx.Clients.Log.Infof("Check if we have the repository set as succeeded") - repo, err := runcnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(targetNS).Get(ctx, targetNS, metav1.GetOptions{}) + g.Cnx.Clients.Log.Infof("Check if we have the repository set as succeeded") + repo, err := g.Cnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(g.TargetNamespace).Get(ctx, g.TargetNamespace, metav1.GetOptions{}) assert.NilError(t, err) assert.Assert(t, repo.Status[len(repo.Status)-1].Conditions[0].Status == corev1.ConditionTrue) } diff --git a/test/github_pullrequest_privaterepository_test.go b/test/github_pullrequest_privaterepository_test.go index f0a0d4ca5..fa7d2779e 100644 --- a/test/github_pullrequest_privaterepository_test.go +++ b/test/github_pullrequest_privaterepository_test.go @@ -16,16 +16,23 @@ func TestGithubPullRequestGitClone(t *testing.T) { t.Skip("Skipping test since only enabled for nightly") } ctx := context.Background() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPullRequest(ctx, t, - "Github Private Repo", []string{"testdata/pipelinerun_git_clone_private.yaml"}, false, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github - Private Repo", + YamlFiles: []string{"testdata/pipelinerun_git_clone_private.yaml"}, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) } func TestGithubSecondPullRequestGitClone(t *testing.T) { ctx := context.Background() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPullRequest(ctx, t, - "Github Private Repo", []string{"testdata/pipelinerun_git_clone_private.yaml"}, true, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github GHE - Private Repo", + YamlFiles: []string{"testdata/pipelinerun_git_clone_private.yaml"}, + SecondController: true, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) } func TestGithubPullRequestPrivateRepositoryOnWebhook(t *testing.T) { @@ -33,9 +40,13 @@ func TestGithubPullRequestPrivateRepositoryOnWebhook(t *testing.T) { t.Skip("Skipping test since only enabled for nightly") } ctx := context.Background() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPullRequest(ctx, t, - "Github Private Repo OnWebhook", []string{"testdata/pipelinerun_git_clone_private.yaml"}, false, true) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github Rerequest", + YamlFiles: []string{"testdata/pipelinerun_git_clone_private.yaml"}, + Webhook: true, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) } // Local Variables: diff --git a/test/github_pullrequest_rerequest_test.go b/test/github_pullrequest_rerequest_test.go index e69b33ed4..55ec5a291 100644 --- a/test/github_pullrequest_rerequest_test.go +++ b/test/github_pullrequest_rerequest_test.go @@ -27,24 +27,27 @@ func TestGithubPullRerequest(t *testing.T) { t.Skip("Skipping test since only enabled for nightly") } ctx := context.TODO() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, sha := tgithub.RunPullRequest(ctx, t, "Github Rerequest", - []string{"testdata/pipelinerun.yaml"}, false, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github Rerequest", + YamlFiles: []string{"testdata/pipelinerun.yaml"}, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) - repoinfo, resp, err := ghcnx.Client.Repositories.Get(ctx, opts.Organization, opts.Repo) + repoinfo, resp, err := g.Provider.Client.Repositories.Get(ctx, g.Options.Organization, g.Options.Repo) assert.NilError(t, err) if resp != nil && resp.StatusCode == http.StatusNotFound { - t.Errorf("Repository %s not found in %s", opts.Organization, opts.Repo) + t.Errorf("Repository %s not found in %s", g.Options.Organization, g.Options.Repo) } runinfo := info.Event{ DefaultBranch: repoinfo.GetDefaultBranch(), - HeadBranch: targetRefName, - Organization: opts.Organization, - Repository: opts.Repo, + HeadBranch: g.TargetRefName, + Organization: g.Options.Organization, + Repository: g.Options.Repo, URL: repoinfo.GetHTMLURL(), - SHA: sha, - Sender: opts.Organization, + SHA: g.SHA, + Sender: g.Options.Organization, } installID, err := strconv.ParseInt(os.Getenv("TEST_GITHUB_REPO_INSTALLATION_ID"), 10, 64) @@ -60,7 +63,7 @@ func TestGithubPullRerequest(t *testing.T) { HeadSHA: &runinfo.SHA, PullRequests: []*github.PullRequest{ { - Number: github.Int(prNumber), + Number: github.Int(g.PRNumber), }, }, }, @@ -77,7 +80,7 @@ func TestGithubPullRerequest(t *testing.T) { } err = payload.Send(ctx, - runcnx, + g.Cnx, os.Getenv("TEST_EL_URL"), os.Getenv("TEST_EL_WEBHOOK_SECRET"), os.Getenv("TEST_GITHUB_API_URL"), @@ -87,18 +90,18 @@ func TestGithubPullRerequest(t *testing.T) { ) assert.NilError(t, err) - runcnx.Clients.Log.Infof("Wait for the second repository update to be updated") - err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, twait.Opts{ - RepoName: targetNS, - Namespace: targetNS, + g.Cnx.Clients.Log.Infof("Wait for the second repository update to be updated") + _, err = twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, twait.Opts{ + RepoName: g.TargetNamespace, + Namespace: g.TargetNamespace, MinNumberStatus: 1, PollTimeout: twait.DefaultTimeout, - TargetSHA: sha, + TargetSHA: g.SHA, }) assert.NilError(t, err) - runcnx.Clients.Log.Infof("Check if we have the repository set as succeeded") - repo, err := runcnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(targetNS).Get(ctx, targetNS, metav1.GetOptions{}) + g.Cnx.Clients.Log.Infof("Check if we have the repository set as succeeded") + repo, err := g.Cnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(g.TargetNamespace).Get(ctx, g.TargetNamespace, metav1.GetOptions{}) assert.NilError(t, err) assert.Assert(t, repo.Status[len(repo.Status)-1].Conditions[0].Status == corev1.ConditionTrue) csEvent := github.CheckSuiteEvent{ @@ -111,7 +114,7 @@ func TestGithubPullRerequest(t *testing.T) { HeadSHA: &runinfo.SHA, PullRequests: []*github.PullRequest{ { - Number: github.Int(prNumber), + Number: github.Int(g.PRNumber), }, }, }, @@ -127,7 +130,7 @@ func TestGithubPullRerequest(t *testing.T) { } err = payload.Send(ctx, - runcnx, + g.Cnx, os.Getenv("TEST_EL_URL"), os.Getenv("TEST_EL_WEBHOOK_SECRET"), os.Getenv("TEST_GITHUB_API_URL"), @@ -137,13 +140,13 @@ func TestGithubPullRerequest(t *testing.T) { ) assert.NilError(t, err) - runcnx.Clients.Log.Infof("Wait for the second repository update to be updated") - err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, twait.Opts{ - RepoName: targetNS, - Namespace: targetNS, + g.Cnx.Clients.Log.Infof("Wait for the second repository update to be updated") + _, err = twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, twait.Opts{ + RepoName: g.TargetNamespace, + Namespace: g.TargetNamespace, MinNumberStatus: 2, PollTimeout: twait.DefaultTimeout, - TargetSHA: sha, + TargetSHA: g.SHA, }) assert.NilError(t, err) } diff --git a/test/github_pullrequest_retest_test.go b/test/github_pullrequest_retest_test.go index f56ab4a85..afe6500b3 100644 --- a/test/github_pullrequest_retest_test.go +++ b/test/github_pullrequest_retest_test.go @@ -23,30 +23,35 @@ func TestGithubPullRequestRetest(t *testing.T) { t.Skip("Skipping test since only enabled for nightly") } ctx := context.Background() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, sha := tgithub.RunPullRequest(ctx, t, - "Github Retest comment", []string{"testdata/pipelinerun.yaml"}, false, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github retest comment", + YamlFiles: []string{"testdata/pipelinerun.yaml"}, + SecondController: true, + NoStatusCheck: true, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) - runcnx.Clients.Log.Infof("Creating /retest in PullRequest") - _, _, err := ghcnx.Client.Issues.CreateComment(ctx, - opts.Organization, - opts.Repo, prNumber, + g.Cnx.Clients.Log.Infof("Creating /retest in PullRequest") + _, _, err := g.Provider.Client.Issues.CreateComment(ctx, + g.Options.Organization, + g.Options.Repo, g.PRNumber, &github.IssueComment{Body: github.String("/retest")}) assert.NilError(t, err) - runcnx.Clients.Log.Infof("Wait for the second repository update to be updated") + g.Cnx.Clients.Log.Infof("Wait for the second repository update to be updated") waitOpts := twait.Opts{ - RepoName: targetNS, - Namespace: targetNS, + RepoName: g.TargetNamespace, + Namespace: g.TargetNamespace, MinNumberStatus: 1, PollTimeout: twait.DefaultTimeout, - TargetSHA: sha, + TargetSHA: g.SHA, } - err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts) + _, err = twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, waitOpts) assert.NilError(t, err) - runcnx.Clients.Log.Infof("Check if we have the repository set as succeeded") - repo, err := runcnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(targetNS).Get(ctx, targetNS, metav1.GetOptions{}) + g.Cnx.Clients.Log.Infof("Check if we have the repository set as succeeded") + repo, err := g.Cnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(g.TargetNamespace).Get(ctx, g.TargetNamespace, metav1.GetOptions{}) assert.NilError(t, err) assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionTrue) } @@ -54,11 +59,15 @@ func TestGithubPullRequestRetest(t *testing.T) { // TestGithubPullRequestGitOpsComments tests GitOps comments /test, /retest and /cancel commands. func TestGithubPullRequestGitOpsComments(t *testing.T) { ctx := context.Background() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, sha := tgithub.RunPullRequest(ctx, t, - "Github Retest comment", []string{"testdata/pipelinerun.yaml", "testdata/pipelinerun-gitops.yaml"}, false, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github PullRequest GitOps Comments test", + YamlFiles: []string{"testdata/pipelinerun.yaml", "testdata/pipelinerun-gitops.yaml"}, + SecondController: false, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) - pruns, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{}) + pruns, err := g.Cnx.Clients.Tekton.TektonV1().PipelineRuns(g.TargetNamespace).List(ctx, metav1.ListOptions{}) assert.NilError(t, err) assert.Equal(t, len(pruns.Items), 2) @@ -80,35 +89,35 @@ func TestGithubPullRequestGitOpsComments(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { waitOpts := twait.Opts{ - RepoName: targetNS, - Namespace: targetNS, + RepoName: g.TargetNamespace, + Namespace: g.TargetNamespace, MinNumberStatus: tt.prNum, PollTimeout: twait.DefaultTimeout, - TargetSHA: sha, + TargetSHA: g.SHA, } if tt.comment == "/cancel pr-gitops-comment" { - runcnx.Clients.Log.Info("/test pr-gitops-comment on Pull Request before canceling") - _, _, err := ghcnx.Client.Issues.CreateComment(ctx, - opts.Organization, - opts.Repo, prNumber, + g.Cnx.Clients.Log.Info("/test pr-gitops-comment on Pull Request before canceling") + _, _, err := g.Provider.Client.Issues.CreateComment(ctx, + g.Options.Organization, + g.Options.Repo, g.PRNumber, &github.IssueComment{Body: github.String("/test pr-gitops-comment")}) assert.NilError(t, err) - err = twait.UntilPipelineRunCreated(ctx, runcnx.Clients, waitOpts) + err = twait.UntilPipelineRunCreated(ctx, g.Cnx.Clients, waitOpts) assert.NilError(t, err) } - runcnx.Clients.Log.Infof("%s on Pull Request", tt.comment) - _, _, err = ghcnx.Client.Issues.CreateComment(ctx, - opts.Organization, - opts.Repo, prNumber, + g.Cnx.Clients.Log.Infof("%s on Pull Request", tt.comment) + _, _, err = g.Provider.Client.Issues.CreateComment(ctx, + g.Options.Organization, + g.Options.Repo, g.PRNumber, &github.IssueComment{Body: github.String(tt.comment)}) assert.NilError(t, err) - runcnx.Clients.Log.Info("Waiting for Repository to be updated") - err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts) + g.Cnx.Clients.Log.Info("Waiting for Repository to be updated") + _, err = twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, waitOpts) assert.NilError(t, err) - runcnx.Clients.Log.Infof("Check if we have the repository set as succeeded") - repo, err := runcnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(targetNS).Get(ctx, targetNS, metav1.GetOptions{}) + g.Cnx.Clients.Log.Infof("Check if we have the repository set as succeeded") + repo, err := g.Cnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(g.TargetNamespace).Get(ctx, g.TargetNamespace, metav1.GetOptions{}) assert.NilError(t, err) if tt.comment == "/cancel pr-gitops-comment" { assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionFalse) @@ -116,8 +125,8 @@ func TestGithubPullRequestGitOpsComments(t *testing.T) { assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionTrue) } - pruns, err = runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{ - LabelSelector: fmt.Sprintf("%s=%s", keys.SHA, sha), + pruns, err = g.Cnx.Clients.Tekton.TektonV1().PipelineRuns(g.TargetNamespace).List(ctx, metav1.ListOptions{ + LabelSelector: fmt.Sprintf("%s=%s", keys.SHA, g.SHA), }) assert.NilError(t, err) assert.Equal(t, len(pruns.Items), tt.prNum) diff --git a/test/github_pullrequest_test.go b/test/github_pullrequest_test.go index c0e6cbb45..4e6d5c13b 100644 --- a/test/github_pullrequest_test.go +++ b/test/github_pullrequest_test.go @@ -13,17 +13,23 @@ import ( func TestGithubPullRequest(t *testing.T) { ctx := context.Background() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPullRequest(ctx, t, "Github PullRequest", - []string{"testdata/pipelinerun.yaml"}, false, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github PullRequest", + YamlFiles: []string{"testdata/pipelinerun.yaml"}, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) } func TestGithubPullRequestSecondController(t *testing.T) { ctx := context.Background() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPullRequest( - ctx, t, "Github PullRequest", []string{"testdata/pipelinerun.yaml"}, true, false, - ) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github Rerequest", + YamlFiles: []string{"testdata/pipelinerun.yaml"}, + SecondController: true, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) } func TestGithubPullRequestMultiples(t *testing.T) { @@ -31,9 +37,12 @@ func TestGithubPullRequestMultiples(t *testing.T) { t.Skip("Skipping test since only enabled for nightly") } ctx := context.Background() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPullRequest(ctx, t, "Github PullRequest", - []string{"testdata/pipelinerun.yaml", "testdata/pipelinerun-clone.yaml"}, false, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github multiple PullRequest", + YamlFiles: []string{"testdata/pipelinerun.yaml", "testdata/pipelinerun-clone.yaml"}, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) } func TestGithubPullRequestMatchOnCEL(t *testing.T) { @@ -41,16 +50,22 @@ func TestGithubPullRequestMatchOnCEL(t *testing.T) { t.Skip("Skipping test since only enabled for nightly") } ctx := context.Background() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPullRequest(ctx, t, "Github PullRequest", - []string{"testdata/pipelinerun-cel-annotation.yaml"}, false, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github CEL Match", + YamlFiles: []string{"testdata/pipelinerun-cel-annotation.yaml"}, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) } func TestGithubPullRequestCELMatchOnTitle(t *testing.T) { ctx := context.Background() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPullRequest(ctx, t, "Github PullRequest", - []string{"testdata/pipelinerun-cel-annotation-for-title-match.yaml"}, false, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github CEL Match on Title", + YamlFiles: []string{"testdata/pipelinerun-cel-annotation-for-title-match.yaml"}, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) } func TestGithubPullRequestWebhook(t *testing.T) { @@ -63,9 +78,13 @@ func TestGithubPullRequestWebhook(t *testing.T) { } ctx := context.Background() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPullRequest(ctx, t, - "Github PullRequest onWebhook", []string{"testdata/pipelinerun.yaml"}, false, true) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github PullRequest onWebhook", + YamlFiles: []string{"testdata/pipelinerun.yaml"}, + Webhook: true, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) } // Local Variables: diff --git a/test/github_pullrequest_test_comment_test.go b/test/github_pullrequest_test_comment_test.go index f6219f8e3..1ffb47cfe 100644 --- a/test/github_pullrequest_test_comment_test.go +++ b/test/github_pullrequest_test_comment_test.go @@ -21,30 +21,34 @@ func TestGithubPullRequestTest(t *testing.T) { t.Skip("Skipping test since only enabled for nightly") } ctx := context.TODO() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, sha := tgithub.RunPullRequest(ctx, t, "Github test comment", - []string{"testdata/pipelinerun.yaml", "testdata/pipelinerun-clone.yaml"}, false, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github test implicit comment", + YamlFiles: []string{"testdata/pipelinerun.yaml", "testdata/pipelinerun-clone.yaml"}, + SecondController: false, + } + g.RunPullRequest(ctx, t) + defer g.TearDown(ctx, t) - runcnx.Clients.Log.Infof("Creating /test in PullRequest") - _, _, err := ghcnx.Client.Issues.CreateComment(ctx, - opts.Organization, - opts.Repo, prNumber, + g.Cnx.Clients.Log.Infof("Creating /test in PullRequest") + _, _, err := g.Provider.Client.Issues.CreateComment(ctx, + g.Options.Organization, + g.Options.Repo, g.PRNumber, &github.IssueComment{Body: github.String("/test pipeline")}) assert.NilError(t, err) - runcnx.Clients.Log.Infof("Wait for the second repository update to be updated") + g.Cnx.Clients.Log.Infof("Wait for the second repository update to be updated") waitOpts := twait.Opts{ - RepoName: targetNS, - Namespace: targetNS, + RepoName: g.TargetNamespace, + Namespace: g.TargetNamespace, MinNumberStatus: 1, PollTimeout: twait.DefaultTimeout, - TargetSHA: sha, + TargetSHA: g.SHA, } - err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts) + _, err = twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, waitOpts) assert.NilError(t, err) - runcnx.Clients.Log.Infof("Check if we have the repository set as succeeded") - repo, err := runcnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(targetNS).Get(ctx, targetNS, metav1.GetOptions{}) + g.Cnx.Clients.Log.Infof("Check if we have the repository set as succeeded") + repo, err := g.Cnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(g.TargetNamespace).Get(ctx, g.TargetNamespace, metav1.GetOptions{}) assert.NilError(t, err) assert.Assert(t, repo.Status[len(repo.Status)-1].Conditions[0].Status == corev1.ConditionTrue) } diff --git a/test/github_push_retest_test.go b/test/github_push_retest_test.go index 14291b7d6..185203945 100644 --- a/test/github_push_retest_test.go +++ b/test/github_push_retest_test.go @@ -17,11 +17,15 @@ import ( func TestGithubSecondPushRequestGitOpsComments(t *testing.T) { ctx := context.Background() - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, sha := tgithub.RunPushRequest(ctx, t, - "Github Push Request", []string{"testdata/pipelinerun-on-push.yaml", "testdata/pipelinerun.yaml"}, true, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github push request", + YamlFiles: []string{"testdata/pipelinerun-on-push.yaml", "testdata/pipelinerun.yaml"}, + SecondController: false, + } + g.RunPushRequest(ctx, t) + defer g.TearDown(ctx, t) - pruns, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{}) + pruns, err := g.Cnx.Clients.Tekton.TektonV1().PipelineRuns(g.TargetNamespace).List(ctx, metav1.ListOptions{}) assert.NilError(t, err) assert.Equal(t, len(pruns.Items), 2) @@ -31,55 +35,55 @@ func TestGithubSecondPushRequestGitOpsComments(t *testing.T) { }{ { name: "Retest", - comment: "/retest branch:" + targetNS, + comment: "/retest branch:" + g.TargetNamespace, prNum: 4, }, { name: "Test and Cancel PipelineRun", - comment: "/cancel pipelinerun-on-push branch:" + targetNS, + comment: "/cancel pipelinerun-on-push branch:" + g.TargetNamespace, prNum: 5, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { waitOpts := twait.Opts{ - RepoName: targetNS, - Namespace: targetNS, + RepoName: g.TargetNamespace, + Namespace: g.TargetNamespace, MinNumberStatus: tt.prNum, PollTimeout: twait.DefaultTimeout, - TargetSHA: sha, + TargetSHA: g.SHA, } - if tt.comment == "/cancel pipelinerun-on-push branch:"+targetNS { - runcnx.Clients.Log.Info("/test pipelinerun-on-push on Push Request before canceling") - _, _, err = ghcnx.Client.Repositories.CreateComment(ctx, - opts.Organization, - opts.Repo, sha, - &github.RepositoryComment{Body: github.String("/test pipelinerun-on-push branch:" + targetNS)}) + if tt.comment == "/cancel pipelinerun-on-push branch:"+g.TargetNamespace { + g.Cnx.Clients.Log.Info("/test pipelinerun-on-push on Push Request before canceling") + _, _, err = g.Provider.Client.Repositories.CreateComment(ctx, + g.Options.Organization, + g.Options.Repo, g.SHA, + &github.RepositoryComment{Body: github.String("/test pipelinerun-on-push branch:" + g.TargetNamespace)}) assert.NilError(t, err) - err = twait.UntilPipelineRunCreated(ctx, runcnx.Clients, waitOpts) + err = twait.UntilPipelineRunCreated(ctx, g.Cnx.Clients, waitOpts) assert.NilError(t, err) } - runcnx.Clients.Log.Infof("%s on Push Request", tt.comment) - _, _, err = ghcnx.Client.Repositories.CreateComment(ctx, - opts.Organization, - opts.Repo, sha, + g.Cnx.Clients.Log.Infof("%s on Push Request", tt.comment) + _, _, err = g.Provider.Client.Repositories.CreateComment(ctx, + g.Options.Organization, + g.Options.Repo, g.SHA, &github.RepositoryComment{Body: github.String(tt.comment)}) assert.NilError(t, err) - runcnx.Clients.Log.Info("Waiting for Repository to be updated") - err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts) + g.Cnx.Clients.Log.Info("Waiting for Repository to be updated") + _, err = twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, waitOpts) assert.NilError(t, err) - runcnx.Clients.Log.Infof("Check if we have the repository set as succeeded") - repo, err := runcnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(targetNS).Get(ctx, targetNS, metav1.GetOptions{}) + g.Cnx.Clients.Log.Infof("Check if we have the repository set as succeeded") + repo, err := g.Cnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(g.TargetNamespace).Get(ctx, g.TargetNamespace, metav1.GetOptions{}) assert.NilError(t, err) - if tt.comment == "/cancel pipelinerun-on-push branch:"+targetNS { + if tt.comment == "/cancel pipelinerun-on-push branch:"+g.TargetNamespace { assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionFalse) } else { assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionTrue) } - pruns, err = runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{}) + pruns, err = g.Cnx.Clients.Tekton.TektonV1().PipelineRuns(g.TargetNamespace).List(ctx, metav1.ListOptions{}) assert.NilError(t, err) assert.Equal(t, len(pruns.Items), tt.prNum) }) diff --git a/test/github_push_test.go b/test/github_push_test.go index a186c1852..4562b9036 100644 --- a/test/github_push_test.go +++ b/test/github_push_test.go @@ -5,6 +5,7 @@ package test import ( "context" + "fmt" "os" "testing" @@ -17,23 +18,34 @@ func TestGithubPush(t *testing.T) { } ctx := context.Background() if os.Getenv("TEST_GITHUB_REPO_OWNER_WEBHOOK") == "" { - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPushRequest(ctx, t, - "Github Push Request Webhook", []string{"testdata/pipelinerun-on-push.yaml"}, false, true) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github push request on Webhook", + YamlFiles: []string{"testdata/pipelinerun-on-push.yaml"}, + SecondController: false, + Webhook: true, + } + g.RunPushRequest(ctx, t) + defer g.TearDown(ctx, t) } else { t.Skip("TEST_GITHUB_REPO_OWNER_WEBHOOK is not set") } - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPushRequest(ctx, t, - "Github Push Request Apps", []string{"testdata/pipelinerun-on-push.yaml"}, false, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github apps push request", + YamlFiles: []string{"testdata/pipelinerun-on-push.yaml"}, + } + g.RunPushRequest(ctx, t) + defer g.TearDown(ctx, t) } func TestGithubSecondPush(t *testing.T) { ctx := context.Background() - - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPushRequest(ctx, t, - "Github Push Request GHE Apps", []string{"testdata/pipelinerun-on-push.yaml"}, true, false) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: "Github push request", + YamlFiles: []string{"testdata/pipelinerun-on-push.yaml"}, + SecondController: true, + } + g.RunPushRequest(ctx, t) + defer g.TearDown(ctx, t) } func TestGithubPushRequestCELMatchOnTitle(t *testing.T) { @@ -43,8 +55,12 @@ func TestGithubPushRequestCELMatchOnTitle(t *testing.T) { t.Skip("TEST_GITHUB_REPO_OWNER_WEBHOOK is not set") continue } - runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPushRequest(ctx, t, - "Github Push Request", []string{"testdata/pipelinerun-cel-annotation-for-title-match.yaml"}, false, onWebhook) - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts) + g := &tgithub.PRTest{ + Label: fmt.Sprintf("Github push request test CEL match on title onWebhook=%v", onWebhook), + YamlFiles: []string{"testdata/pipelinerun-cel-annotation-for-title-match.yaml"}, + Webhook: onWebhook, + } + g.RunPushRequest(ctx, t) + defer g.TearDown(ctx, t) } } diff --git a/test/github_scope_token_to_list_of_private_public_repos_test.go b/test/github_scope_token_to_list_of_private_public_repos_test.go index c1ad83831..51eef9b89 100644 --- a/test/github_scope_token_to_list_of_private_public_repos_test.go +++ b/test/github_scope_token_to_list_of_private_public_repos_test.go @@ -157,8 +157,17 @@ func verifyGHTokenScope(t *testing.T, remoteTaskURL, remoteTaskName string, data title := "TestPullRequestRemoteAnnotations - " + targetRefName number, err := tgithub.PRCreate(ctx, runcnx, ghcnx, opts.Organization, opts.Repo, targetRefName, repoinfo.GetDefaultBranch(), title) assert.NilError(t, err) - - defer tgithub.TearDown(ctx, t, runcnx, ghcnx, number, targetRefName, targetNS, opts) + g := tgithub.PRTest{ + Cnx: runcnx, + Options: opts, + Provider: ghcnx, + TargetNamespace: targetNS, + TargetRefName: targetRefName, + PRNumber: number, + SHA: sha, + Logger: runcnx.Clients.Log, + } + defer g.TearDown(ctx, t) runcnx.Clients.Log.Infof("Waiting for Repository to be updated") waitOpts := twait.Opts{ @@ -168,7 +177,7 @@ func verifyGHTokenScope(t *testing.T, remoteTaskURL, remoteTaskName string, data PollTimeout: twait.DefaultTimeout, TargetSHA: sha, } - err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts) + _, err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts) assert.NilError(t, err) runcnx.Clients.Log.Infof("Check if we have the repository set as succeeded") diff --git a/test/github_tkn_pac_cli_test.go b/test/github_tkn_pac_cli_test.go index 0207ea45c..998698bc5 100644 --- a/test/github_tkn_pac_cli_test.go +++ b/test/github_tkn_pac_cli_test.go @@ -97,7 +97,17 @@ spec: number, err := tgithub.PRCreate(ctx, runcnx, ghprovider, opts.Organization, opts.Repo, targetRefName, repoinfo.GetDefaultBranch(), title) assert.NilError(t, err) - defer tgithub.TearDown(ctx, t, runcnx, ghprovider, number, targetRefName, targetNS, opts) + g := tgithub.PRTest{ + Cnx: runcnx, + Options: opts, + Provider: ghprovider, + TargetNamespace: targetNS, + TargetRefName: targetRefName, + PRNumber: number, + SHA: sha, + Logger: runcnx.Clients.Log, + } + defer g.TearDown(ctx, t) runcnx.Clients.Log.Infof("Waiting for Repository to be updated") waitOpts := twait.Opts{ @@ -107,7 +117,7 @@ spec: PollTimeout: twait.DefaultTimeout, TargetSHA: sha, } - err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts) + _, err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts) assert.NilError(t, err) runcnx.Clients.Log.Infof("Check if we have the repository set as succeeded") diff --git a/test/pkg/github/pr.go b/test/pkg/github/pr.go index 17bc4534f..55092aa93 100644 --- a/test/pkg/github/pr.go +++ b/test/pkg/github/pr.go @@ -5,32 +5,35 @@ import ( "encoding/base64" "fmt" "net/http" + "os" "path/filepath" "testing" - "github.com/google/go-github/v56/github" + ghlib "github.com/google/go-github/v56/github" "github.com/openshift-pipelines/pipelines-as-code/pkg/params" "github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype" ghprovider "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/github" "github.com/openshift-pipelines/pipelines-as-code/test/pkg/options" "github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload" + "github.com/openshift-pipelines/pipelines-as-code/test/pkg/repository" "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait" "github.com/tektoncd/pipeline/pkg/names" + "go.uber.org/zap" "gotest.tools/v3/assert" ) -func PushFilesToRef(ctx context.Context, client *github.Client, commitMessage, baseBranch, targetRef, owner, repo string, files map[string]string) (string, *github.Reference, error) { +func PushFilesToRef(ctx context.Context, client *ghlib.Client, commitMessage, baseBranch, targetRef, owner, repo string, files map[string]string) (string, *ghlib.Reference, error) { maintree, _, err := client.Git.GetTree(ctx, owner, repo, baseBranch, false) if err != nil { return "", nil, fmt.Errorf("error getting tree: %w", err) } mainSha := maintree.GetSHA() - entries := []*github.TreeEntry{} + entries := []*ghlib.TreeEntry{} defaultMode := "100644" for path, fcontent := range files { content := base64.StdEncoding.EncodeToString([]byte(fcontent)) encoding := "base64" - blob, _, err := client.Git.CreateBlob(ctx, owner, repo, &github.Blob{ + blob, _, err := client.Git.CreateBlob(ctx, owner, repo, &ghlib.Blob{ Content: &content, Encoding: &encoding, }) @@ -41,7 +44,7 @@ func PushFilesToRef(ctx context.Context, client *github.Client, commitMessage, b _path := path entries = append(entries, - &github.TreeEntry{ + &ghlib.TreeEntry{ Path: &_path, Mode: &defaultMode, SHA: &sha, @@ -55,26 +58,26 @@ func PushFilesToRef(ctx context.Context, client *github.Client, commitMessage, b commitAuthor := "OpenShift Pipelines E2E test" commitEmail := "e2e-pipelines@redhat.com" - commit, _, err := client.Git.CreateCommit(ctx, owner, repo, &github.Commit{ - Author: &github.CommitAuthor{ + commit, _, err := client.Git.CreateCommit(ctx, owner, repo, &ghlib.Commit{ + Author: &ghlib.CommitAuthor{ Name: &commitAuthor, Email: &commitEmail, }, Message: &commitMessage, Tree: tree, - Parents: []*github.Commit{ + Parents: []*ghlib.Commit{ { SHA: &mainSha, }, }, - }, &github.CreateCommitOptions{}) + }, &ghlib.CreateCommitOptions{}) if err != nil { return "", nil, err } - ref := &github.Reference{ + ref := &ghlib.Reference{ Ref: &targetRef, - Object: &github.GitObject{ + Object: &ghlib.GitObject{ SHA: commit.SHA, }, } @@ -87,11 +90,11 @@ func PushFilesToRef(ctx context.Context, client *github.Client, commitMessage, b } func PRCreate(ctx context.Context, cs *params.Run, ghcnx *ghprovider.Provider, owner, repo, targetRef, defaultBranch, title string) (int, error) { - pr, _, err := ghcnx.Client.PullRequests.Create(ctx, owner, repo, &github.NewPullRequest{ + pr, _, err := ghcnx.Client.PullRequests.Create(ctx, owner, repo, &ghlib.NewPullRequest{ Title: &title, Head: &targetRef, Base: &defaultBranch, - Body: github.String("Add a new PR for testing"), + Body: ghlib.String("Add a new PR for testing"), }) if err != nil { return -1, err @@ -100,14 +103,32 @@ func PRCreate(ctx context.Context, cs *params.Run, ghcnx *ghprovider.Provider, o return pr.GetNumber(), nil } -func RunPullRequest(ctx context.Context, t *testing.T, label string, yamlFiles []string, secondcontroller, webhook bool) (*params.Run, *ghprovider.Provider, options.E2E, string, string, int, string) { +type PRTest struct { + Label string + YamlFiles []string + SecondController bool + Webhook bool + NoStatusCheck bool + + Cnx *params.Run + Options options.E2E + Provider *ghprovider.Provider + TargetNamespace string + TargetRefName string + PRNumber int + SHA string + Logger *zap.SugaredLogger +} + +func (g *PRTest) RunPullRequest(ctx context.Context, t *testing.T) { targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns") - ctx, runcnx, opts, ghcnx, err := Setup(ctx, secondcontroller, webhook) + ctx, runcnx, opts, ghcnx, err := Setup(ctx, g.SecondController, g.Webhook) assert.NilError(t, err) + g.Logger = runcnx.Clients.Log - logmsg := fmt.Sprintf("Testing %s with Github APPS integration on %s", label, targetNS) - runcnx.Clients.Log.Info(logmsg) + logmsg := fmt.Sprintf("Testing %s with Github APPS integration on %s", g.Label, targetNS) + g.Logger.Info(logmsg) repoinfo, resp, err := ghcnx.Client.Repositories.Get(ctx, opts.Organization, opts.Repo) assert.NilError(t, err) @@ -119,7 +140,7 @@ func RunPullRequest(ctx context.Context, t *testing.T, label string, yamlFiles [ assert.NilError(t, err) yamlEntries := map[string]string{} - for _, v := range yamlFiles { + for _, v := range g.YamlFiles { yamlEntries[filepath.Join(".tekton", filepath.Base(v))] = v } @@ -133,36 +154,66 @@ func RunPullRequest(ctx context.Context, t *testing.T, label string, yamlFiles [ sha, vref, err := PushFilesToRef(ctx, ghcnx.Client, logmsg, repoinfo.GetDefaultBranch(), targetRefName, opts.Organization, opts.Repo, entries) assert.NilError(t, err) - runcnx.Clients.Log.Infof("Commit %s has been created and pushed to %s", sha, vref.GetURL()) + g.Logger.Infof("Commit %s has been created and pushed to %s", sha, vref.GetURL()) number, err := PRCreate(ctx, runcnx, ghcnx, opts.Organization, opts.Repo, targetRefName, repoinfo.GetDefaultBranch(), logmsg) assert.NilError(t, err) - sopt := wait.SuccessOpt{ - Title: logmsg, - OnEvent: triggertype.PullRequest.String(), - TargetNS: targetNS, - NumberofPRMatch: len(yamlFiles), - SHA: sha, + if !g.NoStatusCheck { + sopt := wait.SuccessOpt{ + Title: logmsg, + OnEvent: triggertype.PullRequest.String(), + TargetNS: targetNS, + NumberofPRMatch: len(g.YamlFiles), + SHA: sha, + } + wait.Succeeded(ctx, t, runcnx, opts, sopt) } - wait.Succeeded(ctx, t, runcnx, opts, sopt) - return runcnx, ghcnx, opts, targetNS, targetRefName, number, sha + g.Cnx = runcnx + g.Options = opts + g.Provider = ghcnx + g.TargetNamespace = targetNS + g.TargetRefName = targetRefName + g.PRNumber = number + g.SHA = sha } -func RunPushRequest(ctx context.Context, t *testing.T, label string, yamlFiles []string, onSecondController, onWebhook bool) (*params.Run, *ghprovider.Provider, options.E2E, string, string, int, string) { +func (g *PRTest) TearDown(ctx context.Context, t *testing.T) { + if os.Getenv("TEST_NOCLEANUP") == "true" { + g.Logger.Infof("Not cleaning up and closing PR since TEST_NOCLEANUP is set") + return + } + g.Logger.Infof("Closing PR %d", g.PRNumber) + if g.PRNumber != -1 { + state := "closed" + _, _, err := g.Provider.Client.PullRequests.Edit(ctx, + g.Options.Organization, g.Options.Repo, g.PRNumber, + &ghlib.PullRequest{State: &state}) + if err != nil { + t.Fatal(err) + } + } + repository.NSTearDown(ctx, t, g.Cnx, g.TargetNamespace) + g.Logger.Infof("Deleting Ref %s", g.TargetRefName) + _, err := g.Provider.Client.Git.DeleteRef(ctx, g.Options.Organization, g.Options.Repo, g.TargetRefName) + assert.NilError(t, err) +} + +func (g *PRTest) RunPushRequest(ctx context.Context, t *testing.T) { targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-push") targetBranch := targetNS targetEvent := "push" - ctx, runcnx, opts, ghcnx, err := Setup(ctx, onSecondController, onWebhook) + ctx, runcnx, opts, ghcnx, err := Setup(ctx, g.SecondController, g.Webhook) assert.NilError(t, err) + g.Logger = runcnx.Clients.Log var logmsg string - if onWebhook { - logmsg = fmt.Sprintf("Testing %s with Direct Webhook integration on %s", label, targetNS) - runcnx.Clients.Log.Info(logmsg) + if g.Webhook { + logmsg = fmt.Sprintf("Testing %s with Direct Webhook integration on %s", g.Label, targetNS) + g.Logger.Info(logmsg) } else { - logmsg = fmt.Sprintf("Testing %s with Github APPS integration on %s", label, targetNS) - runcnx.Clients.Log.Info(logmsg) + logmsg = fmt.Sprintf("Testing %s with Github APPS integration on %s", g.Label, targetNS) + g.Logger.Info(logmsg) } repoinfo, resp, err := ghcnx.Client.Repositories.Get(ctx, opts.Organization, opts.Repo) assert.NilError(t, err) @@ -173,7 +224,7 @@ func RunPushRequest(ctx context.Context, t *testing.T, label string, yamlFiles [ assert.NilError(t, err) yamlEntries := map[string]string{} - for _, v := range yamlFiles { + for _, v := range g.YamlFiles { yamlEntries[filepath.Join(".tekton", filepath.Base(v))] = v } @@ -183,16 +234,23 @@ func RunPushRequest(ctx context.Context, t *testing.T, label string, yamlFiles [ targetRefName := fmt.Sprintf("refs/heads/%s", targetBranch) sha, vref, err := PushFilesToRef(ctx, ghcnx.Client, logmsg, repoinfo.GetDefaultBranch(), targetRefName, opts.Organization, opts.Repo, entries) - runcnx.Clients.Log.Infof("Commit %s has been created and pushed to %s", sha, vref.GetURL()) + g.Logger.Infof("Commit %s has been created and pushed to %s", sha, vref.GetURL()) assert.NilError(t, err) sopt := wait.SuccessOpt{ Title: logmsg, OnEvent: triggertype.Push.String(), TargetNS: targetNS, - NumberofPRMatch: len(yamlFiles), + NumberofPRMatch: len(g.YamlFiles), SHA: sha, } wait.Succeeded(ctx, t, runcnx, opts, sopt) - return runcnx, ghcnx, opts, targetNS, targetRefName, -1, sha + + g.Cnx = runcnx + g.Options = opts + g.Provider = ghcnx + g.TargetNamespace = targetNS + g.TargetRefName = targetRefName + g.PRNumber = -1 + g.SHA = sha } diff --git a/test/pkg/github/setup.go b/test/pkg/github/setup.go index d8fe81001..9b63ab1db 100644 --- a/test/pkg/github/setup.go +++ b/test/pkg/github/setup.go @@ -6,16 +6,12 @@ import ( "os" "strconv" "strings" - "testing" - ghlib "github.com/google/go-github/v56/github" "github.com/openshift-pipelines/pipelines-as-code/pkg/params" "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info" "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/github" "github.com/openshift-pipelines/pipelines-as-code/test/pkg/cctx" "github.com/openshift-pipelines/pipelines-as-code/test/pkg/options" - "github.com/openshift-pipelines/pipelines-as-code/test/pkg/repository" - "gotest.tools/v3/assert" ) func Setup(ctx context.Context, onSecondController, viaDirectWebhook bool) (context.Context, *params.Run, options.E2E, *github.Provider, error) { @@ -118,24 +114,3 @@ func Setup(ctx context.Context, onSecondController, viaDirectWebhook bool) (cont return ctx, run, e2eoptions, gprovider, nil } - -func TearDown(ctx context.Context, t *testing.T, runcnx *params.Run, ghprovider *github.Provider, prNumber int, ref, targetNS string, opts options.E2E) { - if os.Getenv("TEST_NOCLEANUP") == "true" { - runcnx.Clients.Log.Infof("Not cleaning up and closing PR since TEST_NOCLEANUP is set") - return - } - runcnx.Clients.Log.Infof("Closing PR %d", prNumber) - if prNumber != -1 { - state := "closed" - _, _, err := ghprovider.Client.PullRequests.Edit(ctx, - opts.Organization, opts.Repo, prNumber, - &ghlib.PullRequest{State: &state}) - if err != nil { - t.Fatal(err) - } - } - repository.NSTearDown(ctx, t, runcnx, targetNS) - runcnx.Clients.Log.Infof("Deleting Ref %s", ref) - _, err := ghprovider.Client.Git.DeleteRef(ctx, opts.Organization, opts.Repo, ref) - assert.NilError(t, err) -} diff --git a/test/pkg/wait/check.go b/test/pkg/wait/check.go index 03bfb01b2..671728499 100644 --- a/test/pkg/wait/check.go +++ b/test/pkg/wait/check.go @@ -3,6 +3,7 @@ package wait import ( "context" "path/filepath" + "strings" "testing" "time" @@ -39,7 +40,7 @@ func Succeeded(ctx context.Context, t *testing.T, runcnx *params.Run, opts optio PollTimeout: DefaultTimeout, TargetSHA: sopt.SHA, } - err := UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts) + _, err := UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts) assert.NilError(t, err) runcnx.Clients.Log.Infof("Check if we have the repository set as succeeded") @@ -52,7 +53,12 @@ func Succeeded(ctx context.Context, t *testing.T, runcnx *params.Run, opts optio assert.Equal(t, sopt.SHA, *laststatus.SHA) assert.Equal(t, sopt.SHA, filepath.Base(*laststatus.SHAURL)) } - assert.Equal(t, sopt.Title, *laststatus.Title) + laststatustitle := strings.TrimSpace(*laststatus.Title) + if sopt.Title != "" { + assert.Equal(t, sopt.Title, laststatustitle) + } else { + assert.Assert(t, *laststatus.Title != "") + } assert.Assert(t, *laststatus.LogURL != "") pr, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(sopt.TargetNS).Get(ctx, laststatus.PipelineRunName, v1.GetOptions{}) @@ -71,7 +77,6 @@ func Succeeded(ctx context.Context, t *testing.T, runcnx *params.Run, opts optio assert.Equal(t, sopt.SHA, pr.Annotations[keys.SHA]) assert.Equal(t, sopt.SHA, filepath.Base(pr.Annotations[keys.ShaURL])) } - assert.Equal(t, sopt.Title, pr.Annotations[keys.ShaTitle]) - + assert.Equal(t, laststatustitle, strings.TrimSpace(pr.Annotations[keys.ShaTitle])) runcnx.Clients.Log.Infof("Success, number of status %d has been matched", sopt.NumberofPRMatch) } diff --git a/test/pkg/wait/wait.go b/test/pkg/wait/wait.go index e889dab25..c3b0d8c65 100644 --- a/test/pkg/wait/wait.go +++ b/test/pkg/wait/wait.go @@ -6,6 +6,7 @@ import ( "time" "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys" + pacv1alpha1 "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1" "github.com/openshift-pipelines/pipelines-as-code/pkg/kubeinteraction" "github.com/openshift-pipelines/pipelines-as-code/pkg/params/clients" corev1 "k8s.io/api/core/v1" @@ -36,14 +37,14 @@ func UntilMinPRAppeared(ctx context.Context, clients clients.Clients, opts Opts, }) } -func UntilRepositoryUpdated(ctx context.Context, clients clients.Clients, opts Opts) error { +func UntilRepositoryUpdated(ctx context.Context, clients clients.Clients, opts Opts) (*pacv1alpha1.Repository, error) { ctx, cancel := context.WithTimeout(ctx, opts.PollTimeout) defer cancel() - return kubeinteraction.PollImmediateWithContext(ctx, opts.PollTimeout, func() (bool, error) { + var repo *pacv1alpha1.Repository + return repo, kubeinteraction.PollImmediateWithContext(ctx, opts.PollTimeout, func() (bool, error) { clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(opts.Namespace) - r, err := clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(opts.Namespace).Get(ctx, opts.RepoName, - metav1.GetOptions{}) - if err != nil { + var err error + if repo, err = clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(opts.Namespace).Get(ctx, opts.RepoName, metav1.GetOptions{}); err != nil { return true, err } @@ -60,9 +61,9 @@ func UntilRepositoryUpdated(ctx context.Context, clients clients.Clients, opts O } } - clients.Log.Infof("Still waiting for repository status to be updated: %d/%d", len(r.Status), opts.MinNumberStatus) + clients.Log.Infof("Still waiting for repository status to be updated: %d/%d", len(repo.Status), opts.MinNumberStatus) time.Sleep(2 * time.Second) - return len(r.Status) >= opts.MinNumberStatus, nil + return len(repo.Status) >= opts.MinNumberStatus, nil }) }