@@ -171,3 +171,117 @@ var _ = Describe("Test pod obj template handling", func() {
171171 })
172172 })
173173})
174+
175+ var _ = Describe ("Test related object property status" , Ordered , func () {
176+ Describe ("Create a policy missing a field added by kubernetes" , Ordered , func () {
177+ const (
178+ policyName = "policy-service"
179+ serviceName = "grc-policy-propagator-metrics"
180+ policyYAML = "../resources/case8_status_check/case8_service_inform.yaml"
181+ serviceYAML = "../resources/case8_status_check/case8_service.yaml"
182+ )
183+
184+ It ("Should be compliant when the inform policy omits a field added by the apiserver" , func () {
185+ By ("Creating a Service with explicit type: ClusterIP" )
186+ utils .Kubectl ("apply" , "-f" , serviceYAML )
187+
188+ By ("Creating the " + policyName + " policy that omits the type field)" )
189+ utils .Kubectl ("apply" , "-f" , policyYAML , "-n" , testNamespace )
190+
191+ By ("Verifying that the " + policyName + " policy is compliant" )
192+ Eventually (func (g Gomega ) {
193+ managedPlc := utils .GetWithTimeout (
194+ clientManagedDynamic , gvrConfigPolicy , policyName , testNamespace , true , defaultTimeoutSeconds ,
195+ )
196+
197+ utils .CheckComplianceStatus (g , managedPlc , "Compliant" )
198+
199+ relatedObjects , _ , err := unstructured .NestedSlice (managedPlc .Object , "status" , "relatedObjects" )
200+ g .Expect (err ).ToNot (HaveOccurred ())
201+ g .Expect (relatedObjects ).To (HaveLen (1 ))
202+
203+ relatedObj := relatedObjects [0 ].(map [string ]interface {})
204+ matchesAfterDryRun , _ , _ := unstructured .NestedBool (relatedObj , "properties" , "matchesAfterDryRun" )
205+
206+ g .Expect (matchesAfterDryRun ).To (BeFalse ())
207+ }, defaultTimeoutSeconds , 1 ).Should (Succeed ())
208+ })
209+
210+ It ("Should be compliant when the enforce policy omits a field added by the apiserver" , func () {
211+ By ("Changing the policy to enforce mode" )
212+ utils .EnforceConfigurationPolicy (policyName , testNamespace )
213+
214+ By ("Verifying that the " + policyName + " policy is compliant" )
215+ Eventually (func (g Gomega ) {
216+ managedPlc := utils .GetWithTimeout (
217+ clientManagedDynamic , gvrConfigPolicy , policyName , testNamespace , true , defaultTimeoutSeconds ,
218+ )
219+
220+ utils .CheckComplianceStatus (g , managedPlc , "Compliant" )
221+
222+ relatedObjects , _ , err := unstructured .NestedSlice (managedPlc .Object , "status" , "relatedObjects" )
223+ g .Expect (err ).ToNot (HaveOccurred ())
224+ g .Expect (relatedObjects ).To (HaveLen (1 ))
225+
226+ relatedObj := relatedObjects [0 ].(map [string ]interface {})
227+ matchesAfterDryRun , _ , _ := unstructured .NestedBool (relatedObj , "properties" , "matchesAfterDryRun" )
228+
229+ g .Expect (matchesAfterDryRun ).To (BeFalse ())
230+ }, defaultTimeoutSeconds , 1 ).Should (Succeed ())
231+ })
232+
233+ It ("Should be compliant when the enforce policy includes all fields" , func () {
234+ By ("Patching the " + policyName + " policy with the Service type field)" )
235+ utils .Kubectl ("patch" , "configurationpolicy" , policyName , "-n" , testNamespace , "--type=json" , "-p" ,
236+ `[{"op": "add", "path": "/spec/object-templates/0/objectDefinition/spec/type", "value": "ClusterIP"}]` )
237+
238+ By ("Verifying that the " + policyName + " policy is compliant" )
239+ Eventually (func (g Gomega ) {
240+ managedPlc := utils .GetWithTimeout (
241+ clientManagedDynamic , gvrConfigPolicy , policyName , testNamespace , true , defaultTimeoutSeconds ,
242+ )
243+
244+ utils .CheckComplianceStatus (g , managedPlc , "Compliant" )
245+
246+ relatedObjects , _ , err := unstructured .NestedSlice (managedPlc .Object , "status" , "relatedObjects" )
247+ g .Expect (err ).ToNot (HaveOccurred ())
248+ g .Expect (relatedObjects ).To (HaveLen (1 ))
249+
250+ relatedObj := relatedObjects [0 ].(map [string ]interface {})
251+ matchesAfterDryRun , _ , _ := unstructured .NestedBool (relatedObj , "properties" , "matchesAfterDryRun" )
252+
253+ g .Expect (matchesAfterDryRun ).To (BeTrue ())
254+ }, defaultTimeoutSeconds , 1 ).Should (Succeed ())
255+ })
256+
257+ It ("Should be compliant when the inform policy includes all fields" , func () {
258+ By ("Changing the policy to inform mode" )
259+ utils .Kubectl ("patch" , "configurationpolicy" , policyName , `--type=json` ,
260+ `-p=[{"op":"replace","path":"/spec/remediationAction","value":"inform"}]` , "-n" , testNamespace )
261+
262+ By ("Verifying that the " + policyName + " policy is compliant" )
263+ Eventually (func (g Gomega ) {
264+ managedPlc := utils .GetWithTimeout (
265+ clientManagedDynamic , gvrConfigPolicy , policyName , testNamespace , true , defaultTimeoutSeconds ,
266+ )
267+
268+ utils .CheckComplianceStatus (g , managedPlc , "Compliant" )
269+
270+ relatedObjects , _ , err := unstructured .NestedSlice (managedPlc .Object , "status" , "relatedObjects" )
271+ g .Expect (err ).ToNot (HaveOccurred ())
272+ g .Expect (relatedObjects ).To (HaveLen (1 ))
273+
274+ relatedObj := relatedObjects [0 ].(map [string ]interface {})
275+ matchesAfterDryRun , _ , _ := unstructured .NestedBool (relatedObj , "properties" , "matchesAfterDryRun" )
276+
277+ g .Expect (matchesAfterDryRun ).To (BeTrue ())
278+ }, defaultTimeoutSeconds , 1 ).Should (Succeed ())
279+ })
280+
281+ AfterAll (func () {
282+ deleteConfigPolicies ([]string {policyName , policyName })
283+
284+ utils .KubectlDelete ("service" , serviceName , "-n" , "managed" )
285+ })
286+ })
287+ })
0 commit comments