Skip to content

Commit d6b83bc

Browse files
committed
feat: improve tests coverage
1 parent fefb270 commit d6b83bc

1 file changed

Lines changed: 169 additions & 4 deletions

File tree

e2e-tests/tests/image_controller.go

Lines changed: 169 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ var _ = Describe("Image Controller E2E tests", Label("image-controller"), func()
2727

2828
var gitClient git.Client
2929

30-
Describe("PaC component build", Ordered, func() {
30+
Describe("using generate annotation", Ordered, func() {
3131
var applicationName, customDefaultComponentName, customBranchComponentName, componentBaseBranchName string
3232
var testNamespace, imageRepoName, pullRobotAccountName, pushRobotAccountName string
33+
var imageRepositoryCRName, firstGenerateTimestamp string
3334
var helloWorldComponentGitSourceURL string
3435
var component *appservice.Component
3536
var plr *pipeline.PipelineRun
@@ -67,6 +68,7 @@ var _ = Describe("Image Controller E2E tests", Label("image-controller"), func()
6768
customDefaultComponentName = fmt.Sprintf("gh-%s-%s", "test-custom-default", util.GenerateRandomString(6))
6869
customBranchComponentName = fmt.Sprintf("gh-%s-%s", "test-custom-branch", util.GenerateRandomString(6))
6970
componentBaseBranchName = fmt.Sprintf("base-%s", util.GenerateRandomString(6))
71+
imageRepositoryCRName = "imagerepository-for-" + applicationName + "-" + customDefaultComponentName
7072

7173
gitClient, helloWorldComponentGitSourceURL, helloWorldRepository = setupGitProvider(f)
7274
buildPipelineAnnotation = build.GetBuildPipelineBundleAnnotation(constants.DockerBuildOciTAMin)
@@ -88,7 +90,7 @@ var _ = Describe("Image Controller E2E tests", Label("image-controller"), func()
8890

8991
})
9092

91-
When("a new component is created with visibility private", Label("pac-custom-default-branch"), func() {
93+
When("a new component is created with visibility private", Label("private"), func() {
9294
var componentObj appservice.ComponentSpec
9395

9496
BeforeAll(func() {
@@ -164,10 +166,29 @@ var _ = Describe("Image Controller E2E tests", Label("image-controller"), func()
164166
Expect(err).ShouldNot(HaveOccurred(), fmt.Sprintf("failed while checking if the image repo %s is private", imageRepoName))
165167
Expect(isPublic).To(BeFalse(), "Expected image repo to be private, but it is public")
166168
})
169+
It("credential rotaion is successful", func() {
170+
firstGenerateTimestamp, err = f.AsKubeAdmin.ImageController.GetGenerateTimestamp(imageRepositoryCRName, testNamespace)
171+
Expect(err).ShouldNot(HaveOccurred(), fmt.Sprintf("failed to get generate timestamp of image repository: %q", imageRepositoryCRName))
172+
GinkgoWriter.Printf("Image Repository initial generateTimestamp: %s\n", firstGenerateTimestamp)
167173

174+
err = f.AsKubeAdmin.ImageController.RegenerateToken(imageRepositoryCRName, testNamespace)
175+
Expect(err).ShouldNot(HaveOccurred(), fmt.Sprintf("failed to regenerate token for image repository: %q", imageRepositoryCRName))
176+
177+
Eventually(func() error {
178+
currentGenerateTimestamp, err := f.AsKubeAdmin.ImageController.GetGenerateTimestamp(imageRepositoryCRName, testNamespace)
179+
if err != nil {
180+
GinkgoWriter.Printf("failed to get generate timestamp after rotation with error %v\n", err)
181+
return err
182+
}
183+
if currentGenerateTimestamp == firstGenerateTimestamp {
184+
return fmt.Errorf("Current generate timestamp %q is not equal to earlier generate timestamp %q\n", currentGenerateTimestamp, firstGenerateTimestamp)
185+
}
186+
return nil
187+
}, time.Second*30, time.Second*2).Should(Succeed(), fmt.Sprintf("timed out when checking generate timestamp is updated for %s/%s", testNamespace, imageRepositoryCRName))
188+
})
168189
})
169190

170-
When("a new Component with specified custom branch is created and with visibility public", Label("build-custom-branch"), func() {
191+
When("a new component is created and with visibility public", Label("public"), func() {
171192
var componentObj appservice.ComponentSpec
172193

173194
BeforeAll(func() {
@@ -223,8 +244,8 @@ var _ = Describe("Image Controller E2E tests", Label("image-controller"), func()
223244
pushRobotAccountExist, err := build.DoesRobotAccountExistInQuay(pushRobotAccountName)
224245
Expect(err).ShouldNot(HaveOccurred(), "failed while checking if push robot account exists in quay with error: %+v", err)
225246
Expect(pushRobotAccountExist).To(BeTrue(), "push robot account does not exists in quay")
226-
227247
})
248+
228249
It("created image repo is public", func() {
229250
isPublic, err := build.IsImageRepoPublic(imageRepoName)
230251
Expect(err).ShouldNot(HaveOccurred(), fmt.Sprintf("failed while checking if the image repo %s is public", imageRepoName))
@@ -251,4 +272,148 @@ var _ = Describe("Image Controller E2E tests", Label("image-controller"), func()
251272

252273
})
253274
})
275+
Describe("general puspose image repository", Ordered, Label("without-component"), func() {
276+
var testNamespace, imageRepoName string
277+
var imageRepositoryCRName = "sample-image-repo"
278+
279+
BeforeAll(func() {
280+
f, err = framework.NewFramework(utils.GetGeneratedNamespace("image-controller-e2e"))
281+
Expect(err).NotTo(HaveOccurred())
282+
testNamespace = f.UserNamespace
283+
})
284+
It("create an image repository", func() {
285+
_, err = f.AsKubeAdmin.ImageController.CreateImageRepositoryCR(imageRepositoryCRName, testNamespace, "public", "", "", "", false)
286+
Expect(err).ShouldNot(HaveOccurred(), fmt.Sprintf("failed to create image repository cr: %q", imageRepositoryCRName))
287+
288+
// Wait for image repository to be ready
289+
err = f.AsKubeAdmin.ImageController.WaitForImageRepositoryToBeReady(imageRepositoryCRName, testNamespace)
290+
Expect(err).ShouldNot(HaveOccurred(), fmt.Sprintf("failed while waiting for image repository %q to be ready", imageRepositoryCRName))
291+
})
292+
It("image repo and robot accounts created successfully", func() {
293+
imageRepoName, err = f.AsKubeAdmin.ImageController.GetImageNameFromImageRepositoryCR(testNamespace, imageRepositoryCRName)
294+
Expect(err).ShouldNot(HaveOccurred(), "failed to read image repo from image repository: %s", imageRepositoryCRName)
295+
Expect(imageRepoName).ShouldNot(BeEmpty(), "image repo name is empty")
296+
297+
imageExist, err := build.DoesImageRepoExistInQuay(imageRepoName)
298+
Expect(err).ShouldNot(HaveOccurred(), "failed while checking if image repo exists in quay with error: %+v", err)
299+
Expect(imageExist).To(BeTrue(), "quay image does not exists")
300+
301+
pullRobotAccountName, pushRobotAccountName, err := f.AsKubeAdmin.ImageController.GetRobotAccountsFromImageRepositoryCR(testNamespace, imageRepositoryCRName)
302+
Expect(err).ShouldNot(HaveOccurred(), "failed to get robot account names")
303+
pullRobotAccountExist, err := build.DoesRobotAccountExistInQuay(pullRobotAccountName)
304+
Expect(err).ShouldNot(HaveOccurred(), "failed while checking if pull robot account exists in quay with error: %+v", err)
305+
Expect(pullRobotAccountExist).To(BeTrue(), "pull robot account does not exists in quay")
306+
pushRobotAccountExist, err := build.DoesRobotAccountExistInQuay(pushRobotAccountName)
307+
Expect(err).ShouldNot(HaveOccurred(), "failed while checking if push robot account exists in quay with error: %+v", err)
308+
Expect(pushRobotAccountExist).To(BeTrue(), "push robot account does not exists in quay")
309+
310+
pullSecretName, pushSecretName, err := f.AsKubeAdmin.ImageController.GetSecretsFromImageRepositoryCR(testNamespace, imageRepositoryCRName)
311+
Expect(err).ShouldNot(HaveOccurred(), "failed to get secrets from image repository")
312+
_, err = f.AsKubeAdmin.CommonController.GetSecret(testNamespace, pullSecretName)
313+
Expect(err).ShouldNot(HaveOccurred(), "failed while getting pull secret: %+v", err)
314+
_, err = f.AsKubeAdmin.CommonController.GetSecret(testNamespace, pushSecretName)
315+
Expect(err).ShouldNot(HaveOccurred(), "failed while getting push secret: %+v", err)
316+
})
317+
It("image repo deletion is successful", func() {
318+
err = f.AsKubeAdmin.ImageController.DeleteImageRepositoryCR(imageRepositoryCRName, testNamespace)
319+
Expect(err).ShouldNot(HaveOccurred(), "failed to delete image repository")
320+
321+
Eventually(func() (bool, error) {
322+
imageExist, err := build.DoesImageRepoExistInQuay(imageRepoName)
323+
if err != nil {
324+
GinkgoWriter.Printf("failed to check if image exists in quay with error %v\n", err)
325+
return false, err
326+
}
327+
return !imageExist, nil
328+
}, time.Second*60, time.Second*2).Should(BeTrue(), fmt.Sprintf("even after deleting image repository, quay repo %q still exists", imageRepoName))
329+
330+
})
331+
})
332+
333+
Describe("verify secret linking", Ordered, Label("secret-linking"), func() {
334+
var componentObj appservice.ComponentSpec
335+
var buildPipelineAnnotation map[string]string
336+
var helloWorldComponentGitSourceURL, helloWorldRepository, testNamespace string
337+
338+
var imageRepositoryCRName = "image-repository" + "-" + util.GenerateRandomString(4)
339+
var userDefinedImageName = "image-name" + "-" + util.GenerateRandomString(4)
340+
var applicationName = fmt.Sprintf("build-suite-test-application-%s", util.GenerateRandomString(4))
341+
var componentName = fmt.Sprintf("%s-%s", "test-secret-linking", util.GenerateRandomString(6))
342+
var componentBaseBranchName = fmt.Sprintf("base-%s", util.GenerateRandomString(6))
343+
var serviceAccountName = "build-pipeline-" + componentName
344+
var secretName = "components-namespace-pull"
345+
346+
BeforeAll(func() {
347+
f, err = framework.NewFramework(utils.GetGeneratedNamespace("image-controller-e2e"))
348+
Expect(err).NotTo(HaveOccurred())
349+
testNamespace = f.UserNamespace
350+
351+
_, err = f.AsKubeAdmin.HasController.CreateApplication(applicationName, testNamespace)
352+
Expect(err).NotTo(HaveOccurred())
353+
354+
gitClient, helloWorldComponentGitSourceURL, helloWorldRepository = setupGitProvider(f)
355+
err = gitClient.CreateBranch(helloWorldRepository, helloWorldComponentDefaultBranch, helloWorldComponentRevision, componentBaseBranchName)
356+
Expect(err).ShouldNot(HaveOccurred())
357+
358+
buildPipelineAnnotation = build.GetBuildPipelineBundleAnnotation(constants.DockerBuildOciTAMin)
359+
})
360+
AfterAll(func() {
361+
if !CurrentSpecReport().Failed() {
362+
Eventually(func() error {
363+
return f.AsKubeAdmin.HasController.DeleteAllComponentsInASpecificNamespace(testNamespace, time.Minute*2)
364+
}, 2*time.Minute, 10*time.Second).Should(Succeed())
365+
Eventually(func() error {
366+
return f.AsKubeAdmin.HasController.DeleteAllApplicationsInASpecificNamespace(testNamespace, time.Minute*2)
367+
}, 2*time.Minute, 10*time.Second).Should(Succeed())
368+
Expect(gitClient.DeleteRepositoryIfExists(helloWorldRepository)).To(Succeed())
369+
}
370+
})
371+
It("create an image repository with user defined image name", func() {
372+
_, err = f.AsKubeAdmin.ImageController.CreateImageRepositoryCR(imageRepositoryCRName, testNamespace, "public", userDefinedImageName, applicationName, componentName, true)
373+
Expect(err).ShouldNot(HaveOccurred(), fmt.Sprintf("failed to create image repository cr: %q", imageRepositoryCRName))
374+
})
375+
It("create a component linking the image repository", func() {
376+
componentObj = appservice.ComponentSpec{
377+
ComponentName: componentName,
378+
Application: applicationName,
379+
Source: appservice.ComponentSource{
380+
ComponentSourceUnion: appservice.ComponentSourceUnion{
381+
GitSource: &appservice.GitSource{
382+
URL: helloWorldComponentGitSourceURL,
383+
Revision: componentBaseBranchName,
384+
DockerfileURL: constants.DockerFilePath,
385+
},
386+
},
387+
},
388+
}
389+
_, err = f.AsKubeAdmin.HasController.CreateComponentCheckImageRepository(componentObj, testNamespace, "", "", applicationName, false, utils.MergeMaps(constants.ComponentPaCRequestAnnotation, buildPipelineAnnotation))
390+
Expect(err).ShouldNot(HaveOccurred())
391+
})
392+
It("check secret is linked to service accounts", func() {
393+
secretLinked, err := f.AsKubeAdmin.CommonController.IsSecretLinkedToServiceAccount(serviceAccountName, testNamespace, secretName)
394+
Expect(err).ShouldNot(HaveOccurred(), "failed to check if secret is linked to service account")
395+
Expect(secretLinked).To(BeTrue(), fmt.Sprintf("secret %q is not linked to service account %q", secretName, serviceAccountName))
396+
})
397+
It("remove the secret linking", func() {
398+
err = f.AsKubeAdmin.CommonController.UnlinkSecretFromServiceAccount(testNamespace, secretName, serviceAccountName, false)
399+
Expect(err).ShouldNot(HaveOccurred(), "failed to remove secret linked to service account")
400+
401+
secretLinked, err := f.AsKubeAdmin.CommonController.IsSecretLinkedToServiceAccount(serviceAccountName, testNamespace, secretName)
402+
Expect(err).ShouldNot(HaveOccurred(), "failed to check if secret is linked to service account after removing the link")
403+
Expect(secretLinked).To(BeFalse(), fmt.Sprintf("secret %q is linked to service account %q, which is unexpected", secretName, serviceAccountName))
404+
})
405+
It("set verify linking to true in image repo", func() {
406+
err = f.AsKubeAdmin.ImageController.VerifyLinking(imageRepositoryCRName, testNamespace)
407+
Expect(err).ShouldNot(HaveOccurred(), "failed to set verify linking to true")
408+
409+
Eventually(func() (bool, error) {
410+
secretLinked, err := f.AsKubeAdmin.CommonController.IsSecretLinkedToServiceAccount(serviceAccountName, testNamespace, secretName)
411+
if err != nil {
412+
GinkgoWriter.Printf("failed to check if secret is linked to service account with error %v\n", err)
413+
return false, err
414+
}
415+
return secretLinked, nil
416+
}, time.Second*60, time.Second*2).Should(BeTrue(), fmt.Sprintf("secret %q is not linked to service account %q after verify linking", secretName, serviceAccountName))
417+
})
418+
})
254419
})

0 commit comments

Comments
 (0)