@@ -6,9 +6,11 @@ import (
66 "io/ioutil"
77 "path"
88 "path/filepath"
9+ "reflect"
910 "strings"
1011 "testing"
1112
13+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1214 "open-cluster-management.io/ocm-kustomize-generator-plugins/internal/types"
1315)
1416
@@ -2365,3 +2367,103 @@ func TestCreatePolicyWithConfigPolicyAnnotations(t *testing.T) {
23652367 })
23662368 }
23672369}
2370+
2371+ func TestCreatePolicyWithNamespaceSelector (t * testing.T ) {
2372+ t .Parallel ()
2373+ tmpDir := t .TempDir ()
2374+ createConfigMap (t , tmpDir , "configmap.yaml" )
2375+
2376+ tests := map [string ]struct {
2377+ name string
2378+ namespaceSelector types.NamespaceSelector
2379+ }{
2380+ "nil-selector" : {namespaceSelector : types.NamespaceSelector {}},
2381+ "empty-selector-values" : {
2382+ namespaceSelector : types.NamespaceSelector {
2383+ Include : []string {},
2384+ Exclude : []string {},
2385+ MatchLabels : & map [string ]string {},
2386+ MatchExpressions : & []metav1.LabelSelectorRequirement {},
2387+ },
2388+ },
2389+ "completely-filled-values" : {
2390+ namespaceSelector : types.NamespaceSelector {
2391+ Include : []string {"test-ns-1" , "test-ns-2" },
2392+ Exclude : []string {"*-ns-[1]" },
2393+ MatchLabels : & map [string ]string {
2394+ "testing" : "is awesome" ,
2395+ },
2396+ MatchExpressions : & []metav1.LabelSelectorRequirement {{
2397+ Key : "door" ,
2398+ Operator : "Exists" ,
2399+ }},
2400+ },
2401+ },
2402+ "include-exclude-only" : {
2403+ namespaceSelector : types.NamespaceSelector {
2404+ Include : []string {"test-ns-1" , "test-ns-2" },
2405+ Exclude : []string {"*-ns-[1]" },
2406+ },
2407+ },
2408+ "label-selectors-only" : {
2409+ namespaceSelector : types.NamespaceSelector {
2410+ MatchLabels : & map [string ]string {
2411+ "testing" : "is awesome" ,
2412+ },
2413+ MatchExpressions : & []metav1.LabelSelectorRequirement {{
2414+ Key : "door" ,
2415+ Operator : "Exists" ,
2416+ }},
2417+ },
2418+ },
2419+ }
2420+
2421+ for name , test := range tests {
2422+ test := test
2423+
2424+ t .Run (name , func (t * testing.T ) {
2425+ t .Parallel ()
2426+
2427+ p := Plugin {}
2428+ p .PolicyDefaults .Namespace = "my-policies"
2429+ p .PolicyDefaults .NamespaceSelector = types.NamespaceSelector {
2430+ MatchLabels : & map [string ]string {},
2431+ }
2432+ policyConf := types.PolicyConfig {
2433+ Name : "policy-app-config" , Manifests : []types.Manifest {
2434+ {Path : path .Join (tmpDir , "configmap.yaml" )},
2435+ },
2436+ }
2437+ policyConf .NamespaceSelector = test .namespaceSelector
2438+
2439+ p .Policies = append (p .Policies , policyConf )
2440+ p .applyDefaults (map [string ]interface {}{})
2441+
2442+ err := p .createPolicy (& p .Policies [0 ])
2443+ if err != nil {
2444+ t .Fatal (err .Error ())
2445+ }
2446+
2447+ output := p .outputBuffer .Bytes ()
2448+ policyManifests , err := unmarshalManifestBytes (output )
2449+ if err != nil {
2450+ t .Fatal (err .Error ())
2451+ }
2452+ // nolint: forcetypeassert
2453+ spec := (* policyManifests )[0 ]["spec" ].(map [string ]interface {})
2454+ policyTemplates := spec ["policy-templates" ].([]interface {})
2455+ // nolint: forcetypeassert
2456+ configPolicy := policyTemplates [0 ].(map [string ]interface {})["objectDefinition" ].(map [string ]interface {})
2457+ // nolint: forcetypeassert
2458+ configPolicySpec := configPolicy ["spec" ].(map [string ]interface {})
2459+ // nolint: forcetypeassert
2460+ configPolicySelector := configPolicySpec ["namespaceSelector" ].(map [string ]interface {})
2461+
2462+ if reflect .DeepEqual (test .namespaceSelector , types.NamespaceSelector {}) {
2463+ assertSelectorEqual (t , configPolicySelector , p .PolicyDefaults .NamespaceSelector )
2464+ } else {
2465+ assertSelectorEqual (t , configPolicySelector , test .namespaceSelector )
2466+ }
2467+ })
2468+ }
2469+ }
0 commit comments