Skip to content

Commit 0004cb7

Browse files
committed
Fix metrics test failure by setting controllerPodName in BeforeAll
The metrics endpoint test was failing with "You must provide one or more resources" because controllerPodName was empty. This happened because the controller pod name was only set in the first test, but the metrics test in the nested Context ran independently. Moved the controller pod validation to BeforeAll at the Describe level so controllerPodName is set once before any tests run, making it available to all tests including the metrics endpoint test.
1 parent a538c10 commit 0004cb7

1 file changed

Lines changed: 35 additions & 29 deletions

File tree

test/e2e/metrics_test.go

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,39 @@ const metricsPort = "8080"
3636
var _ = Describe("Manager", func() {
3737
var controllerPodName string
3838

39+
// BeforeAll ensures controllerPodName is set before any tests run
40+
BeforeAll(func() {
41+
By("validating that the controller-manager pod is running as expected")
42+
verifyControllerUp := func(g Gomega) {
43+
// Get the name of the controller-manager pod
44+
cmd := exec.Command("kubectl", "get",
45+
"pods", "-l", "control-plane=controller-manager",
46+
"-o", "go-template={{ range .items }}"+
47+
"{{ if not .metadata.deletionTimestamp }}"+
48+
"{{ .metadata.name }}"+
49+
"{{ \"\\n\" }}{{ end }}{{ end }}",
50+
"-n", namespace,
51+
)
52+
53+
podOutput, err := utils.Run(cmd)
54+
g.Expect(err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod information")
55+
podNames := utils.GetNonEmptyLines(podOutput)
56+
g.Expect(podNames).To(HaveLen(1), "expected 1 controller pod running")
57+
controllerPodName = podNames[0]
58+
g.Expect(controllerPodName).To(ContainSubstring("controller-manager"))
59+
60+
// Validate the pod's status
61+
cmd = exec.Command("kubectl", "get",
62+
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
63+
"-n", namespace,
64+
)
65+
output, err := utils.Run(cmd)
66+
g.Expect(err).NotTo(HaveOccurred())
67+
g.Expect(output).To(Equal("Running"), "Incorrect controller-manager pod status")
68+
}
69+
Eventually(verifyControllerUp).Should(Succeed())
70+
})
71+
3972
// After each test, check for failures and collect logs, events,
4073
// and pod descriptions for debugging.
4174
AfterEach(func() {
@@ -84,35 +117,8 @@ var _ = Describe("Manager", func() {
84117

85118
Context("Manager", func() {
86119
It("should run successfully", func() {
87-
By("validating that the controller-manager pod is running as expected")
88-
verifyControllerUp := func(g Gomega) {
89-
// Get the name of the controller-manager pod
90-
cmd := exec.Command("kubectl", "get",
91-
"pods", "-l", "control-plane=controller-manager",
92-
"-o", "go-template={{ range .items }}"+
93-
"{{ if not .metadata.deletionTimestamp }}"+
94-
"{{ .metadata.name }}"+
95-
"{{ \"\\n\" }}{{ end }}{{ end }}",
96-
"-n", namespace,
97-
)
98-
99-
podOutput, err := utils.Run(cmd)
100-
g.Expect(err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod information")
101-
podNames := utils.GetNonEmptyLines(podOutput)
102-
g.Expect(podNames).To(HaveLen(1), "expected 1 controller pod running")
103-
controllerPodName = podNames[0]
104-
g.Expect(controllerPodName).To(ContainSubstring("controller-manager"))
105-
106-
// Validate the pod's status
107-
cmd = exec.Command("kubectl", "get",
108-
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
109-
"-n", namespace,
110-
)
111-
output, err := utils.Run(cmd)
112-
g.Expect(err).NotTo(HaveOccurred())
113-
g.Expect(output).To(Equal("Running"), "Incorrect controller-manager pod status")
114-
}
115-
Eventually(verifyControllerUp).Should(Succeed())
120+
// Controller pod validation happens in BeforeAll
121+
Expect(controllerPodName).NotTo(BeEmpty())
116122
})
117123

118124
Context("with curl-metrics-pod", func() {

0 commit comments

Comments
 (0)