From 11ce0128ad3366a115cc19cb67d88f2c254c27dd Mon Sep 17 00:00:00 2001 From: Phillip Wittrock Date: Sun, 29 Mar 2020 19:00:52 -0700 Subject: [PATCH] NamespaceTransformer tests Follow up items to #2288 --- api/builtins/NamespaceTransformer.go | 10 +-- .../NamespaceTransformer.go | 10 +-- .../NamespaceTransformer_test.go | 82 +++++++++++-------- 3 files changed, 59 insertions(+), 43 deletions(-) diff --git a/api/builtins/NamespaceTransformer.go b/api/builtins/NamespaceTransformer.go index 0eea910da..6b6ee67de 100644 --- a/api/builtins/NamespaceTransformer.go +++ b/api/builtins/NamespaceTransformer.go @@ -7,13 +7,12 @@ import ( "fmt" "sigs.k8s.io/kustomize/api/filters/namespace" - "sigs.k8s.io/kustomize/api/transform" - "sigs.k8s.io/kustomize/kyaml/filtersutil" - "sigs.k8s.io/kustomize/api/resid" "sigs.k8s.io/kustomize/api/resmap" "sigs.k8s.io/kustomize/api/resource" + "sigs.k8s.io/kustomize/api/transform" "sigs.k8s.io/kustomize/api/types" + "sigs.k8s.io/kustomize/kyaml/filtersutil" "sigs.k8s.io/yaml" ) @@ -23,8 +22,9 @@ type NamespaceTransformerPlugin struct { FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` // YAMLSupport can be set to true to use the kyaml filter instead of the - // kunstruct transformer - YAMLSupport bool + // kunstruct transformer. + // TODO: change the default to use kyaml when it is stable + YAMLSupport bool `json:"yamlSupport,omitempty" yaml:"yamlSupport,omitempty"` } func (p *NamespaceTransformerPlugin) Config( diff --git a/plugin/builtin/namespacetransformer/NamespaceTransformer.go b/plugin/builtin/namespacetransformer/NamespaceTransformer.go index 7cbc19815..9487e3350 100644 --- a/plugin/builtin/namespacetransformer/NamespaceTransformer.go +++ b/plugin/builtin/namespacetransformer/NamespaceTransformer.go @@ -8,13 +8,12 @@ import ( "fmt" "sigs.k8s.io/kustomize/api/filters/namespace" - "sigs.k8s.io/kustomize/api/transform" - "sigs.k8s.io/kustomize/kyaml/filtersutil" - "sigs.k8s.io/kustomize/api/resid" "sigs.k8s.io/kustomize/api/resmap" "sigs.k8s.io/kustomize/api/resource" + "sigs.k8s.io/kustomize/api/transform" "sigs.k8s.io/kustomize/api/types" + "sigs.k8s.io/kustomize/kyaml/filtersutil" "sigs.k8s.io/yaml" ) @@ -24,8 +23,9 @@ type plugin struct { FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` // YAMLSupport can be set to true to use the kyaml filter instead of the - // kunstruct transformer - YAMLSupport bool + // kunstruct transformer. + // TODO: change the default to use kyaml when it is stable + YAMLSupport bool `json:"yamlSupport,omitempty" yaml:"yamlSupport,omitempty"` } //noinspection GoUnusedGlobalVariable diff --git a/plugin/builtin/namespacetransformer/NamespaceTransformer_test.go b/plugin/builtin/namespacetransformer/NamespaceTransformer_test.go index 00b173591..a31a1b318 100644 --- a/plugin/builtin/namespacetransformer/NamespaceTransformer_test.go +++ b/plugin/builtin/namespacetransformer/NamespaceTransformer_test.go @@ -4,6 +4,7 @@ package main_test import ( + "fmt" "strings" "testing" @@ -11,16 +12,18 @@ import ( ) func TestNamespaceTransformer1(t *testing.T) { - th := kusttest_test.MakeEnhancedHarness(t). - PrepBuiltin("NamespaceTransformer") - defer th.Reset() - - rm := th.LoadAndRunTransformer(` + for _, b := range []bool{true, false} { + t.Run(fmt.Sprintf("yaml-%v", b), func(t *testing.T) { + th := kusttest_test.MakeEnhancedHarness(t). + PrepBuiltin("NamespaceTransformer") + defer th.Reset() + rm := th.LoadAndRunTransformer(fmt.Sprintf(` apiVersion: builtin kind: NamespaceTransformer metadata: name: notImportantHere namespace: test +yamlSupport: %v fieldSpecs: - path: metadata/namespace create: true @@ -30,7 +33,7 @@ fieldSpecs: - path: subjects kind: ClusterRoleBinding group: rbac.authorization.k8s.io -`, ` +`, b), ` apiVersion: v1 kind: ConfigMap metadata: @@ -101,16 +104,16 @@ metadata: name: crd `) - // Import note: The namespace transformer is in charge of - // the metadata.namespace field. The namespace transformer SHOULD - // NOT modify neither the "namespace" subfield within the - // ClusterRoleBinding.subjects field nor the "namespace" - // subfield in the ValidatingWebhookConfiguration.webhooks field. - // This is the role of the namereference Transformer to handle - // object reference changes (prefix/suffix and namespace). - // For use cases involving simultaneous change of name and namespace, - // refer to namespaces tests in pkg/target test suites. - th.AssertActualEqualsExpected(rm, ` + // Import note: The namespace transformer is in charge of + // the metadata.namespace field. The namespace transformer SHOULD + // NOT modify neither the "namespace" subfield within the + // ClusterRoleBinding.subjects field nor the "namespace" + // subfield in the ValidatingWebhookConfiguration.webhooks field. + // This is the role of the namereference Transformer to handle + // object reference changes (prefix/suffix and namespace). + // For use cases involving simultaneous change of name and namespace, + // refer to namespaces tests in pkg/target test suites. + th.AssertActualEqualsExpected(rm, ` apiVersion: v1 kind: ConfigMap metadata: @@ -182,14 +185,18 @@ kind: CustomResourceDefinition metadata: name: crd `) + }) + } } func TestNamespaceTransformerClusterLevelKinds(t *testing.T) { - th := kusttest_test.MakeEnhancedHarness(t). - PrepBuiltin("NamespaceTransformer") - defer th.Reset() + for _, b := range []bool{true, false} { + t.Run(fmt.Sprintf("yaml-%v", b), func(t *testing.T) { + th := kusttest_test.MakeEnhancedHarness(t). + PrepBuiltin("NamespaceTransformer") + defer th.Reset() - const noChangeExpected = ` + const noChangeExpected = ` apiVersion: v1 kind: Namespace metadata: @@ -211,12 +218,13 @@ kind: PersistentVolume metadata: name: pv1 ` - rm := th.LoadAndRunTransformer(` + rm := th.LoadAndRunTransformer(fmt.Sprintf(` apiVersion: builtin kind: NamespaceTransformer metadata: name: notImportantHere namespace: test +yamlSupport: %v fieldSpecs: - path: metadata/namespace create: true @@ -226,26 +234,32 @@ fieldSpecs: - path: subjects kind: ClusterRoleBinding group: rbac.authorization.k8s.io -`, noChangeExpected) +`, b), noChangeExpected) - th.AssertActualEqualsExpected(rm, noChangeExpected) + th.AssertActualEqualsExpected(rm, noChangeExpected) + }) + } } func TestNamespaceTransformerObjectConflict(t *testing.T) { - th := kusttest_test.MakeEnhancedHarness(t). - PrepBuiltin("NamespaceTransformer") - defer th.Reset() + for _, b := range []bool{true, false} { + t.Run(fmt.Sprintf("yaml-%v", b), func(t *testing.T) { - err := th.ErrorFromLoadAndRunTransformer(` + th := kusttest_test.MakeEnhancedHarness(t). + PrepBuiltin("NamespaceTransformer") + defer th.Reset() + + err := th.ErrorFromLoadAndRunTransformer(fmt.Sprintf(` apiVersion: builtin kind: NamespaceTransformer metadata: name: notImportantHere namespace: test +yamlSupport: %v fieldSpecs: - path: metadata/namespace create: true -`, ` +`, b), ` apiVersion: v1 kind: ConfigMap metadata: @@ -259,10 +273,12 @@ metadata: namespace: bar `) - if err == nil { - t.Fatal("expected error") - } - if !strings.Contains(err.Error(), "ID conflict") { - t.Fatalf("unexpected error: %s", err.Error()) + if err == nil { + t.Fatal("expected error") + } + if !strings.Contains(err.Error(), "ID conflict") { + t.Fatalf("unexpected error: %s", err.Error()) + } + }) } }