Skip to content

Commit b2d7bfa

Browse files
JustinKuliopenshift-ci[bot]
authored andcommitted
Add and use checkCompliance helper function
This will help tests that check the overall compliance of a policy. There are more tests that seem like they might be able to utilize this, but they use the managed policy retrieved inside the step later in the test, and might rely on the updated object. Signed-off-by: Justin Kulikauskas <[email protected]>
1 parent c214823 commit b2d7bfa

File tree

5 files changed

+34
-97
lines changed

5 files changed

+34
-97
lines changed

test/e2e/case1_mutation_recovery_test.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,7 @@ var _ = Describe("Test mutation recovery", func() {
158158
"policy: managed/case1-test-policy-configurationpolicy",
159159
"Compliant; No violation detected")
160160
By("Checking if policy status is compliant")
161-
Eventually(func() interface{} {
162-
managedPlc = utils.GetWithTimeout(
163-
clientManagedDynamic,
164-
gvrPolicy,
165-
case1PolicyName,
166-
clusterNamespace,
167-
true,
168-
defaultTimeoutSeconds)
169-
170-
return getCompliant(managedPlc)
171-
}, defaultTimeoutSeconds, 1).Should(Equal("Compliant"))
161+
Eventually(checkCompliance(case1PolicyName), defaultTimeoutSeconds, 1).Should(Equal("Compliant"))
172162

173163
By("Update status to NonCompliant")
174164
Eventually(
@@ -188,17 +178,7 @@ var _ = Describe("Test mutation recovery", func() {
188178
).Should(BeNil())
189179
Expect(getCompliant(managedPlc)).To(Equal("NonCompliant"))
190180
By("Checking if policy status was recovered to compliant")
191-
Eventually(func() interface{} {
192-
managedPlc = utils.GetWithTimeout(
193-
clientManagedDynamic,
194-
gvrPolicy,
195-
case1PolicyName,
196-
clusterNamespace,
197-
true,
198-
defaultTimeoutSeconds)
199-
200-
return getCompliant(managedPlc)
201-
}, defaultTimeoutSeconds, 1).Should(Equal("Compliant"))
181+
Eventually(checkCompliance(case1PolicyName), defaultTimeoutSeconds, 1).Should(Equal("Compliant"))
202182
By("clean up all events")
203183
_, err := kubectlManaged("delete", "events", "-n", clusterNamespace, "--all")
204184
Expect(err).Should(BeNil())

test/e2e/case2_status_sync_test.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,10 @@ var _ = Describe("Test status sync", func() {
9696
"Warning",
9797
"policy: managed/case2-test-policy-configurationpolicy",
9898
"NonCompliant; there is violation")
99+
99100
By("Checking if policy status is noncompliant")
100-
Eventually(func() interface{} {
101-
managedPlc := utils.GetWithTimeout(
102-
clientManagedDynamic,
103-
gvrPolicy,
104-
case2PolicyName,
105-
clusterNamespace,
106-
true,
107-
defaultTimeoutSeconds)
101+
Eventually(checkCompliance(case2PolicyName), defaultTimeoutSeconds, 1).Should(Equal("NonCompliant"))
108102

109-
return getCompliant(managedPlc)
110-
}, defaultTimeoutSeconds, 1).Should(Equal("NonCompliant"))
111103
By("Checking if policy history is correct")
112104
managedPlc = utils.GetWithTimeout(
113105
clientManagedDynamic,
@@ -150,18 +142,10 @@ var _ = Describe("Test status sync", func() {
150142
"Normal",
151143
"policy: managed/case2-test-policy-configurationpolicy",
152144
"Compliant; No violation detected")
145+
153146
By("Checking if policy status is compliant")
154-
Eventually(func() interface{} {
155-
managedPlc := utils.GetWithTimeout(
156-
clientManagedDynamic,
157-
gvrPolicy,
158-
case2PolicyName,
159-
clusterNamespace,
160-
true,
161-
defaultTimeoutSeconds)
147+
Eventually(checkCompliance(case2PolicyName), defaultTimeoutSeconds, 1).Should(Equal("Compliant"))
162148

163-
return getCompliant(managedPlc)
164-
}, defaultTimeoutSeconds, 1).Should(Equal("Compliant"))
165149
By("Checking if policy history is correct")
166150
managedPlc = utils.GetWithTimeout(
167151
clientManagedDynamic,

test/e2e/case3_multiple_templates_test.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -494,17 +494,7 @@ var _ = Describe("Test status sync with multiple templates", func() {
494494
"policy: managed/case3-test-policy-configurationpolicy2",
495495
"Compliant; there is no violation")
496496
By("Checking if policy overall status is compliant")
497-
Eventually(func() interface{} {
498-
managedPlc = utils.GetWithTimeout(
499-
clientManagedDynamic,
500-
gvrPolicy,
501-
case3PolicyName,
502-
clusterNamespace,
503-
true,
504-
defaultTimeoutSeconds)
505-
506-
return getCompliant(managedPlc)
507-
}, defaultTimeoutSeconds, 1).Should(Equal("Compliant"))
497+
Eventually(checkCompliance(case3PolicyName), defaultTimeoutSeconds, 1).Should(Equal("Compliant"))
508498
By("Patching policy template to remove template: case3-test-policy-configurationpolicy2")
509499
_, err := kubectlHub(
510500
"apply",

test/e2e/case6_event_msg_test.go

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,7 @@ var _ = Describe("Test event message handling", func() {
139139
return plc.Status.Details[0].History[0].Message
140140
}, defaultTimeoutSeconds, 1).Should(Equal("NonCompliant; Violation detected"))
141141
By("Checking if policy status is noncompliant")
142-
Eventually(func() interface{} {
143-
managedPlc = utils.GetWithTimeout(
144-
clientManagedDynamic,
145-
gvrPolicy,
146-
case6PolicyName,
147-
clusterNamespace,
148-
true,
149-
defaultTimeoutSeconds)
150-
151-
return getCompliant(managedPlc)
152-
}, defaultTimeoutSeconds, 1).Should(Equal("NonCompliant"))
142+
Eventually(checkCompliance(case6PolicyName), defaultTimeoutSeconds, 1).Should(Equal("NonCompliant"))
153143
})
154144
It("Should remove `(combined from similar events):` prefix but still compliant", func() {
155145
By("Generating an event in ns:" + clusterNamespace + " that contains `(combined from similar events):` prefix")
@@ -201,17 +191,7 @@ var _ = Describe("Test event message handling", func() {
201191
return plc.Status.Details[0].History[0].Message
202192
}, defaultTimeoutSeconds, 1).Should(Equal("Compliant; no violation detected"))
203193
By("Checking if policy status is compliant")
204-
Eventually(func() interface{} {
205-
managedPlc = utils.GetWithTimeout(
206-
clientManagedDynamic,
207-
gvrPolicy,
208-
case6PolicyName,
209-
clusterNamespace,
210-
true,
211-
defaultTimeoutSeconds)
212-
213-
return getCompliant(managedPlc)
214-
}, defaultTimeoutSeconds, 1).Should(Equal("Compliant"))
194+
Eventually(checkCompliance(case6PolicyName), defaultTimeoutSeconds, 1).Should(Equal("Compliant"))
215195
})
216196
It("Should handle violation msg with just NonCompliant", func() {
217197
By("Generating an event in ns:" + clusterNamespace + " that only contains `NonCompliant`")
@@ -263,17 +243,7 @@ var _ = Describe("Test event message handling", func() {
263243
return plc.Status.Details[0].History[0].Message
264244
}, defaultTimeoutSeconds, 1).Should(Equal("NonCompliant"))
265245
By("Checking if policy status is noncompliant")
266-
Eventually(func() interface{} {
267-
managedPlc = utils.GetWithTimeout(
268-
clientManagedDynamic,
269-
gvrPolicy,
270-
case6PolicyName,
271-
clusterNamespace,
272-
true,
273-
defaultTimeoutSeconds)
274-
275-
return getCompliant(managedPlc)
276-
}, defaultTimeoutSeconds, 1).Should(Equal("NonCompliant"))
246+
Eventually(checkCompliance(case6PolicyName), defaultTimeoutSeconds, 1).Should(Equal("NonCompliant"))
277247
})
278248
It("Should handle violation msg with just Compliant", func() {
279249
By("Generating an event in ns:" + clusterNamespace + " that only contains `Compliant`")
@@ -325,16 +295,6 @@ var _ = Describe("Test event message handling", func() {
325295
return plc.Status.Details[0].History[0].Message
326296
}, defaultTimeoutSeconds, 1).Should(Equal("Compliant"))
327297
By("Checking if policy status is compliant")
328-
Eventually(func() interface{} {
329-
managedPlc = utils.GetWithTimeout(
330-
clientManagedDynamic,
331-
gvrPolicy,
332-
case6PolicyName,
333-
clusterNamespace,
334-
true,
335-
defaultTimeoutSeconds)
336-
337-
return getCompliant(managedPlc)
338-
}, defaultTimeoutSeconds, 1).Should(Equal("Compliant"))
298+
Eventually(checkCompliance(case6PolicyName), defaultTimeoutSeconds, 1).Should(Equal("Compliant"))
339299
})
340300
})

test/e2e/e2e_suite_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,26 @@ func patchRemediationAction(
243243
context.TODO(), plc.GetName(), types.JSONPatchType, patch, metav1.PatchOptions{},
244244
)
245245
}
246+
247+
func checkCompliance(name string) func() string {
248+
return func() string {
249+
getter := clientManagedDynamic.Resource(gvrPolicy).Namespace(clusterNamespace)
250+
251+
policy, err := getter.Get(context.TODO(), name, metav1.GetOptions{})
252+
if err != nil {
253+
return "policy not found"
254+
}
255+
256+
status, statusOk := policy.Object["status"].(map[string]interface{})
257+
if !statusOk {
258+
return "policy has no status"
259+
}
260+
261+
compliant, compliantOk := status["compliant"].(string)
262+
if !compliantOk {
263+
return "policy status has no complianceState"
264+
}
265+
266+
return compliant
267+
}
268+
}

0 commit comments

Comments
 (0)