@@ -233,6 +233,130 @@ data:
233233 }
234234}
235235
236+ func TestIsPolicyTypeManifest (t * testing.T ) {
237+ t .Parallel ()
238+
239+ invalidAPI := []string {
240+ "policy.open-cluster-management.io/v1" ,
241+ "apps.open-cluster-management.io/v1" ,
242+ }
243+
244+ invalidKind := []string {
245+ "CertificatePolicy" ,
246+ "IamPolicy" ,
247+ }
248+
249+ tests := []struct {
250+ apiVersion string
251+ kind string
252+ invalidAPI []string
253+ invalidKind []string
254+ expectedFlag bool
255+ expectedErrMsg string
256+ }{
257+ {"policy.open-cluster-management.io/v1" , "IamPolicy" , nil , nil , true , "" },
258+ {"policy.open-cluster-management.io/v1" , "CertificatePolicy" , nil , nil , true , "" },
259+ {"policy.open-cluster-management.io/v1" , "ConfigurationPolicy" , nil , nil , true , "" },
260+ {"policy.open-cluster-management.io/v1" , "Policy" , nil , nil , false , "" },
261+ {"apps.open-cluster-management.io/v1" , "PlacementRule" , nil , nil , false , "" },
262+ {"" , "" , nil , nil , false , "" },
263+ {"" , "IamPolicy" , invalidAPI , nil , false , "invalid non-string apiVersion format" },
264+ {"policy.open-cluster-management.io/v1" , "" , nil , invalidKind , false , "invalid non-string kind format" },
265+ }
266+
267+ for _ , test := range tests {
268+ test := test
269+ t .Run (
270+ fmt .Sprintf ("apiVersion=%s, kind=%s" , test .apiVersion , test .kind ),
271+ func (t * testing.T ) {
272+ t .Parallel ()
273+ manifest := map [string ]interface {}{}
274+
275+ if test .invalidAPI == nil {
276+ manifest ["apiVersion" ] = test .apiVersion
277+ } else {
278+ manifest ["apiVersion" ] = test .invalidAPI
279+ }
280+
281+ if test .invalidKind == nil {
282+ manifest ["kind" ] = test .kind
283+ } else {
284+ manifest ["kind" ] = test .invalidKind
285+ }
286+
287+ isPolicyType , err := isPolicyTypeManifest (manifest )
288+ assertEqual (t , isPolicyType , test .expectedFlag )
289+
290+ if test .expectedErrMsg == "" {
291+ assertEqual (t , err , nil )
292+ } else {
293+ assertEqual (t , err .Error (), test .expectedErrMsg )
294+ }
295+ },
296+ )
297+ }
298+ }
299+
300+ func TestGetPolicyTemplateFromPolicyTypeManifest (t * testing.T ) {
301+ t .Parallel ()
302+ tmpDir := t .TempDir ()
303+ manifestFiles := []types.Manifest {}
304+ createIamPolicyManifest (t , tmpDir , "iamKindManifest.yaml" )
305+ // Test manifest is non-root IAM policy type.
306+ IamManifestPath := path .Join (tmpDir , "iamKindManifest.yaml" )
307+
308+ manifestFiles = append (
309+ manifestFiles , types.Manifest {Path : IamManifestPath },
310+ )
311+
312+ // Test both passing in individual files and a flat directory.
313+ tests := []struct {
314+ Manifests []types.Manifest
315+ }{
316+ {Manifests : manifestFiles },
317+ {
318+ Manifests : []types.Manifest {{Path : tmpDir }},
319+ },
320+ }
321+
322+ for _ , test := range tests {
323+ policyConf := types.PolicyConfig {
324+ Manifests : test .Manifests ,
325+ Name : "policy-limitclusteradmin" ,
326+ RemediationAction : "inform" ,
327+ Severity : "low" ,
328+ }
329+
330+ policyTemplates , err := getPolicyTemplates (& policyConf )
331+ if err != nil {
332+ t .Fatalf ("Failed to get the policy templates: %v" , err )
333+ }
334+ assertEqual (t , len (policyTemplates ), 1 )
335+
336+ IamPolicyTemplate := policyTemplates [0 ]
337+ IamObjdef := IamPolicyTemplate ["objectDefinition" ]
338+ assertEqual (t , IamObjdef ["apiVersion" ], "policy.open-cluster-management.io/v1" )
339+ // kind will not be overridden by "ConfigurationPolicy".
340+ assertEqual (t , IamObjdef ["kind" ], "IamPolicy" )
341+ assertEqual (t , IamObjdef ["metadata" ].(map [string ]interface {})["name" ], "policy-limitclusteradmin-example" )
342+ IamSpec , ok := IamObjdef ["spec" ].(map [string ]interface {})
343+ if ! ok {
344+ t .Fatal ("The spec field is an invalid format" )
345+ }
346+ // remediationAction will not be overridden by policyConf.
347+ assertEqual (t , IamSpec ["remediationAction" ], "enforce" )
348+ // severity will not be overridden by policyConf.
349+ assertEqual (t , IamSpec ["severity" ], "medium" )
350+ assertEqual (t , IamSpec ["maxClusterRoleBindingUsers" ], 5 )
351+ namespaceSelector , ok := IamSpec ["namespaceSelector" ].(map [string ]interface {})
352+ if ! ok {
353+ t .Fatal ("The namespaceSelector field is an invalid format" )
354+ }
355+ assertReflectEqual (t , namespaceSelector ["include" ], []interface {}{"*" })
356+ assertReflectEqual (t , namespaceSelector ["exclude" ], []interface {}{"kube-*" , "openshift-*" })
357+ }
358+ }
359+
236360func TestGetPolicyTemplatePatches (t * testing.T ) {
237361 t .Parallel ()
238362 tmpDir := t .TempDir ()
0 commit comments