Skip to content

Commit 2673d70

Browse files
Add func to check autoremediated results pass
1 parent def3a79 commit 2673d70

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

e2e_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ func TestPlatformCompliance(t *testing.T) {
146146
}
147147
afterRemediation = true
148148

149+
err = helpers.CheckAutomatedRemediationResults(tc, c, platformBindingName)
150+
if err != nil {
151+
t.Fatalf("Failed automated remediation check: %s", err)
152+
}
153+
149154
finalResults, err := helpers.CreateResultMap(tc, c, platformBindingName)
150155
if err != nil {
151156
t.Fatalf("Failed to create result map: %s", err)

helpers/utilities.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,48 @@ func CreateResultMap(_ *testConfig.TestConfig, c dynclient.Client, suiteName str
17621762
return resultMap, nil
17631763
}
17641764

1765+
// CheckAutomatedRemediationResults verifies that all ComplianceCheckResults with automated remediation
1766+
// are in PASS status. If any automated remediation results are not PASS, it returns an error.
1767+
func CheckAutomatedRemediationResults(tc *testConfig.TestConfig, c dynclient.Client, suiteName string) error {
1768+
// Build label selector for results with automated remediation that are not PASS
1769+
// compliance.openshift.io/automated-remediation label exists (any value) AND compliance.openshift.io/check-status!=PASS
1770+
// AND compliance.openshift.io/suite=<suiteName>
1771+
labelSelectorStr := fmt.Sprintf("%s,%s!=PASS,%s=%s",
1772+
cmpv1alpha1.ComplianceCheckResultHasRemediation,
1773+
cmpv1alpha1.ComplianceCheckResultStatusLabel,
1774+
cmpv1alpha1.SuiteLabel,
1775+
suiteName,
1776+
)
1777+
labelSelector, err := labels.Parse(labelSelectorStr)
1778+
if err != nil {
1779+
return fmt.Errorf("failed to parse label selector: %w", err)
1780+
}
1781+
1782+
// Get all ComplianceCheckResults matching the criteria
1783+
resultList := &cmpv1alpha1.ComplianceCheckResultList{}
1784+
opts := &dynclient.ListOptions{
1785+
LabelSelector: labelSelector,
1786+
Namespace: tc.OperatorNamespace.Namespace,
1787+
}
1788+
err = c.List(goctx.TODO(), resultList, opts)
1789+
if err != nil {
1790+
return fmt.Errorf("failed to get compliance check results for suite %s: %w", suiteName, err)
1791+
}
1792+
1793+
if len(resultList.Items) > 0 {
1794+
var nonPassResults []string
1795+
for i := range resultList.Items {
1796+
result := &resultList.Items[i]
1797+
nonPassResults = append(nonPassResults, fmt.Sprintf("%s (status: %s)", result.Name, result.Status))
1798+
}
1799+
return fmt.Errorf("found %d ComplianceCheckResult(s) with automated remediation that are not in PASS status for suite %s: %v",
1800+
len(resultList.Items), suiteName, nonPassResults)
1801+
}
1802+
1803+
log.Printf("All ComplianceCheckResults with automated remediation are in PASS status for suite %s", suiteName)
1804+
return nil
1805+
}
1806+
17651807
// SaveResultAsYAML saves YAML data about the scan results to a file in the configured log directory.
17661808
func SaveResultAsYAML(tc *testConfig.TestConfig, results map[string]string, filename string) error {
17671809
p := path.Join(tc.LogDir, filename)

0 commit comments

Comments
 (0)