@@ -4650,6 +4650,213 @@ func TestCreatePolicyWithCopyPolicyMetadata(t *testing.T) {
46504650 }
46514651}
46524652
4653+ func TestCreatePolicyWithCustomMessage (t * testing.T ) {
4654+ t .Parallel ()
4655+ tmpDir := t .TempDir ()
4656+ createConfigMap (t , tmpDir , "configmap.yaml" )
4657+ createConfigMap (t , tmpDir , "configmap2.yaml" )
4658+
4659+ p := Plugin {}
4660+ var err error
4661+
4662+ p .baseDirectory , err = filepath .EvalSymlinks (tmpDir )
4663+ if err != nil {
4664+ t .Fatal (err .Error ())
4665+ }
4666+
4667+ p .PolicyDefaults .Namespace = "my-policies"
4668+ p .PolicyDefaults .CustomMessage = types.CustomMessage {
4669+ Compliant : "{{ default }}" ,
4670+ NonCompliant : "{{ default }}" ,
4671+ }
4672+
4673+ policyConf := types.PolicyConfig {
4674+ Name : "policy-app-config" ,
4675+ PolicyOptions : types.PolicyOptions {
4676+ ConsolidateManifests : false ,
4677+ },
4678+ Manifests : []types.Manifest {
4679+ {
4680+ Path : path .Join (tmpDir , "configmap.yaml" ),
4681+ },
4682+ {
4683+ Path : path .Join (tmpDir , "configmap2.yaml" ),
4684+ },
4685+ },
4686+ }
4687+ p .Policies = append (p .Policies , policyConf )
4688+
4689+ // Ensure values are correctly propagated/overridden
4690+ p .applyDefaults (map [string ]interface {}{})
4691+ assertEqual (t , policyConf .Manifests [0 ].ConfigurationPolicyOptions .CustomMessage .Compliant , "{{ default }}" )
4692+ assertEqual (t , policyConf .Manifests [0 ].ConfigurationPolicyOptions .CustomMessage .NonCompliant , "{{ default }}" )
4693+ assertEqual (t , policyConf .Manifests [1 ].ConfigurationPolicyOptions .CustomMessage .Compliant , "{{ default }}" )
4694+ assertEqual (t , policyConf .Manifests [1 ].ConfigurationPolicyOptions .CustomMessage .NonCompliant , "{{ default }}" )
4695+
4696+ // With consolidateManifest = false
4697+ policyConf .ConfigurationPolicyOptions = types.ConfigurationPolicyOptions {
4698+ CustomMessage : types.CustomMessage {
4699+ Compliant : "{{ root }}" ,
4700+ NonCompliant : "{{ root }}" ,
4701+ },
4702+ }
4703+ policyConf .Manifests [0 ].ConfigurationPolicyOptions = types.ConfigurationPolicyOptions {
4704+ CustomMessage : types.CustomMessage {
4705+ Compliant : "{{ manifest1 }}" ,
4706+ NonCompliant : "{{ manifest1 }}" ,
4707+ },
4708+ }
4709+ policyConf .Manifests [1 ].ConfigurationPolicyOptions = types.ConfigurationPolicyOptions {
4710+ CustomMessage : types.CustomMessage {
4711+ Compliant : "{{ manifest2 }}" ,
4712+ NonCompliant : "{{ manifest2 }}" ,
4713+ },
4714+ }
4715+
4716+ p .applyDefaults (map [string ]interface {}{})
4717+
4718+ err = p .createPolicy (& policyConf )
4719+ if err != nil {
4720+ t .Fatal (err .Error ())
4721+ }
4722+
4723+ output := p .outputBuffer .String ()
4724+ expected := `
4725+ ---
4726+ apiVersion: policy.open-cluster-management.io/v1
4727+ kind: Policy
4728+ metadata:
4729+ annotations:
4730+ policy.open-cluster-management.io/categories: ""
4731+ policy.open-cluster-management.io/controls: ""
4732+ policy.open-cluster-management.io/description: ""
4733+ policy.open-cluster-management.io/standards: ""
4734+ name: policy-app-config
4735+ namespace: my-policies
4736+ spec:
4737+ copyPolicyMetadata: false
4738+ disabled: false
4739+ policy-templates:
4740+ - objectDefinition:
4741+ apiVersion: policy.open-cluster-management.io/v1
4742+ kind: ConfigurationPolicy
4743+ metadata:
4744+ name: policy-app-config
4745+ spec:
4746+ customMessage:
4747+ compliant: '{{ manifest1 }}'
4748+ noncompliant: '{{ manifest1 }}'
4749+ object-templates:
4750+ - complianceType: musthave
4751+ objectDefinition:
4752+ apiVersion: v1
4753+ data:
4754+ game.properties: enemies=potato
4755+ kind: ConfigMap
4756+ metadata:
4757+ name: my-configmap
4758+ remediationAction: inform
4759+ severity: low
4760+ - objectDefinition:
4761+ apiVersion: policy.open-cluster-management.io/v1
4762+ kind: ConfigurationPolicy
4763+ metadata:
4764+ name: policy-app-config2
4765+ spec:
4766+ customMessage:
4767+ compliant: '{{ manifest2 }}'
4768+ noncompliant: '{{ manifest2 }}'
4769+ object-templates:
4770+ - complianceType: musthave
4771+ objectDefinition:
4772+ apiVersion: v1
4773+ data:
4774+ game.properties: enemies=potato
4775+ kind: ConfigMap
4776+ metadata:
4777+ name: my-configmap
4778+ remediationAction: inform
4779+ severity: low
4780+ remediationAction: inform
4781+ `
4782+
4783+ expected = strings .TrimPrefix (expected , "\n " )
4784+ assertEqual (t , output , expected )
4785+ p .outputBuffer .Reset ()
4786+
4787+ // With consolidateManifest = true
4788+ policyConf .PolicyOptions .ConsolidateManifests = true
4789+ err = p .assertValidConfig ()
4790+ expectedErr := "the policy policy-app-config has the customMessage " +
4791+ "value set on manifest[0] but consolidateManifests is true"
4792+ assertEqual (t , err .Error (), expectedErr )
4793+
4794+ // Note: customMessage field at the manifest level must be set to
4795+ // the same value as in the policy level when consolidateManifest = true
4796+ // to successfully generate a policy. If customMessage field is unset
4797+ // at the manifest level, applyDefaults() can be used to populate this field
4798+ // if it's set at the policyDefaults or policy level.
4799+ policyConf .Manifests [0 ].ConfigurationPolicyOptions .CustomMessage .Compliant = "{{ root }}"
4800+ policyConf .Manifests [0 ].ConfigurationPolicyOptions .CustomMessage .NonCompliant = "{{ root }}"
4801+ policyConf .Manifests [1 ].ConfigurationPolicyOptions .CustomMessage .Compliant = "{{ root }}"
4802+ policyConf .Manifests [1 ].ConfigurationPolicyOptions .CustomMessage .NonCompliant = "{{ root }}"
4803+
4804+ err = p .createPolicy (& policyConf )
4805+ if err != nil {
4806+ t .Fatal (err .Error ())
4807+ }
4808+
4809+ output = p .outputBuffer .String ()
4810+ expected = `
4811+ ---
4812+ apiVersion: policy.open-cluster-management.io/v1
4813+ kind: Policy
4814+ metadata:
4815+ annotations:
4816+ policy.open-cluster-management.io/categories: ""
4817+ policy.open-cluster-management.io/controls: ""
4818+ policy.open-cluster-management.io/description: ""
4819+ policy.open-cluster-management.io/standards: ""
4820+ name: policy-app-config
4821+ namespace: my-policies
4822+ spec:
4823+ copyPolicyMetadata: false
4824+ disabled: false
4825+ policy-templates:
4826+ - objectDefinition:
4827+ apiVersion: policy.open-cluster-management.io/v1
4828+ kind: ConfigurationPolicy
4829+ metadata:
4830+ name: policy-app-config
4831+ spec:
4832+ customMessage:
4833+ compliant: '{{ root }}'
4834+ noncompliant: '{{ root }}'
4835+ object-templates:
4836+ - complianceType: musthave
4837+ objectDefinition:
4838+ apiVersion: v1
4839+ data:
4840+ game.properties: enemies=potato
4841+ kind: ConfigMap
4842+ metadata:
4843+ name: my-configmap
4844+ - complianceType: musthave
4845+ objectDefinition:
4846+ apiVersion: v1
4847+ data:
4848+ game.properties: enemies=potato
4849+ kind: ConfigMap
4850+ metadata:
4851+ name: my-configmap
4852+ remediationAction: ""
4853+ severity: ""
4854+ `
4855+
4856+ expected = strings .TrimPrefix (expected , "\n " )
4857+ assertEqual (t , output , expected )
4858+ }
4859+
46534860// Test Patching a CR object, "MyCr", containing a list of profile objects.
46544861// Patching profile interface name and (not profile) recommend
46554862// - metadata:
0 commit comments