@@ -239,6 +239,115 @@ func verifySecuredClusterNotInstalled(t *testing.T, namespace string) {
239239 }
240240}
241241
242+ var clusterDumpNamespaces = []string {
243+ "rhacs-operator-system" ,
244+ "acs-central" ,
245+ "acs-sensor" ,
246+ "stackrox" ,
247+ }
248+
249+ func dumpClusterStateOnFailure (t * testing.T ) {
250+ t .Helper ()
251+ t .Cleanup (func () {
252+ if ! t .Failed () {
253+ return
254+ }
255+ dumpClusterResources (t )
256+ })
257+ }
258+
259+ func dumpClusterResources (t * testing.T ) {
260+ t .Helper ()
261+ fmt .Fprintf (os .Stderr , "=== CLUSTER RESOURCE DUMP (test %s failed) ===\n " , t .Name ())
262+
263+ runKubectlDump ("get" , "namespaces" )
264+
265+ for _ , ns := range clusterDumpNamespaces {
266+ fmt .Fprintf (os .Stderr , "--- Namespace: %s ---\n " , ns )
267+ runKubectlDump ("get" , "pods" , "-n" , ns , "-o" , "wide" )
268+ runKubectlDump ("describe" , "pods" , "-n" , ns )
269+ runKubectlDump ("get" , "deployments" , "-n" , ns , "-o" , "wide" )
270+ runKubectlDump ("describe" , "deployments" , "-n" , ns )
271+ runKubectlDump ("get" , "daemonsets" , "-n" , ns , "-o" , "wide" )
272+ runKubectlDump ("describe" , "daemonsets" , "-n" , ns )
273+ runKubectlDump ("get" , "events" , "-n" , ns , "--sort-by=.lastTimestamp" )
274+ dumpLogsForFailingPods (ns )
275+ }
276+
277+ dumpACSCustomResources ()
278+ dumpOLMResources ()
279+
280+ fmt .Fprintln (os .Stderr , "=== END CLUSTER RESOURCE DUMP ===" )
281+ }
282+
283+ func dumpACSCustomResources () {
284+ fmt .Fprintln (os .Stderr , "--- ACS Custom Resources ---" )
285+ for _ , ns := range clusterDumpNamespaces {
286+ runKubectlDump ("get" , "centrals.platform.stackrox.io" , "-n" , ns , "-o" , "yaml" )
287+ runKubectlDump ("get" , "securedclusters.platform.stackrox.io" , "-n" , ns , "-o" , "yaml" )
288+ }
289+ }
290+
291+ func dumpOLMResources () {
292+ cmd := exec .Command ("kubectl" , "api-resources" , "--api-group=operators.coreos.com" , "-o" , "name" )
293+ output , err := cmd .Output ()
294+ if err != nil || strings .TrimSpace (string (output )) == "" {
295+ fmt .Fprintln (os .Stderr , "[dump] OLM not installed, skipping OLM resource dump" )
296+ return
297+ }
298+
299+ fmt .Fprintln (os .Stderr , "--- OLM Resources ---" )
300+ operatorNamespace := "rhacs-operator-system"
301+ runKubectlDump ("get" , "subscriptions.operators.coreos.com" , "-n" , operatorNamespace , "-o" , "wide" )
302+ runKubectlDump ("describe" , "subscriptions.operators.coreos.com" , "-n" , operatorNamespace )
303+ runKubectlDump ("get" , "installplans.operators.coreos.com" , "-n" , operatorNamespace , "-o" , "wide" )
304+ runKubectlDump ("describe" , "installplans.operators.coreos.com" , "-n" , operatorNamespace )
305+ runKubectlDump ("get" , "catalogsources.operators.coreos.com" , "-n" , operatorNamespace , "-o" , "wide" )
306+ runKubectlDump ("describe" , "catalogsources.operators.coreos.com" , "-n" , operatorNamespace )
307+ runKubectlDump ("get" , "clusterserviceversions.operators.coreos.com" , "-n" , operatorNamespace , "-o" , "wide" )
308+ runKubectlDump ("describe" , "clusterserviceversions.operators.coreos.com" , "-n" , operatorNamespace )
309+ runKubectlDump ("get" , "operatorgroups.operators.coreos.com" , "-n" , operatorNamespace , "-o" , "wide" )
310+ runKubectlDump ("describe" , "operatorgroups.operators.coreos.com" , "-n" , operatorNamespace )
311+ }
312+
313+ func runKubectlDump (args ... string ) {
314+ fmt .Fprintf (os .Stderr , "## kubectl %s\n " , strings .Join (args , " " ))
315+ cmd := exec .Command ("kubectl" , args ... )
316+ cmd .Stdout = os .Stderr
317+ cmd .Stderr = os .Stderr
318+ if err := cmd .Run (); err != nil {
319+ fmt .Fprintf (os .Stderr , "kubectl failed: %v\n " , err )
320+ }
321+ fmt .Fprintln (os .Stderr )
322+ }
323+
324+ func dumpLogsForFailingPods (namespace string ) {
325+ cmd := exec .Command ("kubectl" , "get" , "pods" , "-n" , namespace ,
326+ "-o" , "jsonpath={range .items[*]}{.metadata.name}{\" \\ t\" }{.status.phase}{\" \\ n\" }{end}" )
327+ output , err := cmd .Output ()
328+ if err != nil {
329+ fmt .Fprintf (os .Stderr , "[dump] failed to list pods in %s: %v\n " , namespace , err )
330+ return
331+ }
332+
333+ for line := range strings .SplitSeq (strings .TrimSpace (string (output )), "\n " ) {
334+ if line == "" {
335+ continue
336+ }
337+ parts := strings .SplitN (line , "\t " , 2 )
338+ if len (parts ) != 2 {
339+ continue
340+ }
341+ podName , phase := parts [0 ], parts [1 ]
342+ if phase == "Running" || phase == "Succeeded" {
343+ continue
344+ }
345+ fmt .Fprintf (os .Stderr , "[dump] logs for pod %s/%s (phase=%s):\n " , namespace , podName , phase )
346+ runKubectlDump ("logs" , "-n" , namespace , podName , "--all-containers" , "--tail=100" )
347+ runKubectlDump ("logs" , "-n" , namespace , podName , "--all-containers" , "--previous" , "--tail=50" )
348+ }
349+ }
350+
242351func verifyAnnotation (t * testing.T , resourceType , resourceName , namespace , annotationKey , expectedValue string ) {
243352 t .Helper ()
244353
0 commit comments