@@ -43,6 +43,8 @@ const metricsServiceName = "project-controller-manager-metrics-service"
4343const metricsRoleBindingName = "project-metrics-binding"
4444
4545var _ = Describe ("Manager" , Ordered , func () {
46+ var controllerPodName string
47+
4648 // Before running the tests, set up the environment by creating the namespace,
4749 // installing CRDs, and deploying the controller.
4850 BeforeAll (func () {
@@ -82,12 +84,53 @@ var _ = Describe("Manager", Ordered, func() {
8284 _ , _ = utils .Run (cmd )
8385 })
8486
87+ // After each test, check for failures and collect logs, events,
88+ // and pod descriptions for debugging.
89+ AfterEach (func () {
90+ specReport := CurrentSpecReport ()
91+ if specReport .Failed () {
92+ By ("Fetching controller manager pod logs" )
93+ cmd := exec .Command ("kubectl" , "logs" , controllerPodName , "-n" , namespace )
94+ controllerLogs , err := utils .Run (cmd )
95+ if err == nil {
96+ _ , _ = fmt .Fprintf (GinkgoWriter , fmt .Sprintf ("Controller logs:\n %s" , controllerLogs ))
97+ } else {
98+ _ , _ = fmt .Fprintf (GinkgoWriter , fmt .Sprintf ("Failed to get Controller logs: %s" , err ))
99+ }
100+
101+ By ("Fetching Kubernetes events" )
102+ cmd = exec .Command ("kubectl" , "get" , "events" , "-n" , namespace , "--sort-by=.lastTimestamp" )
103+ eventsOutput , err := utils .Run (cmd )
104+ if err == nil {
105+ _ , _ = fmt .Fprintf (GinkgoWriter , fmt .Sprintf ("Kubernetes events:\n %s" , eventsOutput ))
106+ } else {
107+ _ , _ = fmt .Fprintf (GinkgoWriter , fmt .Sprintf ("Failed to get Kubernetes events: %s" , err ))
108+ }
109+
110+ By ("Fetching curl-metrics logs" )
111+ cmd = exec .Command ("kubectl" , "logs" , "curl-metrics" , "-n" , namespace )
112+ metricsOutput , err := utils .Run (cmd )
113+ if err == nil {
114+ _ , _ = fmt .Fprintf (GinkgoWriter , fmt .Sprintf ("Metrics logs:\n %s" , metricsOutput ))
115+ } else {
116+ _ , _ = fmt .Fprintf (GinkgoWriter , fmt .Sprintf ("Failed to get curl-metrics logs: %s" , err ))
117+ }
118+
119+ By ("Fetching controller manager pod description" )
120+ cmd = exec .Command ("kubectl" , "describe" , "pod" , controllerPodName , "-n" , namespace )
121+ podDescription , err := utils .Run (cmd )
122+ if err == nil {
123+ fmt .Println ("Pod description:\n " , podDescription )
124+ } else {
125+ fmt .Println ("Failed to describe controller pod" )
126+ }
127+ }
128+ })
129+
85130 SetDefaultEventuallyTimeout (2 * time .Minute )
86131 SetDefaultEventuallyPollingInterval (time .Second )
87132
88- // The Context block contains the actual tests that validate the manager's behavior.
89133 Context ("Manager" , func () {
90- var controllerPodName string
91134 It ("should run successfully" , func () {
92135 By ("validating that the controller-manager pod is running as expected" )
93136 verifyControllerUp := func (g Gomega ) {
@@ -103,7 +146,7 @@ var _ = Describe("Manager", Ordered, func() {
103146
104147 podOutput , err := utils .Run (cmd )
105148 g .Expect (err ).NotTo (HaveOccurred (), "Failed to retrieve controller-manager pod information" )
106- podNames := utils .GetNonEmptyLines (string ( podOutput ) )
149+ podNames := utils .GetNonEmptyLines (podOutput )
107150 g .Expect (podNames ).To (HaveLen (1 ), "expected 1 controller pod running" )
108151 controllerPodName = podNames [0 ]
109152 g .Expect (controllerPodName ).To (ContainSubstring ("controller-manager" ))
@@ -115,9 +158,8 @@ var _ = Describe("Manager", Ordered, func() {
115158 )
116159 output , err := utils .Run (cmd )
117160 g .Expect (err ).NotTo (HaveOccurred ())
118- g .Expect (string ( output )) .To (BeEquivalentTo ("Running" ), "Incorrect controller-manager pod status" )
161+ g .Expect (output ).To (Equal ("Running" ), "Incorrect controller-manager pod status" )
119162 }
120- // Repeatedly check if the controller-manager pod is running until it succeeds or times out.
121163 Eventually (verifyControllerUp ).Should (Succeed ())
122164 })
123165
@@ -150,7 +192,7 @@ var _ = Describe("Manager", Ordered, func() {
150192 cmd := exec .Command ("kubectl" , "get" , "endpoints" , metricsServiceName , "-n" , namespace )
151193 output , err := utils .Run (cmd )
152194 g .Expect (err ).NotTo (HaveOccurred ())
153- g .Expect (string ( output ) ).To (ContainSubstring ("8443" ), "Metrics endpoint is not ready" )
195+ g .Expect (output ).To (ContainSubstring ("8443" ), "Metrics endpoint is not ready" )
154196 }
155197 Eventually (verifyMetricsEndpointReady ).Should (Succeed ())
156198
@@ -159,7 +201,7 @@ var _ = Describe("Manager", Ordered, func() {
159201 cmd := exec .Command ("kubectl" , "logs" , controllerPodName , "-n" , namespace )
160202 output , err := utils .Run (cmd )
161203 g .Expect (err ).NotTo (HaveOccurred ())
162- g .Expect (string ( output ) ).To (ContainSubstring ("controller-runtime.metrics\t Serving metrics server" ),
204+ g .Expect (output ).To (ContainSubstring ("controller-runtime.metrics\t Serving metrics server" ),
163205 "Metrics server not yet started" )
164206 }
165207 Eventually (verifyMetricsServerStarted ).Should (Succeed ())
@@ -181,7 +223,7 @@ var _ = Describe("Manager", Ordered, func() {
181223 "-n" , namespace )
182224 output , err := utils .Run (cmd )
183225 g .Expect (err ).NotTo (HaveOccurred ())
184- g .Expect (string ( output ) ).To (Equal ("Succeeded" ), "curl pod in wrong status" )
226+ g .Expect (output ).To (Equal ("Succeeded" ), "curl pod in wrong status" )
185227 }
186228 Eventually (verifyCurlUp , 5 * time .Minute ).Should (Succeed ())
187229
@@ -261,7 +303,6 @@ func serviceAccountToken() (string, error) {
261303 }
262304
263305 var out string
264- var rawJson string
265306 verifyTokenCreation := func (g Gomega ) {
266307 // Execute kubectl command to create the token
267308 cmd := exec .Command ("kubectl" , "create" , "--raw" , fmt .Sprintf (
@@ -273,11 +314,9 @@ func serviceAccountToken() (string, error) {
273314 output , err := cmd .CombinedOutput ()
274315 g .Expect (err ).NotTo (HaveOccurred ())
275316
276- rawJson = string (output )
277-
278317 // Parse the JSON output to extract the token
279318 var token tokenRequest
280- err = json .Unmarshal ([]byte (rawJson ), & token )
319+ err = json .Unmarshal ([]byte (output ), & token )
281320 g .Expect (err ).NotTo (HaveOccurred ())
282321
283322 out = token .Status .Token
@@ -293,9 +332,8 @@ func getMetricsOutput() string {
293332 cmd := exec .Command ("kubectl" , "logs" , "curl-metrics" , "-n" , namespace )
294333 metricsOutput , err := utils .Run (cmd )
295334 Expect (err ).NotTo (HaveOccurred (), "Failed to retrieve logs from curl pod" )
296- metricsOutputStr := string (metricsOutput )
297- Expect (metricsOutputStr ).To (ContainSubstring ("< HTTP/1.1 200 OK" ))
298- return metricsOutputStr
335+ Expect (metricsOutput ).To (ContainSubstring ("< HTTP/1.1 200 OK" ))
336+ return metricsOutput
299337}
300338
301339// tokenRequest is a simplified representation of the Kubernetes TokenRequest API response,
0 commit comments