diff --git a/tests/build/build.go b/tests/build/build.go index 76d78e7b6..b109aec5a 100644 --- a/tests/build/build.go +++ b/tests/build/build.go @@ -7,30 +7,27 @@ import ( "strings" "time" - "github.com/xanzy/go-gitlab" - - tektonutils "github.com/konflux-ci/release-service/tekton/utils" - "github.com/openshift/library-go/pkg/image/reference" - "k8s.io/apimachinery/pkg/runtime" - + "github.com/devfile/library/v2/pkg/util" "github.com/google/go-github/v44/github" appservice "github.com/konflux-ci/application-api/api/v1alpha1" - "github.com/konflux-ci/e2e-tests/pkg/clients/has" - "github.com/konflux-ci/e2e-tests/pkg/utils/build" - "github.com/konflux-ci/e2e-tests/pkg/utils/tekton" - - "github.com/devfile/library/v2/pkg/util" - "github.com/konflux-ci/e2e-tests/pkg/constants" - "github.com/konflux-ci/e2e-tests/pkg/utils" + "github.com/konflux-ci/build-service/controllers" + tektonutils "github.com/konflux-ci/release-service/tekton/utils" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/openshift/library-go/pkg/image/reference" pipeline "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" + v1 "k8s.io/api/core/v1" k8sErrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" - "github.com/konflux-ci/build-service/controllers" + "github.com/konflux-ci/e2e-tests/pkg/clients/git" + "github.com/konflux-ci/e2e-tests/pkg/clients/has" + "github.com/konflux-ci/e2e-tests/pkg/constants" "github.com/konflux-ci/e2e-tests/pkg/framework" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - v1 "k8s.io/api/core/v1" + "github.com/konflux-ci/e2e-tests/pkg/utils" + "github.com/konflux-ci/e2e-tests/pkg/utils/build" + "github.com/konflux-ci/e2e-tests/pkg/utils/tekton" ) var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-service"), func() { @@ -40,8 +37,12 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser var err error defer GinkgoRecover() - DescribeTableSubtree("test PaC component build", Ordered, Label("github-webhook", "pac-build", "pipeline", "image-controller"), func(gitProvider, gitPrefix string) { - var applicationName, customDefaultComponentName, customBranchComponentName, componentBaseBranchName, pacBranchName, testNamespace, imageRepoName, pullRobotAccountName, pushRobotAccountName, helloWorldComponentGitSourceURL string + var gitClient git.Client + + DescribeTableSubtree("test PaC component build", Ordered, Label("github-webhook", "pac-build", "pipeline", "image-controller"), func(gitProvider git.GitProvider, gitPrefix string) { + var applicationName, customDefaultComponentName, customBranchComponentName, componentBaseBranchName string + var pacBranchName, testNamespace, imageRepoName, pullRobotAccountName, pushRobotAccountName string + var helloWorldComponentGitSourceURL, customDefaultComponentBranch string var component *appservice.Component var plr *pipeline.PipelineRun @@ -51,6 +52,8 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser var prHeadSha string var buildPipelineAnnotation map[string]string + var helloWorldRepository string + BeforeAll(func() { if os.Getenv(constants.SKIP_PAC_TESTS_ENV) == "true" { Skip("Skipping this test due to configuration issue with Spray proxy") @@ -83,31 +86,15 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser customDefaultComponentName = fmt.Sprintf("%s-%s-%s", gitPrefix, "test-custom-default", util.GenerateRandomString(6)) customBranchComponentName = fmt.Sprintf("%s-%s-%s", gitPrefix, "test-custom-branch", util.GenerateRandomString(6)) pacBranchName = constants.PaCPullRequestBranchPrefix + customBranchComponentName + customDefaultComponentBranch = constants.PaCPullRequestBranchPrefix + customDefaultComponentName componentBaseBranchName = fmt.Sprintf("base-%s", util.GenerateRandomString(6)) - switch gitProvider { - case "github": - err = f.AsKubeAdmin.CommonController.Github.CreateRef(helloWorldComponentGitSourceRepoName, helloWorldComponentDefaultBranch, helloWorldComponentRevision, componentBaseBranchName) - Expect(err).ShouldNot(HaveOccurred()) - - helloWorldComponentGitSourceURL = helloWorldComponentGitHubURL - case "gitlab": - gitlabToken := utils.GetEnv(constants.GITLAB_BOT_TOKEN_ENV, "") - Expect(gitlabToken).ShouldNot(BeEmpty()) - - secretAnnotations := map[string]string{} - - err = build.CreateGitlabBuildSecret(f, "pipelines-as-code-secret", secretAnnotations, gitlabToken) - Expect(err).ShouldNot(HaveOccurred()) - - err = f.AsKubeAdmin.CommonController.Gitlab.CreateBranch(helloWorldComponentGitLabProjectID, componentBaseBranchName, helloWorldComponentDefaultBranch) - Expect(err).ShouldNot(HaveOccurred()) - - helloWorldComponentGitSourceURL = helloWorldComponentGitLabURL - } + gitClient, helloWorldComponentGitSourceURL, helloWorldRepository = setupGitProvider(f, gitProvider) // get the build pipeline bundle annotation buildPipelineAnnotation = build.GetDockerBuildPipelineBundle() + err = gitClient.CreateBranch(helloWorldRepository, helloWorldComponentDefaultBranch, helloWorldComponentRevision, componentBaseBranchName) + Expect(err).ShouldNot(HaveOccurred()) }) AfterAll(func() { @@ -116,32 +103,24 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser Expect(f.SandboxController.DeleteUserSignup(f.UserName)).To(BeTrue()) } - switch gitProvider { - case "github": - // Delete new branches created by PaC and a testing branch used as a component's base branch - err = f.AsKubeAdmin.CommonController.Github.DeleteRef(helloWorldComponentGitSourceRepoName, pacBranchName) - if err != nil { - Expect(err.Error()).To(ContainSubstring("Reference does not exist")) - } - err = f.AsKubeAdmin.CommonController.Github.DeleteRef(helloWorldComponentGitSourceRepoName, componentBaseBranchName) - if err != nil { - Expect(err.Error()).To(ContainSubstring("Reference does not exist")) - } - err = f.AsKubeAdmin.CommonController.Github.DeleteRef(helloWorldComponentGitSourceRepoName, constants.PaCPullRequestBranchPrefix+customBranchComponentName) - if err != nil { - Expect(err.Error()).To(ContainSubstring("Reference does not exist")) - } - // Delete created webhook from GitHub - Expect(build.CleanupWebhooks(f, helloWorldComponentGitSourceRepoName)).ShouldNot(HaveOccurred()) - case "gitlab": + err = gitClient.DeleteBranch(helloWorldRepository, pacBranchName) + if err != nil { + Expect(err.Error()).To(Or(ContainSubstring("Reference does not exist"), ContainSubstring("404"))) + } + err = gitClient.DeleteBranch(helloWorldRepository, componentBaseBranchName) + if err != nil { + Expect(err.Error()).To(Or(ContainSubstring("Reference does not exist"), ContainSubstring("404"))) + } + + // GitLab does not close MRs automatically when a branch gets deleted + if gitProvider == git.GitLabProvider { err = f.AsKubeAdmin.CommonController.Gitlab.CloseMergeRequest(helloWorldComponentGitLabProjectID, prNumber) if err != nil { Expect(err).To(MatchError(ContainSubstring("404"))) } - - Expect(f.AsKubeAdmin.CommonController.Gitlab.DeleteBranch(helloWorldComponentGitLabProjectID, componentBaseBranchName)).To(Succeed()) - Expect(f.AsKubeAdmin.CommonController.Gitlab.DeleteWebhooks(helloWorldComponentGitLabProjectID, f.ClusterAppDomain)).To(Succeed()) } + + Expect(gitClient.CleanupWebhooks(helloWorldRepository, f.ClusterAppDomain)).To(Succeed()) }) When("a new component without specified branch is created and with visibility private", Label("pac-custom-default-branch"), func() { @@ -170,29 +149,14 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser timeout = time.Second * 300 interval = time.Second * 1 Eventually(func() bool { - switch gitProvider { - case "github": - prs, err := f.AsKubeAdmin.CommonController.Github.ListPullRequests(helloWorldComponentGitSourceRepoName) - Expect(err).ShouldNot(HaveOccurred()) - - for _, pr := range prs { - if pr.Head.GetRef() == constants.PaCPullRequestBranchPrefix+customDefaultComponentName { - Expect(pr.GetBase().GetRef()).To(Equal(helloWorldComponentDefaultBranch)) - return true - } - } - return false - case "gitlab": - mrs, err := f.AsKubeAdmin.CommonController.Gitlab.GetMergeRequests() - Expect(err).ShouldNot(HaveOccurred()) + prs, err := gitClient.ListPullRequests(helloWorldRepository) + Expect(err).ShouldNot(HaveOccurred()) - for _, mr := range mrs { - if mr.SourceBranch == constants.PaCPullRequestBranchPrefix+customDefaultComponentName { - Expect(mr.TargetBranch).To(Equal(helloWorldComponentDefaultBranch)) - return true - } + for _, pr := range prs { + if pr.SourceBranch == customDefaultC { + Expect(pr.TargetBranch).To(Equal(helloWorldComponentDefaultBranch)) + return true } - return false } return false }, timeout, interval).Should(BeTrue(), fmt.Sprintf("timed out when waiting for init PaC PR to be created against %s branch in %s repository", helloWorldComponentDefaultBranch, helloWorldComponentGitSourceRepoName)) @@ -293,24 +257,17 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser return fmt.Errorf("pipelinerun %s/%s is not removed yet", plr.GetNamespace(), plr.GetName()) } return err - }, timeout, constants.PipelineRunPollingInterval).Should(MatchError(ContainSubstring("no pipelinerun found")), fmt.Sprintf("timed out when waiting for the PipelineRun to be removed for Component %s/%s", testNamespace, customBranchComponentName)) + }, timeout, interval).Should(MatchError(ContainSubstring("no pipelinerun found")), fmt.Sprintf("timed out when waiting for the PipelineRun to be removed for Component %s/%s", testNamespace, customBranchComponentName)) }) It("PR branch should not exist in the repo", func() { timeout = time.Second * 60 interval = time.Second * 1 - branchName := constants.PaCPullRequestBranchPrefix + customDefaultComponentName Eventually(func() bool { - var exists bool - switch gitProvider { - case "github": - exists, err = f.AsKubeAdmin.CommonController.Github.ExistsRef(helloWorldComponentGitSourceRepoName, constants.PaCPullRequestBranchPrefix+customDefaultComponentName) - case "gitlab": - exists, err = f.AsKubeAdmin.CommonController.Gitlab.ExistsBranch(helloWorldComponentGitLabProjectID, constants.PaCPullRequestBranchPrefix+customDefaultComponentName) - } + exists, err := gitClient.BranchExists(helloWorldRepository, customDefaultComponentBranch) Expect(err).ShouldNot(HaveOccurred()) return exists - }, timeout, interval).Should(BeFalse(), fmt.Sprintf("timed out when waiting for the branch %s to be deleted from %s repository", branchName, helloWorldComponentGitSourceRepoName)) + }, timeout, interval).Should(BeFalse(), fmt.Sprintf("timed out when waiting for the branch %s to be deleted from %s repository", customDefaultComponentBranch, helloWorldComponentGitSourceRepoName)) }) It("related image repo and the robot account should be deleted after deleting the component", func() { @@ -380,31 +337,15 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser interval = time.Second * 1 Eventually(func() bool { - switch gitProvider { - case "github": - prs, err := f.AsKubeAdmin.CommonController.Github.ListPullRequests(helloWorldComponentGitSourceRepoName) - Expect(err).ShouldNot(HaveOccurred()) - - for _, pr := range prs { - if pr.Head.GetRef() == pacBranchName { - prNumber = pr.GetNumber() - prHeadSha = pr.Head.GetSHA() - return true - } - } - return false - case "gitlab": - mrs, err := f.AsKubeAdmin.CommonController.Gitlab.GetMergeRequests() - Expect(err).ShouldNot(HaveOccurred()) + prs, err := gitClient.ListPullRequests(helloWorldRepository) + Expect(err).ShouldNot(HaveOccurred()) - for _, mr := range mrs { - if mr.SourceBranch == pacBranchName { - prNumber = mr.IID - prHeadSha = mr.DiffRefs.HeadSha - return true - } + for _, pr := range prs { + if pr.SourceBranch == pacBranchName { + prNumber = pr.Number + prHeadSha = pr.HeadSHA + return true } - return false } return false }, timeout, interval).Should(BeTrue(), fmt.Sprintf("timed out when waiting for init PaC PR (branch name '%s') to be created in %s repository", pacBranchName, helloWorldComponentGitSourceRepoName)) @@ -484,10 +425,10 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser }) It("eventually leads to the PipelineRun status report at Checks tab", func() { switch gitProvider { - case "github": + case git.GitHubProvider: expectedCheckRunName := fmt.Sprintf("%s-%s", customBranchComponentName, "on-pull-request") Expect(f.AsKubeAdmin.CommonController.Github.GetCheckRunConclusion(expectedCheckRunName, helloWorldComponentGitSourceRepoName, prHeadSha, prNumber)).To(Equal(constants.CheckrunConclusionSuccess)) - case "gitlab": + case git.GitLabProvider: expectedNote := fmt.Sprintf("**Pipelines as Code CI/%s-on-pull-request** has successfully validated your commit", customBranchComponentName) f.AsKubeAdmin.HasController.GitLab.ValidateNoteInMergeRequestComment(helloWorldComponentGitLabProjectID, expectedNote, prNumber) } @@ -500,13 +441,13 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser BeforeAll(func() { fileToCreatePath := fmt.Sprintf(".tekton/%s-readme.md", customBranchComponentName) switch gitProvider { - case "github": + case git.GitHubProvider: createdFile, err := f.AsKubeAdmin.CommonController.Github.CreateFile(helloWorldComponentGitSourceRepoName, fileToCreatePath, fmt.Sprintf("test PaC branch %s update", pacBranchName), pacBranchName) Expect(err).NotTo(HaveOccurred()) createdFileSHA = createdFile.GetSHA() GinkgoWriter.Println("created file sha:", createdFileSHA) - case "gitlab": + case git.GitLabProvider: _, err = f.AsKubeAdmin.CommonController.Gitlab.CreateFile(helloWorldComponentGitLabProjectID, fileToCreatePath, fmt.Sprintf("test PaC branch %s update", pacBranchName), pacBranchName) Expect(err).NotTo(HaveOccurred()) @@ -538,33 +479,16 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser interval = time.Second * 1 Eventually(func() bool { - switch gitProvider { - case "github": - prs, err := f.AsKubeAdmin.CommonController.Github.ListPullRequests(helloWorldComponentGitSourceRepoName) - Expect(err).ShouldNot(HaveOccurred()) - - for _, pr := range prs { - if pr.Head.GetRef() == pacBranchName { - Expect(prHeadSha).NotTo(Equal(pr.Head.GetSHA())) - prNumber = pr.GetNumber() - prHeadSha = pr.Head.GetSHA() - return true - } - } - return false - case "gitlab": - mrs, err := f.AsKubeAdmin.CommonController.Gitlab.GetMergeRequests() - Expect(err).ShouldNot(HaveOccurred()) + prs, err := gitClient.ListPullRequests(helloWorldRepository) + Expect(err).ShouldNot(HaveOccurred()) - for _, mr := range mrs { - if mr.SourceBranch == pacBranchName { - Expect(prHeadSha).NotTo(Equal(mr.DiffRefs.HeadSha)) - prNumber = mr.IID - prHeadSha = mr.DiffRefs.HeadSha - return true - } + for _, pr := range prs { + if pr.SourceBranch == pacBranchName { + Expect(prHeadSha).NotTo(Equal(pr.HeadSHA)) + prNumber = pr.Number + prHeadSha = pr.HeadSHA + return true } - return false } return false }, timeout, interval).Should(BeTrue(), fmt.Sprintf("timed out when waiting for init PaC PR (branch name '%s') to be created in %s repository", pacBranchName, helloWorldComponentGitSourceRepoName)) @@ -577,10 +501,10 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser }) It("eventually leads to another update of a PR about the PipelineRun status report at Checks tab", func() { switch gitProvider { - case "github": + case git.GitHubProvider: expectedCheckRunName := fmt.Sprintf("%s-%s", customBranchComponentName, "on-pull-request") Expect(f.AsKubeAdmin.CommonController.Github.GetCheckRunConclusion(expectedCheckRunName, helloWorldComponentGitSourceRepoName, createdFileSHA, prNumber)).To(Equal(constants.CheckrunConclusionSuccess)) - case "gitlab": + case git.GitLabProvider: expectedNote := fmt.Sprintf("**Pipelines as Code CI/%s-on-pull-request** has successfully validated your commit", customBranchComponentName) f.AsKubeAdmin.HasController.GitLab.ValidateNoteInMergeRequestComment(helloWorldComponentGitLabProjectID, expectedNote, prNumber) } @@ -588,35 +512,16 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser }) When("the PaC init branch is merged", Label("build-custom-branch"), func() { - var mergeResult *github.PullRequestMergeResult - var mergeRequest *gitlab.MergeRequest + var mergeResult *git.PullRequest var mergeResultSha string BeforeAll(func() { - if gitProvider != "github" { - return - } - Eventually(func() error { - mergeResult, err = f.AsKubeAdmin.CommonController.Github.MergePullRequest(helloWorldComponentGitSourceRepoName, prNumber) + mergeResult, err = gitClient.MergePullRequest(helloWorldRepository, prNumber) return err }, time.Minute).Should(BeNil(), fmt.Sprintf("error when merging PaC pull request #%d in repo %s", prNumber, helloWorldComponentGitSourceRepoName)) - mergeResultSha = mergeResult.GetSHA() - GinkgoWriter.Println("merged result sha:", mergeResultSha) - }) - - BeforeAll(func() { - if gitProvider != "gitlab" { - return - } - - Eventually(func() error { - mergeRequest, err = f.AsKubeAdmin.CommonController.Gitlab.AcceptMergeRequest(helloWorldComponentGitLabProjectID, prNumber) - return err - }, time.Minute).Should(BeNil(), fmt.Sprintf("error when merging PaC pull request #%d in repo %s", prNumber, helloWorldComponentGitSourceRepoName)) - - mergeResultSha = mergeRequest.MergeCommitSHA + mergeResultSha = mergeResult.MergeCommitSHA GinkgoWriter.Println("merged result sha:", mergeResultSha) }) @@ -665,6 +570,7 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser return nil }, time.Second*20, time.Second*1).Should(Succeed(), fmt.Sprintf("timed out when trying to change visibility of the image repos to private in %s/%s", testNamespace, customBranchComponentName)) + GinkgoWriter.Printf("waiting for one minute and expecting to not trigger a PipelineRun") Consistently(func() bool { componentPipelineRun, _ := f.AsKubeAdmin.HasController.GetComponentPipelineRun(customBranchComponentName, applicationName, testNamespace, "") return componentPipelineRun == nil @@ -729,35 +635,21 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser timeout = time.Second * 10 interval = time.Second * 2 Consistently(func() error { - switch gitProvider { - case "github": - prs, err := f.AsKubeAdmin.CommonController.Github.ListPullRequests(helloWorldComponentGitSourceRepoName) - Expect(err).ShouldNot(HaveOccurred()) - - for _, pr := range prs { - if pr.Head.GetRef() == pacBranchName { - return fmt.Errorf("did not expect a new PR created in %s repository after initial PaC configuration was already merged for the same component name and a namespace", helloWorldComponentGitSourceRepoName) - } - } - return nil - case "gitlab": - mrs, err := f.AsKubeAdmin.CommonController.Gitlab.GetMergeRequests() - Expect(err).ShouldNot(HaveOccurred()) + prs, err := gitClient.ListPullRequests(helloWorldRepository) + Expect(err).ShouldNot(HaveOccurred()) - for _, mr := range mrs { - if mr.SourceBranch == pacBranchName { - return fmt.Errorf("did not expect a new PR created in %s repository after initial PaC configuration was already merged for the same component name and a namespace", helloWorldComponentGitSourceRepoName) - } + for _, pr := range prs { + if pr.SourceBranch == pacBranchName { + return fmt.Errorf("did not expect a new PR created in %s repository after initial PaC configuration was already merged for the same component name and a namespace", helloWorldRepository) } - return nil } return nil }, timeout, interval).Should(BeNil()) }) }) }, - Entry("github", "github", "gh"), - Entry("gitlab", "gitlab", "gl"), + Entry("github", git.GitHubProvider, "gh"), + Entry("gitlab", git.GitLabProvider, "gl"), ) Describe("test pac with multiple components using same repository", Ordered, Label("pac-build", "multi-component"), func() { @@ -1781,6 +1673,27 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build-ser }) +func setupGitProvider(f *framework.Framework, gitProvider git.GitProvider) (git.Client, string, string) { + switch gitProvider { + case git.GitHubProvider: + gitClient := git.NewGitHubClient(f.AsKubeAdmin.CommonController.Github) + return gitClient, helloWorldComponentGitHubURL, helloWorldComponentGitSourceRepoName + case git.GitLabProvider: + gitClient := git.NewGitlabClient(f.AsKubeAdmin.CommonController.Gitlab) + + gitlabToken := utils.GetEnv(constants.GITLAB_BOT_TOKEN_ENV, "") + Expect(gitlabToken).ShouldNot(BeEmpty()) + + secretAnnotations := map[string]string{} + + err := build.CreateGitlabBuildSecret(f, "pipelines-as-code-secret", secretAnnotations, gitlabToken) + Expect(err).ShouldNot(HaveOccurred()) + + return gitClient, helloWorldComponentGitLabURL, helloWorldComponentGitLabProjectID + } + return nil, "", "" +} + func createBuildSecret(f *framework.Framework, secretName string, annotations map[string]string, token string) error { buildSecret := v1.Secret{} buildSecret.Name = secretName