Make kusttestharness shareable.

This commit is contained in:
Jeffrey Regan
2019-05-07 14:18:35 -07:00
parent 8c5d4128e0
commit bcc7412ef2
23 changed files with 389 additions and 343 deletions

View File

@@ -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

View File

@@ -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:

View File

@@ -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)
}

View File

@@ -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

View File

@@ -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

View File

@@ -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:

View File

@@ -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:

View File

@@ -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

View File

@@ -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")
}

View File

@@ -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

View File

@@ -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")
}

View File

@@ -1,195 +0,0 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
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.
import (
"fmt"
"path/filepath"
"strings"
"testing"
"sigs.k8s.io/kustomize/internal/loadertest"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
"sigs.k8s.io/kustomize/k8sdeps/transformer"
"sigs.k8s.io/kustomize/pkg/loader"
"sigs.k8s.io/kustomize/pkg/pgmconfig"
"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/transformers/config/defaultconfig"
"sigs.k8s.io/kustomize/pkg/types"
)
type KustTestHarness struct {
t *testing.T
rf *resmap.Factory
ldr loadertest.FakeLoader
pl *plugins.Loader
}
func NewKustTestHarness(t *testing.T, path string) *KustTestHarness {
return NewKustTestHarnessWithPluginConfig(
t, path, plugin.DefaultPluginConfig())
}
func NewKustTestHarnessWithPluginConfig(
t *testing.T, path string,
pc *types.PluginConfig) *KustTestHarness {
return NewKustTestHarnessFull(t, path, loader.RestrictionRootOnly, pc)
}
func NewKustTestHarnessFull(
t *testing.T, path string,
lr loader.LoadRestrictorFunc, pc *types.PluginConfig) *KustTestHarness {
rf := resmap.NewFactory(resource.NewFactory(
kunstruct.NewKunstructuredFactoryWithGeneratorArgs(
&types.GeneratorMetaArgs{PluginConfig: pc})))
return &KustTestHarness{
t: t,
rf: rf,
ldr: loadertest.NewFakeLoaderWithRestrictor(lr, path),
pl: plugins.NewLoader(pc, rf)}
}
func (th *KustTestHarness) makeKustTarget() *KustTarget {
kt, err := NewKustTarget(
th.ldr, th.rf, transformer.NewFactoryImpl(), th.pl)
if err != nil {
th.t.Fatalf("Unexpected construction error %v", err)
}
return kt
}
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]), `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
`+content)
}
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 {
return th.rf.RF().FromMapAndOption(m, args, option)
}
func (th *KustTestHarness) writeDefaultConfigs(fName string) {
m := defaultconfig.GetDefaultFieldSpecsAsMap()
var content []byte
for _, tCfg := range m {
content = append(content, []byte(tCfg)...)
}
err := th.ldr.AddFile(fName, content)
if err != nil {
th.t.Fatalf("unable to add file %s", fName)
}
}
func tabToSpace(input string) string {
var result []string
for _, i := range input {
if i == 9 {
result = append(result, " ")
} else {
result = append(result, string(i))
}
}
return strings.Join(result, "")
}
func convertToArray(x string) ([]string, int) {
a := strings.Split(strings.TrimSuffix(x, "\n"), "\n")
maxLen := 0
for i, v := range a {
z := tabToSpace(v)
if len(z) > maxLen {
maxLen = len(z)
}
a[i] = z
}
return a, maxLen
}
func hint(a, b string) string {
if a == b {
return " "
}
return "X"
}
func (th *KustTestHarness) assertActualEqualsExpected(
m resmap.ResMap, expected string) {
if m == nil {
th.t.Fatalf("Map should not be nil.")
}
// Ignore leading linefeed in expected value
// to ease readability of tests.
if len(expected) > 0 && expected[0] == 10 {
expected = expected[1:]
}
actual, err := m.EncodeAsYaml()
if err != nil {
th.t.Fatalf("Unexpected err: %v", err)
}
if string(actual) != expected {
th.reportDiffAndFail(actual, expected)
}
}
// Pretty printing of file differences.
func (th *KustTestHarness) reportDiffAndFail(actual []byte, expected string) {
sE, maxLen := convertToArray(expected)
sA, _ := convertToArray(string(actual))
fmt.Println("===== ACTUAL BEGIN ========================================")
fmt.Print(string(actual))
fmt.Println("===== ACTUAL END ==========================================")
format := fmt.Sprintf("%%s %%-%ds %%s\n", maxLen+4)
limit := 0
if len(sE) < len(sA) {
limit = len(sE)
} else {
limit = len(sA)
}
fmt.Printf(format, " ", "EXPECTED", "ACTUAL")
fmt.Printf(format, " ", "--------", "------")
for i := 0; i < limit; i++ {
fmt.Printf(format, hint(sE[i], sA[i]), sE[i], sA[i])
}
if len(sE) < len(sA) {
for i := len(sE); i < len(sA); i++ {
fmt.Printf(format, "X", "", sA[i])
}
} else {
for i := len(sA); i < len(sE); i++ {
fmt.Printf(format, "X", sE[i], "")
}
}
th.t.Fatalf("Expected not equal to actual")
}

View File

@@ -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")
}

View File

@@ -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!

View File

@@ -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:

View File

@@ -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:

View File

@@ -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:

View File

@@ -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.")
}

View File

@@ -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:

View File

@@ -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:

View File

@@ -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: