@@ -253,6 +253,150 @@ data:
253253 assertReflectEqual (t , annotations , map [string ]interface {}{"monica" : "geller" })
254254}
255255
256+ func TestGetPolicyTemplateMetadataPatches (t * testing.T ) {
257+ t .Parallel ()
258+ tmpDir := t .TempDir ()
259+ manifestPath := path .Join (tmpDir , "patch-configmap.yaml" )
260+ manifestYAML := `
261+ ---
262+ apiVersion: v1
263+ kind: configmap
264+ metadata:
265+ name: test-configmap
266+ namespace: test-namespace
267+ data:
268+ image: "quay.io/potatos1"
269+ `
270+ err := ioutil .WriteFile (manifestPath , []byte (manifestYAML ), 0o666 )
271+ if err != nil {
272+ t .Fatalf ("Failed to write %s" , manifestPath )
273+ }
274+
275+ patches := []map [string ]interface {}{
276+ {
277+ "metadata" : map [string ]interface {}{
278+ "name" : "patch-configmap" ,
279+ "namespace" : "patch-namespace" ,
280+ },
281+ "data" : map [string ]interface {}{
282+ "image" : "quay.io/potatos2" ,
283+ },
284+ },
285+ }
286+
287+ manifests := []types.Manifest {
288+ {Path : manifestPath , Patches : patches },
289+ }
290+ policyConf := types.PolicyConfig {
291+ Manifests : manifests ,
292+ Name : "policy-app-config" ,
293+ }
294+
295+ policyTemplates , err := getPolicyTemplates (& policyConf )
296+ if err != nil {
297+ t .Fatalf ("Failed to get the policy templates: %v " , err )
298+ }
299+ assertEqual (t , len (policyTemplates ), 1 )
300+
301+ policyTemplate := policyTemplates [0 ]
302+ objdef := policyTemplate ["objectDefinition" ]
303+ assertEqual (t , objdef ["metadata" ].(map [string ]string )["name" ], "policy-app-config" )
304+ spec , ok := objdef ["spec" ].(map [string ]interface {})
305+ if ! ok {
306+ t .Fatal ("The spec field is an invalid format" )
307+ }
308+
309+ objTemplates , ok := spec ["object-templates" ].([]map [string ]interface {})
310+ if ! ok {
311+ t .Fatal ("The object-templates field is an invalid format" )
312+ }
313+ assertEqual (t , len (objTemplates ), 1 )
314+
315+ objDef , ok := objTemplates [0 ]["objectDefinition" ].(map [string ]interface {})
316+ if ! ok {
317+ t .Fatal ("The objectDefinition field is an invalid format" )
318+ }
319+
320+ metadata , ok := objDef ["metadata" ].(map [string ]interface {})
321+ if ! ok {
322+ t .Fatal ("The metadata field is an invalid format" )
323+ }
324+
325+ name , ok := metadata ["name" ].(string )
326+ if ! ok {
327+ t .Fatal ("The metadata.name field is an invalid format" )
328+ }
329+ assertEqual (t , name , "patch-configmap" )
330+
331+ namespace , ok := metadata ["namespace" ].(string )
332+ if ! ok {
333+ t .Fatal ("The metadata.namespace field is an invalid format" )
334+ }
335+ assertEqual (t , namespace , "patch-namespace" )
336+
337+ data , ok := objDef ["data" ].(map [string ]interface {})
338+ if ! ok {
339+ t .Fatal ("The data field is an invalid format" )
340+ }
341+
342+ image , ok := data ["image" ].(string )
343+ if ! ok {
344+ t .Fatal ("The data.image field is an invalid format" )
345+ }
346+ assertEqual (t , image , "quay.io/potatos2" )
347+ }
348+
349+ func TestGetPolicyTemplateMetadataPatchesFail (t * testing.T ) {
350+ t .Parallel ()
351+ tmpDir := t .TempDir ()
352+ manifestPath := path .Join (tmpDir , "multi-configmaps.yaml" )
353+ manifestYAML := `
354+ ---
355+ apiVersion: v1
356+ kind: configmap
357+ metadata:
358+ name: test-configmap
359+ namespace: test-namespace
360+ data:
361+ image: "quay.io/potatos1"
362+ ---
363+ apiVersion: v1
364+ kind: configmap
365+ metadata:
366+ name: test2-configmap
367+ namespace: test2-namespace
368+ data:
369+ image: "quay.io/potatos1"
370+ `
371+ err := ioutil .WriteFile (manifestPath , []byte (manifestYAML ), 0o666 )
372+ if err != nil {
373+ t .Fatalf ("Failed to write %s" , manifestPath )
374+ }
375+
376+ patches := []map [string ]interface {}{
377+ {
378+ "metadata" : map [string ]interface {}{
379+ "name" : "patch-configmap" ,
380+ "namespace" : "patch-namespace" ,
381+ },
382+ "data" : map [string ]interface {}{
383+ "image" : "quay.io/potatos2" ,
384+ },
385+ },
386+ }
387+
388+ manifests := []types.Manifest {
389+ {Path : manifestPath , Patches : patches },
390+ }
391+ policyConf := types.PolicyConfig {
392+ Manifests : manifests ,
393+ Name : "policy-app-config" ,
394+ }
395+
396+ _ , err = getPolicyTemplates (& policyConf )
397+ assertEqual (t , err != nil , true )
398+ }
399+
256400func TestGetPolicyTemplateKyverno (t * testing.T ) {
257401 t .Parallel ()
258402 tmpDir := t .TempDir ()
0 commit comments