@@ -1496,6 +1496,123 @@ spec:
14961496 assertEqual (t , output , expected )
14971497}
14981498
1499+ func TestCreatePolicyHubTemplateOptions (t * testing.T ) {
1500+ t .Parallel ()
1501+ tmpDir := t .TempDir ()
1502+ createConfigMap (t , tmpDir , "configmap.yaml" )
1503+
1504+ p := Plugin {}
1505+ p .PolicyDefaults .Namespace = "my-policies"
1506+ p .PolicyDefaults .HubTemplateOptions = types.HubTemplateOptions {ServiceAccountName : "default-sa" }
1507+
1508+ policyConf := types.PolicyConfig {
1509+ Name : "policy-app-config" ,
1510+ Manifests : []types.Manifest {
1511+ {Path : path .Join (tmpDir , "configmap.yaml" )},
1512+ },
1513+ }
1514+ p .Policies = append (p .Policies , policyConf )
1515+
1516+ p .applyDefaults (map [string ]interface {}{})
1517+
1518+ err := p .createPolicy (& p .Policies [0 ])
1519+ if err != nil {
1520+ t .Fatal (err .Error ())
1521+ }
1522+
1523+ output := p .outputBuffer .String ()
1524+ expected := `
1525+ ---
1526+ apiVersion: policy.open-cluster-management.io/v1
1527+ kind: Policy
1528+ metadata:
1529+ annotations:
1530+ policy.open-cluster-management.io/categories: CM Configuration Management
1531+ policy.open-cluster-management.io/controls: CM-2 Baseline Configuration
1532+ policy.open-cluster-management.io/description: ""
1533+ policy.open-cluster-management.io/standards: NIST SP 800-53
1534+ name: policy-app-config
1535+ namespace: my-policies
1536+ spec:
1537+ disabled: false
1538+ hubTemplateOptions:
1539+ serviceAccountName: default-sa
1540+ policy-templates:
1541+ - objectDefinition:
1542+ apiVersion: policy.open-cluster-management.io/v1
1543+ kind: ConfigurationPolicy
1544+ metadata:
1545+ name: policy-app-config
1546+ spec:
1547+ object-templates:
1548+ - complianceType: musthave
1549+ objectDefinition:
1550+ apiVersion: v1
1551+ data:
1552+ game.properties: enemies=potato
1553+ kind: ConfigMap
1554+ metadata:
1555+ name: my-configmap
1556+ remediationAction: inform
1557+ severity: low
1558+ remediationAction: inform
1559+ `
1560+ expected = strings .TrimPrefix (expected , "\n " )
1561+ assertEqual (t , output , expected )
1562+
1563+ // Override the value on the policy
1564+ p .outputBuffer .Reset ()
1565+ p .Policies [0 ].PolicyOptions = types.PolicyOptions {
1566+ HubTemplateOptions : types.HubTemplateOptions {ServiceAccountName : "override-sa" },
1567+ }
1568+ p .applyDefaults (map [string ]interface {}{})
1569+
1570+ err = p .createPolicy (& p .Policies [0 ])
1571+ if err != nil {
1572+ t .Fatal (err .Error ())
1573+ }
1574+
1575+ output = p .outputBuffer .String ()
1576+ expected = `
1577+ ---
1578+ apiVersion: policy.open-cluster-management.io/v1
1579+ kind: Policy
1580+ metadata:
1581+ annotations:
1582+ policy.open-cluster-management.io/categories: CM Configuration Management
1583+ policy.open-cluster-management.io/controls: CM-2 Baseline Configuration
1584+ policy.open-cluster-management.io/description: ""
1585+ policy.open-cluster-management.io/standards: NIST SP 800-53
1586+ name: policy-app-config
1587+ namespace: my-policies
1588+ spec:
1589+ disabled: false
1590+ hubTemplateOptions:
1591+ serviceAccountName: override-sa
1592+ policy-templates:
1593+ - objectDefinition:
1594+ apiVersion: policy.open-cluster-management.io/v1
1595+ kind: ConfigurationPolicy
1596+ metadata:
1597+ name: policy-app-config
1598+ spec:
1599+ object-templates:
1600+ - complianceType: musthave
1601+ objectDefinition:
1602+ apiVersion: v1
1603+ data:
1604+ game.properties: enemies=potato
1605+ kind: ConfigMap
1606+ metadata:
1607+ name: my-configmap
1608+ remediationAction: inform
1609+ severity: low
1610+ remediationAction: inform
1611+ `
1612+ expected = strings .TrimPrefix (expected , "\n " )
1613+ assertEqual (t , output , expected )
1614+ }
1615+
14991616func TestCreatePolicyFromCertificatePolicyTypeManifest (t * testing.T ) {
15001617 t .Parallel ()
15011618 tmpDir := t .TempDir ()
0 commit comments