Skip to content

Commit

Permalink
remove simple build dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
tisutisu committed Jul 23, 2024
1 parent b3819b6 commit 5e0aef7
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 176 deletions.
168 changes: 34 additions & 134 deletions tests/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,16 +1040,13 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build", "
var componentObj appservice.ComponentSpec
var component *appservice.Component

var timeout, interval time.Duration
invalidAnnotation := "foo"

BeforeAll(func() {
f, err = framework.NewFramework(utils.GetGeneratedNamespace("build-e2e"))
Expect(err).ShouldNot(HaveOccurred())
testNamespace = f.UserNamespace

timeout = 5 * time.Minute
interval = 5 * time.Second

applicationName = fmt.Sprintf("build-suite-test-application-%s", util.GenerateRandomString(4))
_, err = f.AsKubeAdmin.HasController.CreateApplication(applicationName, testNamespace)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -1066,8 +1063,11 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build", "

})

When("component is created", func() {
var lastBuildStartTime string
When("component is created with invalid build request annotations", func() {

invalidBuildAnnotation := map[string]string{
controllers.BuildRequestAnnotationName: invalidAnnotation,
}

BeforeAll(func() {
componentObj = appservice.ComponentSpec{
Expand All @@ -1084,138 +1084,21 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build", "
},
}

component, err = f.AsKubeAdmin.HasController.CreateComponent(componentObj, testNamespace, "", "", applicationName, false, constants.DefaultDockerBuildPipelineBundle)
component, err = f.AsKubeAdmin.HasController.CreateComponent(componentObj, testNamespace, "", "", applicationName, false, utils.MergeMaps(invalidBuildAnnotation, constants.DefaultDockerBuildPipelineBundle))
Expect(component).ToNot(BeNil())
Expect(err).ShouldNot(HaveOccurred())
})

It("triggers a pipeline run", func() {
Eventually(func() error {
pr, err := f.AsKubeAdmin.HasController.GetComponentPipelineRun(componentName, applicationName, testNamespace, "")
if err != nil {
GinkgoWriter.Printf("PipelineRun has not been created yet for the component %s/%s\n", testNamespace, componentName)
return err
}
if !pr.HasStarted() {
return fmt.Errorf("pipelinerun %s/%s hasn't started yet", pr.GetNamespace(), pr.GetName())
}
return nil
}, time.Minute*5, constants.PipelineRunPollingInterval).Should(Succeed(), fmt.Sprintf("timed out when waiting for the PipelineRun to start for the component %s/%s", testNamespace, componentName))
})

It("component build status annotation is set correctly", func() {
var buildStatus *controllers.BuildStatus

Eventually(func() (bool, error) {
component, err := f.AsKubeAdmin.HasController.GetComponent(componentName, testNamespace)
if err != nil {
GinkgoWriter.Printf("cannot get the component: %v\n", err)
return false, err
}

buildStatusAnnotationValue := component.Annotations[controllers.BuildStatusAnnotationName]
GinkgoWriter.Printf(buildStatusAnnotationValueLoggingFormat, buildStatusAnnotationValue)
statusBytes := []byte(buildStatusAnnotationValue)

err = json.Unmarshal(statusBytes, &buildStatus)
if err != nil {
GinkgoWriter.Printf("cannot unmarshal build status: %v\n", err)
return false, err
}

if buildStatus.Simple != nil {
GinkgoWriter.Printf("buildStartTime: %s\n", buildStatus.Simple.BuildStartTime)
lastBuildStartTime = buildStatus.Simple.BuildStartTime
} else {
GinkgoWriter.Println("build status does not have simple field")
}

return buildStatus.Simple != nil && buildStatus.Simple.BuildStartTime != "", nil
}, timeout, interval).Should(BeTrue(), "build status has unexpected content")

//Expect pipelinerun count to be 1
Eventually(func() error {
pipelineRuns, err := f.AsKubeAdmin.HasController.GetAllPipelineRunsForApplication(applicationName, testNamespace)
if err != nil {
GinkgoWriter.Println("PiplelineRun has not been created yet")
return err
}
if len(pipelineRuns.Items) != 1 {
return fmt.Errorf("pipelinerun count in the namespace %s is not one, got pipelineruns %v", testNamespace, pipelineRuns.Items)
}
return nil
}, time.Minute*5, constants.PipelineRunPollingInterval).Should(Succeed(), "timeout while waiting for first pipelinerun to start")
})

Specify("simple build can be triggered manually", func() {
// Wait 1 second before sending the second build request, so that we get different buildStatus.Simple.BuildStartTime timestamp
time.Sleep(1 * time.Second)
Expect(f.AsKubeAdmin.HasController.SetComponentAnnotation(componentName, controllers.BuildRequestAnnotationName, controllers.BuildRequestTriggerSimpleBuildAnnotationValue, testNamespace)).To(Succeed())
})

It("another pipelineRun is triggered", func() {
//Expect pipelinerun count to be 2
Eventually(func() error {
pipelineRuns, err := f.AsKubeAdmin.HasController.GetAllPipelineRunsForApplication(applicationName, testNamespace)
if err != nil {
GinkgoWriter.Println("Second piplelineRun has not been created yet")
return err
}
if len(pipelineRuns.Items) != 2 {
return fmt.Errorf("pipelinerun count in the namespace %s is not two, got pipelineruns %v", testNamespace, pipelineRuns.Items)
}
return nil
}, time.Minute*5, constants.PipelineRunPollingInterval).Should(Succeed(), "timeout while waiting for second pipelinerun to start")
})

It("component build annotation is correct", func() {
var buildStatus *controllers.BuildStatus

Eventually(func() (bool, error) {
component, err := f.AsKubeAdmin.HasController.GetComponent(componentName, testNamespace)
if err != nil {
GinkgoWriter.Printf("cannot get the component: %v\n", err)
return false, err
}

buildStatusAnnotationValue := component.Annotations[controllers.BuildStatusAnnotationName]
GinkgoWriter.Printf(buildStatusAnnotationValueLoggingFormat, buildStatusAnnotationValue)
statusBytes := []byte(buildStatusAnnotationValue)

err = json.Unmarshal(statusBytes, &buildStatus)
if err != nil {
GinkgoWriter.Printf("cannot unmarshal build status: %v\n", err)
return false, err
}

if buildStatus.Simple != nil {
GinkgoWriter.Printf("buildStartTime: %s\n", buildStatus.Simple.BuildStartTime)
} else {
GinkgoWriter.Println("build status does not have simple field")
}

return buildStatus.Simple != nil && buildStatus.Simple.BuildStartTime != lastBuildStartTime, nil
}, timeout, interval).Should(BeTrue(), "build status has unexpected content")
})

It("handles invalid request annotation", func() {

invalidAnnotation := "foo"
expectedInvalidAnnotationMessage := fmt.Sprintf("unexpected build request: %s", invalidAnnotation)

Expect(f.AsKubeAdmin.HasController.SetComponentAnnotation(componentName, controllers.BuildRequestAnnotationName, invalidAnnotation, testNamespace)).To(Succeed())

// Waiting for 2 minute to see if any more pipelinerun is triggered
Consistently(func() (bool, error) {
pipelineRuns, err := f.AsKubeAdmin.HasController.GetAllPipelineRunsForApplication(applicationName, testNamespace)
if err != nil {
return false, err
}
if len(pipelineRuns.Items) != 2 {
return false, fmt.Errorf("pipelinerun count in the namespace %s is not two, got pipelineruns %v", testNamespace, pipelineRuns.Items)
}
return true, nil
}, time.Minute*2, constants.PipelineRunPollingInterval).Should(BeTrue(), "timeout while checking if any more pipelinerun is triggered")
// Waiting for 1 minute to see if any pipelinerun is triggered
Consistently(func() bool {
_, err := f.AsKubeAdmin.HasController.GetComponentPipelineRun(componentName, applicationName, testNamespace, "")
Expect(err).To(HaveOccurred())
return strings.Contains(err.Error(), "no pipelinerun found")
}, time.Minute*1, constants.PipelineRunPollingInterval).Should(BeTrue(), "timeout while checking if any pipelinerun is triggered")

buildStatus := &controllers.BuildStatus{}
Eventually(func() error {
Expand Down Expand Up @@ -1286,7 +1169,7 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build", "

Describe("A secret with dummy quay.io credentials is created in the testing namespace", Ordered, func() {

var applicationName, componentName, testNamespace string
var applicationName, componentName, testNamespace, pacBranchName, componentBaseBranchName string
var timeout time.Duration
var err error
var pr *pipeline.PipelineRun
Expand Down Expand Up @@ -1332,19 +1215,25 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build", "
Expect(err).ToNot(HaveOccurred())

componentName = "build-suite-test-secret-overriding"
pacBranchName = constants.PaCPullRequestBranchPrefix + componentName
componentBaseBranchName = fmt.Sprintf("base-%s", util.GenerateRandomString(6))
err = f.AsKubeAdmin.CommonController.Github.CreateRef(helloWorldComponentGitSourceCloneRepoName, "main", helloWorldComponentCloneRevision, componentBaseBranchName)
Expect(err).ShouldNot(HaveOccurred())

componentObj := appservice.ComponentSpec{
ComponentName: componentName,
Application: applicationName,
Source: appservice.ComponentSource{
ComponentSourceUnion: appservice.ComponentSourceUnion{
GitSource: &appservice.GitSource{
URL: helloWorldComponentGitSourceURL,
URL: helloWorldComponentGitSourceCloneURL,
Revision: componentBaseBranchName,
DockerfileURL: constants.DockerFilePath,
},
},
},
}
_, err = f.AsKubeAdmin.HasController.CreateComponent(componentObj, testNamespace, "", "", applicationName, true, constants.DefaultDockerBuildPipelineBundle)
_, err = f.AsKubeAdmin.HasController.CreateComponent(componentObj, testNamespace, "", "", applicationName, true, utils.MergeMaps(constants.ComponentPaCRequestAnnotation, constants.DefaultDockerBuildPipelineBundle))
Expect(err).NotTo(HaveOccurred())
})

Expand All @@ -1355,6 +1244,17 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build", "
Expect(f.AsKubeAdmin.TektonController.DeleteAllPipelineRunsInASpecificNamespace(testNamespace)).To(Succeed())
Expect(f.SandboxController.DeleteUserSignup(f.UserName)).To(BeTrue())
}
// Delete new branches created by PaC and a testing branch used as a component's base branch
err = f.AsKubeAdmin.CommonController.Github.DeleteRef(helloWorldComponentGitSourceCloneRepoName, pacBranchName)
if err != nil {
Expect(err.Error()).To(ContainSubstring("Reference does not exist"))
}
err = f.AsKubeAdmin.CommonController.Github.DeleteRef(helloWorldComponentGitSourceCloneRepoName, componentBaseBranchName)
if err != nil {
Expect(err.Error()).To(ContainSubstring("Reference does not exist"))
}
// Delete created webhook from GitHub
cleanupWebhooks(f, helloWorldComponentGitSourceCloneRepoName)
})

It("should override the shared secret", func() {
Expand All @@ -1372,7 +1272,7 @@ var _ = framework.BuildSuiteDescribe("Build service E2E tests", Label("build", "

pr, err = f.AsKubeAdmin.HasController.GetComponentPipelineRun(componentName, applicationName, testNamespace, "")
Expect(err).ShouldNot(HaveOccurred())
Expect(pr.Spec.Workspaces).To(HaveLen(1))
Expect(pr.Spec.Workspaces).To(HaveLen(2))
})

It("should not be possible to push to quay.io repo (PipelineRun should fail)", func() {
Expand Down
75 changes: 62 additions & 13 deletions tests/build/build_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,20 @@ var (

const pipelineCompletionRetries = 2

type TestBranches struct {
RepoName string
BranchName string
PacBranchName string
BaseBranchName string
}

var pacAndBaseBranches []TestBranches

// CreateComponent creates a component from a test repository URL and returns the component's name
func CreateComponent(ctrl *has.HasController, gitUrl, revision, applicationName, componentName, namespace string) string {
func CreateComponent(f *framework.Framework, ctrl *has.HasController, gitUrl, revision, applicationName, componentName, namespace string) string {
var err error
var buildPipelineAnnotation map[string]string
var baseBranchName, pacBranchName string
contextDir, dockerfilePath, pipelineBundleName, enableHermetic, prefetchInput, checkAdditionalTags := GetComponentScenarioDetailsFromGitUrl(gitUrl)
Expect(pipelineBundleName).ShouldNot(BeEmpty())
if pipelineBundleName == "docker-build" {
Expand Down Expand Up @@ -82,13 +92,38 @@ func CreateComponent(ctrl *has.HasController, gitUrl, revision, applicationName,
}
}

baseBranchName = fmt.Sprintf("base-%s", util.GenerateRandomString(6))
pacBranchName = constants.PaCPullRequestBranchPrefix + componentName

if revision == gitRepoContainsSymlinkBranchName {
revision = symlinkBranchRevision
err = f.AsKubeAdmin.CommonController.Github.CreateRef(utils.GetRepoName(gitUrl), gitRepoContainsSymlinkBranchName, revision, baseBranchName)
Expect(err).ShouldNot(HaveOccurred())
pacAndBaseBranches = append(pacAndBaseBranches, TestBranches{
RepoName: utils.GetRepoName(gitUrl),
BranchName: gitRepoContainsSymlinkBranchName,
PacBranchName: pacBranchName,
BaseBranchName: baseBranchName,
})
} else {
revision = GetGitRevision(gitUrl)
err = f.AsKubeAdmin.CommonController.Github.CreateRef(utils.GetRepoName(gitUrl), "main", revision, baseBranchName)
Expect(err).ShouldNot(HaveOccurred())
pacAndBaseBranches = append(pacAndBaseBranches, TestBranches{
RepoName: utils.GetRepoName(gitUrl),
BranchName: "main",
PacBranchName: pacBranchName,
BaseBranchName: baseBranchName,
})
}

componentObj := appservice.ComponentSpec{
ComponentName: componentName,
Source: appservice.ComponentSource{
ComponentSourceUnion: appservice.ComponentSourceUnion{
GitSource: &appservice.GitSource{
URL: gitUrl,
Revision: revision,
Revision: baseBranchName,
Context: contextDir,
DockerfileURL: dockerfilePath,
},
Expand All @@ -103,7 +138,7 @@ func CreateComponent(ctrl *has.HasController, gitUrl, revision, applicationName,
"build.appstudio.openshift.io/pipeline": fmt.Sprintf(`{"name":"docker-build", "bundle": "%s"}`, customSourceBuildBundle),
}
}
c, err := ctrl.CreateComponent(componentObj, namespace, "", "", applicationName, false, buildPipelineAnnotation)
c, err := ctrl.CreateComponent(componentObj, namespace, "", "", applicationName, false, utils.MergeMaps(constants.ComponentPaCRequestAnnotation, buildPipelineAnnotation))
Expect(err).ShouldNot(HaveOccurred())
return c.Name
}
Expand Down Expand Up @@ -141,7 +176,7 @@ var _ = framework.BuildSuiteDescribe("Build templates E2E test", Label("build",
defer GinkgoRecover()
Describe("HACBS pipelines", Ordered, Label("pipeline"), func() {

var applicationName, componentName, symlinkComponentName, testNamespace string
var applicationName, componentName, symlinkComponentName, symlinkPRunName, testNamespace string
var kubeadminClient *framework.ControllerHub
var pipelineRunsWithE2eFinalizer []string

Expand Down Expand Up @@ -182,15 +217,15 @@ var _ = framework.BuildSuiteDescribe("Build templates E2E test", Label("build",
for _, gitUrl := range componentUrls {
gitUrl := gitUrl
componentName = fmt.Sprintf("%s-%s", "test-comp", util.GenerateRandomString(4))
name := CreateComponent(kubeadminClient.HasController, gitUrl, "", applicationName, componentName, testNamespace)
name := CreateComponent(f, kubeadminClient.HasController, gitUrl, "", applicationName, componentName, testNamespace)
Expect(name).ShouldNot(BeEmpty())
componentNames = append(componentNames, name)
}

// Create component for the repo containing symlink
symlinkComponentName = fmt.Sprintf("%s-%s", "test-symlink-comp", util.GenerateRandomString(4))
symlinkComponentName = CreateComponent(
kubeadminClient.HasController, pythonComponentGitSourceURL, gitRepoContainsSymlinkBranchName,
f, kubeadminClient.HasController, pythonComponentGitSourceURL, gitRepoContainsSymlinkBranchName,
applicationName, symlinkComponentName, testNamespace)
})

Expand Down Expand Up @@ -224,13 +259,24 @@ var _ = framework.BuildSuiteDescribe("Build templates E2E test", Label("build",
Expect(f.SandboxController.DeleteUserSignup(f.UserName)).To(BeTrue())
}
}
//Cleanup pac and base branches
for _, branches := range pacAndBaseBranches {
err = f.AsKubeAdmin.CommonController.Github.DeleteRef(branches.RepoName, branches.PacBranchName)
if err != nil {
Expect(err.Error()).To(ContainSubstring("Reference does not exist"))
}
err = f.AsKubeAdmin.CommonController.Github.DeleteRef(branches.RepoName, branches.BaseBranchName)
if err != nil {
Expect(err.Error()).To(ContainSubstring("Reference does not exist"))
}
}
})

It(fmt.Sprintf("triggers PipelineRun for symlink component with source URL %s", pythonComponentGitSourceURL), Label(buildTemplatesTestLabel, sourceBuildTestLabel), func() {
timeout := time.Minute * 5
prName := WaitForPipelineRunStarts(kubeadminClient, applicationName, symlinkComponentName, testNamespace, timeout)
Expect(prName).ShouldNot(BeEmpty())
pipelineRunsWithE2eFinalizer = append(pipelineRunsWithE2eFinalizer, prName)
symlinkPRunName = WaitForPipelineRunStarts(kubeadminClient, applicationName, symlinkComponentName, testNamespace, timeout)
Expect(symlinkPRunName).ShouldNot(BeEmpty())
pipelineRunsWithE2eFinalizer = append(pipelineRunsWithE2eFinalizer, symlinkPRunName)
})

for i, gitUrl := range componentUrls {
Expand Down Expand Up @@ -637,10 +683,13 @@ var _ = framework.BuildSuiteDescribe("Build templates E2E test", Label("build",
}

It(fmt.Sprintf("pipelineRun should fail for symlink component with Git source URL %s", pythonComponentGitSourceURL), Label(buildTemplatesTestLabel, sourceBuildTestLabel), func() {
component, err := kubeadminClient.HasController.GetComponent(symlinkComponentName, testNamespace)
Expect(err).ShouldNot(HaveOccurred())
Expect(kubeadminClient.HasController.WaitForComponentPipelineToBeFinished(component, "",
kubeadminClient.TektonController, &has.RetryOptions{Retries: pipelineCompletionRetries}, nil)).Should(MatchError(ContainSubstring("cloned repository contains symlink pointing outside of the cloned repository")))
pipelineRunTimeout := int(time.Duration(10) * time.Minute)
Expect(f.AsKubeAdmin.TektonController.WatchPipelineRun(symlinkPRunName, testNamespace, pipelineRunTimeout)).To(Succeed())
pr, err := f.AsKubeAdmin.TektonController.GetPipelineRun(symlinkPRunName, testNamespace)
Expect(err).ShouldNot(HaveOccurred(), "failed to get pipelinerun %s", symlinkPRunName)
tr, err := f.AsKubeAdmin.TektonController.GetTaskRunStatus(f.AsKubeAdmin.CommonController.KubeRest(), pr, "clone-repository")
Expect(err).NotTo(HaveOccurred(), "failed to get taskrun for clone-repository")
Expect(tekton.DidTaskRunSucceed(tr)).To(BeFalse())
})
})
})
Expand Down
Loading

0 comments on commit 5e0aef7

Please sign in to comment.