From a6977f9598e72e287adae8f6a45bc9f6f23a1f1c Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Mon, 16 Sep 2024 06:49:35 +0100 Subject: [PATCH] test --- .../test/e2e/e2e_test.go | 34 ++++++++----------- .../project-v4-multigroup/test/utils/utils.go | 6 ++-- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/testdata/project-v4-multigroup/test/e2e/e2e_test.go b/testdata/project-v4-multigroup/test/e2e/e2e_test.go index 743ca527a79..2df9d6ed690 100644 --- a/testdata/project-v4-multigroup/test/e2e/e2e_test.go +++ b/testdata/project-v4-multigroup/test/e2e/e2e_test.go @@ -44,7 +44,7 @@ const metricsRoleBindingName = "project-v4-multigroup-metrics-binding" var _ = Describe("Manager", Ordered, func() { var controllerPodName string - + // Before running the tests, set up the environment by creating the namespace, // installing CRDs, and deploying the controller. BeforeAll(func() { @@ -91,27 +91,27 @@ var _ = Describe("Manager", Ordered, func() { cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace) controllerLogs, err := utils.Run(cmd) if err == nil { - fmt.Println("Controller logs:\n", controllerLogs) + _, _ = fmt.Fprintf(GinkgoWriter, fmt.Sprintf("Controller logs:\n %s", controllerLogs)) } else { - fmt.Println("Failed to get controller logs") + _, _ = fmt.Fprintf(GinkgoWriter, fmt.Sprintf("Failed to get Controller logs: %s", err)) } By("Fetching Kubernetes events") cmd = exec.Command("kubectl", "get", "events", "-n", namespace, "--sort-by=.lastTimestamp") eventsOutput, err := utils.Run(cmd) if err == nil { - fmt.Println("Kubernetes events:\n", eventsOutput) + _, _ = fmt.Fprintf(GinkgoWriter, fmt.Sprintf("Kubernetes events:\n%s", eventsOutput)) } else { - fmt.Println("Failed to get Kubernetes events") + _, _ = fmt.Fprintf(GinkgoWriter, fmt.Sprintf("Failed to get Kubernetes events: %s", err)) } By("Fetching curl-metrics logs") cmd = exec.Command("kubectl", "logs", "curl-metrics", "-n", namespace) metricsOutput, err := utils.Run(cmd) if err == nil { - fmt.Println("Metrics logs:\n", metricsOutput) + _, _ = fmt.Fprintf(GinkgoWriter, fmt.Sprintf("Metrics logs:\n %s", metricsOutput)) } else { - fmt.Println("Failed to get curl-metrics logs") + _, _ = fmt.Fprintf(GinkgoWriter, fmt.Sprintf("Failed to get curl-metrics logs: %s", err)) } By("Fetching controller manager pod description") @@ -144,7 +144,7 @@ var _ = Describe("Manager", Ordered, func() { podOutput, err := utils.Run(cmd) g.Expect(err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod information") - podNames := utils.GetNonEmptyLines(string(podOutput)) + podNames := utils.GetNonEmptyLines(podOutput) g.Expect(podNames).To(HaveLen(1), "expected 1 controller pod running") controllerPodName = podNames[0] g.Expect(controllerPodName).To(ContainSubstring("controller-manager")) @@ -155,7 +155,7 @@ var _ = Describe("Manager", Ordered, func() { ) output, err := utils.Run(cmd) g.Expect(err).NotTo(HaveOccurred()) - g.Expect(string(output)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") + g.Expect(output).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status") } Eventually(verifyControllerUp).Should(Succeed()) }) @@ -189,7 +189,7 @@ var _ = Describe("Manager", Ordered, func() { cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace) output, err := utils.Run(cmd) g.Expect(err).NotTo(HaveOccurred()) - g.Expect(string(output)).To(ContainSubstring("8443"), "Metrics endpoint is not ready") + g.Expect(output).To(ContainSubstring("8443"), "Metrics endpoint is not ready") } Eventually(verifyMetricsEndpointReady).Should(Succeed()) @@ -198,7 +198,7 @@ var _ = Describe("Manager", Ordered, func() { cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace) output, err := utils.Run(cmd) g.Expect(err).NotTo(HaveOccurred()) - g.Expect(string(output)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), + g.Expect(output).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"), "Metrics server not yet started") } Eventually(verifyMetricsServerStarted).Should(Succeed()) @@ -220,7 +220,7 @@ var _ = Describe("Manager", Ordered, func() { "-n", namespace) output, err := utils.Run(cmd) g.Expect(err).NotTo(HaveOccurred()) - g.Expect(string(output)).To(Equal("Succeeded"), "curl pod in wrong status") + g.Expect(output).To(Equal("Succeeded"), "curl pod in wrong status") } Eventually(verifyCurlUp, 5*time.Minute).Should(Succeed()) @@ -300,7 +300,6 @@ func serviceAccountToken() (string, error) { } var out string - var rawJson string verifyTokenCreation := func(g Gomega) { // Execute kubectl command to create the token cmd := exec.Command("kubectl", "create", "--raw", fmt.Sprintf( @@ -312,11 +311,9 @@ func serviceAccountToken() (string, error) { output, err := cmd.CombinedOutput() g.Expect(err).NotTo(HaveOccurred()) - rawJson = string(output) - // Parse the JSON output to extract the token var token tokenRequest - err = json.Unmarshal([]byte(rawJson), &token) + err = json.Unmarshal([]byte(output), &token) g.Expect(err).NotTo(HaveOccurred()) out = token.Status.Token @@ -332,9 +329,8 @@ func getMetricsOutput() string { cmd := exec.Command("kubectl", "logs", "curl-metrics", "-n", namespace) metricsOutput, err := utils.Run(cmd) Expect(err).NotTo(HaveOccurred(), "Failed to retrieve logs from curl pod") - metricsOutputStr := string(metricsOutput) - Expect(metricsOutputStr).To(ContainSubstring("< HTTP/1.1 200 OK")) - return metricsOutputStr + Expect(metricsOutput).To(ContainSubstring("< HTTP/1.1 200 OK")) + return metricsOutput } // tokenRequest is a simplified representation of the Kubernetes TokenRequest API response, diff --git a/testdata/project-v4-multigroup/test/utils/utils.go b/testdata/project-v4-multigroup/test/utils/utils.go index db4ad3f02f6..16a6383783a 100644 --- a/testdata/project-v4-multigroup/test/utils/utils.go +++ b/testdata/project-v4-multigroup/test/utils/utils.go @@ -41,7 +41,7 @@ func warnError(err error) { } // Run executes the provided command within this context -func Run(cmd *exec.Cmd) ([]byte, error) { +func Run(cmd *exec.Cmd) (string, error) { dir, _ := GetProjectDir() cmd.Dir = dir @@ -54,10 +54,10 @@ func Run(cmd *exec.Cmd) ([]byte, error) { _, _ = fmt.Fprintf(GinkgoWriter, "running: %s\n", command) output, err := cmd.CombinedOutput() if err != nil { - return output, fmt.Errorf("%s failed with error: (%v) %s", command, err, string(output)) + return string(output), fmt.Errorf("%s failed with error: (%v) %s", command, err, string(output)) } - return output, nil + return string(output), nil } // InstallPrometheusOperator installs the prometheus Operator to be used to export the enabled metrics.