@@ -43,6 +43,8 @@ const metricsServiceName = "project-controller-manager-metrics-service"
43
43
const metricsRoleBindingName = "project-metrics-binding"
44
44
45
45
var _ = Describe ("Manager" , Ordered , func () {
46
+ var controllerPodName string
47
+
46
48
// Before running the tests, set up the environment by creating the namespace,
47
49
// installing CRDs, and deploying the controller.
48
50
BeforeAll (func () {
@@ -82,12 +84,53 @@ var _ = Describe("Manager", Ordered, func() {
82
84
_ , _ = utils .Run (cmd )
83
85
})
84
86
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
+
85
130
SetDefaultEventuallyTimeout (2 * time .Minute )
86
131
SetDefaultEventuallyPollingInterval (time .Second )
87
132
88
- // The Context block contains the actual tests that validate the manager's behavior.
89
133
Context ("Manager" , func () {
90
- var controllerPodName string
91
134
It ("should run successfully" , func () {
92
135
By ("validating that the controller-manager pod is running as expected" )
93
136
verifyControllerUp := func (g Gomega ) {
@@ -103,7 +146,7 @@ var _ = Describe("Manager", Ordered, func() {
103
146
104
147
podOutput , err := utils .Run (cmd )
105
148
g .Expect (err ).NotTo (HaveOccurred (), "Failed to retrieve controller-manager pod information" )
106
- podNames := utils .GetNonEmptyLines (string ( podOutput ) )
149
+ podNames := utils .GetNonEmptyLines (podOutput )
107
150
g .Expect (podNames ).To (HaveLen (1 ), "expected 1 controller pod running" )
108
151
controllerPodName = podNames [0 ]
109
152
g .Expect (controllerPodName ).To (ContainSubstring ("controller-manager" ))
@@ -115,9 +158,8 @@ var _ = Describe("Manager", Ordered, func() {
115
158
)
116
159
output , err := utils .Run (cmd )
117
160
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" )
119
162
}
120
- // Repeatedly check if the controller-manager pod is running until it succeeds or times out.
121
163
Eventually (verifyControllerUp ).Should (Succeed ())
122
164
})
123
165
@@ -150,7 +192,7 @@ var _ = Describe("Manager", Ordered, func() {
150
192
cmd := exec .Command ("kubectl" , "get" , "endpoints" , metricsServiceName , "-n" , namespace )
151
193
output , err := utils .Run (cmd )
152
194
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" )
154
196
}
155
197
Eventually (verifyMetricsEndpointReady ).Should (Succeed ())
156
198
@@ -159,7 +201,7 @@ var _ = Describe("Manager", Ordered, func() {
159
201
cmd := exec .Command ("kubectl" , "logs" , controllerPodName , "-n" , namespace )
160
202
output , err := utils .Run (cmd )
161
203
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" ),
163
205
"Metrics server not yet started" )
164
206
}
165
207
Eventually (verifyMetricsServerStarted ).Should (Succeed ())
@@ -181,7 +223,7 @@ var _ = Describe("Manager", Ordered, func() {
181
223
"-n" , namespace )
182
224
output , err := utils .Run (cmd )
183
225
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" )
185
227
}
186
228
Eventually (verifyCurlUp , 5 * time .Minute ).Should (Succeed ())
187
229
@@ -261,7 +303,6 @@ func serviceAccountToken() (string, error) {
261
303
}
262
304
263
305
var out string
264
- var rawJson string
265
306
verifyTokenCreation := func (g Gomega ) {
266
307
// Execute kubectl command to create the token
267
308
cmd := exec .Command ("kubectl" , "create" , "--raw" , fmt .Sprintf (
@@ -273,11 +314,9 @@ func serviceAccountToken() (string, error) {
273
314
output , err := cmd .CombinedOutput ()
274
315
g .Expect (err ).NotTo (HaveOccurred ())
275
316
276
- rawJson = string (output )
277
-
278
317
// Parse the JSON output to extract the token
279
318
var token tokenRequest
280
- err = json .Unmarshal ([]byte (rawJson ), & token )
319
+ err = json .Unmarshal ([]byte (output ), & token )
281
320
g .Expect (err ).NotTo (HaveOccurred ())
282
321
283
322
out = token .Status .Token
@@ -293,9 +332,8 @@ func getMetricsOutput() string {
293
332
cmd := exec .Command ("kubectl" , "logs" , "curl-metrics" , "-n" , namespace )
294
333
metricsOutput , err := utils .Run (cmd )
295
334
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
299
337
}
300
338
301
339
// tokenRequest is a simplified representation of the Kubernetes TokenRequest API response,
0 commit comments