diff --git a/tests/e2e/framework/common.go b/tests/e2e/framework/common.go index 72b4d97099..f19edd8ca9 100644 --- a/tests/e2e/framework/common.go +++ b/tests/e2e/framework/common.go @@ -1647,6 +1647,45 @@ func (f *Framework) GetConfigMapsFromScan(scaninstance *compv1alpha1.ComplianceS return configmaps.Items, nil } +// GetScanExitCodeAndErrorMsg retrieves the exit code and error message from a scan's ConfigMaps +// Returns exit code as a string, error message as a string, and an error if any occurred +// If no ConfigMaps exist or if exit-code/error-msg are not found, returns empty strings +func (f *Framework) GetScanExitCodeAndErrorMsg(scanName, namespace string) (string, string, error) { + // Get the scan + scan := &compv1alpha1.ComplianceScan{} + err := f.Client.Get(context.TODO(), types.NamespacedName{Name: scanName, Namespace: namespace}, scan) + if err != nil { + return "", "", fmt.Errorf("failed to get scan %s: %w", scanName, err) + } + + // Get ConfigMaps from the scan + configMaps, err := f.GetConfigMapsFromScan(scan) + if err != nil { + return "", "", fmt.Errorf("failed to get ConfigMaps from scan %s: %w", scanName, err) + } + + var exitCode string + var errorMsg string + + // Look for exit-code in ConfigMaps + for _, cm := range configMaps { + if code, ok := cm.Data["exit-code"]; ok { + exitCode = code + break + } + } + + // Look for error-msg in ConfigMaps + for _, cm := range configMaps { + if msg, ok := cm.Data["error-msg"]; ok { + errorMsg = msg + break + } + } + + return exitCode, errorMsg, nil +} + func (f *Framework) GetPodsForScan(scanName string) ([]core.Pod, error) { selectPods := map[string]string{ compv1alpha1.ComplianceScanLabel: scanName, diff --git a/tests/e2e/parallel/main_test.go b/tests/e2e/parallel/main_test.go index 214220ef0f..409b1c4a59 100644 --- a/tests/e2e/parallel/main_test.go +++ b/tests/e2e/parallel/main_test.go @@ -682,6 +682,15 @@ func TestSingleScanSucceeds(t *testing.T) { if err != nil { t.Fatalf("failed to assert PVC reference for scan %s: %s", scanName, err) } + + // Validate exit-code is "0" + exitCode, _, err := f.GetScanExitCodeAndErrorMsg(scanName, f.OperatorNamespace) + if err != nil { + t.Fatal(err) + } + if exitCode != "0" { + t.Fatalf("Expected ConfigMap exit-code to be '0', but got: '%s'", exitCode) + } } func TestSingleScanTimestamps(t *testing.T) { @@ -1064,6 +1073,15 @@ func TestScanWithUnexistentResourceFails(t *testing.T) { if err = f.ScanHasWarnings(scanName, f.OperatorNamespace); err != nil { t.Fatal(err) } + + // Validate exit-code is "2" + exitCode, _, err := f.GetScanExitCodeAndErrorMsg(scanName, f.OperatorNamespace) + if err != nil { + t.Fatal(err) + } + if exitCode != "2" { + t.Fatalf("Expected ConfigMap exit-code to be '2', but got: '%s'", exitCode) + } } func TestScanStorageOutOfLimitRangeFails(t *testing.T) {