From 8d4ea33d8f588e2d7831ea37c31d9186dc8e21fc Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Sat, 31 Aug 2024 08:08:44 +0100 Subject: [PATCH] test --- .github/workflows/test-e2e-samples.yml | 5 +- .../project/test/e2e/e2e_suite_test.go | 19 ++++- .../testdata/project/test/e2e/e2e_test.go | 28 ++++++- .../project/test/e2e/e2e_suite_test.go | 19 ++++- .../testdata/project/test/e2e/e2e_test.go | 7 +- pkg/plugins/golang/v4/scaffolds/init.go | 1 + .../internal/templates/test/e2e/suite.go | 19 ++++- .../internal/templates/test/e2e/test.go | 76 ++++++++++++++++++- pkg/plugins/golang/v4/scaffolds/webhook.go | 2 + .../test/e2e/e2e_suite_test.go | 19 ++++- .../test/e2e/e2e_test.go | 28 ++++++- .../test/e2e/e2e_suite_test.go | 19 ++++- .../test/e2e/e2e_test.go | 28 ++++++- .../test/e2e/e2e_suite_test.go | 19 ++++- .../test/e2e/e2e_test.go | 28 ++++++- .../test/e2e/e2e_suite_test.go | 19 ++++- .../test/e2e/e2e_test.go | 7 +- .../project-v4/test/e2e/e2e_suite_test.go | 19 ++++- testdata/project-v4/test/e2e/e2e_test.go | 28 ++++++- 19 files changed, 352 insertions(+), 38 deletions(-) diff --git a/.github/workflows/test-e2e-samples.yml b/.github/workflows/test-e2e-samples.yml index cf5ca8e06cc..3fae809fd46 100644 --- a/.github/workflows/test-e2e-samples.yml +++ b/.github/workflows/test-e2e-samples.yml @@ -39,10 +39,9 @@ jobs: KUSTOMIZATION_FILE_PATH="testdata/project-v4/config/default/kustomization.yaml" sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH sed -i '51s/^#//' $KUSTOMIZATION_FILE_PATH + sed -i '55,151s/^#//' $KUSTOMIZATION_FILE_PATH cd testdata/project-v4/ go mod tidy - make generate - make manifests - name: Testing make test-e2e for project-v4 working-directory: testdata/project-v4/ @@ -57,8 +56,6 @@ jobs: sed -i '55,151s/^#//' $KUSTOMIZATION_FILE_PATH cd testdata/project-v4-with-deploy-image/ go mod tidy - make generate - make manifests - name: Testing make test-e2e for project-v4-with-deploy-image working-directory: testdata/project-v4-with-deploy-image diff --git a/docs/book/src/cronjob-tutorial/testdata/project/test/e2e/e2e_suite_test.go b/docs/book/src/cronjob-tutorial/testdata/project/test/e2e/e2e_suite_test.go index f74f3965316..4cb1fee0b1f 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/test/e2e/e2e_suite_test.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/test/e2e/e2e_suite_test.go @@ -53,9 +53,24 @@ func TestE2E(t *testing.T) { } var _ = BeforeSuite(func() { - By("building the manager(Operator) image") - cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + By("running go mod tidy") + cmd := exec.Command("go", "mod", "tidy") _, err := utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run go mod tidy") + + By("running generating files") + cmd = exec.Command("make", "generate") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate") + + By("generating manifests") + cmd = exec.Command("make", "manifests") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests") + + By("building the manager(Operator) image") + cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + _, err = utils.Run(cmd) ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image") // TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is diff --git a/docs/book/src/cronjob-tutorial/testdata/project/test/e2e/e2e_test.go b/docs/book/src/cronjob-tutorial/testdata/project/test/e2e/e2e_test.go index 2812ef5b973..faba846f5c8 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/test/e2e/e2e_test.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/test/e2e/e2e_test.go @@ -31,6 +31,9 @@ const namespace = "project-system" // Define a set of end-to-end (e2e) tests to validate the behavior of the controller. var _ = Describe("controller", Ordered, func() { + // controllerPodName stores the name of the controller pod + var controllerPodName string + // Before running the tests, set up the environment by creating the namespace, // installing CRDs, and deploying the controller. BeforeAll(func() { @@ -69,8 +72,6 @@ var _ = Describe("controller", Ordered, func() { // The Context block contains the actual tests that validate the operator's behavior. Context("Operator", func() { It("should run successfully", func() { - var controllerPodName string - By("validating that the controller-manager pod is running as expected") verifyControllerUp := func() error { // Get the name of the controller-manager pod @@ -108,6 +109,29 @@ var _ = Describe("controller", Ordered, func() { EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed()) }) + It("cert-manager should be successfully provisioned", func() { + By("validating that cert-manager has provisioned the certificate Secret") + EventuallyWithOffset(1, func() error { + cmd := exec.Command("kubectl", "get", "secrets", "webhook-server-cert") + _, err := utils.Run(cmd) + return err + }, time.Minute, time.Second).Should(Succeed()) + + By("validating that the mutating|validating webhooks have the CA injected") + verifyCAInjection := func() error { + cmd := exec.Command("kubectl", "get", + "mutatingwebhookconfigurations.admissionregistration.k8s.io", + "project-mutating-webhook-configuration", + "-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}") + output, err := utils.Run(cmd) + ExpectWithOffset(2, err).NotTo(HaveOccurred()) + ExpectWithOffset(2, len(output)).To(BeNumerically(">", 10)) + + return nil + } + EventuallyWithOffset(1, verifyCAInjection, time.Minute, time.Second).Should(Succeed()) + }) // +kubebuilder:scaffold:webhookchecks + // TODO(user): Customize the e2e test suite to include // additional scenarios specific to your project. }) diff --git a/docs/book/src/getting-started/testdata/project/test/e2e/e2e_suite_test.go b/docs/book/src/getting-started/testdata/project/test/e2e/e2e_suite_test.go index fc3f37d0234..488ef4c2f40 100644 --- a/docs/book/src/getting-started/testdata/project/test/e2e/e2e_suite_test.go +++ b/docs/book/src/getting-started/testdata/project/test/e2e/e2e_suite_test.go @@ -53,9 +53,24 @@ func TestE2E(t *testing.T) { } var _ = BeforeSuite(func() { - By("building the manager(Operator) image") - cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + By("running go mod tidy") + cmd := exec.Command("go", "mod", "tidy") _, err := utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run go mod tidy") + + By("running generating files") + cmd = exec.Command("make", "generate") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate") + + By("generating manifests") + cmd = exec.Command("make", "manifests") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests") + + By("building the manager(Operator) image") + cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + _, err = utils.Run(cmd) ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image") // TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is diff --git a/docs/book/src/getting-started/testdata/project/test/e2e/e2e_test.go b/docs/book/src/getting-started/testdata/project/test/e2e/e2e_test.go index 66f7dcef8fd..c1face7e5df 100644 --- a/docs/book/src/getting-started/testdata/project/test/e2e/e2e_test.go +++ b/docs/book/src/getting-started/testdata/project/test/e2e/e2e_test.go @@ -31,6 +31,9 @@ const namespace = "project-system" // Define a set of end-to-end (e2e) tests to validate the behavior of the controller. var _ = Describe("controller", Ordered, func() { + // controllerPodName stores the name of the controller pod + var controllerPodName string + // Before running the tests, set up the environment by creating the namespace, // installing CRDs, and deploying the controller. BeforeAll(func() { @@ -69,8 +72,6 @@ var _ = Describe("controller", Ordered, func() { // The Context block contains the actual tests that validate the operator's behavior. Context("Operator", func() { It("should run successfully", func() { - var controllerPodName string - By("validating that the controller-manager pod is running as expected") verifyControllerUp := func() error { // Get the name of the controller-manager pod @@ -108,6 +109,8 @@ var _ = Describe("controller", Ordered, func() { EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed()) }) + // +kubebuilder:scaffold:webhookchecks + // TODO(user): Customize the e2e test suite to include // additional scenarios specific to your project. }) diff --git a/pkg/plugins/golang/v4/scaffolds/init.go b/pkg/plugins/golang/v4/scaffolds/init.go index ab4ce8ad2d3..e90bc4159b2 100644 --- a/pkg/plugins/golang/v4/scaffolds/init.go +++ b/pkg/plugins/golang/v4/scaffolds/init.go @@ -160,6 +160,7 @@ func (s *initScaffolder) Scaffold() error { &templates.Readme{}, &templates.Golangci{}, &e2e.Test{}, + &e2e.WebhookTestUpdater{WireWebhook: false}, &e2e.SuiteTest{}, &utils.Utils{}, &templates.DevContainer{}, diff --git a/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/suite.go b/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/suite.go index d7281a607a0..96bdea06ff8 100644 --- a/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/suite.go +++ b/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/suite.go @@ -79,9 +79,24 @@ func TestE2E(t *testing.T) { } var _ = BeforeSuite(func() { - By("building the manager(Operator) image") - cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + By("running go mod tidy") + cmd := exec.Command("go", "mod", "tidy") _, err := utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run go mod tidy") + + By("running generating files") + cmd = exec.Command("make", "generate") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate") + + By("generating manifests") + cmd = exec.Command("make", "manifests") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests") + + By("building the manager(Operator) image") + cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + _, err = utils.Run(cmd) ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image") // TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is diff --git a/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/test.go b/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/test.go index 6288f9d4cfd..d2cf7492d32 100644 --- a/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/test.go +++ b/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/test.go @@ -17,11 +17,15 @@ limitations under the License. package e2e import ( + "fmt" + "path/filepath" "sigs.k8s.io/kubebuilder/v4/pkg/machinery" ) -var _ machinery.Template = &SuiteTest{} +var _ machinery.Template = &Test{} +var _ machinery.Inserter = &WebhookTestUpdater{} +// Test defines the basic setup for the e2e test type Test struct { machinery.TemplateMixin machinery.BoilerplateMixin @@ -31,13 +35,74 @@ type Test struct { func (f *Test) SetTemplateDefaults() error { if f.Path == "" { - f.Path = "test/e2e/e2e_test.go" + f.Path = filepath.Join("test", "e2e", "e2e_test.go") } + // This is where the template body is defined with markers f.TemplateBody = TestTemplate + return nil } +// WebhookTestUpdater updates e2e_test.go to insert additional webhook validation tests +type WebhookTestUpdater struct { + machinery.RepositoryMixin + machinery.ProjectNameMixin + WireWebhook bool +} + +// GetPath implements file.Builder +func (*WebhookTestUpdater) GetPath() string { + return filepath.Join("test", "e2e", "e2e_test.go") +} + +// GetIfExistsAction implements file.Builder +func (*WebhookTestUpdater) GetIfExistsAction() machinery.IfExistsAction { + return machinery.OverwriteFile // Ensures only the marker is replaced +} + +// GetMarkers implements file.Inserter +func (f *WebhookTestUpdater) GetMarkers() []machinery.Marker { + return []machinery.Marker{ + machinery.NewMarkerFor(f.GetPath(), "webhookchecks"), + } +} + +// GetCodeFragments implements file.Inserter +func (f *WebhookTestUpdater) GetCodeFragments() machinery.CodeFragmentsMap { + if !f.WireWebhook { + return nil + } + + code := fmt.Sprintf(webhookchecksFragment, f.ProjectName) + return machinery.CodeFragmentsMap{ + machinery.NewMarkerFor(f.GetPath(), "webhookchecks"): []string{code}, + } +} + +const webhookchecksFragment = `It("cert-manager should be successfully provisioned", func() { + By("validating that cert-manager has provisioned the certificate Secret") + EventuallyWithOffset(1, func() error { + cmd := exec.Command("kubectl", "get", "secrets", "webhook-server-cert") + _, err := utils.Run(cmd) + return err + }, time.Minute, time.Second).Should(Succeed()) + + By("validating that the mutating|validating webhooks have the CA injected") + verifyCAInjection := func() error { + cmd := exec.Command("kubectl", "get", + "mutatingwebhookconfigurations.admissionregistration.k8s.io", + "%s-mutating-webhook-configuration", + "-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}") + output, err := utils.Run(cmd) + ExpectWithOffset(2, err).NotTo(HaveOccurred()) + ExpectWithOffset(2, len(output)).To(BeNumerically(">", 10)) + + return nil + } + EventuallyWithOffset(1, verifyCAInjection, time.Minute, time.Second).Should(Succeed()) + })` + var TestTemplate = `{{ .Boilerplate }} @@ -58,6 +123,9 @@ const namespace = "{{ .ProjectName }}-system" // Define a set of end-to-end (e2e) tests to validate the behavior of the controller. var _ = Describe("controller", Ordered, func() { + // controllerPodName stores the name of the controller pod + var controllerPodName string + // Before running the tests, set up the environment by creating the namespace, // installing CRDs, and deploying the controller. BeforeAll(func() { @@ -96,8 +164,6 @@ var _ = Describe("controller", Ordered, func() { // The Context block contains the actual tests that validate the operator's behavior. Context("Operator", func() { It("should run successfully", func() { - var controllerPodName string - By("validating that the controller-manager pod is running as expected") verifyControllerUp := func() error { // Get the name of the controller-manager pod @@ -135,6 +201,8 @@ var _ = Describe("controller", Ordered, func() { EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed()) }) + // +kubebuilder:scaffold:webhookchecks + // TODO(user): Customize the e2e test suite to include // additional scenarios specific to your project. }) diff --git a/pkg/plugins/golang/v4/scaffolds/webhook.go b/pkg/plugins/golang/v4/scaffolds/webhook.go index 8b46a17e615..a1e9cad4ea5 100644 --- a/pkg/plugins/golang/v4/scaffolds/webhook.go +++ b/pkg/plugins/golang/v4/scaffolds/webhook.go @@ -18,6 +18,7 @@ package scaffolds import ( "fmt" + "sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e" log "github.com/sirupsen/logrus" "github.com/spf13/afero" @@ -86,6 +87,7 @@ func (s *webhookScaffolder) Scaffold() error { if err := scaffold.Execute( &api.Webhook{Force: s.force}, + &e2e.WebhookTestUpdater{WireWebhook: true}, &templates.MainUpdater{WireWebhook: true}, &api.WebhookTest{Force: s.force}, ); err != nil { diff --git a/testdata/project-v4-multigroup-with-deploy-image/test/e2e/e2e_suite_test.go b/testdata/project-v4-multigroup-with-deploy-image/test/e2e/e2e_suite_test.go index bd22bc701b0..6ffb1b2605d 100644 --- a/testdata/project-v4-multigroup-with-deploy-image/test/e2e/e2e_suite_test.go +++ b/testdata/project-v4-multigroup-with-deploy-image/test/e2e/e2e_suite_test.go @@ -53,9 +53,24 @@ func TestE2E(t *testing.T) { } var _ = BeforeSuite(func() { - By("building the manager(Operator) image") - cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + By("running go mod tidy") + cmd := exec.Command("go", "mod", "tidy") _, err := utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run go mod tidy") + + By("running generating files") + cmd = exec.Command("make", "generate") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate") + + By("generating manifests") + cmd = exec.Command("make", "manifests") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests") + + By("building the manager(Operator) image") + cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + _, err = utils.Run(cmd) ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image") // TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is diff --git a/testdata/project-v4-multigroup-with-deploy-image/test/e2e/e2e_test.go b/testdata/project-v4-multigroup-with-deploy-image/test/e2e/e2e_test.go index fe3a6e4d75d..286ed4c1971 100644 --- a/testdata/project-v4-multigroup-with-deploy-image/test/e2e/e2e_test.go +++ b/testdata/project-v4-multigroup-with-deploy-image/test/e2e/e2e_test.go @@ -31,6 +31,9 @@ const namespace = "project-v4-multigroup-with-deploy-image-system" // Define a set of end-to-end (e2e) tests to validate the behavior of the controller. var _ = Describe("controller", Ordered, func() { + // controllerPodName stores the name of the controller pod + var controllerPodName string + // Before running the tests, set up the environment by creating the namespace, // installing CRDs, and deploying the controller. BeforeAll(func() { @@ -69,8 +72,6 @@ var _ = Describe("controller", Ordered, func() { // The Context block contains the actual tests that validate the operator's behavior. Context("Operator", func() { It("should run successfully", func() { - var controllerPodName string - By("validating that the controller-manager pod is running as expected") verifyControllerUp := func() error { // Get the name of the controller-manager pod @@ -108,6 +109,29 @@ var _ = Describe("controller", Ordered, func() { EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed()) }) + It("cert-manager should be successfully provisioned", func() { + By("validating that cert-manager has provisioned the certificate Secret") + EventuallyWithOffset(1, func() error { + cmd := exec.Command("kubectl", "get", "secrets", "webhook-server-cert") + _, err := utils.Run(cmd) + return err + }, time.Minute, time.Second).Should(Succeed()) + + By("validating that the mutating|validating webhooks have the CA injected") + verifyCAInjection := func() error { + cmd := exec.Command("kubectl", "get", + "mutatingwebhookconfigurations.admissionregistration.k8s.io", + "project-v4-multigroup-with-deploy-image-mutating-webhook-configuration", + "-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}") + output, err := utils.Run(cmd) + ExpectWithOffset(2, err).NotTo(HaveOccurred()) + ExpectWithOffset(2, len(output)).To(BeNumerically(">", 10)) + + return nil + } + EventuallyWithOffset(1, verifyCAInjection, time.Minute, time.Second).Should(Succeed()) + }) // +kubebuilder:scaffold:webhookchecks + // TODO(user): Customize the e2e test suite to include // additional scenarios specific to your project. }) diff --git a/testdata/project-v4-multigroup/test/e2e/e2e_suite_test.go b/testdata/project-v4-multigroup/test/e2e/e2e_suite_test.go index f97a730d667..d7fefcafad3 100644 --- a/testdata/project-v4-multigroup/test/e2e/e2e_suite_test.go +++ b/testdata/project-v4-multigroup/test/e2e/e2e_suite_test.go @@ -53,9 +53,24 @@ func TestE2E(t *testing.T) { } var _ = BeforeSuite(func() { - By("building the manager(Operator) image") - cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + By("running go mod tidy") + cmd := exec.Command("go", "mod", "tidy") _, err := utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run go mod tidy") + + By("running generating files") + cmd = exec.Command("make", "generate") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate") + + By("generating manifests") + cmd = exec.Command("make", "manifests") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests") + + By("building the manager(Operator) image") + cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + _, err = utils.Run(cmd) ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image") // TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is diff --git a/testdata/project-v4-multigroup/test/e2e/e2e_test.go b/testdata/project-v4-multigroup/test/e2e/e2e_test.go index 580c8745b03..c1712f96751 100644 --- a/testdata/project-v4-multigroup/test/e2e/e2e_test.go +++ b/testdata/project-v4-multigroup/test/e2e/e2e_test.go @@ -31,6 +31,9 @@ const namespace = "project-v4-multigroup-system" // Define a set of end-to-end (e2e) tests to validate the behavior of the controller. var _ = Describe("controller", Ordered, func() { + // controllerPodName stores the name of the controller pod + var controllerPodName string + // Before running the tests, set up the environment by creating the namespace, // installing CRDs, and deploying the controller. BeforeAll(func() { @@ -69,8 +72,6 @@ var _ = Describe("controller", Ordered, func() { // The Context block contains the actual tests that validate the operator's behavior. Context("Operator", func() { It("should run successfully", func() { - var controllerPodName string - By("validating that the controller-manager pod is running as expected") verifyControllerUp := func() error { // Get the name of the controller-manager pod @@ -108,6 +109,29 @@ var _ = Describe("controller", Ordered, func() { EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed()) }) + It("cert-manager should be successfully provisioned", func() { + By("validating that cert-manager has provisioned the certificate Secret") + EventuallyWithOffset(1, func() error { + cmd := exec.Command("kubectl", "get", "secrets", "webhook-server-cert") + _, err := utils.Run(cmd) + return err + }, time.Minute, time.Second).Should(Succeed()) + + By("validating that the mutating|validating webhooks have the CA injected") + verifyCAInjection := func() error { + cmd := exec.Command("kubectl", "get", + "mutatingwebhookconfigurations.admissionregistration.k8s.io", + "project-v4-multigroup-mutating-webhook-configuration", + "-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}") + output, err := utils.Run(cmd) + ExpectWithOffset(2, err).NotTo(HaveOccurred()) + ExpectWithOffset(2, len(output)).To(BeNumerically(">", 10)) + + return nil + } + EventuallyWithOffset(1, verifyCAInjection, time.Minute, time.Second).Should(Succeed()) + }) // +kubebuilder:scaffold:webhookchecks + // TODO(user): Customize the e2e test suite to include // additional scenarios specific to your project. }) diff --git a/testdata/project-v4-with-deploy-image/test/e2e/e2e_suite_test.go b/testdata/project-v4-with-deploy-image/test/e2e/e2e_suite_test.go index 63d8dfa3997..d52046aa52e 100644 --- a/testdata/project-v4-with-deploy-image/test/e2e/e2e_suite_test.go +++ b/testdata/project-v4-with-deploy-image/test/e2e/e2e_suite_test.go @@ -53,9 +53,24 @@ func TestE2E(t *testing.T) { } var _ = BeforeSuite(func() { - By("building the manager(Operator) image") - cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + By("running go mod tidy") + cmd := exec.Command("go", "mod", "tidy") _, err := utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run go mod tidy") + + By("running generating files") + cmd = exec.Command("make", "generate") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate") + + By("generating manifests") + cmd = exec.Command("make", "manifests") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests") + + By("building the manager(Operator) image") + cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + _, err = utils.Run(cmd) ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image") // TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is diff --git a/testdata/project-v4-with-deploy-image/test/e2e/e2e_test.go b/testdata/project-v4-with-deploy-image/test/e2e/e2e_test.go index 144c8c76d1d..e7a545e0a7c 100644 --- a/testdata/project-v4-with-deploy-image/test/e2e/e2e_test.go +++ b/testdata/project-v4-with-deploy-image/test/e2e/e2e_test.go @@ -31,6 +31,9 @@ const namespace = "project-v4-with-deploy-image-system" // Define a set of end-to-end (e2e) tests to validate the behavior of the controller. var _ = Describe("controller", Ordered, func() { + // controllerPodName stores the name of the controller pod + var controllerPodName string + // Before running the tests, set up the environment by creating the namespace, // installing CRDs, and deploying the controller. BeforeAll(func() { @@ -69,8 +72,6 @@ var _ = Describe("controller", Ordered, func() { // The Context block contains the actual tests that validate the operator's behavior. Context("Operator", func() { It("should run successfully", func() { - var controllerPodName string - By("validating that the controller-manager pod is running as expected") verifyControllerUp := func() error { // Get the name of the controller-manager pod @@ -108,6 +109,29 @@ var _ = Describe("controller", Ordered, func() { EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed()) }) + It("cert-manager should be successfully provisioned", func() { + By("validating that cert-manager has provisioned the certificate Secret") + EventuallyWithOffset(1, func() error { + cmd := exec.Command("kubectl", "get", "secrets", "webhook-server-cert") + _, err := utils.Run(cmd) + return err + }, time.Minute, time.Second).Should(Succeed()) + + By("validating that the mutating|validating webhooks have the CA injected") + verifyCAInjection := func() error { + cmd := exec.Command("kubectl", "get", + "mutatingwebhookconfigurations.admissionregistration.k8s.io", + "project-v4-with-deploy-image-mutating-webhook-configuration", + "-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}") + output, err := utils.Run(cmd) + ExpectWithOffset(2, err).NotTo(HaveOccurred()) + ExpectWithOffset(2, len(output)).To(BeNumerically(">", 10)) + + return nil + } + EventuallyWithOffset(1, verifyCAInjection, time.Minute, time.Second).Should(Succeed()) + }) // +kubebuilder:scaffold:webhookchecks + // TODO(user): Customize the e2e test suite to include // additional scenarios specific to your project. }) diff --git a/testdata/project-v4-with-grafana/test/e2e/e2e_suite_test.go b/testdata/project-v4-with-grafana/test/e2e/e2e_suite_test.go index 7f5df9e4a7e..224a43117c1 100644 --- a/testdata/project-v4-with-grafana/test/e2e/e2e_suite_test.go +++ b/testdata/project-v4-with-grafana/test/e2e/e2e_suite_test.go @@ -53,9 +53,24 @@ func TestE2E(t *testing.T) { } var _ = BeforeSuite(func() { - By("building the manager(Operator) image") - cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + By("running go mod tidy") + cmd := exec.Command("go", "mod", "tidy") _, err := utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run go mod tidy") + + By("running generating files") + cmd = exec.Command("make", "generate") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate") + + By("generating manifests") + cmd = exec.Command("make", "manifests") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests") + + By("building the manager(Operator) image") + cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + _, err = utils.Run(cmd) ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image") // TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is diff --git a/testdata/project-v4-with-grafana/test/e2e/e2e_test.go b/testdata/project-v4-with-grafana/test/e2e/e2e_test.go index a26e626abbe..b1619e3dd65 100644 --- a/testdata/project-v4-with-grafana/test/e2e/e2e_test.go +++ b/testdata/project-v4-with-grafana/test/e2e/e2e_test.go @@ -31,6 +31,9 @@ const namespace = "project-v4-with-grafana-system" // Define a set of end-to-end (e2e) tests to validate the behavior of the controller. var _ = Describe("controller", Ordered, func() { + // controllerPodName stores the name of the controller pod + var controllerPodName string + // Before running the tests, set up the environment by creating the namespace, // installing CRDs, and deploying the controller. BeforeAll(func() { @@ -69,8 +72,6 @@ var _ = Describe("controller", Ordered, func() { // The Context block contains the actual tests that validate the operator's behavior. Context("Operator", func() { It("should run successfully", func() { - var controllerPodName string - By("validating that the controller-manager pod is running as expected") verifyControllerUp := func() error { // Get the name of the controller-manager pod @@ -108,6 +109,8 @@ var _ = Describe("controller", Ordered, func() { EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed()) }) + // +kubebuilder:scaffold:webhookchecks + // TODO(user): Customize the e2e test suite to include // additional scenarios specific to your project. }) diff --git a/testdata/project-v4/test/e2e/e2e_suite_test.go b/testdata/project-v4/test/e2e/e2e_suite_test.go index 4e3fa205918..7caf8920480 100644 --- a/testdata/project-v4/test/e2e/e2e_suite_test.go +++ b/testdata/project-v4/test/e2e/e2e_suite_test.go @@ -53,9 +53,24 @@ func TestE2E(t *testing.T) { } var _ = BeforeSuite(func() { - By("building the manager(Operator) image") - cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + By("running go mod tidy") + cmd := exec.Command("go", "mod", "tidy") _, err := utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run go mod tidy") + + By("running generating files") + cmd = exec.Command("make", "generate") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate") + + By("generating manifests") + cmd = exec.Command("make", "manifests") + _, err = utils.Run(cmd) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests") + + By("building the manager(Operator) image") + cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) + _, err = utils.Run(cmd) ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image") // TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is diff --git a/testdata/project-v4/test/e2e/e2e_test.go b/testdata/project-v4/test/e2e/e2e_test.go index 690c711c470..f796360a775 100644 --- a/testdata/project-v4/test/e2e/e2e_test.go +++ b/testdata/project-v4/test/e2e/e2e_test.go @@ -31,6 +31,9 @@ const namespace = "project-v4-system" // Define a set of end-to-end (e2e) tests to validate the behavior of the controller. var _ = Describe("controller", Ordered, func() { + // controllerPodName stores the name of the controller pod + var controllerPodName string + // Before running the tests, set up the environment by creating the namespace, // installing CRDs, and deploying the controller. BeforeAll(func() { @@ -69,8 +72,6 @@ var _ = Describe("controller", Ordered, func() { // The Context block contains the actual tests that validate the operator's behavior. Context("Operator", func() { It("should run successfully", func() { - var controllerPodName string - By("validating that the controller-manager pod is running as expected") verifyControllerUp := func() error { // Get the name of the controller-manager pod @@ -108,6 +109,29 @@ var _ = Describe("controller", Ordered, func() { EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed()) }) + It("cert-manager should be successfully provisioned", func() { + By("validating that cert-manager has provisioned the certificate Secret") + EventuallyWithOffset(1, func() error { + cmd := exec.Command("kubectl", "get", "secrets", "webhook-server-cert") + _, err := utils.Run(cmd) + return err + }, time.Minute, time.Second).Should(Succeed()) + + By("validating that the mutating|validating webhooks have the CA injected") + verifyCAInjection := func() error { + cmd := exec.Command("kubectl", "get", + "mutatingwebhookconfigurations.admissionregistration.k8s.io", + "project-v4-mutating-webhook-configuration", + "-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}") + output, err := utils.Run(cmd) + ExpectWithOffset(2, err).NotTo(HaveOccurred()) + ExpectWithOffset(2, len(output)).To(BeNumerically(">", 10)) + + return nil + } + EventuallyWithOffset(1, verifyCAInjection, time.Minute, time.Second).Should(Succeed()) + }) // +kubebuilder:scaffold:webhookchecks + // TODO(user): Customize the e2e test suite to include // additional scenarios specific to your project. })