From 8852281b007c25017e6354cd4826540d3a07814b Mon Sep 17 00:00:00 2001 From: Anna-Koudelkova Date: Mon, 2 Feb 2026 15:57:57 +0100 Subject: [PATCH] Add func to check autoremediated results pass --- e2e_test.go | 10 ++++++++++ helpers/utilities.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/e2e_test.go b/e2e_test.go index c19f9f75..aa7018de 100644 --- a/e2e_test.go +++ b/e2e_test.go @@ -146,6 +146,11 @@ func TestPlatformCompliance(t *testing.T) { } afterRemediation = true + err = helpers.CheckAutomatedRemediationPassed(tc, c, platformBindingName) + if err != nil { + t.Fatalf("Failed automated remediation check: %s", err) + } + finalResults, err := helpers.CreateResultMap(tc, c, platformBindingName) if err != nil { t.Fatalf("Failed to create result map: %s", err) @@ -259,6 +264,11 @@ func TestNodeCompliance(t *testing.T) { } afterRemediation = true + err = helpers.CheckAutomatedRemediationPassed(tc, c, nodeBindingName) + if err != nil { + t.Fatalf("Failed automated remediation check: %s", err) + } + finalResults, err := helpers.CreateResultMap(tc, c, nodeBindingName) if err != nil { t.Fatalf("Failed to create result map: %s", err) diff --git a/helpers/utilities.go b/helpers/utilities.go index 4a8f5afe..575e80be 100644 --- a/helpers/utilities.go +++ b/helpers/utilities.go @@ -1762,6 +1762,44 @@ func CreateResultMap(_ *testConfig.TestConfig, c dynclient.Client, suiteName str return resultMap, nil } +// CheckAutomatedRemediationPassed verifies that all ComplianceCheckResults with automated remediation +// are in PASS status. If any automated remediation results are not PASS, it returns an error. +func CheckAutomatedRemediationPassed(tc *testConfig.TestConfig, c dynclient.Client, suiteName string) error { + labelSelectorStr := fmt.Sprintf("%s,%s!=PASS,%s=%s", + cmpv1alpha1.ComplianceCheckResultHasRemediation, + cmpv1alpha1.ComplianceCheckResultStatusLabel, + cmpv1alpha1.SuiteLabel, + suiteName, + ) + labelSelector, err := labels.Parse(labelSelectorStr) + if err != nil { + return fmt.Errorf("failed to parse label selector: %w", err) + } + + resultList := &cmpv1alpha1.ComplianceCheckResultList{} + opts := &dynclient.ListOptions{ + LabelSelector: labelSelector, + Namespace: tc.OperatorNamespace.Namespace, + } + err = c.List(goctx.TODO(), resultList, opts) + if err != nil { + return fmt.Errorf("failed to get compliance check results for suite %s: %w", suiteName, err) + } + + if len(resultList.Items) > 0 { + var nonPassResults []string + for i := range resultList.Items { + result := &resultList.Items[i] + nonPassResults = append(nonPassResults, fmt.Sprintf("%s (status: %s)", result.Name, result.Status)) + } + return fmt.Errorf("found %d ComplianceCheckResult(s) with automated remediation that are not in PASS status for suite %s: %v", + len(resultList.Items), suiteName, nonPassResults) + } + + log.Printf("All ComplianceCheckResults with automated remediation are in PASS status for suite %s", suiteName) + return nil +} + // SaveResultAsYAML saves YAML data about the scan results to a file in the configured log directory. func SaveResultAsYAML(tc *testConfig.TestConfig, results map[string]string, filename string) error { p := path.Join(tc.LogDir, filename)