Merge pull request #1867 from monopole/moarTests

More tests.
This commit is contained in:
Jeff Regan
2019-11-29 08:53:10 -08:00
committed by GitHub
5 changed files with 46 additions and 67 deletions

View File

@@ -1,15 +1,13 @@
// Copyright 2019 The Kubernetes Authors. // Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package target_test package krusty_test
import ( import (
"testing" "testing"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
) )
func makeBaseReferencingCustomConfig(th *kusttest_test.KustTestHarness) { func makeBaseReferencingCustomConfig(th testingHarness) {
th.WriteK("/app/base", ` th.WriteK("/app/base", `
namePrefix: x- namePrefix: x-
commonLabels: commonLabels:
@@ -74,9 +72,9 @@ spec:
} }
func TestCustomConfig(t *testing.T) { func TestCustomConfig(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app/base") th := makeTestHarness(t)
makeBaseReferencingCustomConfig(th) makeBaseReferencingCustomConfig(th)
th.WriteDefaultConfigs("/app/base/config/defaults.yaml") th.WriteLegacyConfigs("/app/base/config/defaults.yaml")
th.WriteF("/app/base/config/custom.yaml", ` th.WriteF("/app/base/config/custom.yaml", `
nameReference: nameReference:
- kind: Gorilla - kind: Gorilla
@@ -91,10 +89,7 @@ varReference:
- path: spec/food - path: spec/food
kind: AnimalPark kind: AnimalPark
`) `)
m, err := th.MakeKustTarget().MakeCustomizedResMap() m := th.Run("/app/base", th.MakeDefaultOptions())
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
kind: AnimalPark kind: AnimalPark
metadata: metadata:
@@ -140,9 +135,9 @@ spec:
} }
func TestCustomConfigWithDefaultOverspecification(t *testing.T) { func TestCustomConfigWithDefaultOverspecification(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app/base") th := makeTestHarness(t)
makeBaseReferencingCustomConfig(th) 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) // Specifying namePrefix here conflicts with (is the same as)
// the defaults written above. This is intentional in the // the defaults written above. This is intentional in the
// test to assure duplicate config doesn't cause problems. // test to assure duplicate config doesn't cause problems.
@@ -162,10 +157,7 @@ varReference:
- path: spec/food - path: spec/food
kind: AnimalPark kind: AnimalPark
`) `)
m, err := th.MakeKustTarget().MakeCustomizedResMap() m := th.Run("/app/base", th.MakeDefaultOptions())
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
kind: AnimalPark kind: AnimalPark
metadata: metadata:
@@ -211,9 +203,9 @@ spec:
} }
func TestFixedBug605_BaseCustomizationAvailableInOverlay(t *testing.T) { func TestFixedBug605_BaseCustomizationAvailableInOverlay(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app/overlay") th := makeTestHarness(t)
makeBaseReferencingCustomConfig(th) makeBaseReferencingCustomConfig(th)
th.WriteDefaultConfigs("/app/base/config/defaults.yaml") th.WriteLegacyConfigs("/app/base/config/defaults.yaml")
th.WriteF("/app/base/config/custom.yaml", ` th.WriteF("/app/base/config/custom.yaml", `
nameReference: nameReference:
- kind: Gorilla - kind: Gorilla
@@ -255,12 +247,7 @@ spec:
gorillaRef: gorillaRef:
name: ursus name: ursus
`) `)
m := th.Run("/app/overlay", th.MakeDefaultOptions())
m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
kind: AnimalPark kind: AnimalPark
metadata: metadata:

View File

@@ -1,7 +1,6 @@
// Copyright 2019 The Kubernetes Authors. // Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// TODO: move most of the tests in the api/target package to this package.
package krusty_test package krusty_test
import ( import (
@@ -11,12 +10,11 @@ import (
"sigs.k8s.io/kustomize/api/krusty" "sigs.k8s.io/kustomize/api/krusty"
) )
// TODO: move most of the tests in api/internal/target // A simple usage example - just shows what happens when
// to this package, as they are all high level tests and // there are no files to read. For more substantial tests
// examples appropriate to this level and package. // and examples, see other tests in this package.
// The following test isn't much more than a usage example; // TODO: https://github.com/kubernetes-sigs/kustomize/issues/1862
// everything is actually tested down in api/internal/target. func TestEmptyFileSystem(t *testing.T) {
func TestSomething(t *testing.T) {
fSys := filesys.MakeFsInMemory() fSys := filesys.MakeFsInMemory()
b := krusty.MakeKustomizer(fSys, krusty.MakeDefaultOptions()) b := krusty.MakeKustomizer(fSys, krusty.MakeDefaultOptions())
_, err := b.Run("noSuchThing") _, err := b.Run("noSuchThing")

View File

@@ -11,6 +11,7 @@ import (
"sigs.k8s.io/kustomize/api/filesys" "sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/konfig" "sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/konfig/builtinpluginconsts"
. "sigs.k8s.io/kustomize/api/krusty" . "sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/api/resmap" "sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/api/types"
@@ -176,3 +177,15 @@ func tabToSpace(input string) string {
} }
return strings.Join(result, "") 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)
}
}

View File

@@ -1,15 +1,13 @@
// Copyright 2019 The Kubernetes Authors. // Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package target_test package krusty_test
import ( import (
"testing" "testing"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
) )
func makeStatefulSetKustomization(th *kusttest_test.KustTestHarness) { func makeStatefulSetKustomization(th testingHarness) {
th.WriteK("/app", ` th.WriteK("/app", `
commonLabels: commonLabels:
notIn: arrays notIn: arrays
@@ -92,12 +90,9 @@ spec:
} }
func TestTransformersNoCreateArrays(t *testing.T) { func TestTransformersNoCreateArrays(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app") th := makeTestHarness(t)
makeStatefulSetKustomization(th) makeStatefulSetKustomization(th)
m, err := th.MakeKustTarget().MakeCustomizedResMap() m := th.Run("/app", th.MakeDefaultOptions())
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: apps/v1 apiVersion: apps/v1
kind: StatefulSet kind: StatefulSet

View File

@@ -1,15 +1,13 @@
// Copyright 2019 The Kubernetes Authors. // Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package target_test package krusty_test
import ( import (
"testing" "testing"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
) )
func makeTransfomersImageBase(th *kusttest_test.KustTestHarness) { func makeTransfomersImageBase(th testingHarness) {
th.WriteK("/app/base", ` th.WriteK("/app/base", `
resources: resources:
- deploy1.yaml - deploy1.yaml
@@ -94,8 +92,7 @@ spec3:
} }
func TestIssue1281_JsonPatchAndImageTag(t *testing.T) { func TestIssue1281_JsonPatchAndImageTag(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app") th := makeTestHarness(t)
th.WriteK("/app", ` th.WriteK("/app", `
resources: resources:
- deployment.yaml - deployment.yaml
@@ -147,10 +144,7 @@ spec:
"value": { "image": "costello" } } ] "value": { "image": "costello" } } ]
`) `)
m, err := th.MakeKustTarget().MakeCustomizedResMap() m := th.Run("/app", th.MakeDefaultOptions())
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
@@ -178,12 +172,9 @@ spec:
} }
func TestTransfomersImageDefaultConfig(t *testing.T) { func TestTransfomersImageDefaultConfig(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app/base") th := makeTestHarness(t)
makeTransfomersImageBase(th) makeTransfomersImageBase(th)
m, err := th.MakeKustTarget().MakeCustomizedResMap() m := th.Run("/app/base", th.MakeDefaultOptions())
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
group: apps group: apps
@@ -242,7 +233,7 @@ spec3:
`) `)
} }
func makeTransfomersImageCustomBase(th *kusttest_test.KustTestHarness) { func makeTransfomersImageCustomBase(th testingHarness) {
th.WriteK("/app/base", ` th.WriteK("/app/base", `
resources: resources:
- custom.yaml - custom.yaml
@@ -313,13 +304,11 @@ images:
path: spec3/template/spec/myInitContainers/image path: spec3/template/spec/myInitContainers/image
`) `)
} }
func TestTransfomersImageCustomConfig(t *testing.T) { func TestTransfomersImageCustomConfig(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app/base") th := makeTestHarness(t)
makeTransfomersImageCustomBase(th) makeTransfomersImageCustomBase(th)
m, err := th.MakeKustTarget().MakeCustomizedResMap() m := th.Run("/app/base", th.MakeDefaultOptions())
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
kind: customKind kind: customKind
metadata: metadata:
@@ -357,7 +346,7 @@ spec3:
`) `)
} }
func makeTransfomersImageKnativeBase(th *kusttest_test.KustTestHarness) { func makeTransfomersImageKnativeBase(th testingHarness) {
th.WriteK("/app/base", ` th.WriteK("/app/base", `
resources: resources:
- knative.yaml - knative.yaml
@@ -389,12 +378,9 @@ images:
} }
func TestTransfomersImageKnativeConfig(t *testing.T) { func TestTransfomersImageKnativeConfig(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app/base") th := makeTestHarness(t)
makeTransfomersImageKnativeBase(th) makeTransfomersImageKnativeBase(th)
m, err := th.MakeKustTarget().MakeCustomizedResMap() m := th.Run("/app/base", th.MakeDefaultOptions())
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: serving.knative.dev/v1alpha1 apiVersion: serving.knative.dev/v1alpha1
kind: Service kind: Service