@@ -1364,6 +1364,73 @@ spec:
13641364 assertEqual (t , output , expected )
13651365}
13661366
1367+ func TestCreatePolicyFromObjectTemplatesRawManifest (t * testing.T ) {
1368+ t .Parallel ()
1369+ tmpDir := t .TempDir ()
1370+ createObjectTemplatesRawManifest (t , tmpDir , "objectTemplatesRawPluginTest.yaml" )
1371+
1372+ p := Plugin {}
1373+ p .PolicyDefaults .Namespace = "my-policies"
1374+ policyConf := types.PolicyConfig {
1375+ PolicyOptions : types.PolicyOptions {
1376+ Categories : []string {"AC Access Control" },
1377+ Controls : []string {"AC-3 Access Enforcement" },
1378+ Standards : []string {"NIST SP 800-53" },
1379+ },
1380+ Name : "policy-app-config" ,
1381+ Manifests : []types.Manifest {
1382+ {Path : path .Join (tmpDir , "objectTemplatesRawPluginTest.yaml" )},
1383+ },
1384+ }
1385+ p .Policies = append (p .Policies , policyConf )
1386+ p .applyDefaults (map [string ]interface {}{})
1387+
1388+ err := p .createPolicy (& p .Policies [0 ])
1389+ if err != nil {
1390+ t .Fatal (err .Error ())
1391+ }
1392+
1393+ output := p .outputBuffer .String ()
1394+
1395+ expected := `
1396+ ---
1397+ apiVersion: policy.open-cluster-management.io/v1
1398+ kind: Policy
1399+ metadata:
1400+ annotations:
1401+ policy.open-cluster-management.io/categories: AC Access Control
1402+ policy.open-cluster-management.io/controls: AC-3 Access Enforcement
1403+ policy.open-cluster-management.io/description: ""
1404+ policy.open-cluster-management.io/standards: NIST SP 800-53
1405+ name: policy-app-config
1406+ namespace: my-policies
1407+ spec:
1408+ disabled: false
1409+ policy-templates:
1410+ - objectDefinition:
1411+ apiVersion: policy.open-cluster-management.io/v1
1412+ kind: ConfigurationPolicy
1413+ metadata:
1414+ name: policy-app-config
1415+ spec:
1416+ object-templates-raw: |-
1417+ - complianceType: musthave
1418+ objectDefinition:
1419+ apiVersion: v1
1420+ kind: ConfigMap
1421+ metadata:
1422+ name: example
1423+ namespace: default
1424+ data:
1425+ extraData: data
1426+ remediationAction: inform
1427+ severity: low
1428+ remediationAction: inform
1429+ `
1430+ expected = strings .TrimPrefix (expected , "\n " )
1431+ assertEqual (t , output , expected )
1432+ }
1433+
13671434func TestCreatePolicyWithGkConstraintTemplate (t * testing.T ) {
13681435 t .Parallel ()
13691436 tmpDir := t .TempDir ()
@@ -3228,6 +3295,7 @@ func TestGenerateEvaluationInterval(t *testing.T) {
32283295 t .Parallel ()
32293296 tmpDir := t .TempDir ()
32303297 createConfigMap (t , tmpDir , "configmap.yaml" )
3298+ createObjectTemplatesRawManifest (t , tmpDir , "object-templates-raw.yaml" )
32313299
32323300 p := Plugin {}
32333301 var err error
@@ -3289,7 +3357,14 @@ func TestGenerateEvaluationInterval(t *testing.T) {
32893357 {Path : path .Join (tmpDir , "configmap.yaml" )},
32903358 },
32913359 }
3292- p .Policies = append (p .Policies , policyConf , policyConf2 , policyConf3 )
3360+ // Test that the policy defaults get inherited with object-templates-raw.
3361+ policyConf4 := types.PolicyConfig {
3362+ Name : "policy-app-config4" ,
3363+ Manifests : []types.Manifest {
3364+ {Path : path .Join (tmpDir , "object-templates-raw.yaml" )},
3365+ },
3366+ }
3367+ p .Policies = append (p .Policies , policyConf , policyConf2 , policyConf3 , policyConf4 )
32933368 p .applyDefaults (
32943369 map [string ]interface {}{
32953370 "policies" : []interface {}{
@@ -3331,7 +3406,7 @@ func TestGenerateEvaluationInterval(t *testing.T) {
33313406 t .Fatal (err .Error ())
33323407 }
33333408
3334- assertEqual (t , len (generatedManifests ), 9 )
3409+ assertEqual (t , len (generatedManifests ), 12 )
33353410
33363411 for _ , manifest := range generatedManifests {
33373412 kind , _ := manifest ["kind" ].(string )
@@ -3367,6 +3442,11 @@ func TestGenerateEvaluationInterval(t *testing.T) {
33673442 assertEqual (t , len (policyTemplates ), 1 )
33683443 evaluationInterval := getYAMLEvaluationInterval (t , policyTemplates [0 ], true )
33693444 assertEqual (t , len (evaluationInterval ), 0 )
3445+ } else if name == "policy-app-config4" {
3446+ assertEqual (t , len (policyTemplates ), 1 )
3447+ evaluationInterval := getYAMLEvaluationInterval (t , policyTemplates [0 ], false )
3448+ assertEqual (t , evaluationInterval ["compliant" ], "never" )
3449+ assertEqual (t , evaluationInterval ["noncompliant" ], "15s" )
33703450 }
33713451 }
33723452}
0 commit comments