@@ -1117,6 +1117,168 @@ spec:
11171117 assertEqual (t , output , expected )
11181118}
11191119
1120+ func TestCreatePolicyWithLabels (t * testing.T ) {
1121+ t .Parallel ()
1122+ tmpDir := t .TempDir ()
1123+ createConfigMap (t , tmpDir , "configmap.yaml" )
1124+
1125+ p := Plugin {}
1126+ p .PolicyDefaults .Namespace = "my-policies"
1127+ p .PolicyDefaults .PolicyLabels = map [string ]string {"test-default-label" : "default" }
1128+
1129+ policyConf := types.PolicyConfig {
1130+ Name : "policy-app-config" ,
1131+ Manifests : []types.Manifest {
1132+ {Path : path .Join (tmpDir , "configmap.yaml" )},
1133+ },
1134+ }
1135+ p .Policies = append (p .Policies , policyConf )
1136+ p .applyDefaults (map [string ]interface {}{})
1137+
1138+ err := p .createPolicy (& p .Policies [0 ])
1139+ if err != nil {
1140+ t .Fatal (err .Error ())
1141+ }
1142+
1143+ output := p .outputBuffer .String ()
1144+ expected := `
1145+ ---
1146+ apiVersion: policy.open-cluster-management.io/v1
1147+ kind: Policy
1148+ metadata:
1149+ annotations:
1150+ policy.open-cluster-management.io/categories: CM Configuration Management
1151+ policy.open-cluster-management.io/controls: CM-2 Baseline Configuration
1152+ policy.open-cluster-management.io/description: ""
1153+ policy.open-cluster-management.io/standards: NIST SP 800-53
1154+ labels:
1155+ test-default-label: default
1156+ name: policy-app-config
1157+ namespace: my-policies
1158+ spec:
1159+ disabled: false
1160+ policy-templates:
1161+ - objectDefinition:
1162+ apiVersion: policy.open-cluster-management.io/v1
1163+ kind: ConfigurationPolicy
1164+ metadata:
1165+ name: policy-app-config
1166+ spec:
1167+ object-templates:
1168+ - complianceType: musthave
1169+ objectDefinition:
1170+ apiVersion: v1
1171+ data:
1172+ game.properties: enemies=potato
1173+ kind: ConfigMap
1174+ metadata:
1175+ name: my-configmap
1176+ remediationAction: inform
1177+ severity: low
1178+ remediationAction: inform
1179+ `
1180+ expected = strings .TrimPrefix (expected , "\n " )
1181+ assertEqual (t , output , expected )
1182+
1183+ // Check for override default policy with empty map to skip default labels from the policy
1184+ p .outputBuffer .Reset ()
1185+ p .Policies [0 ].PolicyLabels = map [string ]string {}
1186+ p .applyDefaults (map [string ]interface {}{})
1187+
1188+ err = p .createPolicy (& p .Policies [0 ])
1189+ if err != nil {
1190+ t .Fatal (err .Error ())
1191+ }
1192+
1193+ output = p .outputBuffer .String ()
1194+ expected = `
1195+ ---
1196+ apiVersion: policy.open-cluster-management.io/v1
1197+ kind: Policy
1198+ metadata:
1199+ annotations:
1200+ policy.open-cluster-management.io/categories: CM Configuration Management
1201+ policy.open-cluster-management.io/controls: CM-2 Baseline Configuration
1202+ policy.open-cluster-management.io/description: ""
1203+ policy.open-cluster-management.io/standards: NIST SP 800-53
1204+ name: policy-app-config
1205+ namespace: my-policies
1206+ spec:
1207+ disabled: false
1208+ policy-templates:
1209+ - objectDefinition:
1210+ apiVersion: policy.open-cluster-management.io/v1
1211+ kind: ConfigurationPolicy
1212+ metadata:
1213+ name: policy-app-config
1214+ spec:
1215+ object-templates:
1216+ - complianceType: musthave
1217+ objectDefinition:
1218+ apiVersion: v1
1219+ data:
1220+ game.properties: enemies=potato
1221+ kind: ConfigMap
1222+ metadata:
1223+ name: my-configmap
1224+ remediationAction: inform
1225+ severity: low
1226+ remediationAction: inform
1227+ `
1228+ expected = strings .TrimPrefix (expected , "\n " )
1229+ assertEqual (t , output , expected )
1230+
1231+ // Check for override default policy labels
1232+ p .outputBuffer .Reset ()
1233+ p .Policies [0 ].PolicyLabels = map [string ]string {"test-wave-label" : "100" }
1234+ p .applyDefaults (map [string ]interface {}{})
1235+
1236+ err = p .createPolicy (& p .Policies [0 ])
1237+ if err != nil {
1238+ t .Fatal (err .Error ())
1239+ }
1240+
1241+ output = p .outputBuffer .String ()
1242+ expected = `
1243+ ---
1244+ apiVersion: policy.open-cluster-management.io/v1
1245+ kind: Policy
1246+ metadata:
1247+ annotations:
1248+ policy.open-cluster-management.io/categories: CM Configuration Management
1249+ policy.open-cluster-management.io/controls: CM-2 Baseline Configuration
1250+ policy.open-cluster-management.io/description: ""
1251+ policy.open-cluster-management.io/standards: NIST SP 800-53
1252+ labels:
1253+ test-wave-label: "100"
1254+ name: policy-app-config
1255+ namespace: my-policies
1256+ spec:
1257+ disabled: false
1258+ policy-templates:
1259+ - objectDefinition:
1260+ apiVersion: policy.open-cluster-management.io/v1
1261+ kind: ConfigurationPolicy
1262+ metadata:
1263+ name: policy-app-config
1264+ spec:
1265+ object-templates:
1266+ - complianceType: musthave
1267+ objectDefinition:
1268+ apiVersion: v1
1269+ data:
1270+ game.properties: enemies=potato
1271+ kind: ConfigMap
1272+ metadata:
1273+ name: my-configmap
1274+ remediationAction: inform
1275+ severity: low
1276+ remediationAction: inform
1277+ `
1278+ expected = strings .TrimPrefix (expected , "\n " )
1279+ assertEqual (t , output , expected )
1280+ }
1281+
11201282func TestCreatePolicyFromIamPolicyTypeManifest (t * testing.T ) {
11211283 t .Parallel ()
11221284 tmpDir := t .TempDir ()
0 commit comments