@@ -1556,6 +1556,73 @@ spec:
15561556 assertEqual (t , output , expected )
15571557}
15581558
1559+ func TestCreatePolicyFromObjectTemplatesRawManifest (t * testing.T ) {
1560+ t .Parallel ()
1561+ tmpDir := t .TempDir ()
1562+ createObjectTemplatesRawManifest (t , tmpDir , "objectTemplatesRawPluginTest.yaml" )
1563+
1564+ p := Plugin {}
1565+ p .PolicyDefaults .Namespace = "my-policies"
1566+ policyConf := types.PolicyConfig {
1567+ PolicyOptions : types.PolicyOptions {
1568+ Categories : []string {"AC Access Control" },
1569+ Controls : []string {"AC-3 Access Enforcement" },
1570+ Standards : []string {"NIST SP 800-53" },
1571+ },
1572+ Name : "policy-app-config" ,
1573+ Manifests : []types.Manifest {
1574+ {Path : path .Join (tmpDir , "objectTemplatesRawPluginTest.yaml" )},
1575+ },
1576+ }
1577+ p .Policies = append (p .Policies , policyConf )
1578+ p .applyDefaults (map [string ]interface {}{})
1579+
1580+ err := p .createPolicy (& p .Policies [0 ])
1581+ if err != nil {
1582+ t .Fatal (err .Error ())
1583+ }
1584+
1585+ output := p .outputBuffer .String ()
1586+
1587+ expected := `
1588+ ---
1589+ apiVersion: policy.open-cluster-management.io/v1
1590+ kind: Policy
1591+ metadata:
1592+ annotations:
1593+ policy.open-cluster-management.io/categories: AC Access Control
1594+ policy.open-cluster-management.io/controls: AC-3 Access Enforcement
1595+ policy.open-cluster-management.io/description: ""
1596+ policy.open-cluster-management.io/standards: NIST SP 800-53
1597+ name: policy-app-config
1598+ namespace: my-policies
1599+ spec:
1600+ disabled: false
1601+ policy-templates:
1602+ - objectDefinition:
1603+ apiVersion: policy.open-cluster-management.io/v1
1604+ kind: ConfigurationPolicy
1605+ metadata:
1606+ name: policy-app-config
1607+ spec:
1608+ object-templates-raw: |-
1609+ - complianceType: musthave
1610+ objectDefinition:
1611+ apiVersion: v1
1612+ kind: ConfigMap
1613+ metadata:
1614+ name: example
1615+ namespace: default
1616+ data:
1617+ extraData: data
1618+ remediationAction: inform
1619+ severity: low
1620+ remediationAction: inform
1621+ `
1622+ expected = strings .TrimPrefix (expected , "\n " )
1623+ assertEqual (t , output , expected )
1624+ }
1625+
15591626func TestCreatePolicyWithGkConstraintTemplate (t * testing.T ) {
15601627 t .Parallel ()
15611628 tmpDir := t .TempDir ()
@@ -3420,6 +3487,7 @@ func TestGenerateEvaluationInterval(t *testing.T) {
34203487 t .Parallel ()
34213488 tmpDir := t .TempDir ()
34223489 createConfigMap (t , tmpDir , "configmap.yaml" )
3490+ createObjectTemplatesRawManifest (t , tmpDir , "object-templates-raw.yaml" )
34233491
34243492 p := Plugin {}
34253493 var err error
@@ -3481,7 +3549,14 @@ func TestGenerateEvaluationInterval(t *testing.T) {
34813549 {Path : path .Join (tmpDir , "configmap.yaml" )},
34823550 },
34833551 }
3484- p .Policies = append (p .Policies , policyConf , policyConf2 , policyConf3 )
3552+ // Test that the policy defaults get inherited with object-templates-raw.
3553+ policyConf4 := types.PolicyConfig {
3554+ Name : "policy-app-config4" ,
3555+ Manifests : []types.Manifest {
3556+ {Path : path .Join (tmpDir , "object-templates-raw.yaml" )},
3557+ },
3558+ }
3559+ p .Policies = append (p .Policies , policyConf , policyConf2 , policyConf3 , policyConf4 )
34853560 p .applyDefaults (
34863561 map [string ]interface {}{
34873562 "policies" : []interface {}{
@@ -3523,7 +3598,7 @@ func TestGenerateEvaluationInterval(t *testing.T) {
35233598 t .Fatal (err .Error ())
35243599 }
35253600
3526- assertEqual (t , len (generatedManifests ), 9 )
3601+ assertEqual (t , len (generatedManifests ), 12 )
35273602
35283603 for _ , manifest := range generatedManifests {
35293604 kind , _ := manifest ["kind" ].(string )
@@ -3559,6 +3634,11 @@ func TestGenerateEvaluationInterval(t *testing.T) {
35593634 assertEqual (t , len (policyTemplates ), 1 )
35603635 evaluationInterval := getYAMLEvaluationInterval (t , policyTemplates [0 ], true )
35613636 assertEqual (t , len (evaluationInterval ), 0 )
3637+ } else if name == "policy-app-config4" {
3638+ assertEqual (t , len (policyTemplates ), 1 )
3639+ evaluationInterval := getYAMLEvaluationInterval (t , policyTemplates [0 ], false )
3640+ assertEqual (t , evaluationInterval ["compliant" ], "never" )
3641+ assertEqual (t , evaluationInterval ["noncompliant" ], "15s" )
35623642 }
35633643 }
35643644}
0 commit comments