From 77a240fa2a0c29b2ed72262046c718a30ef1ebdb Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Wed, 11 Sep 2024 10:36:17 +0100 Subject: [PATCH] (go/v4): Refactor e2e-tests for clearer error handling and readable logs Refactors all test cases that use g.Expect(utils.Run(cmd)) to improve both the readability of logs and the clarity of the code. Changes: - Replaced occurrences of g.Expect(utils.Run(cmd)) with: ```go output, err := utils.Run(cmd) g.Expect(err).NotTo(HaveOccurred()) g.Expect(string(output)).To() ``` OR ```go _, err := utils.Run(cmd) g.Expect(err).NotTo(HaveOccurred()) ``` **Motivation** - **Human-readable logs:** Output is now converted to a string, providing more understandable logs by displaying actual kubectl command results instead of raw byte arrays. - **Improved code clarity:** The previous `g.Expect(utils.Run(cmd))` usage did not make it clear that the function returns both output and error. By explicitly handling the output and err variables, the code is more transparent and easier to maintain. **Example of problematic scenario** Otherwise, we are unable to check the output when errors are faced. For example Before the changes: ```sh [FAILED] Timed out after 120.001s. The function passed to Eventually failed at /home/runner/work/kubebuilder/kubebuilder/testdata/project-v4-multigroup/test/e2e/e2e_test.go:145 with: Metrics endpoint is not ready Expected <[]uint8 | len:151, cap:1024>: [78, 65, 77, 69, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 69, 78, 68, 80, 79, 73, 78, 84, 83, 32, 32, 32, 65, 71, 69, 10, 112, 114, 111, 106, 101, 99, 116, 45, 118, 52, 45, 109, 117, 108, 116, 105, 103, 114, 111, 117, 112, 45, 99, 111, 110, 116, 114, 111, 108, 108, 101, 114, 45, 109, 97, 110, 97, 103, 101, 114, 45, 109, 101, 116, 114, 105, 99, 115, 45, 115, 101, 114, 118, 105, 99, 101, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 50, 109, 51, 115, 10] to contain substring : 8443 ``` And then, after the changes: ```sh [FAILED] Timed out after 120.001s. The function passed to Eventually failed at /home/runner/work/kubebuilder/kubebuilder/testdata/project-v4-multigroup/test/e2e/e2e_test.go:145 with: Metrics endpoint is not ready Expected : NAME ENDPOINTS AGE project-v4-multigroup-controller-manager-metrics-service 2m3s to contain substring : 8443 In [It] at: /home/runner/work/kubebuilder/kubebuilder/testdata/project-v4-multigroup/test/e2e/e2e_test.go:147 @ 09/11/ ``` --- .../testdata/project/test/e2e/e2e_test.go | 28 +++++++++++++------ .../testdata/project/test/e2e/e2e_test.go | 28 +++++++++++++------ .../testdata/project/test/e2e/e2e_test.go | 28 +++++++++++++------ .../internal/templates/test/e2e/test.go | 28 +++++++++++++------ .../test/e2e/e2e_test.go | 28 +++++++++++++------ .../test/e2e/e2e_test.go | 28 +++++++++++++------ testdata/project-v4/test/e2e/e2e_test.go | 28 +++++++++++++------ 7 files changed, 140 insertions(+), 56 deletions(-) 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 d006bec43aa..b82c7637d3d 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 @@ -110,7 +110,9 @@ var _ = Describe("Manager", Ordered, func() { "pods", controllerPodName, "-o", "jsonpath={.status.phase}", "-n", namespace, ) - g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") } // Repeatedly check if the controller-manager pod is running until it succeeds or times out. Eventually(verifyControllerUp).Should(Succeed()) @@ -122,15 +124,18 @@ var _ = Describe("Manager", Ordered, func() { "--clusterrole=project-metrics-reader", fmt.Sprintf("--serviceaccount=%s:%s", namespace, serviceAccountName), ) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding") + _, err := utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding") By("validating that the metrics service is available") cmd = exec.Command("kubectl", "get", "service", metricsServiceName, "-n", namespace) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Metrics service should exist") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Metrics service should exist") By("validating that the ServiceMonitor for Prometheus is applied in the namespace") cmd = exec.Command("kubectl", "get", "ServiceMonitor", "-n", namespace) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist") By("getting the service account token") token, err := serviceAccountToken() @@ -140,14 +145,18 @@ var _ = Describe("Manager", Ordered, func() { By("waiting for the metrics endpoint to be ready") verifyMetricsEndpointReady := func(g Gomega) { cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace) - g.Expect(utils.Run(cmd)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") } Eventually(verifyMetricsEndpointReady).Should(Succeed()) By("verifying that the controller manager is serving the metrics server") verifyMetricsServerStarted := func(g Gomega) { cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace) - g.Expect(utils.Run(cmd)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), "Metrics server not yet started") } Eventually(verifyMetricsServerStarted).Should(Succeed()) @@ -159,14 +168,17 @@ var _ = Describe("Manager", Ordered, func() { "--", "/bin/sh", "-c", fmt.Sprintf( "curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics", token, metricsServiceName, namespace)) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod") By("waiting for the curl-metrics pod to complete.") verifyCurlUp := func(g Gomega) { cmd := exec.Command("kubectl", "get", "pods", "curl-metrics", "-o", "jsonpath={.status.phase}", "-n", namespace) - g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status") } Eventually(verifyCurlUp, 5*time.Minute).Should(Succeed()) 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 cc26cf65037..2387c6e3587 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 @@ -110,7 +110,9 @@ var _ = Describe("Manager", Ordered, func() { "pods", controllerPodName, "-o", "jsonpath={.status.phase}", "-n", namespace, ) - g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") } // Repeatedly check if the controller-manager pod is running until it succeeds or times out. Eventually(verifyControllerUp).Should(Succeed()) @@ -122,15 +124,18 @@ var _ = Describe("Manager", Ordered, func() { "--clusterrole=project-metrics-reader", fmt.Sprintf("--serviceaccount=%s:%s", namespace, serviceAccountName), ) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding") + _, err := utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding") By("validating that the metrics service is available") cmd = exec.Command("kubectl", "get", "service", metricsServiceName, "-n", namespace) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Metrics service should exist") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Metrics service should exist") By("validating that the ServiceMonitor for Prometheus is applied in the namespace") cmd = exec.Command("kubectl", "get", "ServiceMonitor", "-n", namespace) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist") By("getting the service account token") token, err := serviceAccountToken() @@ -140,14 +145,18 @@ var _ = Describe("Manager", Ordered, func() { By("waiting for the metrics endpoint to be ready") verifyMetricsEndpointReady := func(g Gomega) { cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace) - g.Expect(utils.Run(cmd)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") } Eventually(verifyMetricsEndpointReady).Should(Succeed()) By("verifying that the controller manager is serving the metrics server") verifyMetricsServerStarted := func(g Gomega) { cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace) - g.Expect(utils.Run(cmd)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), "Metrics server not yet started") } Eventually(verifyMetricsServerStarted).Should(Succeed()) @@ -159,14 +168,17 @@ var _ = Describe("Manager", Ordered, func() { "--", "/bin/sh", "-c", fmt.Sprintf( "curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics", token, metricsServiceName, namespace)) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod") By("waiting for the curl-metrics pod to complete.") verifyCurlUp := func(g Gomega) { cmd := exec.Command("kubectl", "get", "pods", "curl-metrics", "-o", "jsonpath={.status.phase}", "-n", namespace) - g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status") } Eventually(verifyCurlUp, 5*time.Minute).Should(Succeed()) diff --git a/docs/book/src/multiversion-tutorial/testdata/project/test/e2e/e2e_test.go b/docs/book/src/multiversion-tutorial/testdata/project/test/e2e/e2e_test.go index d006bec43aa..b82c7637d3d 100644 --- a/docs/book/src/multiversion-tutorial/testdata/project/test/e2e/e2e_test.go +++ b/docs/book/src/multiversion-tutorial/testdata/project/test/e2e/e2e_test.go @@ -110,7 +110,9 @@ var _ = Describe("Manager", Ordered, func() { "pods", controllerPodName, "-o", "jsonpath={.status.phase}", "-n", namespace, ) - g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") } // Repeatedly check if the controller-manager pod is running until it succeeds or times out. Eventually(verifyControllerUp).Should(Succeed()) @@ -122,15 +124,18 @@ var _ = Describe("Manager", Ordered, func() { "--clusterrole=project-metrics-reader", fmt.Sprintf("--serviceaccount=%s:%s", namespace, serviceAccountName), ) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding") + _, err := utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding") By("validating that the metrics service is available") cmd = exec.Command("kubectl", "get", "service", metricsServiceName, "-n", namespace) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Metrics service should exist") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Metrics service should exist") By("validating that the ServiceMonitor for Prometheus is applied in the namespace") cmd = exec.Command("kubectl", "get", "ServiceMonitor", "-n", namespace) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist") By("getting the service account token") token, err := serviceAccountToken() @@ -140,14 +145,18 @@ var _ = Describe("Manager", Ordered, func() { By("waiting for the metrics endpoint to be ready") verifyMetricsEndpointReady := func(g Gomega) { cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace) - g.Expect(utils.Run(cmd)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") } Eventually(verifyMetricsEndpointReady).Should(Succeed()) By("verifying that the controller manager is serving the metrics server") verifyMetricsServerStarted := func(g Gomega) { cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace) - g.Expect(utils.Run(cmd)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), "Metrics server not yet started") } Eventually(verifyMetricsServerStarted).Should(Succeed()) @@ -159,14 +168,17 @@ var _ = Describe("Manager", Ordered, func() { "--", "/bin/sh", "-c", fmt.Sprintf( "curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics", token, metricsServiceName, namespace)) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod") By("waiting for the curl-metrics pod to complete.") verifyCurlUp := func(g Gomega) { cmd := exec.Command("kubectl", "get", "pods", "curl-metrics", "-o", "jsonpath={.status.phase}", "-n", namespace) - g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status") } Eventually(verifyCurlUp, 5*time.Minute).Should(Succeed()) 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 daad17105f1..0044dc3fb75 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 @@ -243,7 +243,9 @@ var _ = Describe("Manager", Ordered, func() { "pods", controllerPodName, "-o", "jsonpath={.status.phase}", "-n", namespace, ) - g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") } // Repeatedly check if the controller-manager pod is running until it succeeds or times out. Eventually(verifyControllerUp).Should(Succeed()) @@ -255,15 +257,18 @@ var _ = Describe("Manager", Ordered, func() { "--clusterrole={{ .ProjectName}}-metrics-reader", fmt.Sprintf("--serviceaccount=%s:%s", namespace, serviceAccountName), ) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding") + _, err := utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding") By("validating that the metrics service is available") cmd = exec.Command("kubectl", "get", "service", metricsServiceName, "-n", namespace) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Metrics service should exist") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Metrics service should exist") By("validating that the ServiceMonitor for Prometheus is applied in the namespace") cmd = exec.Command("kubectl", "get", "ServiceMonitor", "-n", namespace) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist") By("getting the service account token") token, err := serviceAccountToken() @@ -273,14 +278,18 @@ var _ = Describe("Manager", Ordered, func() { By("waiting for the metrics endpoint to be ready") verifyMetricsEndpointReady := func(g Gomega) { cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace) - g.Expect(utils.Run(cmd)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") } Eventually(verifyMetricsEndpointReady).Should(Succeed()) By("verifying that the controller manager is serving the metrics server") verifyMetricsServerStarted := func(g Gomega) { cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace) - g.Expect(utils.Run(cmd)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), "Metrics server not yet started") } Eventually(verifyMetricsServerStarted).Should(Succeed()) @@ -292,14 +301,17 @@ var _ = Describe("Manager", Ordered, func() { "--", "/bin/sh", "-c", fmt.Sprintf( "curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics", token, metricsServiceName, namespace)) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod") By("waiting for the curl-metrics pod to complete.") verifyCurlUp := func(g Gomega) { cmd := exec.Command("kubectl", "get", "pods", "curl-metrics", "-o", "jsonpath={.status.phase}", "-n", namespace) - g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status") } Eventually(verifyCurlUp, 5 * time.Minute).Should(Succeed()) diff --git a/testdata/project-v4-multigroup-with-plugins/test/e2e/e2e_test.go b/testdata/project-v4-multigroup-with-plugins/test/e2e/e2e_test.go index 7daa08340f5..a21d945e919 100644 --- a/testdata/project-v4-multigroup-with-plugins/test/e2e/e2e_test.go +++ b/testdata/project-v4-multigroup-with-plugins/test/e2e/e2e_test.go @@ -110,7 +110,9 @@ var _ = Describe("Manager", Ordered, func() { "pods", controllerPodName, "-o", "jsonpath={.status.phase}", "-n", namespace, ) - g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") } // Repeatedly check if the controller-manager pod is running until it succeeds or times out. Eventually(verifyControllerUp).Should(Succeed()) @@ -122,15 +124,18 @@ var _ = Describe("Manager", Ordered, func() { "--clusterrole=project-v4-multigroup-with-plugins-metrics-reader", fmt.Sprintf("--serviceaccount=%s:%s", namespace, serviceAccountName), ) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding") + _, err := utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding") By("validating that the metrics service is available") cmd = exec.Command("kubectl", "get", "service", metricsServiceName, "-n", namespace) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Metrics service should exist") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Metrics service should exist") By("validating that the ServiceMonitor for Prometheus is applied in the namespace") cmd = exec.Command("kubectl", "get", "ServiceMonitor", "-n", namespace) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist") By("getting the service account token") token, err := serviceAccountToken() @@ -140,14 +145,18 @@ var _ = Describe("Manager", Ordered, func() { By("waiting for the metrics endpoint to be ready") verifyMetricsEndpointReady := func(g Gomega) { cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace) - g.Expect(utils.Run(cmd)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") } Eventually(verifyMetricsEndpointReady).Should(Succeed()) By("verifying that the controller manager is serving the metrics server") verifyMetricsServerStarted := func(g Gomega) { cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace) - g.Expect(utils.Run(cmd)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), "Metrics server not yet started") } Eventually(verifyMetricsServerStarted).Should(Succeed()) @@ -159,14 +168,17 @@ var _ = Describe("Manager", Ordered, func() { "--", "/bin/sh", "-c", fmt.Sprintf( "curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics", token, metricsServiceName, namespace)) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod") By("waiting for the curl-metrics pod to complete.") verifyCurlUp := func(g Gomega) { cmd := exec.Command("kubectl", "get", "pods", "curl-metrics", "-o", "jsonpath={.status.phase}", "-n", namespace) - g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status") } Eventually(verifyCurlUp, 5*time.Minute).Should(Succeed()) diff --git a/testdata/project-v4-with-plugins/test/e2e/e2e_test.go b/testdata/project-v4-with-plugins/test/e2e/e2e_test.go index 6687582890d..6b433a19bb0 100644 --- a/testdata/project-v4-with-plugins/test/e2e/e2e_test.go +++ b/testdata/project-v4-with-plugins/test/e2e/e2e_test.go @@ -110,7 +110,9 @@ var _ = Describe("Manager", Ordered, func() { "pods", controllerPodName, "-o", "jsonpath={.status.phase}", "-n", namespace, ) - g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") } // Repeatedly check if the controller-manager pod is running until it succeeds or times out. Eventually(verifyControllerUp).Should(Succeed()) @@ -122,15 +124,18 @@ var _ = Describe("Manager", Ordered, func() { "--clusterrole=project-v4-with-plugins-metrics-reader", fmt.Sprintf("--serviceaccount=%s:%s", namespace, serviceAccountName), ) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding") + _, err := utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding") By("validating that the metrics service is available") cmd = exec.Command("kubectl", "get", "service", metricsServiceName, "-n", namespace) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Metrics service should exist") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Metrics service should exist") By("validating that the ServiceMonitor for Prometheus is applied in the namespace") cmd = exec.Command("kubectl", "get", "ServiceMonitor", "-n", namespace) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist") By("getting the service account token") token, err := serviceAccountToken() @@ -140,14 +145,18 @@ var _ = Describe("Manager", Ordered, func() { By("waiting for the metrics endpoint to be ready") verifyMetricsEndpointReady := func(g Gomega) { cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace) - g.Expect(utils.Run(cmd)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") } Eventually(verifyMetricsEndpointReady).Should(Succeed()) By("verifying that the controller manager is serving the metrics server") verifyMetricsServerStarted := func(g Gomega) { cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace) - g.Expect(utils.Run(cmd)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), "Metrics server not yet started") } Eventually(verifyMetricsServerStarted).Should(Succeed()) @@ -159,14 +168,17 @@ var _ = Describe("Manager", Ordered, func() { "--", "/bin/sh", "-c", fmt.Sprintf( "curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics", token, metricsServiceName, namespace)) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod") By("waiting for the curl-metrics pod to complete.") verifyCurlUp := func(g Gomega) { cmd := exec.Command("kubectl", "get", "pods", "curl-metrics", "-o", "jsonpath={.status.phase}", "-n", namespace) - g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status") } Eventually(verifyCurlUp, 5*time.Minute).Should(Succeed()) diff --git a/testdata/project-v4/test/e2e/e2e_test.go b/testdata/project-v4/test/e2e/e2e_test.go index 9a2bef28adc..93f1424cafe 100644 --- a/testdata/project-v4/test/e2e/e2e_test.go +++ b/testdata/project-v4/test/e2e/e2e_test.go @@ -110,7 +110,9 @@ var _ = Describe("Manager", Ordered, func() { "pods", controllerPodName, "-o", "jsonpath={.status.phase}", "-n", namespace, ) - g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") } // Repeatedly check if the controller-manager pod is running until it succeeds or times out. Eventually(verifyControllerUp).Should(Succeed()) @@ -122,15 +124,18 @@ var _ = Describe("Manager", Ordered, func() { "--clusterrole=project-v4-metrics-reader", fmt.Sprintf("--serviceaccount=%s:%s", namespace, serviceAccountName), ) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding") + _, err := utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding") By("validating that the metrics service is available") cmd = exec.Command("kubectl", "get", "service", metricsServiceName, "-n", namespace) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Metrics service should exist") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Metrics service should exist") By("validating that the ServiceMonitor for Prometheus is applied in the namespace") cmd = exec.Command("kubectl", "get", "ServiceMonitor", "-n", namespace) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist") By("getting the service account token") token, err := serviceAccountToken() @@ -140,14 +145,18 @@ var _ = Describe("Manager", Ordered, func() { By("waiting for the metrics endpoint to be ready") verifyMetricsEndpointReady := func(g Gomega) { cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace) - g.Expect(utils.Run(cmd)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") } Eventually(verifyMetricsEndpointReady).Should(Succeed()) By("verifying that the controller manager is serving the metrics server") verifyMetricsServerStarted := func(g Gomega) { cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace) - g.Expect(utils.Run(cmd)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), "Metrics server not yet started") } Eventually(verifyMetricsServerStarted).Should(Succeed()) @@ -159,14 +168,17 @@ var _ = Describe("Manager", Ordered, func() { "--", "/bin/sh", "-c", fmt.Sprintf( "curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics", token, metricsServiceName, namespace)) - Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod") + _, err = utils.Run(cmd) + Expect(err).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod") By("waiting for the curl-metrics pod to complete.") verifyCurlUp := func(g Gomega) { cmd := exec.Command("kubectl", "get", "pods", "curl-metrics", "-o", "jsonpath={.status.phase}", "-n", namespace) - g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status") + output, err := utils.Run(cmd) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(output)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status") } Eventually(verifyCurlUp, 5*time.Minute).Should(Succeed())