Skip to content

Commit 6ee25f1

Browse files
Add func to check autoremediated results pass
1 parent def3a79 commit 6ee25f1

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

e2e_test.go

Lines changed: 10 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.CheckAutomatedRemediationPassed(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)
@@ -259,6 +264,11 @@ func TestNodeCompliance(t *testing.T) {
259264
}
260265
afterRemediation = true
261266

267+
err = helpers.CheckAutomatedRemediationPassed(tc, c, nodeBindingName)
268+
if err != nil {
269+
t.Fatalf("Failed automated remediation check: %s", err)
270+
}
271+
262272
finalResults, err := helpers.CreateResultMap(tc, c, nodeBindingName)
263273
if err != nil {
264274
t.Fatalf("Failed to create result map: %s", err)

helpers/utilities.go

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

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

0 commit comments

Comments
 (0)