diff --git a/api/internal/target/customconfig_test.go b/api/krusty/customconfig_test.go similarity index 85% rename from api/internal/target/customconfig_test.go rename to api/krusty/customconfig_test.go index ce2b9efc6..bba81d5ac 100644 --- a/api/internal/target/customconfig_test.go +++ b/api/krusty/customconfig_test.go @@ -1,15 +1,13 @@ // Copyright 2019 The Kubernetes Authors. // SPDX-License-Identifier: Apache-2.0 -package target_test +package krusty_test import ( "testing" - - kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" ) -func makeBaseReferencingCustomConfig(th *kusttest_test.KustTestHarness) { +func makeBaseReferencingCustomConfig(th testingHarness) { th.WriteK("/app/base", ` namePrefix: x- commonLabels: @@ -74,9 +72,9 @@ spec: } func TestCustomConfig(t *testing.T) { - th := kusttest_test.NewKustTestHarness(t, "/app/base") + th := makeTestHarness(t) makeBaseReferencingCustomConfig(th) - th.WriteDefaultConfigs("/app/base/config/defaults.yaml") + th.WriteLegacyConfigs("/app/base/config/defaults.yaml") th.WriteF("/app/base/config/custom.yaml", ` nameReference: - kind: Gorilla @@ -91,10 +89,7 @@ varReference: - path: spec/food kind: AnimalPark `) - m, err := th.MakeKustTarget().MakeCustomizedResMap() - if err != nil { - t.Fatalf("Err: %v", err) - } + m := th.Run("/app/base", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, ` kind: AnimalPark metadata: @@ -140,9 +135,9 @@ spec: } func TestCustomConfigWithDefaultOverspecification(t *testing.T) { - th := kusttest_test.NewKustTestHarness(t, "/app/base") + th := makeTestHarness(t) makeBaseReferencingCustomConfig(th) - th.WriteDefaultConfigs("/app/base/config/defaults.yaml") + th.WriteLegacyConfigs("/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. @@ -162,10 +157,7 @@ varReference: - path: spec/food kind: AnimalPark `) - m, err := th.MakeKustTarget().MakeCustomizedResMap() - if err != nil { - t.Fatalf("Err: %v", err) - } + m := th.Run("/app/base", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, ` kind: AnimalPark metadata: @@ -211,9 +203,9 @@ spec: } func TestFixedBug605_BaseCustomizationAvailableInOverlay(t *testing.T) { - th := kusttest_test.NewKustTestHarness(t, "/app/overlay") + th := makeTestHarness(t) makeBaseReferencingCustomConfig(th) - th.WriteDefaultConfigs("/app/base/config/defaults.yaml") + th.WriteLegacyConfigs("/app/base/config/defaults.yaml") th.WriteF("/app/base/config/custom.yaml", ` nameReference: - kind: Gorilla @@ -255,12 +247,7 @@ spec: gorillaRef: name: ursus `) - - m, err := th.MakeKustTarget().MakeCustomizedResMap() - if err != nil { - t.Fatalf("Err: %v", err) - } - + m := th.Run("/app/overlay", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, ` kind: AnimalPark metadata: diff --git a/api/krusty/kustomizer_test.go b/api/krusty/kustomizer_test.go index 8c88732c4..1dc5315d6 100644 --- a/api/krusty/kustomizer_test.go +++ b/api/krusty/kustomizer_test.go @@ -1,7 +1,6 @@ // Copyright 2019 The Kubernetes Authors. // SPDX-License-Identifier: Apache-2.0 -// TODO: move most of the tests in the api/target package to this package. package krusty_test import ( @@ -11,12 +10,11 @@ import ( "sigs.k8s.io/kustomize/api/krusty" ) -// TODO: move most of the tests in api/internal/target -// to this package, as they are all high level tests and -// examples appropriate to this level and package. -// The following test isn't much more than a usage example; -// everything is actually tested down in api/internal/target. -func TestSomething(t *testing.T) { +// A simple usage example - just shows what happens when +// there are no files to read. For more substantial tests +// and examples, see other tests in this package. +// TODO: https://github.com/kubernetes-sigs/kustomize/issues/1862 +func TestEmptyFileSystem(t *testing.T) { fSys := filesys.MakeFsInMemory() b := krusty.MakeKustomizer(fSys, krusty.MakeDefaultOptions()) _, err := b.Run("noSuchThing") diff --git a/api/krusty/testingharness_test.go b/api/krusty/testingharness_test.go index 17d0ac790..b37a1c0f3 100644 --- a/api/krusty/testingharness_test.go +++ b/api/krusty/testingharness_test.go @@ -11,6 +11,7 @@ import ( "sigs.k8s.io/kustomize/api/filesys" "sigs.k8s.io/kustomize/api/konfig" + "sigs.k8s.io/kustomize/api/konfig/builtinpluginconsts" . "sigs.k8s.io/kustomize/api/krusty" "sigs.k8s.io/kustomize/api/resmap" "sigs.k8s.io/kustomize/api/types" @@ -176,3 +177,15 @@ func tabToSpace(input string) string { } return strings.Join(result, "") } + +func (th testingHarness) WriteLegacyConfigs(fName string) { + m := builtinpluginconsts.GetDefaultFieldSpecsAsMap() + var content []byte + for _, tCfg := range m { + content = append(content, []byte(tCfg)...) + } + err := th.fSys.WriteFile(fName, content) + if err != nil { + th.t.Fatalf("unable to add file %s", fName) + } +} diff --git a/api/internal/target/transformersarrays_test.go b/api/krusty/transformersarrays_test.go similarity index 91% rename from api/internal/target/transformersarrays_test.go rename to api/krusty/transformersarrays_test.go index 6e043d31b..90f13874d 100644 --- a/api/internal/target/transformersarrays_test.go +++ b/api/krusty/transformersarrays_test.go @@ -1,15 +1,13 @@ // Copyright 2019 The Kubernetes Authors. // SPDX-License-Identifier: Apache-2.0 -package target_test +package krusty_test import ( "testing" - - kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" ) -func makeStatefulSetKustomization(th *kusttest_test.KustTestHarness) { +func makeStatefulSetKustomization(th testingHarness) { th.WriteK("/app", ` commonLabels: notIn: arrays @@ -92,12 +90,9 @@ spec: } func TestTransformersNoCreateArrays(t *testing.T) { - th := kusttest_test.NewKustTestHarness(t, "/app") + th := makeTestHarness(t) makeStatefulSetKustomization(th) - m, err := th.MakeKustTarget().MakeCustomizedResMap() - if err != nil { - t.Fatalf("Err: %v", err) - } + m := th.Run("/app", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, ` apiVersion: apps/v1 kind: StatefulSet diff --git a/api/internal/target/transformersimage_test.go b/api/krusty/transformersimage_test.go similarity index 89% rename from api/internal/target/transformersimage_test.go rename to api/krusty/transformersimage_test.go index 6d56c3ee7..1b6d4da77 100644 --- a/api/internal/target/transformersimage_test.go +++ b/api/krusty/transformersimage_test.go @@ -1,15 +1,13 @@ // Copyright 2019 The Kubernetes Authors. // SPDX-License-Identifier: Apache-2.0 -package target_test +package krusty_test import ( "testing" - - kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" ) -func makeTransfomersImageBase(th *kusttest_test.KustTestHarness) { +func makeTransfomersImageBase(th testingHarness) { th.WriteK("/app/base", ` resources: - deploy1.yaml @@ -94,8 +92,7 @@ spec3: } func TestIssue1281_JsonPatchAndImageTag(t *testing.T) { - th := kusttest_test.NewKustTestHarness(t, "/app") - + th := makeTestHarness(t) th.WriteK("/app", ` resources: - deployment.yaml @@ -147,10 +144,7 @@ spec: "value": { "image": "costello" } } ] `) - m, err := th.MakeKustTarget().MakeCustomizedResMap() - if err != nil { - t.Fatalf("Err: %v", err) - } + m := th.Run("/app", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, ` apiVersion: apps/v1 kind: Deployment @@ -178,12 +172,9 @@ spec: } func TestTransfomersImageDefaultConfig(t *testing.T) { - th := kusttest_test.NewKustTestHarness(t, "/app/base") + th := makeTestHarness(t) makeTransfomersImageBase(th) - m, err := th.MakeKustTarget().MakeCustomizedResMap() - if err != nil { - t.Fatalf("Err: %v", err) - } + m := th.Run("/app/base", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, ` apiVersion: v1 group: apps @@ -242,7 +233,7 @@ spec3: `) } -func makeTransfomersImageCustomBase(th *kusttest_test.KustTestHarness) { +func makeTransfomersImageCustomBase(th testingHarness) { th.WriteK("/app/base", ` resources: - custom.yaml @@ -313,13 +304,11 @@ images: path: spec3/template/spec/myInitContainers/image `) } + func TestTransfomersImageCustomConfig(t *testing.T) { - th := kusttest_test.NewKustTestHarness(t, "/app/base") + th := makeTestHarness(t) makeTransfomersImageCustomBase(th) - m, err := th.MakeKustTarget().MakeCustomizedResMap() - if err != nil { - t.Fatalf("Err: %v", err) - } + m := th.Run("/app/base", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, ` kind: customKind metadata: @@ -357,7 +346,7 @@ spec3: `) } -func makeTransfomersImageKnativeBase(th *kusttest_test.KustTestHarness) { +func makeTransfomersImageKnativeBase(th testingHarness) { th.WriteK("/app/base", ` resources: - knative.yaml @@ -389,12 +378,9 @@ images: } func TestTransfomersImageKnativeConfig(t *testing.T) { - th := kusttest_test.NewKustTestHarness(t, "/app/base") + th := makeTestHarness(t) makeTransfomersImageKnativeBase(th) - m, err := th.MakeKustTarget().MakeCustomizedResMap() - if err != nil { - t.Fatalf("Err: %v", err) - } + m := th.Run("/app/base", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, ` apiVersion: serving.knative.dev/v1alpha1 kind: Service