@@ -175,6 +175,7 @@ data:
175175 ConfigurationPolicyOptions : types.ConfigurationPolicyOptions {
176176 ComplianceType : "musthave" ,
177177 MetadataComplianceType : "mustonlyhave" ,
178+ RecordDiff : "Log" ,
178179 },
179180 Path : manifestPath ,
180181 },
@@ -186,6 +187,7 @@ data:
186187 ConfigurationPolicyOptions : types.ConfigurationPolicyOptions {
187188 ComplianceType : "mustnothave" ,
188189 MetadataComplianceType : "musthave" ,
190+ RecordDiff : "None" ,
189191 },
190192 Path : manifestPath ,
191193 },
@@ -202,25 +204,29 @@ data:
202204 }
203205
204206 // Test both passing in individual files and a flat directory
205- tests := [ ]struct {
207+ tests := map [ string ]struct {
206208 ExpectedComplianceType string
207209 ExpectedMetadataComplianceType string
210+ ExpectedRecordDiff string
208211 Manifests []types.Manifest
209212 }{
210- {
213+ "musthave compType/mustonlyhave metaCompType/Log recDiff" : {
211214 ExpectedComplianceType : "musthave" ,
212215 ExpectedMetadataComplianceType : "mustonlyhave" ,
216+ ExpectedRecordDiff : "Log" ,
213217 Manifests : manifestFiles ,
214218 },
215- {
219+ "mustnothave compType/musthave metaCompType/None recDiff" : {
216220 ExpectedComplianceType : "mustnothave" ,
217221 ExpectedMetadataComplianceType : "musthave" ,
222+ ExpectedRecordDiff : "None" ,
218223 Manifests : manifestFilesMustNotHave ,
219224 },
220225 // The applyDefaults method would normally fill in ComplianceType on each manifest entry.
221- {
226+ "musthave compType/empty metaCompType/empty recDiff" : {
222227 ExpectedComplianceType : "musthave" ,
223228 ExpectedMetadataComplianceType : "" ,
229+ ExpectedRecordDiff : "" ,
224230 Manifests : []types.Manifest {{
225231 ConfigurationPolicyOptions : types.ConfigurationPolicyOptions {ComplianceType : "musthave" },
226232 Path : tmpDir ,
@@ -230,74 +236,91 @@ data:
230236 // test ConsolidateManifests = true (default case)
231237 // policyTemplates will have only one policyTemplate
232238 // and two objTemplate under this policyTemplate
233- for _ , test := range tests {
234- policyConf := types.PolicyConfig {
235- PolicyOptions : types.PolicyOptions {
236- ConsolidateManifests : true ,
237- },
238- ConfigurationPolicyOptions : types.ConfigurationPolicyOptions {
239- ComplianceType : "musthave" ,
240- RemediationAction : "inform" ,
241- Severity : "low" ,
242- },
243- Manifests : test .Manifests ,
244- Name : "policy-app-config" ,
245- }
239+ for testName , test := range tests {
240+ test := test
246241
247- policyTemplates , err := getPolicyTemplates (& policyConf )
248- if err != nil {
249- t .Fatalf ("Failed to get the policy templates: %v" , err )
250- }
242+ t .Run (testName , func (t * testing.T ) {
243+ t .Parallel ()
244+ policyConf := types.PolicyConfig {
245+ PolicyOptions : types.PolicyOptions {
246+ ConsolidateManifests : true ,
247+ },
248+ ConfigurationPolicyOptions : types.ConfigurationPolicyOptions {
249+ ComplianceType : "musthave" ,
250+ RemediationAction : "inform" ,
251+ Severity : "low" ,
252+ },
253+ Manifests : test .Manifests ,
254+ Name : "policy-app-config" ,
255+ }
251256
252- assertEqual (t , len (policyTemplates ), 1 )
257+ policyTemplates , err := getPolicyTemplates (& policyConf )
258+ if err != nil {
259+ t .Fatalf ("Failed to get the policy templates: %v" , err )
260+ }
253261
254- policyTemplate := policyTemplates [0 ]
255- objdef := policyTemplate ["objectDefinition" ].(map [string ]interface {})
262+ assertEqual (t , len (policyTemplates ), 1 )
256263
257- assertEqual (t , objdef ["metadata" ].(map [string ]interface {})["name" ].(string ), "policy-app-config" )
264+ policyTemplate := policyTemplates [0 ]
265+ objdef := policyTemplate ["objectDefinition" ].(map [string ]interface {})
258266
259- spec , ok := objdef ["spec" ].(map [string ]interface {})
260- if ! ok {
261- t .Fatal ("The spec field is an invalid format" )
262- }
267+ assertEqual (t , objdef ["metadata" ].(map [string ]interface {})["name" ].(string ), "policy-app-config" )
263268
264- assertEqual (t , spec ["remediationAction" ], "inform" )
265- assertEqual (t , spec ["severity" ], "low" )
269+ spec , ok := objdef ["spec" ].(map [string ]interface {})
270+ if ! ok {
271+ t .Fatal ("The spec field is an invalid format" )
272+ }
266273
267- objTemplates , ok := spec ["object-templates" ].([]map [string ]interface {})
268- if ! ok {
269- t .Fatal ("The object-templates field is an invalid format" )
270- }
274+ assertEqual (t , spec ["remediationAction" ], "inform" )
275+ assertEqual (t , spec ["severity" ], "low" )
271276
272- assertEqual (t , len (objTemplates ), 2 )
273- assertEqual (t , objTemplates [0 ]["complianceType" ], test .ExpectedComplianceType )
277+ objTemplates , ok := spec ["object-templates" ].([]map [string ]interface {})
278+ if ! ok {
279+ t .Fatal ("The object-templates field is an invalid format" )
280+ }
274281
275- if test .ExpectedMetadataComplianceType != "" {
276- assertEqual (t , objTemplates [0 ]["metadataComplianceType" ], test .ExpectedMetadataComplianceType )
277- } else {
278- assertEqual (t , objTemplates [0 ]["metadataComplianceType" ], nil )
279- }
282+ assertEqual (t , len (objTemplates ), 2 )
283+ assertEqual (t , objTemplates [0 ]["complianceType" ], test .ExpectedComplianceType )
280284
281- kind1 , ok := objTemplates [0 ]["objectDefinition" ].(map [string ]interface {})["kind" ]
282- if ! ok {
283- t .Fatal ("The objectDefinition field is an invalid format" )
284- }
285+ if test .ExpectedMetadataComplianceType != "" {
286+ assertEqual (t , objTemplates [0 ]["metadataComplianceType" ], test .ExpectedMetadataComplianceType )
287+ } else {
288+ assertEqual (t , objTemplates [0 ]["metadataComplianceType" ], nil )
289+ }
285290
286- assertEqual (t , kind1 , "ConfigMap" )
287- assertEqual (t , objTemplates [1 ]["complianceType" ], test .ExpectedComplianceType )
291+ if test .ExpectedRecordDiff != "" {
292+ assertEqual (t , objTemplates [0 ]["recordDiff" ], test .ExpectedRecordDiff )
293+ } else {
294+ assertEqual (t , objTemplates [0 ]["recordDiff" ], nil )
295+ }
288296
289- if test .ExpectedMetadataComplianceType != "" {
290- assertEqual (t , objTemplates [0 ]["metadataComplianceType" ], test .ExpectedMetadataComplianceType )
291- } else {
292- assertEqual (t , objTemplates [0 ]["metadataComplianceType" ], nil )
293- }
297+ kind1 , ok := objTemplates [0 ]["objectDefinition" ].(map [string ]interface {})["kind" ]
298+ if ! ok {
299+ t .Fatal ("The objectDefinition field is an invalid format" )
300+ }
294301
295- kind2 , ok := objTemplates [1 ]["objectDefinition" ].(map [string ]interface {})["kind" ]
296- if ! ok {
297- t .Fatal ("The objectDefinition field is an invalid format" )
298- }
302+ assertEqual (t , kind1 , "ConfigMap" )
303+ assertEqual (t , objTemplates [1 ]["complianceType" ], test .ExpectedComplianceType )
299304
300- assertEqual (t , kind2 , "ConfigMap" )
305+ if test .ExpectedMetadataComplianceType != "" {
306+ assertEqual (t , objTemplates [1 ]["metadataComplianceType" ], test .ExpectedMetadataComplianceType )
307+ } else {
308+ assertEqual (t , objTemplates [1 ]["metadataComplianceType" ], nil )
309+ }
310+
311+ if test .ExpectedRecordDiff != "" {
312+ assertEqual (t , objTemplates [1 ]["recordDiff" ], test .ExpectedRecordDiff )
313+ } else {
314+ assertEqual (t , objTemplates [1 ]["recordDiff" ], nil )
315+ }
316+
317+ kind2 , ok := objTemplates [1 ]["objectDefinition" ].(map [string ]interface {})["kind" ]
318+ if ! ok {
319+ t .Fatal ("The objectDefinition field is an invalid format" )
320+ }
321+
322+ assertEqual (t , kind2 , "ConfigMap" )
323+ })
301324 }
302325}
303326
0 commit comments