From bcc7412ef2b0271a17f7b60f74f5a4c4bbadefd6 Mon Sep 17 00:00:00 2001 From: Jeffrey Regan Date: Tue, 7 May 2019 14:18:35 -0700 Subject: [PATCH] Make kusttestharness shareable. --- .../kusttestharness.go} | 25 +++--- pkg/plugins/loader.go | 2 + pkg/resource/factory.go | 13 +++ pkg/target/baseandoverlaymedium_test.go | 34 ++++---- pkg/target/baseandoverlaysmall_test.go | 57 ++++++------- pkg/target/builtinplugins_test.go | 24 +++--- pkg/target/chartinflatorexecplugin_test.go | 11 +-- pkg/target/configmaps_test.go | 44 +++++----- pkg/target/crd_test.go | 40 ++++----- pkg/target/customconfig_test.go | 48 ++++++----- pkg/target/generatormergeandreplace_test.go | 40 ++++----- pkg/target/generatoroptions_test.go | 23 ++--- pkg/target/generatorplugin_test.go | 42 +++++----- pkg/target/kusttarget_test.go | 83 ++++++++++--------- pkg/target/multiplepatch_test.go | 30 +++---- pkg/target/namespacedgenerators_test.go | 10 ++- pkg/target/nullvalues_test.go | 12 +-- pkg/target/plugindir_test.go | 5 +- pkg/target/pruneconfigmap_test.go | 16 ++-- pkg/target/resourceconflict_test.go | 30 +++---- pkg/target/transformerplugin_test.go | 43 +++++----- pkg/target/transformersimage_test.go | 44 +++++----- pkg/target/variableref_test.go | 56 +++++++------ 23 files changed, 389 insertions(+), 343 deletions(-) rename pkg/{target/kusttestharness_test.go => kusttest/kusttestharness.go} (87%) diff --git a/pkg/target/kusttestharness_test.go b/pkg/kusttest/kusttestharness.go similarity index 87% rename from pkg/target/kusttestharness_test.go rename to pkg/kusttest/kusttestharness.go index dc9ed1ed4..e3d3b245e 100644 --- a/pkg/target/kusttestharness_test.go +++ b/pkg/kusttest/kusttestharness.go @@ -14,9 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package target_test - -// A collection of utilities used in target tests. +package kusttest_test import ( "fmt" @@ -33,11 +31,12 @@ import ( "sigs.k8s.io/kustomize/pkg/plugins" "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" - . "sigs.k8s.io/kustomize/pkg/target" + "sigs.k8s.io/kustomize/pkg/target" "sigs.k8s.io/kustomize/pkg/transformers/config/defaultconfig" "sigs.k8s.io/kustomize/pkg/types" ) +// KustTestHarness helps test kustomization generation and transformation. type KustTestHarness struct { t *testing.T rf *resmap.Factory @@ -69,8 +68,8 @@ func NewKustTestHarnessFull( pl: plugins.NewLoader(pc, rf)} } -func (th *KustTestHarness) makeKustTarget() *KustTarget { - kt, err := NewKustTarget( +func (th *KustTestHarness) MakeKustTarget() *target.KustTarget { + kt, err := target.NewKustTarget( th.ldr, th.rf, transformer.NewFactoryImpl(), th.pl) if err != nil { th.t.Fatalf("Unexpected construction error %v", err) @@ -78,29 +77,29 @@ func (th *KustTestHarness) makeKustTarget() *KustTarget { return kt } -func (th *KustTestHarness) writeF(dir string, content string) { +func (th *KustTestHarness) WriteF(dir string, content string) { err := th.ldr.AddFile(dir, []byte(content)) if err != nil { th.t.Fatalf("failed write to %s; %v", dir, err) } } -func (th *KustTestHarness) writeK(dir string, content string) { - th.writeF(filepath.Join(dir, pgmconfig.KustomizationFileNames[0]), ` +func (th *KustTestHarness) WriteK(dir string, content string) { + th.WriteF(filepath.Join(dir, pgmconfig.KustomizationFileNames[0]), ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization `+content) } -func (th *KustTestHarness) fromMap(m map[string]interface{}) *resource.Resource { +func (th *KustTestHarness) FromMap(m map[string]interface{}) *resource.Resource { return th.rf.RF().FromMap(m) } -func (th *KustTestHarness) fromMapAndOption(m map[string]interface{}, args *types.GeneratorArgs, option *types.GeneratorOptions) *resource.Resource { +func (th *KustTestHarness) FromMapAndOption(m map[string]interface{}, args *types.GeneratorArgs, option *types.GeneratorOptions) *resource.Resource { return th.rf.RF().FromMapAndOption(m, args, option) } -func (th *KustTestHarness) writeDefaultConfigs(fName string) { +func (th *KustTestHarness) WriteDefaultConfigs(fName string) { m := defaultconfig.GetDefaultFieldSpecsAsMap() var content []byte for _, tCfg := range m { @@ -144,7 +143,7 @@ func hint(a, b string) string { return "X" } -func (th *KustTestHarness) assertActualEqualsExpected( +func (th *KustTestHarness) AssertActualEqualsExpected( m resmap.ResMap, expected string) { if m == nil { th.t.Fatalf("Map should not be nil.") diff --git a/pkg/plugins/loader.go b/pkg/plugins/loader.go index 40068a7d1..07a0d89e2 100644 --- a/pkg/plugins/loader.go +++ b/pkg/plugins/loader.go @@ -104,6 +104,8 @@ func (l *Loader) loadAndConfigurePlugin( return c, nil } +// registry is a means to avoid trying to load the same .so file +// into memory more than once, which results in an error. // Each test makes its own loader, and tries to load its own plugins, // but the loaded .so files are in shared memory, so one will get // "this plugin already loaded" errors if the registry is maintained diff --git a/pkg/resource/factory.go b/pkg/resource/factory.go index f3543a8df..6a5a8f8cc 100644 --- a/pkg/resource/factory.go +++ b/pkg/resource/factory.go @@ -85,6 +85,19 @@ func (rf *Factory) SliceFromPatches( return result, nil } +// FromBytes unmarshalls bytes into one Resource. +func (rf *Factory) FromBytes(in []byte) (*Resource, error) { + result, err := rf.SliceFromBytes(in) + if err != nil { + return nil, err + } + if len(result) != 1 { + return nil, fmt.Errorf( + "expected 1 resource, found %d in %v", len(result), in) + } + return result[0], nil +} + // SliceFromBytes unmarshalls bytes into a Resource slice. func (rf *Factory) SliceFromBytes(in []byte) ([]*Resource, error) { kunStructs, err := rf.kf.SliceFromBytes(in) diff --git a/pkg/target/baseandoverlaymedium_test.go b/pkg/target/baseandoverlaymedium_test.go index ade613b22..9507e6d51 100644 --- a/pkg/target/baseandoverlaymedium_test.go +++ b/pkg/target/baseandoverlaymedium_test.go @@ -18,6 +18,8 @@ package target_test import ( "testing" + + "sigs.k8s.io/kustomize/pkg/kusttest" ) // TODO(monopole): Add a feature test example covering secret generation. @@ -35,8 +37,8 @@ import ( // To eventually fix this, we could write the data to a real filesystem, and // clean up after, or use some other trick compatible with exec. -func writeMediumBase(th *KustTestHarness) { - th.writeK("/app/base", ` +func writeMediumBase(th *kusttest_test.KustTestHarness) { + th.WriteK("/app/base", ` namePrefix: baseprefix- commonLabels: foo: bar @@ -46,7 +48,7 @@ resources: - deployment/deployment.yaml - service/service.yaml `) - th.writeF("/app/base/service/service.yaml", ` + th.WriteF("/app/base/service/service.yaml", ` apiVersion: v1 kind: Service metadata: @@ -59,7 +61,7 @@ spec: selector: app: mungebot `) - th.writeF("/app/base/deployment/deployment.yaml", ` + th.WriteF("/app/base/deployment/deployment.yaml", ` apiVersion: extensions/v1beta1 kind: Deployment metadata: @@ -85,13 +87,13 @@ spec: } func TestMediumBase(t *testing.T) { - th := NewKustTestHarness(t, "/app/base") + th := kusttest_test.NewKustTestHarness(t, "/app/base") writeMediumBase(th) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 kind: Service metadata: @@ -142,9 +144,9 @@ spec: } func TestMediumOverlay(t *testing.T) { - th := NewKustTestHarness(t, "/app/overlay") + th := kusttest_test.NewKustTestHarness(t, "/app/overlay") writeMediumBase(th) - th.writeK("/app/overlay", ` + th.WriteK("/app/overlay", ` namePrefix: test-infra- commonLabels: app: mungebot @@ -169,24 +171,24 @@ images: - name: nginx newTag: 1.8.0`) - th.writeF("/app/overlay/configmap/db.env", ` + th.WriteF("/app/overlay/configmap/db.env", ` DB_USERNAME=admin DB_PASSWORD=somepw `) - th.writeF("/app/overlay/configmap/units.ini", ` + th.WriteF("/app/overlay/configmap/units.ini", ` LENGTH=kilometer ENERGY=electronvolt `) - th.writeF("/app/overlay/configmap/food.ini", ` + th.WriteF("/app/overlay/configmap/food.ini", ` FRUIT=banana LEGUME=chickpea `) - th.writeF("/app/overlay/configmap/dummy.txt", + th.WriteF("/app/overlay/configmap/dummy.txt", `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. `) - th.writeF("/app/overlay/deployment/deployment.yaml", ` + th.WriteF("/app/overlay/deployment/deployment.yaml", ` apiVersion: extensions/v1beta1 kind: Deployment metadata: @@ -219,14 +221,14 @@ spec: name: app-env name: app-env `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } // TODO(#669): The name of the patched Deployment is // test-infra-baseprefix-mungebot, retaining the base // prefix (example of correct behavior). - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: nonsense: "Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod diff --git a/pkg/target/baseandoverlaysmall_test.go b/pkg/target/baseandoverlaysmall_test.go index 5743f4adf..fbb406ec9 100644 --- a/pkg/target/baseandoverlaysmall_test.go +++ b/pkg/target/baseandoverlaysmall_test.go @@ -21,11 +21,12 @@ import ( "testing" "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" + "sigs.k8s.io/kustomize/pkg/kusttest" "sigs.k8s.io/kustomize/pkg/loader" ) -func writeSmallBase(th *KustTestHarness) { - th.writeK("/app/base", ` +func writeSmallBase(th *kusttest_test.KustTestHarness) { + th.WriteK("/app/base", ` namePrefix: a- commonLabels: app: myApp @@ -33,7 +34,7 @@ resources: - deployment.yaml - service.yaml `) - th.writeF("/app/base/service.yaml", ` + th.WriteF("/app/base/service.yaml", ` apiVersion: v1 kind: Service metadata: @@ -44,7 +45,7 @@ spec: ports: - port: 7002 `) - th.writeF("/app/base/deployment.yaml", ` + th.WriteF("/app/base/deployment.yaml", ` apiVersion: apps/v1 kind: Deployment metadata: @@ -62,13 +63,13 @@ spec: } func TestSmallBase(t *testing.T) { - th := NewKustTestHarness(t, "/app/base") + th := kusttest_test.NewKustTestHarness(t, "/app/base") writeSmallBase(th) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 kind: Service metadata: @@ -105,9 +106,9 @@ spec: } func TestSmallOverlay(t *testing.T) { - th := NewKustTestHarness(t, "/app/overlay") + th := kusttest_test.NewKustTestHarness(t, "/app/overlay") writeSmallBase(th) - th.writeK("/app/overlay", ` + th.WriteK("/app/overlay", ` namePrefix: b- commonLabels: env: prod @@ -120,15 +121,15 @@ images: newTag: 1.8.0 `) - th.writeF("/app/overlay/configmap/app.env", ` + th.WriteF("/app/overlay/configmap/app.env", ` DB_USERNAME=admin DB_PASSWORD=somepw `) - th.writeF("/app/overlay/configmap/app-init.ini", ` + th.WriteF("/app/overlay/configmap/app-init.ini", ` FOO=bar BAR=baz `) - th.writeF("/app/overlay/deployment/deployment.yaml", ` + th.WriteF("/app/overlay/deployment/deployment.yaml", ` apiVersion: apps/v1 kind: Deployment metadata: @@ -136,14 +137,14 @@ metadata: spec: replicas: 1000 `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } // TODO(#669): The name of the patched Deployment is // b-a-myDeployment, retaining the base prefix // (example of correct behavior). - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 kind: Service metadata: @@ -186,11 +187,11 @@ spec: } func TestSharedPatchDisAllowed(t *testing.T) { - th := NewKustTestHarnessFull( + th := kusttest_test.NewKustTestHarnessFull( t, "/app/overlay", loader.RestrictionRootOnly, plugin.DefaultPluginConfig()) writeSmallBase(th) - th.writeK("/app/overlay", ` + th.WriteK("/app/overlay", ` commonLabels: env: prod bases: @@ -198,7 +199,7 @@ bases: patchesStrategicMerge: - ../shared/deployment-patch.yaml `) - th.writeF("/app/shared/deployment-patch.yaml", ` + th.WriteF("/app/shared/deployment-patch.yaml", ` apiVersion: apps/v1 kind: Deployment metadata: @@ -206,7 +207,7 @@ metadata: spec: replicas: 1000 `) - _, err := th.makeKustTarget().MakeCustomizedResMap() + _, err := th.MakeKustTarget().MakeCustomizedResMap() if err == nil { t.Fatalf("expected error") } @@ -218,11 +219,11 @@ spec: } func TestSharedPatchAllowed(t *testing.T) { - th := NewKustTestHarnessFull( + th := kusttest_test.NewKustTestHarnessFull( t, "/app/overlay", loader.RestrictionNone, plugin.DefaultPluginConfig()) writeSmallBase(th) - th.writeK("/app/overlay", ` + th.WriteK("/app/overlay", ` commonLabels: env: prod bases: @@ -230,7 +231,7 @@ bases: patchesStrategicMerge: - ../shared/deployment-patch.yaml `) - th.writeF("/app/shared/deployment-patch.yaml", ` + th.WriteF("/app/shared/deployment-patch.yaml", ` apiVersion: apps/v1 kind: Deployment metadata: @@ -238,11 +239,11 @@ metadata: spec: replicas: 1000 `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 kind: Service metadata: @@ -285,9 +286,9 @@ spec: } func TestSmallOverlayJSONPatch(t *testing.T) { - th := NewKustTestHarness(t, "/app/overlay") + th := kusttest_test.NewKustTestHarness(t, "/app/overlay") writeSmallBase(th) - th.writeK("/app/overlay", ` + th.WriteK("/app/overlay", ` bases: - ../base patchesJson6902: @@ -298,16 +299,16 @@ patchesJson6902: path: service-patch.yaml `) - th.writeF("/app/overlay/service-patch.yaml", ` + th.WriteF("/app/overlay/service-patch.yaml", ` - op: add path: /spec/selector/backend value: beagle `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 kind: Service metadata: diff --git a/pkg/target/builtinplugins_test.go b/pkg/target/builtinplugins_test.go index 90fc27f76..c7a190cd7 100644 --- a/pkg/target/builtinplugins_test.go +++ b/pkg/target/builtinplugins_test.go @@ -18,6 +18,8 @@ package target_test import ( "testing" + + "sigs.k8s.io/kustomize/pkg/kusttest" ) const result = ` @@ -35,17 +37,17 @@ metadata: type: Opaque ` -func writeDataFiles(th *KustTestHarness) { - th.writeF("/app/foo.env", ` +func writeDataFiles(th *kusttest_test.KustTestHarness) { + th.WriteF("/app/foo.env", ` MOUNTAIN=everest OCEAN=pacific `) - th.writeF("/app/phrase.dat", "dat phrase") + th.WriteF("/app/phrase.dat", "dat phrase") } func TestBuiltinPlugins(t *testing.T) { - th := NewKustTestHarness(t, "/app") - th.writeK("/app", ` + th := kusttest_test.NewKustTestHarness(t, "/app") + th.WriteK("/app", ` secretGenerator: - name: bob kvSources: @@ -65,16 +67,16 @@ secretGenerator: - foo.env `) writeDataFiles(th) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, result) + th.AssertActualEqualsExpected(m, result) } func TestBuiltinIsTheDefault(t *testing.T) { - th := NewKustTestHarness(t, "/app") - th.writeK("/app", ` + th := kusttest_test.NewKustTestHarness(t, "/app") + th.WriteK("/app", ` secretGenerator: - name: bob kvSources: @@ -91,9 +93,9 @@ secretGenerator: - foo.env `) writeDataFiles(th) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, result) + th.AssertActualEqualsExpected(m, result) } diff --git a/pkg/target/chartinflatorexecplugin_test.go b/pkg/target/chartinflatorexecplugin_test.go index b7e8494d3..eb47ae627 100644 --- a/pkg/target/chartinflatorexecplugin_test.go +++ b/pkg/target/chartinflatorexecplugin_test.go @@ -22,6 +22,7 @@ import ( "sigs.k8s.io/kustomize/internal/plugintest" "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" + "sigs.k8s.io/kustomize/pkg/kusttest" ) // This is an example of using a helm chart as a base, @@ -44,15 +45,15 @@ func TestChartInflatorExecPlugin(t *testing.T) { tc.BuildExecPlugin( "someteam.example.com", "v1", "ChartInflatorExec") - th := NewKustTestHarnessWithPluginConfig( + th := kusttest_test.NewKustTestHarnessWithPluginConfig( t, "/app", plugin.ActivePluginConfig()) - th.writeK("/app", ` + th.WriteK("/app", ` generators: - chartInflatorExec.yaml namePrefix: LOOOOOOOONG- `) - th.writeF("/app/chartInflatorExec.yaml", ` + th.WriteF("/app/chartInflatorExec.yaml", ` apiVersion: someteam.example.com/v1 kind: ChartInflatorExec metadata: @@ -60,11 +61,11 @@ metadata: chartName: minecraft `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: rcon-password: Q0hBTkdFTUUh diff --git a/pkg/target/configmaps_test.go b/pkg/target/configmaps_test.go index dd69adb3b..2a7dc7738 100644 --- a/pkg/target/configmaps_test.go +++ b/pkg/target/configmaps_test.go @@ -18,13 +18,15 @@ package target_test import ( "testing" + + "sigs.k8s.io/kustomize/pkg/kusttest" ) // Generate a Secret and a ConfigMap from the same data // to compare the result. func TestGeneratorBasics(t *testing.T) { - th := NewKustTestHarness(t, "/app") - th.writeK("/app", ` + th := kusttest_test.NewKustTestHarness(t, "/app") + th.WriteK("/app", ` namePrefix: blah- configMapGenerator: - name: bob @@ -48,26 +50,26 @@ secretGenerator: - passphrase=phrase.dat - forces.txt `) - th.writeF("/app/foo.env", ` + th.WriteF("/app/foo.env", ` MOUNTAIN=everest OCEAN=pacific `) - th.writeF("/app/phrase.dat", ` + th.WriteF("/app/phrase.dat", ` Life is short. But the years are long. Not while the evil days come not. `) - th.writeF("/app/forces.txt", ` + th.WriteF("/app/forces.txt", ` gravitational electromagnetic strong nuclear weak nuclear `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: MOUNTAIN: everest @@ -113,8 +115,8 @@ type: Opaque // TODO: These should be errors instead. func TestGeneratorRepeatsInKustomization(t *testing.T) { - th := NewKustTestHarness(t, "/app") - th.writeK("/app", ` + th := kusttest_test.NewKustTestHarness(t, "/app") + th.WriteK("/app", ` namePrefix: blah- configMapGenerator: - name: bob @@ -130,13 +132,13 @@ configMapGenerator: files: - nobles=nobility.txt `) - th.writeF("/app/forces.txt", ` + th.WriteF("/app/forces.txt", ` gravitational electromagnetic strong nuclear weak nuclear `) - th.writeF("/app/nobility.txt", ` + th.WriteF("/app/nobility.txt", ` helium neon argon @@ -144,11 +146,11 @@ krypton xenon radon `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: fruit: apple @@ -168,8 +170,8 @@ metadata: } func TestGeneratorOverlays(t *testing.T) { - th := NewKustTestHarness(t, "/app/overlay") - th.writeK("/app/base1", ` + th := kusttest_test.NewKustTestHarness(t, "/app/overlay") + th.WriteK("/app/base1", ` namePrefix: p1- configMapGenerator: - name: com1 @@ -177,7 +179,7 @@ configMapGenerator: literals: - from=base `) - th.writeK("/app/base2", ` + th.WriteK("/app/base2", ` namePrefix: p2- configMapGenerator: - name: com2 @@ -185,7 +187,7 @@ configMapGenerator: literals: - from=base `) - th.writeK("/app/overlay/o1", ` + th.WriteK("/app/overlay/o1", ` bases: - ../../base1 configMapGenerator: @@ -194,7 +196,7 @@ configMapGenerator: literals: - from=overlay `) - th.writeK("/app/overlay/o2", ` + th.WriteK("/app/overlay/o2", ` bases: - ../../base2 configMapGenerator: @@ -203,7 +205,7 @@ configMapGenerator: literals: - from=overlay `) - th.writeK("/app/overlay", ` + th.WriteK("/app/overlay", ` bases: - o1 - o2 @@ -214,11 +216,11 @@ configMapGenerator: - foo=bar - baz=qux `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: baz: qux diff --git a/pkg/target/crd_test.go b/pkg/target/crd_test.go index 6ffa923ba..7e19bcd82 100644 --- a/pkg/target/crd_test.go +++ b/pkg/target/crd_test.go @@ -18,10 +18,12 @@ package target_test import ( "testing" + + "sigs.k8s.io/kustomize/pkg/kusttest" ) -func writeBaseWithCrd(th *KustTestHarness) { - th.writeK("/app/base", ` +func writeBaseWithCrd(th *kusttest_test.KustTestHarness) { + th.WriteK("/app/base", ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization crds: @@ -34,7 +36,7 @@ resources: namePrefix: x- `) - th.writeF("/app/base/bee.yaml", ` + th.WriteF("/app/base/bee.yaml", ` apiVersion: v1beta1 kind: Bee metadata: @@ -42,7 +44,7 @@ metadata: spec: action: fly `) - th.writeF("/app/base/mykind.yaml", ` + th.WriteF("/app/base/mykind.yaml", ` apiVersion: jingfang.example.com/v1beta1 kind: MyKind metadata: @@ -53,7 +55,7 @@ spec: beeRef: name: bee `) - th.writeF("/app/base/secret.yaml", ` + th.WriteF("/app/base/secret.yaml", ` apiVersion: v1 kind: Secret metadata: @@ -61,7 +63,7 @@ metadata: data: PATH: yellowBrickRoad `) - th.writeF("/app/base/mycrd.json", ` + th.WriteF("/app/base/mycrd.json", ` { "github.com/example/pkg/apis/jingfang/v1beta1.Bee": { "Schema": { @@ -236,13 +238,13 @@ data: } func TestCrdBase(t *testing.T) { - th := NewKustTestHarness(t, "/app/base") + th := kusttest_test.NewKustTestHarness(t, "/app/base") writeBaseWithCrd(th) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: PATH: yellowBrickRoad @@ -270,9 +272,9 @@ spec: } func TestCrdWithOverlay(t *testing.T) { - th := NewKustTestHarness(t, "/app/overlay") + th := kusttest_test.NewKustTestHarness(t, "/app/overlay") writeBaseWithCrd(th) - th.writeK("/app/overlay", ` + th.WriteK("/app/overlay", ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization namePrefix: prod- @@ -281,7 +283,7 @@ bases: patchesStrategicMerge: - bee.yaml `) - th.writeF("/app/overlay/bee.yaml", ` + th.WriteF("/app/overlay/bee.yaml", ` apiVersion: v1beta1 kind: Bee metadata: @@ -289,12 +291,12 @@ metadata: spec: action: makehoney `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } // TODO(#669): Bee's name should be "prod-x-bee", not "prod-bee". - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: PATH: yellowBrickRoad @@ -322,8 +324,8 @@ spec: } func TestCrdWithContainers(t *testing.T) { - th := NewKustTestHarness(t, "/app/crd/containers") - th.writeK("/app/crd/containers", ` + th := kusttest_test.NewKustTestHarness(t, "/app/crd/containers") + th.WriteK("/app/crd/containers", ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources: @@ -334,7 +336,7 @@ images: newName: registry.gitlab.com/test newTag: latest `) - th.writeF("/app/crd/containers/crd.yaml", ` + th.WriteF("/app/crd/containers/crd.yaml", ` apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition metadata: @@ -355,11 +357,11 @@ spec: containers: description: Containers allows injecting additional containers `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition metadata: diff --git a/pkg/target/customconfig_test.go b/pkg/target/customconfig_test.go index 8aaa91a96..1a7fd7e3c 100644 --- a/pkg/target/customconfig_test.go +++ b/pkg/target/customconfig_test.go @@ -18,10 +18,12 @@ package target_test import ( "testing" + + "sigs.k8s.io/kustomize/pkg/kusttest" ) -func makeBaseReferencingCustomConfig(th *KustTestHarness) { - th.writeK("/app/base", ` +func makeBaseReferencingCustomConfig(th *kusttest_test.KustTestHarness) { + th.WriteK("/app/base", ` namePrefix: x- commonLabels: app: myApp @@ -46,7 +48,7 @@ configurations: - config/defaults.yaml - config/custom.yaml `) - th.writeF("/app/base/giraffes.yaml", ` + th.WriteF("/app/base/giraffes.yaml", ` kind: Giraffe metadata: name: may @@ -61,7 +63,7 @@ spec: diet: mimosa location: NE `) - th.writeF("/app/base/gorilla.yaml", ` + th.WriteF("/app/base/gorilla.yaml", ` kind: Gorilla metadata: name: koko @@ -69,7 +71,7 @@ spec: diet: bambooshoots location: SW `) - th.writeF("/app/base/animalPark.yaml", ` + th.WriteF("/app/base/animalPark.yaml", ` kind: AnimalPark metadata: name: sandiego @@ -85,10 +87,10 @@ spec: } func TestCustomConfig(t *testing.T) { - th := NewKustTestHarness(t, "/app/base") + th := kusttest_test.NewKustTestHarness(t, "/app/base") makeBaseReferencingCustomConfig(th) - th.writeDefaultConfigs("/app/base/config/defaults.yaml") - th.writeF("/app/base/config/custom.yaml", ` + th.WriteDefaultConfigs("/app/base/config/defaults.yaml") + th.WriteF("/app/base/config/custom.yaml", ` nameReference: - kind: Gorilla fieldSpecs: @@ -102,11 +104,11 @@ varReference: - path: spec/food kind: AnimalPark `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` kind: AnimalPark metadata: labels: @@ -151,13 +153,13 @@ spec: } func TestCustomConfigWithDefaultOverspecification(t *testing.T) { - th := NewKustTestHarness(t, "/app/base") + th := kusttest_test.NewKustTestHarness(t, "/app/base") makeBaseReferencingCustomConfig(th) - th.writeDefaultConfigs("/app/base/config/defaults.yaml") + th.WriteDefaultConfigs("/app/base/config/defaults.yaml") // Specifying namePrefix here conflicts with (is the same as) // the defaults written above. This is intentional in the // test to assure duplicate config doesn't cause problems. - th.writeF("/app/base/config/custom.yaml", ` + th.WriteF("/app/base/config/custom.yaml", ` namePrefix: - path: metadata/name nameReference: @@ -173,11 +175,11 @@ varReference: - path: spec/food kind: AnimalPark `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` kind: AnimalPark metadata: labels: @@ -222,10 +224,10 @@ spec: } func TestFixedBug605_BaseCustomizationAvailableInOverlay(t *testing.T) { - th := NewKustTestHarness(t, "/app/overlay") + th := kusttest_test.NewKustTestHarness(t, "/app/overlay") makeBaseReferencingCustomConfig(th) - th.writeDefaultConfigs("/app/base/config/defaults.yaml") - th.writeF("/app/base/config/custom.yaml", ` + th.WriteDefaultConfigs("/app/base/config/defaults.yaml") + th.WriteF("/app/base/config/custom.yaml", ` nameReference: - kind: Gorilla fieldSpecs: @@ -239,7 +241,7 @@ varReference: - path: spec/food kind: AnimalPark `) - th.writeK("/app/overlay", ` + th.WriteK("/app/overlay", ` namePrefix: o- commonLabels: movie: planetOfTheApes @@ -250,7 +252,7 @@ resources: bases: - ../base `) - th.writeF("/app/overlay/ursus.yaml", ` + th.WriteF("/app/overlay/ursus.yaml", ` kind: Gorilla metadata: name: ursus @@ -259,7 +261,7 @@ spec: location: Arizona `) // The following replaces the gorillaRef in the AnimalPark. - th.writeF("/app/overlay/animalPark.yaml", ` + th.WriteF("/app/overlay/animalPark.yaml", ` kind: AnimalPark metadata: name: sandiego @@ -268,13 +270,13 @@ spec: name: ursus `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } // TODO(#669): The name of AnimalPark should be x-o-sandiego, // not o-sandiego, since AnimalPark appears in the base. - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` kind: AnimalPark metadata: labels: diff --git a/pkg/target/generatormergeandreplace_test.go b/pkg/target/generatormergeandreplace_test.go index 469c9c9e1..0bc66fe55 100644 --- a/pkg/target/generatormergeandreplace_test.go +++ b/pkg/target/generatormergeandreplace_test.go @@ -18,11 +18,13 @@ package target_test import ( "testing" + + "sigs.k8s.io/kustomize/pkg/kusttest" ) func TestSimpleBase(t *testing.T) { - th := NewKustTestHarness(t, "/app/base") - th.writeK("/app/base", ` + th := kusttest_test.NewKustTestHarness(t, "/app/base") + th.WriteK("/app/base", ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization namePrefix: team-foo- @@ -37,7 +39,7 @@ resources: - networkpolicy.yaml - service.yaml `) - th.writeF("/app/base/service.yaml", ` + th.WriteF("/app/base/service.yaml", ` apiVersion: v1 kind: Service metadata: @@ -50,7 +52,7 @@ spec: selector: app: nginx `) - th.writeF("/app/base/networkpolicy.yaml", ` + th.WriteF("/app/base/networkpolicy.yaml", ` apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: @@ -65,7 +67,7 @@ spec: matchLabels: app: nginx `) - th.writeF("/app/base/deployment.yaml", ` + th.WriteF("/app/base/deployment.yaml", ` apiVersion: apps/v1beta2 kind: Deployment metadata: @@ -82,11 +84,11 @@ spec: - name: nginx image: nginx `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 kind: Service metadata: @@ -161,8 +163,8 @@ spec: `) } -func makeBaseWithGenerators(th *KustTestHarness) { - th.writeK("/app", ` +func makeBaseWithGenerators(th *kusttest_test.KustTestHarness) { + th.WriteK("/app", ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization namePrefix: team-foo- @@ -185,7 +187,7 @@ secretGenerator: - username=admin - password=somepw `) - th.writeF("/app/deployment.yaml", ` + th.WriteF("/app/deployment.yaml", ` apiVersion: apps/v1beta2 kind: Deployment metadata: @@ -211,7 +213,7 @@ spec: name: configmap-in-base name: configmap-in-base `) - th.writeF("/app/service.yaml", ` + th.WriteF("/app/service.yaml", ` apiVersion: v1 kind: Service metadata: @@ -227,13 +229,13 @@ spec: } func TestBaseWithGeneratorsAlone(t *testing.T) { - th := NewKustTestHarness(t, "/app") + th := kusttest_test.NewKustTestHarness(t, "/app") makeBaseWithGenerators(th) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: foo: bar @@ -321,9 +323,9 @@ spec: } func TestMergeAndReplaceGenerators(t *testing.T) { - th := NewKustTestHarness(t, "/overlay") + th := kusttest_test.NewKustTestHarness(t, "/overlay") makeBaseWithGenerators(th) - th.writeF("/overlay/deployment.yaml", ` + th.WriteF("/overlay/deployment.yaml", ` apiVersion: apps/v1beta2 kind: Deployment metadata: @@ -340,7 +342,7 @@ spec: name: configmap-in-overlay name: configmap-in-overlay `) - th.writeK("/overlay", ` + th.WriteK("/overlay", ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization namePrefix: staging- @@ -365,11 +367,11 @@ secretGenerator: literals: - proxy=haproxy `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: foo: override-bar diff --git a/pkg/target/generatoroptions_test.go b/pkg/target/generatoroptions_test.go index d1b9e9006..d55c541a9 100644 --- a/pkg/target/generatoroptions_test.go +++ b/pkg/target/generatoroptions_test.go @@ -19,11 +19,12 @@ import ( "testing" "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" + "sigs.k8s.io/kustomize/pkg/kusttest" ) func TestGeneratorOptionsWithBases(t *testing.T) { - th := NewKustTestHarness(t, "/app/overlay") - th.writeK("/app/base", ` + th := kusttest_test.NewKustTestHarness(t, "/app/overlay") + th.WriteK("/app/base", ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization generatorOptions: @@ -35,7 +36,7 @@ configMapGenerator: literals: - foo=bar `) - th.writeK("/app/overlay", ` + th.WriteK("/app/overlay", ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization bases: @@ -49,11 +50,11 @@ configMapGenerator: literals: - fruit=apple `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: fruit: apple @@ -75,8 +76,8 @@ metadata: } func TestGoPluginNotEnabled(t *testing.T) { - th := NewKustTestHarness(t, "/app") - th.writeK("/app", ` + th := kusttest_test.NewKustTestHarness(t, "/app") + th.WriteK("/app", ` secretGenerator: - name: attemptGoPlugin kvSources: @@ -86,7 +87,7 @@ secretGenerator: - someArg - someOtherArg `) - _, err := th.makeKustTarget().MakeCustomizedResMap() + _, err := th.MakeKustTarget().MakeCustomizedResMap() if err == nil { t.Fatalf("expected error") } @@ -96,9 +97,9 @@ secretGenerator: } func TestGoPluginDoesNotExist(t *testing.T) { - th := NewKustTestHarnessWithPluginConfig( + th := kusttest_test.NewKustTestHarnessWithPluginConfig( t, "/app", plugin.ActivePluginConfig()) - th.writeK("/app", ` + th.WriteK("/app", ` secretGenerator: - name: attemptGoPlugin kvSources: @@ -108,7 +109,7 @@ secretGenerator: - someArg - someOtherArg `) - _, err := th.makeKustTarget().MakeCustomizedResMap() + _, err := th.MakeKustTarget().MakeCustomizedResMap() if err == nil { t.Fatalf("expected error") } diff --git a/pkg/target/generatorplugin_test.go b/pkg/target/generatorplugin_test.go index 657b06c60..3c1ad153f 100644 --- a/pkg/target/generatorplugin_test.go +++ b/pkg/target/generatorplugin_test.go @@ -19,10 +19,11 @@ import ( "sigs.k8s.io/kustomize/internal/plugintest" "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" + "sigs.k8s.io/kustomize/pkg/kusttest" ) -func writeServiceGenerator(th *KustTestHarness, path string) { - th.writeF(path, ` +func writeServiceGenerator(th *kusttest_test.KustTestHarness, path string) { + th.WriteF(path, ` apiVersion: someteam.example.com/v1 kind: ServiceGenerator metadata: @@ -39,18 +40,18 @@ func TestServiceGeneratorPlugin(t *testing.T) { tc.BuildGoPlugin( "someteam.example.com", "v1", "ServiceGenerator") - th := NewKustTestHarnessWithPluginConfig( + th := kusttest_test.NewKustTestHarnessWithPluginConfig( t, "/app", plugin.ActivePluginConfig()) - th.writeK("/app", ` + th.WriteK("/app", ` generators: - serviceGenerator.yaml `) writeServiceGenerator(th, "/app/serviceGenerator.yaml") - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 kind: Service metadata: @@ -65,13 +66,12 @@ spec: `) } -func writeSecretGeneratorConfig(th *KustTestHarness, root string) { - th.writeF(filepath.Join(root, "secretGenerator.yaml"), ` +func writeSecretGeneratorConfig(th *kusttest_test.KustTestHarness, root string) { + th.WriteF(filepath.Join(root, "secretGenerator.yaml"), ` apiVersion: builtin kind: SecretGenerator metadata: name: secretGenerator -name: mySecret behavior: merge envFiles: - a.env @@ -82,13 +82,13 @@ literals: - FRUIT=apple - VEGETABLE=carrot `) - th.writeF(filepath.Join(root, "a.env"), ` + th.WriteF(filepath.Join(root, "a.env"), ` ROUTER_PASSWORD=admin `) - th.writeF(filepath.Join(root, "b.env"), ` + th.WriteF(filepath.Join(root, "b.env"), ` DB_PASSWORD=iloveyou `) - th.writeF(filepath.Join(root, "longsecret.txt"), ` + th.WriteF(filepath.Join(root, "longsecret.txt"), ` Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt @@ -104,18 +104,18 @@ func TestSecretGenerator(t *testing.T) { tc.BuildGoPlugin( "builtin", "", "SecretGenerator") - th := NewKustTestHarnessWithPluginConfig( + th := kusttest_test.NewKustTestHarnessWithPluginConfig( t, "/app", plugin.ActivePluginConfig()) - th.writeK("/app", ` + th.WriteK("/app", ` generators: - secretGenerator.yaml `) writeSecretGeneratorConfig(th, "/app") - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: DB_PASSWORD: aWxvdmV5b3U= @@ -137,24 +137,24 @@ func TestConfigMapGenerator(t *testing.T) { tc.BuildExecPlugin( "someteam.example.com", "v1", "ConfigMapGenerator") - th := NewKustTestHarnessWithPluginConfig( + th := kusttest_test.NewKustTestHarnessWithPluginConfig( t, "/app", plugin.ActivePluginConfig()) - th.writeK("/app", ` + th.WriteK("/app", ` generators: - configmapGenerator.yaml `) - th.writeF("/app/configmapGenerator.yaml", ` + th.WriteF("/app/configmapGenerator.yaml", ` apiVersion: someteam.example.com/v1 kind: ConfigMapGenerator metadata: name: some-random-name argsOneLiner: "admin secret" `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: password: secret diff --git a/pkg/target/kusttarget_test.go b/pkg/target/kusttarget_test.go index 627e9dc8f..032bf56c8 100644 --- a/pkg/target/kusttarget_test.go +++ b/pkg/target/kusttarget_test.go @@ -26,6 +26,7 @@ import ( "sigs.k8s.io/kustomize/internal/loadertest" "sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/ifc" + "sigs.k8s.io/kustomize/pkg/kusttest" "sigs.k8s.io/kustomize/pkg/resid" "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" @@ -86,16 +87,16 @@ metadata: ) func TestResources(t *testing.T) { - th := NewKustTestHarness(t, "/whatever") - th.writeK("/whatever/", kustomizationContent) - th.writeF("/whatever/deployment.yaml", deploymentContent) - th.writeF("/whatever/namespace.yaml", namespaceContent) - th.writeF("/whatever/jsonpatch.json", jsonpatchContent) + th := kusttest_test.NewKustTestHarness(t, "/whatever") + th.WriteK("/whatever/", kustomizationContent) + th.WriteF("/whatever/deployment.yaml", deploymentContent) + th.WriteF("/whatever/namespace.yaml", namespaceContent) + th.WriteF("/whatever/jsonpatch.json", jsonpatchContent) expected := resmap.ResMap{ resid.NewResIdWithPrefixSuffixNamespace( gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"}, - "dply1", "foo-", "-bar", "ns1"): th.fromMap( + "dply1", "foo-", "-bar", "ns1"): th.FromMap( map[string]interface{}{ "apiVersion": "apps/v1", "kind": "Deployment", @@ -130,7 +131,7 @@ func TestResources(t *testing.T) { }), resid.NewResIdWithPrefixSuffixNamespace( gvk.Gvk{Version: "v1", Kind: "ConfigMap"}, - "literalConfigMap", "foo-", "-bar", "ns1"): th.fromMapAndOption( + "literalConfigMap", "foo-", "-bar", "ns1"): th.FromMapAndOption( map[string]interface{}{ "apiVersion": "v1", "kind": "ConfigMap", @@ -153,7 +154,7 @@ func TestResources(t *testing.T) { &types.GeneratorOptions{}), resid.NewResIdWithPrefixSuffixNamespace( gvk.Gvk{Version: "v1", Kind: "Secret"}, - "secret", "foo-", "-bar", "ns1"): th.fromMapAndOption( + "secret", "foo-", "-bar", "ns1"): th.FromMapAndOption( map[string]interface{}{ "apiVersion": "v1", "kind": "Secret", @@ -177,7 +178,7 @@ func TestResources(t *testing.T) { &types.GeneratorOptions{}), resid.NewResIdWithPrefixSuffixNamespace( gvk.Gvk{Version: "v1", Kind: "Namespace"}, - "ns1", "foo-", "-bar", ""): th.fromMap( + "ns1", "foo-", "-bar", ""): th.FromMap( map[string]interface{}{ "apiVersion": "v1", "kind": "Namespace", @@ -192,7 +193,7 @@ func TestResources(t *testing.T) { }, }), } - actual, err := th.makeKustTarget().MakeCustomizedResMap() + actual, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("unexpected Resources error %v", err) } @@ -216,9 +217,9 @@ func TestKustomizationNotFound(t *testing.T) { } func TestResourceNotFound(t *testing.T) { - th := NewKustTestHarness(t, "/whatever") - th.writeK("/whatever", kustomizationContent) - _, err := th.makeKustTarget().MakeCustomizedResMap() + th := kusttest_test.NewKustTestHarness(t, "/whatever") + th.WriteK("/whatever", kustomizationContent) + _, err := th.MakeKustTarget().MakeCustomizedResMap() if err == nil { t.Fatalf("Didn't get the expected error for an unknown resource") } @@ -237,13 +238,13 @@ func findSecret(m resmap.ResMap) *resource.Resource { } func TestDisableNameSuffixHash(t *testing.T) { - th := NewKustTestHarness(t, "/whatever") - th.writeK("/whatever/", kustomizationContent) - th.writeF("/whatever/deployment.yaml", deploymentContent) - th.writeF("/whatever/namespace.yaml", namespaceContent) - th.writeF("/whatever/jsonpatch.json", jsonpatchContent) + th := kusttest_test.NewKustTestHarness(t, "/whatever") + th.WriteK("/whatever/", kustomizationContent) + th.WriteF("/whatever/deployment.yaml", deploymentContent) + th.WriteF("/whatever/namespace.yaml", namespaceContent) + th.WriteF("/whatever/jsonpatch.json", jsonpatchContent) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("unexpected Resources error %v", err) } @@ -255,11 +256,11 @@ func TestDisableNameSuffixHash(t *testing.T) { t.Errorf("unexpected secret resource name: %s", secret.GetName()) } - th.writeK("/whatever/", + th.WriteK("/whatever/", strings.Replace(kustomizationContent, "disableNameSuffixHash: false", "disableNameSuffixHash: true", -1)) - m, err = th.makeKustTarget().MakeCustomizedResMap() + m, err = th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("unexpected Resources error %v", err) } @@ -273,25 +274,25 @@ func TestDisableNameSuffixHash(t *testing.T) { } func TestIssue596AllowDirectoriesThatAreSubstringsOfEachOther(t *testing.T) { - th := NewKustTestHarness(t, "/app/overlays/aws-sandbox2.us-east-1") - th.writeK("/app/base", "") - th.writeK("/app/overlays/aws", ` + th := kusttest_test.NewKustTestHarness(t, "/app/overlays/aws-sandbox2.us-east-1") + th.WriteK("/app/base", "") + th.WriteK("/app/overlays/aws", ` bases: - ../../base `) - th.writeK("/app/overlays/aws-nonprod", ` + th.WriteK("/app/overlays/aws-nonprod", ` bases: - ../aws `) - th.writeK("/app/overlays/aws-sandbox2.us-east-1", ` + th.WriteK("/app/overlays/aws-sandbox2.us-east-1", ` bases: - ../aws-nonprod `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, "") + th.AssertActualEqualsExpected(m, "") } // To simplify tests, these vars specified in alphabetical order. @@ -329,8 +330,8 @@ var someVars = []types.Var{ } func TestGetAllVarsSimple(t *testing.T) { - th := NewKustTestHarness(t, "/app") - th.writeK("/app", ` + th := kusttest_test.NewKustTestHarness(t, "/app") + th.WriteK("/app", ` vars: - name: AWARD objref: @@ -345,7 +346,7 @@ vars: name: heron apiVersion: v300 `) - ra, err := th.makeKustTarget().AccumulateTarget() + ra, err := th.MakeKustTarget().AccumulateTarget() if err != nil { t.Fatalf("Err: %v", err) } @@ -361,8 +362,8 @@ vars: } func TestGetAllVarsNested(t *testing.T) { - th := NewKustTestHarness(t, "/app/overlays/o2") - th.writeK("/app/base", ` + th := kusttest_test.NewKustTestHarness(t, "/app/overlays/o2") + th.WriteK("/app/base", ` vars: - name: AWARD objref: @@ -377,7 +378,7 @@ vars: name: heron apiVersion: v300 `) - th.writeK("/app/overlays/o1", ` + th.WriteK("/app/overlays/o1", ` vars: - name: FRUIT objref: @@ -386,7 +387,7 @@ vars: bases: - ../../base `) - th.writeK("/app/overlays/o2", ` + th.WriteK("/app/overlays/o2", ` vars: - name: VEGETABLE objref: @@ -395,7 +396,7 @@ vars: bases: - ../o1 `) - ra, err := th.makeKustTarget().AccumulateTarget() + ra, err := th.MakeKustTarget().AccumulateTarget() if err != nil { t.Fatalf("Err: %v", err) } @@ -414,8 +415,8 @@ bases: } func TestVarCollisionsForbidden(t *testing.T) { - th := NewKustTestHarness(t, "/app/overlays/o2") - th.writeK("/app/base", ` + th := kusttest_test.NewKustTestHarness(t, "/app/overlays/o2") + th.WriteK("/app/base", ` vars: - name: AWARD objref: @@ -430,7 +431,7 @@ vars: name: heron apiVersion: v300 `) - th.writeK("/app/overlays/o1", ` + th.WriteK("/app/overlays/o1", ` vars: - name: AWARD objref: @@ -439,7 +440,7 @@ vars: bases: - ../../base `) - th.writeK("/app/overlays/o2", ` + th.WriteK("/app/overlays/o2", ` vars: - name: VEGETABLE objref: @@ -448,7 +449,7 @@ vars: bases: - ../o1 `) - _, err := th.makeKustTarget().AccumulateTarget() + _, err := th.MakeKustTarget().AccumulateTarget() if err == nil { t.Fatalf("expected var collision") } diff --git a/pkg/target/multiplepatch_test.go b/pkg/target/multiplepatch_test.go index 6aa3bb6aa..7d75e7eb8 100644 --- a/pkg/target/multiplepatch_test.go +++ b/pkg/target/multiplepatch_test.go @@ -19,10 +19,12 @@ package target_test import ( "strings" "testing" + + "sigs.k8s.io/kustomize/pkg/kusttest" ) -func makeCommonFileForMultiplePatchTest(th *KustTestHarness) { - th.writeK("/app/base", ` +func makeCommonFileForMultiplePatchTest(th *kusttest_test.KustTestHarness) { + th.WriteK("/app/base", ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization namePrefix: team-foo- @@ -40,7 +42,7 @@ configMapGenerator: literals: - foo=bar `) - th.writeF("/app/base/deployment.yaml", ` + th.WriteF("/app/base/deployment.yaml", ` apiVersion: apps/v1beta2 kind: Deployment metadata: @@ -66,7 +68,7 @@ spec: name: configmap-in-base name: configmap-in-base `) - th.writeF("/app/base/service.yaml", ` + th.WriteF("/app/base/service.yaml", ` apiVersion: v1 kind: Service metadata: @@ -79,7 +81,7 @@ spec: selector: app: nginx `) - th.writeK("/app/overlay/staging", ` + th.WriteK("/app/overlay/staging", ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization namePrefix: staging- @@ -98,9 +100,9 @@ configMapGenerator: } func TestMultiplePatchesNoConflict(t *testing.T) { - th := NewKustTestHarness(t, "/app/overlay/staging") + th := kusttest_test.NewKustTestHarness(t, "/app/overlay/staging") makeCommonFileForMultiplePatchTest(th) - th.writeF("/app/overlay/staging/deployment-patch1.yaml", ` + th.WriteF("/app/overlay/staging/deployment-patch1.yaml", ` apiVersion: apps/v1beta2 kind: Deployment metadata: @@ -123,7 +125,7 @@ spec: name: configmap-in-overlay name: configmap-in-overlay `) - th.writeF("/app/overlay/staging/deployment-patch2.yaml", ` + th.WriteF("/app/overlay/staging/deployment-patch2.yaml", ` apiVersion: apps/v1beta2 kind: Deployment metadata: @@ -141,11 +143,11 @@ spec: volumes: - name: nginx-persistent-storage `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: foo: bar @@ -244,9 +246,9 @@ spec: } func TestMultiplePatchesWithConflict(t *testing.T) { - th := NewKustTestHarness(t, "/app/overlay/staging") + th := kusttest_test.NewKustTestHarness(t, "/app/overlay/staging") makeCommonFileForMultiplePatchTest(th) - th.writeF("/app/overlay/staging/deployment-patch1.yaml", ` + th.WriteF("/app/overlay/staging/deployment-patch1.yaml", ` apiVersion: apps/v1beta2 kind: Deployment metadata: @@ -268,7 +270,7 @@ spec: name: configmap-in-overlay name: configmap-in-overlay `) - th.writeF("/app/overlay/staging/deployment-patch2.yaml", ` + th.WriteF("/app/overlay/staging/deployment-patch2.yaml", ` apiVersion: apps/v1beta2 kind: Deployment metadata: @@ -282,7 +284,7 @@ spec: - name: ENABLE_FEATURE_FOO value: FALSE `) - _, err := th.makeKustTarget().MakeCustomizedResMap() + _, err := th.MakeKustTarget().MakeCustomizedResMap() if err == nil { t.Fatalf("expected conflict") } diff --git a/pkg/target/namespacedgenerators_test.go b/pkg/target/namespacedgenerators_test.go index d6e550efc..3d598ab4e 100644 --- a/pkg/target/namespacedgenerators_test.go +++ b/pkg/target/namespacedgenerators_test.go @@ -18,11 +18,13 @@ package target_test import ( "testing" + + "sigs.k8s.io/kustomize/pkg/kusttest" ) func TestNamespacedGenerator(t *testing.T) { - th := NewKustTestHarness(t, "/app") - th.writeK("/app", ` + th := kusttest_test.NewKustTestHarness(t, "/app") + th.WriteK("/app", ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization configMapGenerator: @@ -45,11 +47,11 @@ secretGenerator: literals: - password.txt=anotherSecret `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: altGreeting: Good Morning from non-default namespace! diff --git a/pkg/target/nullvalues_test.go b/pkg/target/nullvalues_test.go index 7ccbbf1fa..f77cdaf96 100644 --- a/pkg/target/nullvalues_test.go +++ b/pkg/target/nullvalues_test.go @@ -18,11 +18,13 @@ package target_test import ( "testing" + + "sigs.k8s.io/kustomize/pkg/kusttest" ) func TestNullValues(t *testing.T) { - th := NewKustTestHarness(t, "/app") - th.writeF("/app/deployment.yaml", ` + th := kusttest_test.NewKustTestHarness(t, "/app") + th.WriteF("/app/deployment.yaml", ` apiVersion: apps/v1 kind: Deployment metadata: @@ -43,18 +45,18 @@ spec: image: image name: example `) - th.writeF("/app/kustomization.yaml", ` + th.WriteF("/app/kustomization.yaml", ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources: - deployment.yaml `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: apps/v1 kind: Deployment metadata: diff --git a/pkg/target/plugindir_test.go b/pkg/target/plugindir_test.go index 90f823450..698edafcb 100644 --- a/pkg/target/plugindir_test.go +++ b/pkg/target/plugindir_test.go @@ -24,6 +24,7 @@ import ( "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/k8sdeps/transformer" "sigs.k8s.io/kustomize/pkg/fs" + "sigs.k8s.io/kustomize/pkg/kusttest" "sigs.k8s.io/kustomize/pkg/loader" "sigs.k8s.io/kustomize/pkg/plugins" "sigs.k8s.io/kustomize/pkg/resmap" @@ -86,8 +87,8 @@ metadata: t.Fatalf("Err: %v", err) } - th := NewKustTestHarness(t, ".") - th.assertActualEqualsExpected(m, ` + th := kusttest_test.NewKustTestHarness(t, ".") + th.AssertActualEqualsExpected(m, ` apiVersion: v1 kind: WorkDir metadata: diff --git a/pkg/target/pruneconfigmap_test.go b/pkg/target/pruneconfigmap_test.go index 5bed65ae8..bd31b13d0 100644 --- a/pkg/target/pruneconfigmap_test.go +++ b/pkg/target/pruneconfigmap_test.go @@ -18,11 +18,13 @@ package target_test import ( "testing" + + "sigs.k8s.io/kustomize/pkg/kusttest" ) func TestPruneConfigMap(t *testing.T) { - th := NewKustTestHarness(t, "/app/base") - th.writeK("/app/base", ` + th := kusttest_test.NewKustTestHarness(t, "/app/base") + th.WriteK("/app/base", ` resources: - deployment.yaml - service.yaml @@ -37,7 +39,7 @@ inventory: namePrefix: my- namespace: default `) - th.writeF("/app/base/deployment.yaml", ` + th.WriteF("/app/base/deployment.yaml", ` apiVersion: apps/v1beta2 kind: Deployment metadata: @@ -74,7 +76,7 @@ spec: - name: mysql-persistent-storage emptyDir: {} `) - th.writeF("/app/base/service.yaml", ` + th.WriteF("/app/base/service.yaml", ` apiVersion: v1 kind: Service metadata: @@ -87,7 +89,7 @@ spec: selector: app: mysql `) - th.writeF("/app/base/secret.yaml", ` + th.WriteF("/app/base/secret.yaml", ` apiVersion: v1 kind: Secret metadata: @@ -99,12 +101,12 @@ data: username: jingfang `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } //nolint - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 kind: ConfigMap metadata: diff --git a/pkg/target/resourceconflict_test.go b/pkg/target/resourceconflict_test.go index 27a5bd18b..e21f27ebb 100644 --- a/pkg/target/resourceconflict_test.go +++ b/pkg/target/resourceconflict_test.go @@ -19,18 +19,20 @@ package target_test import ( "strings" "testing" + + "sigs.k8s.io/kustomize/pkg/kusttest" ) -func writeCombinedOverlays(th *KustTestHarness) { +func writeCombinedOverlays(th *kusttest_test.KustTestHarness) { // Base - th.writeK("/app/base", ` + th.WriteK("/app/base", ` resources: - serviceaccount.yaml - rolebinding.yaml namePrefix: base- nameSuffix: -suffix `) - th.writeF("/app/base/rolebinding.yaml", ` + th.WriteF("/app/base/rolebinding.yaml", ` apiVersion: rbac.authorization.k8s.io/v1beta1 kind: RoleBinding metadata: @@ -43,7 +45,7 @@ subjects: - kind: ServiceAccount name: serviceaccount `) - th.writeF("/app/base/serviceaccount.yaml", ` + th.WriteF("/app/base/serviceaccount.yaml", ` apiVersion: v1 kind: ServiceAccount metadata: @@ -51,13 +53,13 @@ metadata: `) // Mid-level overlays - th.writeK("/app/overlays/a", ` + th.WriteK("/app/overlays/a", ` bases: - ../../base namePrefix: a- nameSuffix: -suffixA `) - th.writeK("/app/overlays/b", ` + th.WriteK("/app/overlays/b", ` bases: - ../../base namePrefix: b- @@ -65,7 +67,7 @@ nameSuffix: -suffixB `) // Top overlay, combining the mid-level overlays - th.writeK("/app/combined", ` + th.WriteK("/app/combined", ` bases: - ../overlays/a - ../overlays/b @@ -73,13 +75,13 @@ bases: } func TestMultibasesNoConflict(t *testing.T) { - th := NewKustTestHarness(t, "/app/combined") + th := kusttest_test.NewKustTestHarness(t, "/app/combined") writeCombinedOverlays(th) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Unexpected err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 kind: ServiceAccount metadata: @@ -117,10 +119,10 @@ subjects: } func TestMultibasesWithConflict(t *testing.T) { - th := NewKustTestHarness(t, "/app/combined") + th := kusttest_test.NewKustTestHarness(t, "/app/combined") writeCombinedOverlays(th) - th.writeK("/app/overlays/a", ` + th.WriteK("/app/overlays/a", ` bases: - ../../base namePrefix: a- @@ -130,14 +132,14 @@ resources: `) // Expect an error because this resource in the overlay // matches a resource in the base. - th.writeF("/app/overlays/a/serviceaccount.yaml", ` + th.WriteF("/app/overlays/a/serviceaccount.yaml", ` apiVersion: v1 kind: ServiceAccount metadata: name: serviceaccount `) - _, err := th.makeKustTarget().MakeCustomizedResMap() + _, err := th.MakeKustTarget().MakeCustomizedResMap() if err == nil { t.Fatalf("Expected resource conflict.") } diff --git a/pkg/target/transformerplugin_test.go b/pkg/target/transformerplugin_test.go index 6f22323a1..933424764 100644 --- a/pkg/target/transformerplugin_test.go +++ b/pkg/target/transformerplugin_test.go @@ -18,10 +18,11 @@ import ( "sigs.k8s.io/kustomize/internal/plugintest" "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" + "sigs.k8s.io/kustomize/pkg/kusttest" ) -func writeDeployment(th *KustTestHarness, path string) { - th.writeF(path, ` +func writeDeployment(th *kusttest_test.KustTestHarness, path string) { + th.WriteF(path, ` apiVersion: apps/v1 kind: Deployment metadata: @@ -38,8 +39,8 @@ spec: `) } -func writeStringPrefixer(th *KustTestHarness, path string) { - th.writeF(path, ` +func writeStringPrefixer(th *kusttest_test.KustTestHarness, path string) { + th.WriteF(path, ` apiVersion: someteam.example.com/v1 kind: StringPrefixer metadata: @@ -47,8 +48,8 @@ metadata: `) } -func writeDatePrefixer(th *KustTestHarness, path string) { - th.writeF(path, ` +func writeDatePrefixer(th *kusttest_test.KustTestHarness, path string) { + th.WriteF(path, ` apiVersion: someteam.example.com/v1 kind: DatePrefixer metadata: @@ -66,9 +67,9 @@ func TestOrderedTransformers(t *testing.T) { tc.BuildGoPlugin( "someteam.example.com", "v1", "DatePrefixer") - th := NewKustTestHarnessWithPluginConfig( + th := kusttest_test.NewKustTestHarnessWithPluginConfig( t, "/app", plugin.ActivePluginConfig()) - th.writeK("/app", ` + th.WriteK("/app", ` resources: - deployment.yaml transformers: @@ -81,11 +82,11 @@ transformers: writeDeployment(th, "/app/deployment.yaml") writeStringPrefixer(th, "/app/stringPrefixer.yaml") writeDatePrefixer(th, "/app/datePrefixer.yaml") - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: apps/v1 kind: Deployment metadata: @@ -109,9 +110,9 @@ func TestSedTransformer(t *testing.T) { tc.BuildExecPlugin( "someteam.example.com", "v1", "SedTransformer") - th := NewKustTestHarnessWithPluginConfig( + th := kusttest_test.NewKustTestHarnessWithPluginConfig( t, "/app", plugin.ActivePluginConfig()) - th.writeK("/app", ` + th.WriteK("/app", ` transformers: - sed-transformer.yaml @@ -121,23 +122,23 @@ configMapGenerator: - FOO=$FOO - BAR=$BAR `) - th.writeF("/app/sed-transformer.yaml", ` + th.WriteF("/app/sed-transformer.yaml", ` apiVersion: someteam.example.com/v1 kind: SedTransformer metadata: name: some-random-name argsFromFile: sed-input.txt `) - th.writeF("/app/sed-input.txt", ` + th.WriteF("/app/sed-input.txt", ` s/$FOO/foo/g s/$BAR/bar/g `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 data: BAR: bar @@ -158,10 +159,10 @@ func TestTransformedTransformers(t *testing.T) { tc.BuildGoPlugin( "someteam.example.com", "v1", "DatePrefixer") - th := NewKustTestHarnessWithPluginConfig( + th := kusttest_test.NewKustTestHarnessWithPluginConfig( t, "/app/overlay", plugin.ActivePluginConfig()) - th.writeK("/app/base", ` + th.WriteK("/app/base", ` resources: - stringPrefixer.yaml transformers: @@ -170,7 +171,7 @@ transformers: writeStringPrefixer(th, "/app/base/stringPrefixer.yaml") writeDatePrefixer(th, "/app/base/datePrefixer.yaml") - th.writeK("/app/overlay", ` + th.WriteK("/app/overlay", ` resources: - deployment.yaml transformers: @@ -178,11 +179,11 @@ transformers: `) writeDeployment(th, "/app/overlay/deployment.yaml") - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: apps/v1 kind: Deployment metadata: diff --git a/pkg/target/transformersimage_test.go b/pkg/target/transformersimage_test.go index 450129274..ead15f27d 100644 --- a/pkg/target/transformersimage_test.go +++ b/pkg/target/transformersimage_test.go @@ -18,10 +18,12 @@ package target_test import ( "testing" + + "sigs.k8s.io/kustomize/pkg/kusttest" ) -func makeTransfomersImageBase(th *KustTestHarness) { - th.writeK("/app/base", ` +func makeTransfomersImageBase(th *kusttest_test.KustTestHarness) { + th.WriteK("/app/base", ` resources: - deploy1.yaml - random.yaml @@ -45,7 +47,7 @@ images: newName: my-docker digest: sha256:25a0d4b4 `) - th.writeF("/app/base/deploy1.yaml", ` + th.WriteF("/app/base/deploy1.yaml", ` group: apps apiVersion: v1 kind: Deployment @@ -67,7 +69,7 @@ spec: - name: postgresdb image: postgres:1.8.0 `) - th.writeF("/app/base/random.yaml", ` + th.WriteF("/app/base/random.yaml", ` kind: randomKind metadata: name: random @@ -105,13 +107,13 @@ spec3: } func TestTransfomersImageDefaultConfig(t *testing.T) { - th := NewKustTestHarness(t, "/app/base") + th := kusttest_test.NewKustTestHarness(t, "/app/base") makeTransfomersImageBase(th) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 group: apps kind: Deployment @@ -169,8 +171,8 @@ spec3: `) } -func makeTransfomersImageCustomBase(th *KustTestHarness) { - th.writeK("/app/base", ` +func makeTransfomersImageCustomBase(th *kusttest_test.KustTestHarness) { + th.WriteK("/app/base", ` resources: - custom.yaml configurations: @@ -195,7 +197,7 @@ images: newName: my-docker digest: sha256:25a0d4b4 `) - th.writeF("/app/base/custom.yaml", ` + th.WriteF("/app/base/custom.yaml", ` kind: customKind metadata: name: custom @@ -230,7 +232,7 @@ spec3: - name: my-cool-app image: gcr.io:8080/my-project/my-cool-app:latest `) - th.writeF("/app/base/config/custom.yaml", ` + th.WriteF("/app/base/config/custom.yaml", ` images: - kind: Custom path: spec/template/spec/myContainers/image @@ -241,13 +243,13 @@ images: `) } func TestTransfomersImageCustomConfig(t *testing.T) { - th := NewKustTestHarness(t, "/app/base") + th := kusttest_test.NewKustTestHarness(t, "/app/base") makeTransfomersImageCustomBase(th) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` kind: customKind metadata: name: custom @@ -284,8 +286,8 @@ spec3: `) } -func makeTransfomersImageKnativeBase(th *KustTestHarness) { - th.writeK("/app/base", ` +func makeTransfomersImageKnativeBase(th *kusttest_test.KustTestHarness) { + th.WriteK("/app/base", ` resources: - knative.yaml configurations: @@ -294,7 +296,7 @@ images: - name: solsa-echo newTag: foo `) - th.writeF("/app/base/knative.yaml", ` + th.WriteF("/app/base/knative.yaml", ` apiVersion: serving.knative.dev/v1alpha1 kind: Service metadata: @@ -307,7 +309,7 @@ spec: container: image: solsa-echo `) - th.writeF("/app/base/config/knative.yaml", ` + th.WriteF("/app/base/config/knative.yaml", ` images: - path: spec/runLatest/configuration/revisionTemplate/spec/container/image apiVersion: serving.knative.dev/v1alpha1 @@ -316,13 +318,13 @@ images: } func TestTransfomersImageKnativeConfig(t *testing.T) { - th := NewKustTestHarness(t, "/app/base") + th := kusttest_test.NewKustTestHarness(t, "/app/base") makeTransfomersImageKnativeBase(th) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: serving.knative.dev/v1alpha1 kind: Service metadata: diff --git a/pkg/target/variableref_test.go b/pkg/target/variableref_test.go index f7f88eb18..b664e860c 100644 --- a/pkg/target/variableref_test.go +++ b/pkg/target/variableref_test.go @@ -18,11 +18,13 @@ package target_test import ( "testing" + + "sigs.k8s.io/kustomize/pkg/kusttest" ) func TestVariableRef(t *testing.T) { - th := NewKustTestHarness(t, "/app/overlay/staging") - th.writeK("/app/base", ` + th := kusttest_test.NewKustTestHarness(t, "/app/overlay/staging") + th.WriteK("/app/base", ` namePrefix: base- resources: - cockroachdb-statefulset-secure.yaml @@ -62,7 +64,7 @@ vars: apiVersion: v1 fieldref: fieldpath: metadata.name`) - th.writeF("/app/base/cronjob.yaml", ` + th.WriteF("/app/base/cronjob.yaml", ` apiVersion: batch/v1beta1 kind: CronJob metadata: @@ -85,7 +87,7 @@ spec: - name: CDB_PUBLIC_SVC value: "$(CDB_PUBLIC_SVC)" `) - th.writeF("/app/base/cockroachdb-statefulset-secure.yaml", ` + th.WriteF("/app/base/cockroachdb-statefulset-secure.yaml", ` apiVersion: v1 kind: ServiceAccount metadata: @@ -320,16 +322,16 @@ spec: requests: storage: 1Gi `) - th.writeK("/app/overlay/staging", ` + th.WriteK("/app/overlay/staging", ` namePrefix: dev- bases: - ../../base `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 kind: ServiceAccount metadata: @@ -577,8 +579,8 @@ spec: } func TestVariableRefIngress(t *testing.T) { - th := NewKustTestHarness(t, "/app/overlay") - th.writeK("/app/base", ` + th := kusttest_test.NewKustTestHarness(t, "/app/overlay") + th.WriteK("/app/base", ` resources: - deployment.yaml - ingress.yaml @@ -593,7 +595,7 @@ vars: fieldref: fieldpath: metadata.name `) - th.writeF("/app/base/deployment.yaml", ` + th.WriteF("/app/base/deployment.yaml", ` apiVersion: apps/v1 kind: Deployment metadata: @@ -616,7 +618,7 @@ spec: - name: http containerPort: 80 `) - th.writeF("/app/base/ingress.yaml", ` + th.WriteF("/app/base/ingress.yaml", ` apiVersion: extensions/v1beta1 kind: Ingress metadata: @@ -637,7 +639,7 @@ spec: - $(DEPLOYMENT_NAME).example.com secretName: $(DEPLOYMENT_NAME).example.com-tls `) - th.writeF("/app/base/service.yaml", ` + th.WriteF("/app/base/service.yaml", ` apiVersion: v1 kind: Service metadata: @@ -651,16 +653,16 @@ spec: protocol: TCP targetPort: http `) - th.writeK("/app/overlay", ` + th.WriteK("/app/overlay", ` nameprefix: kustomized- bases: - ../base `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 kind: Service metadata: @@ -719,8 +721,8 @@ spec: } func TestVariableRefMounthPath(t *testing.T) { - th := NewKustTestHarness(t, "/app/base") - th.writeK("/app/base", ` + th := kusttest_test.NewKustTestHarness(t, "/app/base") + th.WriteK("/app/base", ` resources: - deployment.yaml - namespace.yaml @@ -733,7 +735,7 @@ vars: name: my-namespace `) - th.writeF("/app/base/deployment.yaml", ` + th.WriteF("/app/base/deployment.yaml", ` apiVersion: apps/v1 kind: Deployment metadata: @@ -754,18 +756,18 @@ vars: - name: my-volume emptyDir: {} `) - th.writeF("/app/base/namespace.yaml", ` + th.WriteF("/app/base/namespace.yaml", ` apiVersion: v1 kind: Namespace metadata: name: my-namespace `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 kind: Namespace metadata: @@ -794,8 +796,8 @@ spec: } func TestVariableRefMaps(t *testing.T) { - th := NewKustTestHarness(t, "/app/base") - th.writeK("/app/base", ` + th := kusttest_test.NewKustTestHarness(t, "/app/base") + th.WriteK("/app/base", ` resources: - deployment.yaml - namespace.yaml @@ -806,7 +808,7 @@ vars: kind: Namespace name: my-namespace `) - th.writeF("/app/base/deployment.yaml", ` + th.WriteF("/app/base/deployment.yaml", ` apiVersion: apps/v1 kind: Deployment metadata: @@ -820,18 +822,18 @@ vars: - name: app image: busybox `) - th.writeF("/app/base/namespace.yaml", ` + th.WriteF("/app/base/namespace.yaml", ` apiVersion: v1 kind: Namespace metadata: name: my-namespace `) - m, err := th.makeKustTarget().MakeCustomizedResMap() + m, err := th.MakeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` + th.AssertActualEqualsExpected(m, ` apiVersion: v1 kind: Namespace metadata: