Skip to content

Commit bb94cdd

Browse files
(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(<condition>) ``` 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 <string>: 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 <string>: NAME ENDPOINTS AGE project-v4-multigroup-controller-manager-metrics-service 2m3s to contain substring <string>: 8443 In [It] at: /home/runner/work/kubebuilder/kubebuilder/testdata/project-v4-multigroup/test/e2e/e2e_test.go:147 @ 09/11/ ```
1 parent d26664f commit bb94cdd

File tree

7 files changed

+140
-56
lines changed
  • docs/book/src
  • pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e
  • testdata

7 files changed

+140
-56
lines changed

docs/book/src/cronjob-tutorial/testdata/project/test/e2e/e2e_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ var _ = Describe("Manager", Ordered, func() {
110110
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
111111
"-n", namespace,
112112
)
113-
g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status")
113+
output, err := utils.Run(cmd)
114+
g.Expect(err).NotTo(HaveOccurred())
115+
g.Expect(string(output)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status")
114116
}
115117
// Repeatedly check if the controller-manager pod is running until it succeeds or times out.
116118
Eventually(verifyControllerUp).Should(Succeed())
@@ -122,15 +124,18 @@ var _ = Describe("Manager", Ordered, func() {
122124
"--clusterrole=project-metrics-reader",
123125
fmt.Sprintf("--serviceaccount=%s:%s", namespace, serviceAccountName),
124126
)
125-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding")
127+
_, err := utils.Run(cmd)
128+
Expect(err).NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding")
126129

127130
By("validating that the metrics service is available")
128131
cmd = exec.Command("kubectl", "get", "service", metricsServiceName, "-n", namespace)
129-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Metrics service should exist")
132+
_, err = utils.Run(cmd)
133+
Expect(err).NotTo(HaveOccurred(), "Metrics service should exist")
130134

131135
By("validating that the ServiceMonitor for Prometheus is applied in the namespace")
132136
cmd = exec.Command("kubectl", "get", "ServiceMonitor", "-n", namespace)
133-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist")
137+
_, err = utils.Run(cmd)
138+
Expect(err).NotTo(HaveOccurred(), "ServiceMonitor should exist")
134139

135140
By("getting the service account token")
136141
token, err := serviceAccountToken()
@@ -140,14 +145,18 @@ var _ = Describe("Manager", Ordered, func() {
140145
By("waiting for the metrics endpoint to be ready")
141146
verifyMetricsEndpointReady := func(g Gomega) {
142147
cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace)
143-
g.Expect(utils.Run(cmd)).To(ContainSubstring("8443"), "Metrics endpoint is not ready")
148+
output, err := utils.Run(cmd)
149+
g.Expect(err).NotTo(HaveOccurred())
150+
g.Expect(string(output)).To(ContainSubstring("8443"), "Metrics endpoint is not ready")
144151
}
145152
Eventually(verifyMetricsEndpointReady).Should(Succeed())
146153

147154
By("verifying that the controller manager is serving the metrics server")
148155
verifyMetricsServerStarted := func(g Gomega) {
149156
cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace)
150-
g.Expect(utils.Run(cmd)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"),
157+
output, err := utils.Run(cmd)
158+
g.Expect(err).NotTo(HaveOccurred())
159+
g.Expect(string(output)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"),
151160
"Metrics server not yet started")
152161
}
153162
Eventually(verifyMetricsServerStarted).Should(Succeed())
@@ -159,14 +168,17 @@ var _ = Describe("Manager", Ordered, func() {
159168
"--", "/bin/sh", "-c", fmt.Sprintf(
160169
"curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics",
161170
token, metricsServiceName, namespace))
162-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod")
171+
_, err = utils.Run(cmd)
172+
Expect(err).NotTo(HaveOccurred(), "Failed to create curl-metrics pod")
163173

164174
By("waiting for the curl-metrics pod to complete.")
165175
verifyCurlUp := func(g Gomega) {
166176
cmd := exec.Command("kubectl", "get", "pods", "curl-metrics",
167177
"-o", "jsonpath={.status.phase}",
168178
"-n", namespace)
169-
g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status")
179+
output, err := utils.Run(cmd)
180+
g.Expect(err).NotTo(HaveOccurred())
181+
g.Expect(string(output)).To(Equal("Succeeded"), "curl pod in wrong status")
170182
}
171183
Eventually(verifyCurlUp, 5*time.Minute).Should(Succeed())
172184

docs/book/src/getting-started/testdata/project/test/e2e/e2e_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ var _ = Describe("Manager", Ordered, func() {
110110
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
111111
"-n", namespace,
112112
)
113-
g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status")
113+
output, err := utils.Run(cmd)
114+
g.Expect(err).NotTo(HaveOccurred())
115+
g.Expect(string(output)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status")
114116
}
115117
// Repeatedly check if the controller-manager pod is running until it succeeds or times out.
116118
Eventually(verifyControllerUp).Should(Succeed())
@@ -122,15 +124,18 @@ var _ = Describe("Manager", Ordered, func() {
122124
"--clusterrole=project-metrics-reader",
123125
fmt.Sprintf("--serviceaccount=%s:%s", namespace, serviceAccountName),
124126
)
125-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding")
127+
_, err := utils.Run(cmd)
128+
Expect(err).NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding")
126129

127130
By("validating that the metrics service is available")
128131
cmd = exec.Command("kubectl", "get", "service", metricsServiceName, "-n", namespace)
129-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Metrics service should exist")
132+
_, err = utils.Run(cmd)
133+
Expect(err).NotTo(HaveOccurred(), "Metrics service should exist")
130134

131135
By("validating that the ServiceMonitor for Prometheus is applied in the namespace")
132136
cmd = exec.Command("kubectl", "get", "ServiceMonitor", "-n", namespace)
133-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist")
137+
_, err = utils.Run(cmd)
138+
Expect(err).NotTo(HaveOccurred(), "ServiceMonitor should exist")
134139

135140
By("getting the service account token")
136141
token, err := serviceAccountToken()
@@ -140,14 +145,18 @@ var _ = Describe("Manager", Ordered, func() {
140145
By("waiting for the metrics endpoint to be ready")
141146
verifyMetricsEndpointReady := func(g Gomega) {
142147
cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace)
143-
g.Expect(utils.Run(cmd)).To(ContainSubstring("8443"), "Metrics endpoint is not ready")
148+
output, err := utils.Run(cmd)
149+
g.Expect(err).NotTo(HaveOccurred())
150+
g.Expect(string(output)).To(ContainSubstring("8443"), "Metrics endpoint is not ready")
144151
}
145152
Eventually(verifyMetricsEndpointReady).Should(Succeed())
146153

147154
By("verifying that the controller manager is serving the metrics server")
148155
verifyMetricsServerStarted := func(g Gomega) {
149156
cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace)
150-
g.Expect(utils.Run(cmd)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"),
157+
output, err := utils.Run(cmd)
158+
g.Expect(err).NotTo(HaveOccurred())
159+
g.Expect(string(output)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"),
151160
"Metrics server not yet started")
152161
}
153162
Eventually(verifyMetricsServerStarted).Should(Succeed())
@@ -159,14 +168,17 @@ var _ = Describe("Manager", Ordered, func() {
159168
"--", "/bin/sh", "-c", fmt.Sprintf(
160169
"curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics",
161170
token, metricsServiceName, namespace))
162-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod")
171+
_, err = utils.Run(cmd)
172+
Expect(err).NotTo(HaveOccurred(), "Failed to create curl-metrics pod")
163173

164174
By("waiting for the curl-metrics pod to complete.")
165175
verifyCurlUp := func(g Gomega) {
166176
cmd := exec.Command("kubectl", "get", "pods", "curl-metrics",
167177
"-o", "jsonpath={.status.phase}",
168178
"-n", namespace)
169-
g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status")
179+
output, err := utils.Run(cmd)
180+
g.Expect(err).NotTo(HaveOccurred())
181+
g.Expect(string(output)).To(Equal("Succeeded"), "curl pod in wrong status")
170182
}
171183
Eventually(verifyCurlUp, 5*time.Minute).Should(Succeed())
172184

docs/book/src/multiversion-tutorial/testdata/project/test/e2e/e2e_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ var _ = Describe("Manager", Ordered, func() {
110110
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
111111
"-n", namespace,
112112
)
113-
g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status")
113+
output, err := utils.Run(cmd)
114+
g.Expect(err).NotTo(HaveOccurred())
115+
g.Expect(string(output)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status")
114116
}
115117
// Repeatedly check if the controller-manager pod is running until it succeeds or times out.
116118
Eventually(verifyControllerUp).Should(Succeed())
@@ -122,15 +124,18 @@ var _ = Describe("Manager", Ordered, func() {
122124
"--clusterrole=project-metrics-reader",
123125
fmt.Sprintf("--serviceaccount=%s:%s", namespace, serviceAccountName),
124126
)
125-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding")
127+
_, err := utils.Run(cmd)
128+
Expect(err).NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding")
126129

127130
By("validating that the metrics service is available")
128131
cmd = exec.Command("kubectl", "get", "service", metricsServiceName, "-n", namespace)
129-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Metrics service should exist")
132+
_, err = utils.Run(cmd)
133+
Expect(err).NotTo(HaveOccurred(), "Metrics service should exist")
130134

131135
By("validating that the ServiceMonitor for Prometheus is applied in the namespace")
132136
cmd = exec.Command("kubectl", "get", "ServiceMonitor", "-n", namespace)
133-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist")
137+
_, err = utils.Run(cmd)
138+
Expect(err).NotTo(HaveOccurred(), "ServiceMonitor should exist")
134139

135140
By("getting the service account token")
136141
token, err := serviceAccountToken()
@@ -140,14 +145,18 @@ var _ = Describe("Manager", Ordered, func() {
140145
By("waiting for the metrics endpoint to be ready")
141146
verifyMetricsEndpointReady := func(g Gomega) {
142147
cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace)
143-
g.Expect(utils.Run(cmd)).To(ContainSubstring("8443"), "Metrics endpoint is not ready")
148+
output, err := utils.Run(cmd)
149+
g.Expect(err).NotTo(HaveOccurred())
150+
g.Expect(string(output)).To(ContainSubstring("8443"), "Metrics endpoint is not ready")
144151
}
145152
Eventually(verifyMetricsEndpointReady).Should(Succeed())
146153

147154
By("verifying that the controller manager is serving the metrics server")
148155
verifyMetricsServerStarted := func(g Gomega) {
149156
cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace)
150-
g.Expect(utils.Run(cmd)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"),
157+
output, err := utils.Run(cmd)
158+
g.Expect(err).NotTo(HaveOccurred())
159+
g.Expect(string(output)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"),
151160
"Metrics server not yet started")
152161
}
153162
Eventually(verifyMetricsServerStarted).Should(Succeed())
@@ -159,14 +168,17 @@ var _ = Describe("Manager", Ordered, func() {
159168
"--", "/bin/sh", "-c", fmt.Sprintf(
160169
"curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics",
161170
token, metricsServiceName, namespace))
162-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod")
171+
_, err = utils.Run(cmd)
172+
Expect(err).NotTo(HaveOccurred(), "Failed to create curl-metrics pod")
163173

164174
By("waiting for the curl-metrics pod to complete.")
165175
verifyCurlUp := func(g Gomega) {
166176
cmd := exec.Command("kubectl", "get", "pods", "curl-metrics",
167177
"-o", "jsonpath={.status.phase}",
168178
"-n", namespace)
169-
g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status")
179+
output, err := utils.Run(cmd)
180+
g.Expect(err).NotTo(HaveOccurred())
181+
g.Expect(string(output)).To(Equal("Succeeded"), "curl pod in wrong status")
170182
}
171183
Eventually(verifyCurlUp, 5*time.Minute).Should(Succeed())
172184

pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ var _ = Describe("Manager", Ordered, func() {
243243
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
244244
"-n", namespace,
245245
)
246-
g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status")
246+
output, err := utils.Run(cmd)
247+
g.Expect(err).NotTo(HaveOccurred())
248+
g.Expect(string(output)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status")
247249
}
248250
// Repeatedly check if the controller-manager pod is running until it succeeds or times out.
249251
Eventually(verifyControllerUp).Should(Succeed())
@@ -255,15 +257,18 @@ var _ = Describe("Manager", Ordered, func() {
255257
"--clusterrole={{ .ProjectName}}-metrics-reader",
256258
fmt.Sprintf("--serviceaccount=%s:%s", namespace, serviceAccountName),
257259
)
258-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding")
260+
_, err := utils.Run(cmd)
261+
Expect(err).NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding")
259262
260263
By("validating that the metrics service is available")
261264
cmd = exec.Command("kubectl", "get", "service", metricsServiceName, "-n", namespace)
262-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Metrics service should exist")
265+
_, err = utils.Run(cmd)
266+
Expect(err).NotTo(HaveOccurred(), "Metrics service should exist")
263267
264268
By("validating that the ServiceMonitor for Prometheus is applied in the namespace")
265269
cmd = exec.Command("kubectl", "get", "ServiceMonitor", "-n", namespace)
266-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist")
270+
_, err = utils.Run(cmd)
271+
Expect(err).NotTo(HaveOccurred(), "ServiceMonitor should exist")
267272
268273
By("getting the service account token")
269274
token, err := serviceAccountToken()
@@ -273,14 +278,18 @@ var _ = Describe("Manager", Ordered, func() {
273278
By("waiting for the metrics endpoint to be ready")
274279
verifyMetricsEndpointReady := func(g Gomega) {
275280
cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace)
276-
g.Expect(utils.Run(cmd)).To(ContainSubstring("8443"), "Metrics endpoint is not ready")
281+
output, err := utils.Run(cmd)
282+
g.Expect(err).NotTo(HaveOccurred())
283+
g.Expect(string(output)).To(ContainSubstring("8443"), "Metrics endpoint is not ready")
277284
}
278285
Eventually(verifyMetricsEndpointReady).Should(Succeed())
279286
280287
By("verifying that the controller manager is serving the metrics server")
281288
verifyMetricsServerStarted := func(g Gomega) {
282289
cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace)
283-
g.Expect(utils.Run(cmd)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"),
290+
output, err := utils.Run(cmd)
291+
g.Expect(err).NotTo(HaveOccurred())
292+
g.Expect(string(output)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"),
284293
"Metrics server not yet started")
285294
}
286295
Eventually(verifyMetricsServerStarted).Should(Succeed())
@@ -292,14 +301,17 @@ var _ = Describe("Manager", Ordered, func() {
292301
"--", "/bin/sh", "-c", fmt.Sprintf(
293302
"curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics",
294303
token, metricsServiceName, namespace))
295-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod")
304+
_, err = utils.Run(cmd)
305+
Expect(err).NotTo(HaveOccurred(), "Failed to create curl-metrics pod")
296306
297307
By("waiting for the curl-metrics pod to complete.")
298308
verifyCurlUp := func(g Gomega) {
299309
cmd := exec.Command("kubectl", "get", "pods", "curl-metrics",
300310
"-o", "jsonpath={.status.phase}",
301311
"-n", namespace)
302-
g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status")
312+
output, err := utils.Run(cmd)
313+
g.Expect(err).NotTo(HaveOccurred())
314+
g.Expect(string(output)).To(Equal("Succeeded"), "curl pod in wrong status")
303315
}
304316
Eventually(verifyCurlUp, 5 * time.Minute).Should(Succeed())
305317

testdata/project-v4-multigroup-with-plugins/test/e2e/e2e_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ var _ = Describe("Manager", Ordered, func() {
110110
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
111111
"-n", namespace,
112112
)
113-
g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status")
113+
output, err := utils.Run(cmd)
114+
g.Expect(err).NotTo(HaveOccurred())
115+
g.Expect(string(output)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status")
114116
}
115117
// Repeatedly check if the controller-manager pod is running until it succeeds or times out.
116118
Eventually(verifyControllerUp).Should(Succeed())
@@ -122,15 +124,18 @@ var _ = Describe("Manager", Ordered, func() {
122124
"--clusterrole=project-v4-multigroup-with-plugins-metrics-reader",
123125
fmt.Sprintf("--serviceaccount=%s:%s", namespace, serviceAccountName),
124126
)
125-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding")
127+
_, err := utils.Run(cmd)
128+
Expect(err).NotTo(HaveOccurred(), "Failed to create ClusterRoleBinding")
126129

127130
By("validating that the metrics service is available")
128131
cmd = exec.Command("kubectl", "get", "service", metricsServiceName, "-n", namespace)
129-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Metrics service should exist")
132+
_, err = utils.Run(cmd)
133+
Expect(err).NotTo(HaveOccurred(), "Metrics service should exist")
130134

131135
By("validating that the ServiceMonitor for Prometheus is applied in the namespace")
132136
cmd = exec.Command("kubectl", "get", "ServiceMonitor", "-n", namespace)
133-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "ServiceMonitor should exist")
137+
_, err = utils.Run(cmd)
138+
Expect(err).NotTo(HaveOccurred(), "ServiceMonitor should exist")
134139

135140
By("getting the service account token")
136141
token, err := serviceAccountToken()
@@ -140,14 +145,18 @@ var _ = Describe("Manager", Ordered, func() {
140145
By("waiting for the metrics endpoint to be ready")
141146
verifyMetricsEndpointReady := func(g Gomega) {
142147
cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace)
143-
g.Expect(utils.Run(cmd)).To(ContainSubstring("8443"), "Metrics endpoint is not ready")
148+
output, err := utils.Run(cmd)
149+
g.Expect(err).NotTo(HaveOccurred())
150+
g.Expect(string(output)).To(ContainSubstring("8443"), "Metrics endpoint is not ready")
144151
}
145152
Eventually(verifyMetricsEndpointReady).Should(Succeed())
146153

147154
By("verifying that the controller manager is serving the metrics server")
148155
verifyMetricsServerStarted := func(g Gomega) {
149156
cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace)
150-
g.Expect(utils.Run(cmd)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"),
157+
output, err := utils.Run(cmd)
158+
g.Expect(err).NotTo(HaveOccurred())
159+
g.Expect(string(output)).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"),
151160
"Metrics server not yet started")
152161
}
153162
Eventually(verifyMetricsServerStarted).Should(Succeed())
@@ -159,14 +168,17 @@ var _ = Describe("Manager", Ordered, func() {
159168
"--", "/bin/sh", "-c", fmt.Sprintf(
160169
"curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics",
161170
token, metricsServiceName, namespace))
162-
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod")
171+
_, err = utils.Run(cmd)
172+
Expect(err).NotTo(HaveOccurred(), "Failed to create curl-metrics pod")
163173

164174
By("waiting for the curl-metrics pod to complete.")
165175
verifyCurlUp := func(g Gomega) {
166176
cmd := exec.Command("kubectl", "get", "pods", "curl-metrics",
167177
"-o", "jsonpath={.status.phase}",
168178
"-n", namespace)
169-
g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Succeeded"), "curl pod in wrong status")
179+
output, err := utils.Run(cmd)
180+
g.Expect(err).NotTo(HaveOccurred())
181+
g.Expect(string(output)).To(Equal("Succeeded"), "curl pod in wrong status")
170182
}
171183
Eventually(verifyCurlUp, 5*time.Minute).Should(Succeed())
172184

0 commit comments

Comments
 (0)