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

@@ -14,9 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package target_test package kusttest_test
// A collection of utilities used in target tests.
import ( import (
"fmt" "fmt"
@@ -33,11 +31,12 @@ import (
"sigs.k8s.io/kustomize/pkg/plugins" "sigs.k8s.io/kustomize/pkg/plugins"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource" "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/transformers/config/defaultconfig"
"sigs.k8s.io/kustomize/pkg/types" "sigs.k8s.io/kustomize/pkg/types"
) )
// KustTestHarness helps test kustomization generation and transformation.
type KustTestHarness struct { type KustTestHarness struct {
t *testing.T t *testing.T
rf *resmap.Factory rf *resmap.Factory
@@ -69,8 +68,8 @@ func NewKustTestHarnessFull(
pl: plugins.NewLoader(pc, rf)} pl: plugins.NewLoader(pc, rf)}
} }
func (th *KustTestHarness) makeKustTarget() *KustTarget { func (th *KustTestHarness) MakeKustTarget() *target.KustTarget {
kt, err := NewKustTarget( kt, err := target.NewKustTarget(
th.ldr, th.rf, transformer.NewFactoryImpl(), th.pl) th.ldr, th.rf, transformer.NewFactoryImpl(), th.pl)
if err != nil { if err != nil {
th.t.Fatalf("Unexpected construction error %v", err) th.t.Fatalf("Unexpected construction error %v", err)
@@ -78,29 +77,29 @@ func (th *KustTestHarness) makeKustTarget() *KustTarget {
return kt 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)) err := th.ldr.AddFile(dir, []byte(content))
if err != nil { if err != nil {
th.t.Fatalf("failed write to %s; %v", dir, err) th.t.Fatalf("failed write to %s; %v", dir, err)
} }
} }
func (th *KustTestHarness) writeK(dir string, content string) { func (th *KustTestHarness) WriteK(dir string, content string) {
th.writeF(filepath.Join(dir, pgmconfig.KustomizationFileNames[0]), ` th.WriteF(filepath.Join(dir, pgmconfig.KustomizationFileNames[0]), `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
`+content) `+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) 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) return th.rf.RF().FromMapAndOption(m, args, option)
} }
func (th *KustTestHarness) writeDefaultConfigs(fName string) { func (th *KustTestHarness) WriteDefaultConfigs(fName string) {
m := defaultconfig.GetDefaultFieldSpecsAsMap() m := defaultconfig.GetDefaultFieldSpecsAsMap()
var content []byte var content []byte
for _, tCfg := range m { for _, tCfg := range m {
@@ -144,7 +143,7 @@ func hint(a, b string) string {
return "X" return "X"
} }
func (th *KustTestHarness) assertActualEqualsExpected( func (th *KustTestHarness) AssertActualEqualsExpected(
m resmap.ResMap, expected string) { m resmap.ResMap, expected string) {
if m == nil { if m == nil {
th.t.Fatalf("Map should not be nil.") th.t.Fatalf("Map should not be nil.")

View File

@@ -104,6 +104,8 @@ func (l *Loader) loadAndConfigurePlugin(
return c, nil 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, // 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 // but the loaded .so files are in shared memory, so one will get
// "this plugin already loaded" errors if the registry is maintained // "this plugin already loaded" errors if the registry is maintained

View File

@@ -85,6 +85,19 @@ func (rf *Factory) SliceFromPatches(
return result, nil 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. // SliceFromBytes unmarshalls bytes into a Resource slice.
func (rf *Factory) SliceFromBytes(in []byte) ([]*Resource, error) { func (rf *Factory) SliceFromBytes(in []byte) ([]*Resource, error) {
kunStructs, err := rf.kf.SliceFromBytes(in) kunStructs, err := rf.kf.SliceFromBytes(in)

View File

@@ -18,6 +18,8 @@ package target_test
import ( import (
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
// TODO(monopole): Add a feature test example covering secret generation. // 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 // 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. // clean up after, or use some other trick compatible with exec.
func writeMediumBase(th *KustTestHarness) { func writeMediumBase(th *kusttest_test.KustTestHarness) {
th.writeK("/app/base", ` th.WriteK("/app/base", `
namePrefix: baseprefix- namePrefix: baseprefix-
commonLabels: commonLabels:
foo: bar foo: bar
@@ -46,7 +48,7 @@ resources:
- deployment/deployment.yaml - deployment/deployment.yaml
- service/service.yaml - service/service.yaml
`) `)
th.writeF("/app/base/service/service.yaml", ` th.WriteF("/app/base/service/service.yaml", `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@@ -59,7 +61,7 @@ spec:
selector: selector:
app: mungebot app: mungebot
`) `)
th.writeF("/app/base/deployment/deployment.yaml", ` th.WriteF("/app/base/deployment/deployment.yaml", `
apiVersion: extensions/v1beta1 apiVersion: extensions/v1beta1
kind: Deployment kind: Deployment
metadata: metadata:
@@ -85,13 +87,13 @@ spec:
} }
func TestMediumBase(t *testing.T) { func TestMediumBase(t *testing.T) {
th := NewKustTestHarness(t, "/app/base") th := kusttest_test.NewKustTestHarness(t, "/app/base")
writeMediumBase(th) writeMediumBase(th)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@@ -142,9 +144,9 @@ spec:
} }
func TestMediumOverlay(t *testing.T) { func TestMediumOverlay(t *testing.T) {
th := NewKustTestHarness(t, "/app/overlay") th := kusttest_test.NewKustTestHarness(t, "/app/overlay")
writeMediumBase(th) writeMediumBase(th)
th.writeK("/app/overlay", ` th.WriteK("/app/overlay", `
namePrefix: test-infra- namePrefix: test-infra-
commonLabels: commonLabels:
app: mungebot app: mungebot
@@ -169,24 +171,24 @@ images:
- name: nginx - name: nginx
newTag: 1.8.0`) newTag: 1.8.0`)
th.writeF("/app/overlay/configmap/db.env", ` th.WriteF("/app/overlay/configmap/db.env", `
DB_USERNAME=admin DB_USERNAME=admin
DB_PASSWORD=somepw DB_PASSWORD=somepw
`) `)
th.writeF("/app/overlay/configmap/units.ini", ` th.WriteF("/app/overlay/configmap/units.ini", `
LENGTH=kilometer LENGTH=kilometer
ENERGY=electronvolt ENERGY=electronvolt
`) `)
th.writeF("/app/overlay/configmap/food.ini", ` th.WriteF("/app/overlay/configmap/food.ini", `
FRUIT=banana FRUIT=banana
LEGUME=chickpea LEGUME=chickpea
`) `)
th.writeF("/app/overlay/configmap/dummy.txt", th.WriteF("/app/overlay/configmap/dummy.txt",
`Lorem ipsum dolor sit amet, consectetur `Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. incididunt ut labore et dolore magna aliqua.
`) `)
th.writeF("/app/overlay/deployment/deployment.yaml", ` th.WriteF("/app/overlay/deployment/deployment.yaml", `
apiVersion: extensions/v1beta1 apiVersion: extensions/v1beta1
kind: Deployment kind: Deployment
metadata: metadata:
@@ -219,14 +221,14 @@ spec:
name: app-env name: app-env
name: app-env name: app-env
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
// TODO(#669): The name of the patched Deployment is // TODO(#669): The name of the patched Deployment is
// test-infra-baseprefix-mungebot, retaining the base // test-infra-baseprefix-mungebot, retaining the base
// prefix (example of correct behavior). // prefix (example of correct behavior).
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
nonsense: "Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod nonsense: "Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod

View File

@@ -21,11 +21,12 @@ import (
"testing" "testing"
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
"sigs.k8s.io/kustomize/pkg/kusttest"
"sigs.k8s.io/kustomize/pkg/loader" "sigs.k8s.io/kustomize/pkg/loader"
) )
func writeSmallBase(th *KustTestHarness) { func writeSmallBase(th *kusttest_test.KustTestHarness) {
th.writeK("/app/base", ` th.WriteK("/app/base", `
namePrefix: a- namePrefix: a-
commonLabels: commonLabels:
app: myApp app: myApp
@@ -33,7 +34,7 @@ resources:
- deployment.yaml - deployment.yaml
- service.yaml - service.yaml
`) `)
th.writeF("/app/base/service.yaml", ` th.WriteF("/app/base/service.yaml", `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@@ -44,7 +45,7 @@ spec:
ports: ports:
- port: 7002 - port: 7002
`) `)
th.writeF("/app/base/deployment.yaml", ` th.WriteF("/app/base/deployment.yaml", `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
@@ -62,13 +63,13 @@ spec:
} }
func TestSmallBase(t *testing.T) { func TestSmallBase(t *testing.T) {
th := NewKustTestHarness(t, "/app/base") th := kusttest_test.NewKustTestHarness(t, "/app/base")
writeSmallBase(th) writeSmallBase(th)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@@ -105,9 +106,9 @@ spec:
} }
func TestSmallOverlay(t *testing.T) { func TestSmallOverlay(t *testing.T) {
th := NewKustTestHarness(t, "/app/overlay") th := kusttest_test.NewKustTestHarness(t, "/app/overlay")
writeSmallBase(th) writeSmallBase(th)
th.writeK("/app/overlay", ` th.WriteK("/app/overlay", `
namePrefix: b- namePrefix: b-
commonLabels: commonLabels:
env: prod env: prod
@@ -120,15 +121,15 @@ images:
newTag: 1.8.0 newTag: 1.8.0
`) `)
th.writeF("/app/overlay/configmap/app.env", ` th.WriteF("/app/overlay/configmap/app.env", `
DB_USERNAME=admin DB_USERNAME=admin
DB_PASSWORD=somepw DB_PASSWORD=somepw
`) `)
th.writeF("/app/overlay/configmap/app-init.ini", ` th.WriteF("/app/overlay/configmap/app-init.ini", `
FOO=bar FOO=bar
BAR=baz BAR=baz
`) `)
th.writeF("/app/overlay/deployment/deployment.yaml", ` th.WriteF("/app/overlay/deployment/deployment.yaml", `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
@@ -136,14 +137,14 @@ metadata:
spec: spec:
replicas: 1000 replicas: 1000
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
// TODO(#669): The name of the patched Deployment is // TODO(#669): The name of the patched Deployment is
// b-a-myDeployment, retaining the base prefix // b-a-myDeployment, retaining the base prefix
// (example of correct behavior). // (example of correct behavior).
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@@ -186,11 +187,11 @@ spec:
} }
func TestSharedPatchDisAllowed(t *testing.T) { func TestSharedPatchDisAllowed(t *testing.T) {
th := NewKustTestHarnessFull( th := kusttest_test.NewKustTestHarnessFull(
t, "/app/overlay", t, "/app/overlay",
loader.RestrictionRootOnly, plugin.DefaultPluginConfig()) loader.RestrictionRootOnly, plugin.DefaultPluginConfig())
writeSmallBase(th) writeSmallBase(th)
th.writeK("/app/overlay", ` th.WriteK("/app/overlay", `
commonLabels: commonLabels:
env: prod env: prod
bases: bases:
@@ -198,7 +199,7 @@ bases:
patchesStrategicMerge: patchesStrategicMerge:
- ../shared/deployment-patch.yaml - ../shared/deployment-patch.yaml
`) `)
th.writeF("/app/shared/deployment-patch.yaml", ` th.WriteF("/app/shared/deployment-patch.yaml", `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
@@ -206,7 +207,7 @@ metadata:
spec: spec:
replicas: 1000 replicas: 1000
`) `)
_, err := th.makeKustTarget().MakeCustomizedResMap() _, err := th.MakeKustTarget().MakeCustomizedResMap()
if err == nil { if err == nil {
t.Fatalf("expected error") t.Fatalf("expected error")
} }
@@ -218,11 +219,11 @@ spec:
} }
func TestSharedPatchAllowed(t *testing.T) { func TestSharedPatchAllowed(t *testing.T) {
th := NewKustTestHarnessFull( th := kusttest_test.NewKustTestHarnessFull(
t, "/app/overlay", t, "/app/overlay",
loader.RestrictionNone, plugin.DefaultPluginConfig()) loader.RestrictionNone, plugin.DefaultPluginConfig())
writeSmallBase(th) writeSmallBase(th)
th.writeK("/app/overlay", ` th.WriteK("/app/overlay", `
commonLabels: commonLabels:
env: prod env: prod
bases: bases:
@@ -230,7 +231,7 @@ bases:
patchesStrategicMerge: patchesStrategicMerge:
- ../shared/deployment-patch.yaml - ../shared/deployment-patch.yaml
`) `)
th.writeF("/app/shared/deployment-patch.yaml", ` th.WriteF("/app/shared/deployment-patch.yaml", `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
@@ -238,11 +239,11 @@ metadata:
spec: spec:
replicas: 1000 replicas: 1000
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@@ -285,9 +286,9 @@ spec:
} }
func TestSmallOverlayJSONPatch(t *testing.T) { func TestSmallOverlayJSONPatch(t *testing.T) {
th := NewKustTestHarness(t, "/app/overlay") th := kusttest_test.NewKustTestHarness(t, "/app/overlay")
writeSmallBase(th) writeSmallBase(th)
th.writeK("/app/overlay", ` th.WriteK("/app/overlay", `
bases: bases:
- ../base - ../base
patchesJson6902: patchesJson6902:
@@ -298,16 +299,16 @@ patchesJson6902:
path: service-patch.yaml path: service-patch.yaml
`) `)
th.writeF("/app/overlay/service-patch.yaml", ` th.WriteF("/app/overlay/service-patch.yaml", `
- op: add - op: add
path: /spec/selector/backend path: /spec/selector/backend
value: beagle value: beagle
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:

View File

@@ -18,6 +18,8 @@ package target_test
import ( import (
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
const result = ` const result = `
@@ -35,17 +37,17 @@ metadata:
type: Opaque type: Opaque
` `
func writeDataFiles(th *KustTestHarness) { func writeDataFiles(th *kusttest_test.KustTestHarness) {
th.writeF("/app/foo.env", ` th.WriteF("/app/foo.env", `
MOUNTAIN=everest MOUNTAIN=everest
OCEAN=pacific OCEAN=pacific
`) `)
th.writeF("/app/phrase.dat", "dat phrase") th.WriteF("/app/phrase.dat", "dat phrase")
} }
func TestBuiltinPlugins(t *testing.T) { func TestBuiltinPlugins(t *testing.T) {
th := NewKustTestHarness(t, "/app") th := kusttest_test.NewKustTestHarness(t, "/app")
th.writeK("/app", ` th.WriteK("/app", `
secretGenerator: secretGenerator:
- name: bob - name: bob
kvSources: kvSources:
@@ -65,16 +67,16 @@ secretGenerator:
- foo.env - foo.env
`) `)
writeDataFiles(th) writeDataFiles(th)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, result) th.AssertActualEqualsExpected(m, result)
} }
func TestBuiltinIsTheDefault(t *testing.T) { func TestBuiltinIsTheDefault(t *testing.T) {
th := NewKustTestHarness(t, "/app") th := kusttest_test.NewKustTestHarness(t, "/app")
th.writeK("/app", ` th.WriteK("/app", `
secretGenerator: secretGenerator:
- name: bob - name: bob
kvSources: kvSources:
@@ -91,9 +93,9 @@ secretGenerator:
- foo.env - foo.env
`) `)
writeDataFiles(th) writeDataFiles(th)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) 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/internal/plugintest"
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "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, // This is an example of using a helm chart as a base,
@@ -44,15 +45,15 @@ func TestChartInflatorExecPlugin(t *testing.T) {
tc.BuildExecPlugin( tc.BuildExecPlugin(
"someteam.example.com", "v1", "ChartInflatorExec") "someteam.example.com", "v1", "ChartInflatorExec")
th := NewKustTestHarnessWithPluginConfig( th := kusttest_test.NewKustTestHarnessWithPluginConfig(
t, "/app", plugin.ActivePluginConfig()) t, "/app", plugin.ActivePluginConfig())
th.writeK("/app", ` th.WriteK("/app", `
generators: generators:
- chartInflatorExec.yaml - chartInflatorExec.yaml
namePrefix: LOOOOOOOONG- namePrefix: LOOOOOOOONG-
`) `)
th.writeF("/app/chartInflatorExec.yaml", ` th.WriteF("/app/chartInflatorExec.yaml", `
apiVersion: someteam.example.com/v1 apiVersion: someteam.example.com/v1
kind: ChartInflatorExec kind: ChartInflatorExec
metadata: metadata:
@@ -60,11 +61,11 @@ metadata:
chartName: minecraft chartName: minecraft
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
rcon-password: Q0hBTkdFTUUh rcon-password: Q0hBTkdFTUUh

View File

@@ -18,13 +18,15 @@ package target_test
import ( import (
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
// Generate a Secret and a ConfigMap from the same data // Generate a Secret and a ConfigMap from the same data
// to compare the result. // to compare the result.
func TestGeneratorBasics(t *testing.T) { func TestGeneratorBasics(t *testing.T) {
th := NewKustTestHarness(t, "/app") th := kusttest_test.NewKustTestHarness(t, "/app")
th.writeK("/app", ` th.WriteK("/app", `
namePrefix: blah- namePrefix: blah-
configMapGenerator: configMapGenerator:
- name: bob - name: bob
@@ -48,26 +50,26 @@ secretGenerator:
- passphrase=phrase.dat - passphrase=phrase.dat
- forces.txt - forces.txt
`) `)
th.writeF("/app/foo.env", ` th.WriteF("/app/foo.env", `
MOUNTAIN=everest MOUNTAIN=everest
OCEAN=pacific OCEAN=pacific
`) `)
th.writeF("/app/phrase.dat", ` th.WriteF("/app/phrase.dat", `
Life is short. Life is short.
But the years are long. But the years are long.
Not while the evil days come not. Not while the evil days come not.
`) `)
th.writeF("/app/forces.txt", ` th.WriteF("/app/forces.txt", `
gravitational gravitational
electromagnetic electromagnetic
strong nuclear strong nuclear
weak nuclear weak nuclear
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
MOUNTAIN: everest MOUNTAIN: everest
@@ -113,8 +115,8 @@ type: Opaque
// TODO: These should be errors instead. // TODO: These should be errors instead.
func TestGeneratorRepeatsInKustomization(t *testing.T) { func TestGeneratorRepeatsInKustomization(t *testing.T) {
th := NewKustTestHarness(t, "/app") th := kusttest_test.NewKustTestHarness(t, "/app")
th.writeK("/app", ` th.WriteK("/app", `
namePrefix: blah- namePrefix: blah-
configMapGenerator: configMapGenerator:
- name: bob - name: bob
@@ -130,13 +132,13 @@ configMapGenerator:
files: files:
- nobles=nobility.txt - nobles=nobility.txt
`) `)
th.writeF("/app/forces.txt", ` th.WriteF("/app/forces.txt", `
gravitational gravitational
electromagnetic electromagnetic
strong nuclear strong nuclear
weak nuclear weak nuclear
`) `)
th.writeF("/app/nobility.txt", ` th.WriteF("/app/nobility.txt", `
helium helium
neon neon
argon argon
@@ -144,11 +146,11 @@ krypton
xenon xenon
radon radon
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
fruit: apple fruit: apple
@@ -168,8 +170,8 @@ metadata:
} }
func TestGeneratorOverlays(t *testing.T) { func TestGeneratorOverlays(t *testing.T) {
th := NewKustTestHarness(t, "/app/overlay") th := kusttest_test.NewKustTestHarness(t, "/app/overlay")
th.writeK("/app/base1", ` th.WriteK("/app/base1", `
namePrefix: p1- namePrefix: p1-
configMapGenerator: configMapGenerator:
- name: com1 - name: com1
@@ -177,7 +179,7 @@ configMapGenerator:
literals: literals:
- from=base - from=base
`) `)
th.writeK("/app/base2", ` th.WriteK("/app/base2", `
namePrefix: p2- namePrefix: p2-
configMapGenerator: configMapGenerator:
- name: com2 - name: com2
@@ -185,7 +187,7 @@ configMapGenerator:
literals: literals:
- from=base - from=base
`) `)
th.writeK("/app/overlay/o1", ` th.WriteK("/app/overlay/o1", `
bases: bases:
- ../../base1 - ../../base1
configMapGenerator: configMapGenerator:
@@ -194,7 +196,7 @@ configMapGenerator:
literals: literals:
- from=overlay - from=overlay
`) `)
th.writeK("/app/overlay/o2", ` th.WriteK("/app/overlay/o2", `
bases: bases:
- ../../base2 - ../../base2
configMapGenerator: configMapGenerator:
@@ -203,7 +205,7 @@ configMapGenerator:
literals: literals:
- from=overlay - from=overlay
`) `)
th.writeK("/app/overlay", ` th.WriteK("/app/overlay", `
bases: bases:
- o1 - o1
- o2 - o2
@@ -214,11 +216,11 @@ configMapGenerator:
- foo=bar - foo=bar
- baz=qux - baz=qux
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
baz: qux baz: qux

View File

@@ -18,10 +18,12 @@ package target_test
import ( import (
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
func writeBaseWithCrd(th *KustTestHarness) { func writeBaseWithCrd(th *kusttest_test.KustTestHarness) {
th.writeK("/app/base", ` th.WriteK("/app/base", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
crds: crds:
@@ -34,7 +36,7 @@ resources:
namePrefix: x- namePrefix: x-
`) `)
th.writeF("/app/base/bee.yaml", ` th.WriteF("/app/base/bee.yaml", `
apiVersion: v1beta1 apiVersion: v1beta1
kind: Bee kind: Bee
metadata: metadata:
@@ -42,7 +44,7 @@ metadata:
spec: spec:
action: fly action: fly
`) `)
th.writeF("/app/base/mykind.yaml", ` th.WriteF("/app/base/mykind.yaml", `
apiVersion: jingfang.example.com/v1beta1 apiVersion: jingfang.example.com/v1beta1
kind: MyKind kind: MyKind
metadata: metadata:
@@ -53,7 +55,7 @@ spec:
beeRef: beeRef:
name: bee name: bee
`) `)
th.writeF("/app/base/secret.yaml", ` th.WriteF("/app/base/secret.yaml", `
apiVersion: v1 apiVersion: v1
kind: Secret kind: Secret
metadata: metadata:
@@ -61,7 +63,7 @@ metadata:
data: data:
PATH: yellowBrickRoad PATH: yellowBrickRoad
`) `)
th.writeF("/app/base/mycrd.json", ` th.WriteF("/app/base/mycrd.json", `
{ {
"github.com/example/pkg/apis/jingfang/v1beta1.Bee": { "github.com/example/pkg/apis/jingfang/v1beta1.Bee": {
"Schema": { "Schema": {
@@ -236,13 +238,13 @@ data:
} }
func TestCrdBase(t *testing.T) { func TestCrdBase(t *testing.T) {
th := NewKustTestHarness(t, "/app/base") th := kusttest_test.NewKustTestHarness(t, "/app/base")
writeBaseWithCrd(th) writeBaseWithCrd(th)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
PATH: yellowBrickRoad PATH: yellowBrickRoad
@@ -270,9 +272,9 @@ spec:
} }
func TestCrdWithOverlay(t *testing.T) { func TestCrdWithOverlay(t *testing.T) {
th := NewKustTestHarness(t, "/app/overlay") th := kusttest_test.NewKustTestHarness(t, "/app/overlay")
writeBaseWithCrd(th) writeBaseWithCrd(th)
th.writeK("/app/overlay", ` th.WriteK("/app/overlay", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
namePrefix: prod- namePrefix: prod-
@@ -281,7 +283,7 @@ bases:
patchesStrategicMerge: patchesStrategicMerge:
- bee.yaml - bee.yaml
`) `)
th.writeF("/app/overlay/bee.yaml", ` th.WriteF("/app/overlay/bee.yaml", `
apiVersion: v1beta1 apiVersion: v1beta1
kind: Bee kind: Bee
metadata: metadata:
@@ -289,12 +291,12 @@ metadata:
spec: spec:
action: makehoney action: makehoney
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
// TODO(#669): Bee's name should be "prod-x-bee", not "prod-bee". // TODO(#669): Bee's name should be "prod-x-bee", not "prod-bee".
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
PATH: yellowBrickRoad PATH: yellowBrickRoad
@@ -322,8 +324,8 @@ spec:
} }
func TestCrdWithContainers(t *testing.T) { func TestCrdWithContainers(t *testing.T) {
th := NewKustTestHarness(t, "/app/crd/containers") th := kusttest_test.NewKustTestHarness(t, "/app/crd/containers")
th.writeK("/app/crd/containers", ` th.WriteK("/app/crd/containers", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
resources: resources:
@@ -334,7 +336,7 @@ images:
newName: registry.gitlab.com/test newName: registry.gitlab.com/test
newTag: latest newTag: latest
`) `)
th.writeF("/app/crd/containers/crd.yaml", ` th.WriteF("/app/crd/containers/crd.yaml", `
apiVersion: apiextensions.k8s.io/v1beta1 apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition kind: CustomResourceDefinition
metadata: metadata:
@@ -355,11 +357,11 @@ spec:
containers: containers:
description: Containers allows injecting additional containers description: Containers allows injecting additional containers
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: apiextensions.k8s.io/v1beta1 apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition kind: CustomResourceDefinition
metadata: metadata:

View File

@@ -18,10 +18,12 @@ package target_test
import ( import (
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
func makeBaseReferencingCustomConfig(th *KustTestHarness) { func makeBaseReferencingCustomConfig(th *kusttest_test.KustTestHarness) {
th.writeK("/app/base", ` th.WriteK("/app/base", `
namePrefix: x- namePrefix: x-
commonLabels: commonLabels:
app: myApp app: myApp
@@ -46,7 +48,7 @@ configurations:
- config/defaults.yaml - config/defaults.yaml
- config/custom.yaml - config/custom.yaml
`) `)
th.writeF("/app/base/giraffes.yaml", ` th.WriteF("/app/base/giraffes.yaml", `
kind: Giraffe kind: Giraffe
metadata: metadata:
name: may name: may
@@ -61,7 +63,7 @@ spec:
diet: mimosa diet: mimosa
location: NE location: NE
`) `)
th.writeF("/app/base/gorilla.yaml", ` th.WriteF("/app/base/gorilla.yaml", `
kind: Gorilla kind: Gorilla
metadata: metadata:
name: koko name: koko
@@ -69,7 +71,7 @@ spec:
diet: bambooshoots diet: bambooshoots
location: SW location: SW
`) `)
th.writeF("/app/base/animalPark.yaml", ` th.WriteF("/app/base/animalPark.yaml", `
kind: AnimalPark kind: AnimalPark
metadata: metadata:
name: sandiego name: sandiego
@@ -85,10 +87,10 @@ spec:
} }
func TestCustomConfig(t *testing.T) { func TestCustomConfig(t *testing.T) {
th := NewKustTestHarness(t, "/app/base") th := kusttest_test.NewKustTestHarness(t, "/app/base")
makeBaseReferencingCustomConfig(th) makeBaseReferencingCustomConfig(th)
th.writeDefaultConfigs("/app/base/config/defaults.yaml") th.WriteDefaultConfigs("/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
fieldSpecs: fieldSpecs:
@@ -102,11 +104,11 @@ varReference:
- path: spec/food - path: spec/food
kind: AnimalPark kind: AnimalPark
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
kind: AnimalPark kind: AnimalPark
metadata: metadata:
labels: labels:
@@ -151,13 +153,13 @@ spec:
} }
func TestCustomConfigWithDefaultOverspecification(t *testing.T) { func TestCustomConfigWithDefaultOverspecification(t *testing.T) {
th := NewKustTestHarness(t, "/app/base") th := kusttest_test.NewKustTestHarness(t, "/app/base")
makeBaseReferencingCustomConfig(th) 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) // 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.
th.writeF("/app/base/config/custom.yaml", ` th.WriteF("/app/base/config/custom.yaml", `
namePrefix: namePrefix:
- path: metadata/name - path: metadata/name
nameReference: nameReference:
@@ -173,11 +175,11 @@ varReference:
- path: spec/food - path: spec/food
kind: AnimalPark kind: AnimalPark
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
kind: AnimalPark kind: AnimalPark
metadata: metadata:
labels: labels:
@@ -222,10 +224,10 @@ spec:
} }
func TestFixedBug605_BaseCustomizationAvailableInOverlay(t *testing.T) { func TestFixedBug605_BaseCustomizationAvailableInOverlay(t *testing.T) {
th := NewKustTestHarness(t, "/app/overlay") th := kusttest_test.NewKustTestHarness(t, "/app/overlay")
makeBaseReferencingCustomConfig(th) makeBaseReferencingCustomConfig(th)
th.writeDefaultConfigs("/app/base/config/defaults.yaml") th.WriteDefaultConfigs("/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
fieldSpecs: fieldSpecs:
@@ -239,7 +241,7 @@ varReference:
- path: spec/food - path: spec/food
kind: AnimalPark kind: AnimalPark
`) `)
th.writeK("/app/overlay", ` th.WriteK("/app/overlay", `
namePrefix: o- namePrefix: o-
commonLabels: commonLabels:
movie: planetOfTheApes movie: planetOfTheApes
@@ -250,7 +252,7 @@ resources:
bases: bases:
- ../base - ../base
`) `)
th.writeF("/app/overlay/ursus.yaml", ` th.WriteF("/app/overlay/ursus.yaml", `
kind: Gorilla kind: Gorilla
metadata: metadata:
name: ursus name: ursus
@@ -259,7 +261,7 @@ spec:
location: Arizona location: Arizona
`) `)
// The following replaces the gorillaRef in the AnimalPark. // The following replaces the gorillaRef in the AnimalPark.
th.writeF("/app/overlay/animalPark.yaml", ` th.WriteF("/app/overlay/animalPark.yaml", `
kind: AnimalPark kind: AnimalPark
metadata: metadata:
name: sandiego name: sandiego
@@ -268,13 +270,13 @@ spec:
name: ursus name: ursus
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
// TODO(#669): The name of AnimalPark should be x-o-sandiego, // TODO(#669): The name of AnimalPark should be x-o-sandiego,
// not o-sandiego, since AnimalPark appears in the base. // not o-sandiego, since AnimalPark appears in the base.
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
kind: AnimalPark kind: AnimalPark
metadata: metadata:
labels: labels:

View File

@@ -18,11 +18,13 @@ package target_test
import ( import (
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
func TestSimpleBase(t *testing.T) { func TestSimpleBase(t *testing.T) {
th := NewKustTestHarness(t, "/app/base") th := kusttest_test.NewKustTestHarness(t, "/app/base")
th.writeK("/app/base", ` th.WriteK("/app/base", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
namePrefix: team-foo- namePrefix: team-foo-
@@ -37,7 +39,7 @@ resources:
- networkpolicy.yaml - networkpolicy.yaml
- service.yaml - service.yaml
`) `)
th.writeF("/app/base/service.yaml", ` th.WriteF("/app/base/service.yaml", `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@@ -50,7 +52,7 @@ spec:
selector: selector:
app: nginx app: nginx
`) `)
th.writeF("/app/base/networkpolicy.yaml", ` th.WriteF("/app/base/networkpolicy.yaml", `
apiVersion: networking.k8s.io/v1 apiVersion: networking.k8s.io/v1
kind: NetworkPolicy kind: NetworkPolicy
metadata: metadata:
@@ -65,7 +67,7 @@ spec:
matchLabels: matchLabels:
app: nginx app: nginx
`) `)
th.writeF("/app/base/deployment.yaml", ` th.WriteF("/app/base/deployment.yaml", `
apiVersion: apps/v1beta2 apiVersion: apps/v1beta2
kind: Deployment kind: Deployment
metadata: metadata:
@@ -82,11 +84,11 @@ spec:
- name: nginx - name: nginx
image: nginx image: nginx
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@@ -161,8 +163,8 @@ spec:
`) `)
} }
func makeBaseWithGenerators(th *KustTestHarness) { func makeBaseWithGenerators(th *kusttest_test.KustTestHarness) {
th.writeK("/app", ` th.WriteK("/app", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
namePrefix: team-foo- namePrefix: team-foo-
@@ -185,7 +187,7 @@ secretGenerator:
- username=admin - username=admin
- password=somepw - password=somepw
`) `)
th.writeF("/app/deployment.yaml", ` th.WriteF("/app/deployment.yaml", `
apiVersion: apps/v1beta2 apiVersion: apps/v1beta2
kind: Deployment kind: Deployment
metadata: metadata:
@@ -211,7 +213,7 @@ spec:
name: configmap-in-base name: configmap-in-base
name: configmap-in-base name: configmap-in-base
`) `)
th.writeF("/app/service.yaml", ` th.WriteF("/app/service.yaml", `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@@ -227,13 +229,13 @@ spec:
} }
func TestBaseWithGeneratorsAlone(t *testing.T) { func TestBaseWithGeneratorsAlone(t *testing.T) {
th := NewKustTestHarness(t, "/app") th := kusttest_test.NewKustTestHarness(t, "/app")
makeBaseWithGenerators(th) makeBaseWithGenerators(th)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
foo: bar foo: bar
@@ -321,9 +323,9 @@ spec:
} }
func TestMergeAndReplaceGenerators(t *testing.T) { func TestMergeAndReplaceGenerators(t *testing.T) {
th := NewKustTestHarness(t, "/overlay") th := kusttest_test.NewKustTestHarness(t, "/overlay")
makeBaseWithGenerators(th) makeBaseWithGenerators(th)
th.writeF("/overlay/deployment.yaml", ` th.WriteF("/overlay/deployment.yaml", `
apiVersion: apps/v1beta2 apiVersion: apps/v1beta2
kind: Deployment kind: Deployment
metadata: metadata:
@@ -340,7 +342,7 @@ spec:
name: configmap-in-overlay name: configmap-in-overlay
name: configmap-in-overlay name: configmap-in-overlay
`) `)
th.writeK("/overlay", ` th.WriteK("/overlay", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
namePrefix: staging- namePrefix: staging-
@@ -365,11 +367,11 @@ secretGenerator:
literals: literals:
- proxy=haproxy - proxy=haproxy
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
foo: override-bar foo: override-bar

View File

@@ -19,11 +19,12 @@ import (
"testing" "testing"
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
func TestGeneratorOptionsWithBases(t *testing.T) { func TestGeneratorOptionsWithBases(t *testing.T) {
th := NewKustTestHarness(t, "/app/overlay") th := kusttest_test.NewKustTestHarness(t, "/app/overlay")
th.writeK("/app/base", ` th.WriteK("/app/base", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
generatorOptions: generatorOptions:
@@ -35,7 +36,7 @@ configMapGenerator:
literals: literals:
- foo=bar - foo=bar
`) `)
th.writeK("/app/overlay", ` th.WriteK("/app/overlay", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
bases: bases:
@@ -49,11 +50,11 @@ configMapGenerator:
literals: literals:
- fruit=apple - fruit=apple
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
fruit: apple fruit: apple
@@ -75,8 +76,8 @@ metadata:
} }
func TestGoPluginNotEnabled(t *testing.T) { func TestGoPluginNotEnabled(t *testing.T) {
th := NewKustTestHarness(t, "/app") th := kusttest_test.NewKustTestHarness(t, "/app")
th.writeK("/app", ` th.WriteK("/app", `
secretGenerator: secretGenerator:
- name: attemptGoPlugin - name: attemptGoPlugin
kvSources: kvSources:
@@ -86,7 +87,7 @@ secretGenerator:
- someArg - someArg
- someOtherArg - someOtherArg
`) `)
_, err := th.makeKustTarget().MakeCustomizedResMap() _, err := th.MakeKustTarget().MakeCustomizedResMap()
if err == nil { if err == nil {
t.Fatalf("expected error") t.Fatalf("expected error")
} }
@@ -96,9 +97,9 @@ secretGenerator:
} }
func TestGoPluginDoesNotExist(t *testing.T) { func TestGoPluginDoesNotExist(t *testing.T) {
th := NewKustTestHarnessWithPluginConfig( th := kusttest_test.NewKustTestHarnessWithPluginConfig(
t, "/app", plugin.ActivePluginConfig()) t, "/app", plugin.ActivePluginConfig())
th.writeK("/app", ` th.WriteK("/app", `
secretGenerator: secretGenerator:
- name: attemptGoPlugin - name: attemptGoPlugin
kvSources: kvSources:
@@ -108,7 +109,7 @@ secretGenerator:
- someArg - someArg
- someOtherArg - someOtherArg
`) `)
_, err := th.makeKustTarget().MakeCustomizedResMap() _, err := th.MakeKustTarget().MakeCustomizedResMap()
if err == nil { if err == nil {
t.Fatalf("expected error") t.Fatalf("expected error")
} }

View File

@@ -19,10 +19,11 @@ import (
"sigs.k8s.io/kustomize/internal/plugintest" "sigs.k8s.io/kustomize/internal/plugintest"
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
func writeServiceGenerator(th *KustTestHarness, path string) { func writeServiceGenerator(th *kusttest_test.KustTestHarness, path string) {
th.writeF(path, ` th.WriteF(path, `
apiVersion: someteam.example.com/v1 apiVersion: someteam.example.com/v1
kind: ServiceGenerator kind: ServiceGenerator
metadata: metadata:
@@ -39,18 +40,18 @@ func TestServiceGeneratorPlugin(t *testing.T) {
tc.BuildGoPlugin( tc.BuildGoPlugin(
"someteam.example.com", "v1", "ServiceGenerator") "someteam.example.com", "v1", "ServiceGenerator")
th := NewKustTestHarnessWithPluginConfig( th := kusttest_test.NewKustTestHarnessWithPluginConfig(
t, "/app", plugin.ActivePluginConfig()) t, "/app", plugin.ActivePluginConfig())
th.writeK("/app", ` th.WriteK("/app", `
generators: generators:
- serviceGenerator.yaml - serviceGenerator.yaml
`) `)
writeServiceGenerator(th, "/app/serviceGenerator.yaml") writeServiceGenerator(th, "/app/serviceGenerator.yaml")
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@@ -65,13 +66,12 @@ spec:
`) `)
} }
func writeSecretGeneratorConfig(th *KustTestHarness, root string) { func writeSecretGeneratorConfig(th *kusttest_test.KustTestHarness, root string) {
th.writeF(filepath.Join(root, "secretGenerator.yaml"), ` th.WriteF(filepath.Join(root, "secretGenerator.yaml"), `
apiVersion: builtin apiVersion: builtin
kind: SecretGenerator kind: SecretGenerator
metadata: metadata:
name: secretGenerator name: secretGenerator
name: mySecret
behavior: merge behavior: merge
envFiles: envFiles:
- a.env - a.env
@@ -82,13 +82,13 @@ literals:
- FRUIT=apple - FRUIT=apple
- VEGETABLE=carrot - VEGETABLE=carrot
`) `)
th.writeF(filepath.Join(root, "a.env"), ` th.WriteF(filepath.Join(root, "a.env"), `
ROUTER_PASSWORD=admin ROUTER_PASSWORD=admin
`) `)
th.writeF(filepath.Join(root, "b.env"), ` th.WriteF(filepath.Join(root, "b.env"), `
DB_PASSWORD=iloveyou DB_PASSWORD=iloveyou
`) `)
th.writeF(filepath.Join(root, "longsecret.txt"), ` th.WriteF(filepath.Join(root, "longsecret.txt"), `
Lorem ipsum dolor sit amet, Lorem ipsum dolor sit amet,
consectetur adipiscing elit, consectetur adipiscing elit,
sed do eiusmod tempor incididunt sed do eiusmod tempor incididunt
@@ -104,18 +104,18 @@ func TestSecretGenerator(t *testing.T) {
tc.BuildGoPlugin( tc.BuildGoPlugin(
"builtin", "", "SecretGenerator") "builtin", "", "SecretGenerator")
th := NewKustTestHarnessWithPluginConfig( th := kusttest_test.NewKustTestHarnessWithPluginConfig(
t, "/app", plugin.ActivePluginConfig()) t, "/app", plugin.ActivePluginConfig())
th.writeK("/app", ` th.WriteK("/app", `
generators: generators:
- secretGenerator.yaml - secretGenerator.yaml
`) `)
writeSecretGeneratorConfig(th, "/app") writeSecretGeneratorConfig(th, "/app")
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
DB_PASSWORD: aWxvdmV5b3U= DB_PASSWORD: aWxvdmV5b3U=
@@ -137,24 +137,24 @@ func TestConfigMapGenerator(t *testing.T) {
tc.BuildExecPlugin( tc.BuildExecPlugin(
"someteam.example.com", "v1", "ConfigMapGenerator") "someteam.example.com", "v1", "ConfigMapGenerator")
th := NewKustTestHarnessWithPluginConfig( th := kusttest_test.NewKustTestHarnessWithPluginConfig(
t, "/app", plugin.ActivePluginConfig()) t, "/app", plugin.ActivePluginConfig())
th.writeK("/app", ` th.WriteK("/app", `
generators: generators:
- configmapGenerator.yaml - configmapGenerator.yaml
`) `)
th.writeF("/app/configmapGenerator.yaml", ` th.WriteF("/app/configmapGenerator.yaml", `
apiVersion: someteam.example.com/v1 apiVersion: someteam.example.com/v1
kind: ConfigMapGenerator kind: ConfigMapGenerator
metadata: metadata:
name: some-random-name name: some-random-name
argsOneLiner: "admin secret" argsOneLiner: "admin secret"
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
password: secret password: secret

View File

@@ -26,6 +26,7 @@ import (
"sigs.k8s.io/kustomize/internal/loadertest" "sigs.k8s.io/kustomize/internal/loadertest"
"sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/ifc" "sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/kusttest"
"sigs.k8s.io/kustomize/pkg/resid" "sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource" "sigs.k8s.io/kustomize/pkg/resource"
@@ -86,16 +87,16 @@ metadata:
) )
func TestResources(t *testing.T) { func TestResources(t *testing.T) {
th := NewKustTestHarness(t, "/whatever") th := kusttest_test.NewKustTestHarness(t, "/whatever")
th.writeK("/whatever/", kustomizationContent) th.WriteK("/whatever/", kustomizationContent)
th.writeF("/whatever/deployment.yaml", deploymentContent) th.WriteF("/whatever/deployment.yaml", deploymentContent)
th.writeF("/whatever/namespace.yaml", namespaceContent) th.WriteF("/whatever/namespace.yaml", namespaceContent)
th.writeF("/whatever/jsonpatch.json", jsonpatchContent) th.WriteF("/whatever/jsonpatch.json", jsonpatchContent)
expected := resmap.ResMap{ expected := resmap.ResMap{
resid.NewResIdWithPrefixSuffixNamespace( resid.NewResIdWithPrefixSuffixNamespace(
gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"}, gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"},
"dply1", "foo-", "-bar", "ns1"): th.fromMap( "dply1", "foo-", "-bar", "ns1"): th.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "apps/v1", "apiVersion": "apps/v1",
"kind": "Deployment", "kind": "Deployment",
@@ -130,7 +131,7 @@ func TestResources(t *testing.T) {
}), }),
resid.NewResIdWithPrefixSuffixNamespace( resid.NewResIdWithPrefixSuffixNamespace(
gvk.Gvk{Version: "v1", Kind: "ConfigMap"}, gvk.Gvk{Version: "v1", Kind: "ConfigMap"},
"literalConfigMap", "foo-", "-bar", "ns1"): th.fromMapAndOption( "literalConfigMap", "foo-", "-bar", "ns1"): th.FromMapAndOption(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "ConfigMap", "kind": "ConfigMap",
@@ -153,7 +154,7 @@ func TestResources(t *testing.T) {
&types.GeneratorOptions{}), &types.GeneratorOptions{}),
resid.NewResIdWithPrefixSuffixNamespace( resid.NewResIdWithPrefixSuffixNamespace(
gvk.Gvk{Version: "v1", Kind: "Secret"}, gvk.Gvk{Version: "v1", Kind: "Secret"},
"secret", "foo-", "-bar", "ns1"): th.fromMapAndOption( "secret", "foo-", "-bar", "ns1"): th.FromMapAndOption(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "Secret", "kind": "Secret",
@@ -177,7 +178,7 @@ func TestResources(t *testing.T) {
&types.GeneratorOptions{}), &types.GeneratorOptions{}),
resid.NewResIdWithPrefixSuffixNamespace( resid.NewResIdWithPrefixSuffixNamespace(
gvk.Gvk{Version: "v1", Kind: "Namespace"}, gvk.Gvk{Version: "v1", Kind: "Namespace"},
"ns1", "foo-", "-bar", ""): th.fromMap( "ns1", "foo-", "-bar", ""): th.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "Namespace", "kind": "Namespace",
@@ -192,7 +193,7 @@ func TestResources(t *testing.T) {
}, },
}), }),
} }
actual, err := th.makeKustTarget().MakeCustomizedResMap() actual, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("unexpected Resources error %v", err) t.Fatalf("unexpected Resources error %v", err)
} }
@@ -216,9 +217,9 @@ func TestKustomizationNotFound(t *testing.T) {
} }
func TestResourceNotFound(t *testing.T) { func TestResourceNotFound(t *testing.T) {
th := NewKustTestHarness(t, "/whatever") th := kusttest_test.NewKustTestHarness(t, "/whatever")
th.writeK("/whatever", kustomizationContent) th.WriteK("/whatever", kustomizationContent)
_, err := th.makeKustTarget().MakeCustomizedResMap() _, err := th.MakeKustTarget().MakeCustomizedResMap()
if err == nil { if err == nil {
t.Fatalf("Didn't get the expected error for an unknown resource") 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) { func TestDisableNameSuffixHash(t *testing.T) {
th := NewKustTestHarness(t, "/whatever") th := kusttest_test.NewKustTestHarness(t, "/whatever")
th.writeK("/whatever/", kustomizationContent) th.WriteK("/whatever/", kustomizationContent)
th.writeF("/whatever/deployment.yaml", deploymentContent) th.WriteF("/whatever/deployment.yaml", deploymentContent)
th.writeF("/whatever/namespace.yaml", namespaceContent) th.WriteF("/whatever/namespace.yaml", namespaceContent)
th.writeF("/whatever/jsonpatch.json", jsonpatchContent) th.WriteF("/whatever/jsonpatch.json", jsonpatchContent)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("unexpected Resources error %v", err) 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()) t.Errorf("unexpected secret resource name: %s", secret.GetName())
} }
th.writeK("/whatever/", th.WriteK("/whatever/",
strings.Replace(kustomizationContent, strings.Replace(kustomizationContent,
"disableNameSuffixHash: false", "disableNameSuffixHash: false",
"disableNameSuffixHash: true", -1)) "disableNameSuffixHash: true", -1))
m, err = th.makeKustTarget().MakeCustomizedResMap() m, err = th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("unexpected Resources error %v", err) t.Fatalf("unexpected Resources error %v", err)
} }
@@ -273,25 +274,25 @@ func TestDisableNameSuffixHash(t *testing.T) {
} }
func TestIssue596AllowDirectoriesThatAreSubstringsOfEachOther(t *testing.T) { func TestIssue596AllowDirectoriesThatAreSubstringsOfEachOther(t *testing.T) {
th := NewKustTestHarness(t, "/app/overlays/aws-sandbox2.us-east-1") th := kusttest_test.NewKustTestHarness(t, "/app/overlays/aws-sandbox2.us-east-1")
th.writeK("/app/base", "") th.WriteK("/app/base", "")
th.writeK("/app/overlays/aws", ` th.WriteK("/app/overlays/aws", `
bases: bases:
- ../../base - ../../base
`) `)
th.writeK("/app/overlays/aws-nonprod", ` th.WriteK("/app/overlays/aws-nonprod", `
bases: bases:
- ../aws - ../aws
`) `)
th.writeK("/app/overlays/aws-sandbox2.us-east-1", ` th.WriteK("/app/overlays/aws-sandbox2.us-east-1", `
bases: bases:
- ../aws-nonprod - ../aws-nonprod
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, "") th.AssertActualEqualsExpected(m, "")
} }
// To simplify tests, these vars specified in alphabetical order. // To simplify tests, these vars specified in alphabetical order.
@@ -329,8 +330,8 @@ var someVars = []types.Var{
} }
func TestGetAllVarsSimple(t *testing.T) { func TestGetAllVarsSimple(t *testing.T) {
th := NewKustTestHarness(t, "/app") th := kusttest_test.NewKustTestHarness(t, "/app")
th.writeK("/app", ` th.WriteK("/app", `
vars: vars:
- name: AWARD - name: AWARD
objref: objref:
@@ -345,7 +346,7 @@ vars:
name: heron name: heron
apiVersion: v300 apiVersion: v300
`) `)
ra, err := th.makeKustTarget().AccumulateTarget() ra, err := th.MakeKustTarget().AccumulateTarget()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
@@ -361,8 +362,8 @@ vars:
} }
func TestGetAllVarsNested(t *testing.T) { func TestGetAllVarsNested(t *testing.T) {
th := NewKustTestHarness(t, "/app/overlays/o2") th := kusttest_test.NewKustTestHarness(t, "/app/overlays/o2")
th.writeK("/app/base", ` th.WriteK("/app/base", `
vars: vars:
- name: AWARD - name: AWARD
objref: objref:
@@ -377,7 +378,7 @@ vars:
name: heron name: heron
apiVersion: v300 apiVersion: v300
`) `)
th.writeK("/app/overlays/o1", ` th.WriteK("/app/overlays/o1", `
vars: vars:
- name: FRUIT - name: FRUIT
objref: objref:
@@ -386,7 +387,7 @@ vars:
bases: bases:
- ../../base - ../../base
`) `)
th.writeK("/app/overlays/o2", ` th.WriteK("/app/overlays/o2", `
vars: vars:
- name: VEGETABLE - name: VEGETABLE
objref: objref:
@@ -395,7 +396,7 @@ vars:
bases: bases:
- ../o1 - ../o1
`) `)
ra, err := th.makeKustTarget().AccumulateTarget() ra, err := th.MakeKustTarget().AccumulateTarget()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
@@ -414,8 +415,8 @@ bases:
} }
func TestVarCollisionsForbidden(t *testing.T) { func TestVarCollisionsForbidden(t *testing.T) {
th := NewKustTestHarness(t, "/app/overlays/o2") th := kusttest_test.NewKustTestHarness(t, "/app/overlays/o2")
th.writeK("/app/base", ` th.WriteK("/app/base", `
vars: vars:
- name: AWARD - name: AWARD
objref: objref:
@@ -430,7 +431,7 @@ vars:
name: heron name: heron
apiVersion: v300 apiVersion: v300
`) `)
th.writeK("/app/overlays/o1", ` th.WriteK("/app/overlays/o1", `
vars: vars:
- name: AWARD - name: AWARD
objref: objref:
@@ -439,7 +440,7 @@ vars:
bases: bases:
- ../../base - ../../base
`) `)
th.writeK("/app/overlays/o2", ` th.WriteK("/app/overlays/o2", `
vars: vars:
- name: VEGETABLE - name: VEGETABLE
objref: objref:
@@ -448,7 +449,7 @@ vars:
bases: bases:
- ../o1 - ../o1
`) `)
_, err := th.makeKustTarget().AccumulateTarget() _, err := th.MakeKustTarget().AccumulateTarget()
if err == nil { if err == nil {
t.Fatalf("expected var collision") t.Fatalf("expected var collision")
} }

View File

@@ -19,10 +19,12 @@ package target_test
import ( import (
"strings" "strings"
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
func makeCommonFileForMultiplePatchTest(th *KustTestHarness) { func makeCommonFileForMultiplePatchTest(th *kusttest_test.KustTestHarness) {
th.writeK("/app/base", ` th.WriteK("/app/base", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
namePrefix: team-foo- namePrefix: team-foo-
@@ -40,7 +42,7 @@ configMapGenerator:
literals: literals:
- foo=bar - foo=bar
`) `)
th.writeF("/app/base/deployment.yaml", ` th.WriteF("/app/base/deployment.yaml", `
apiVersion: apps/v1beta2 apiVersion: apps/v1beta2
kind: Deployment kind: Deployment
metadata: metadata:
@@ -66,7 +68,7 @@ spec:
name: configmap-in-base name: configmap-in-base
name: configmap-in-base name: configmap-in-base
`) `)
th.writeF("/app/base/service.yaml", ` th.WriteF("/app/base/service.yaml", `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@@ -79,7 +81,7 @@ spec:
selector: selector:
app: nginx app: nginx
`) `)
th.writeK("/app/overlay/staging", ` th.WriteK("/app/overlay/staging", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
namePrefix: staging- namePrefix: staging-
@@ -98,9 +100,9 @@ configMapGenerator:
} }
func TestMultiplePatchesNoConflict(t *testing.T) { func TestMultiplePatchesNoConflict(t *testing.T) {
th := NewKustTestHarness(t, "/app/overlay/staging") th := kusttest_test.NewKustTestHarness(t, "/app/overlay/staging")
makeCommonFileForMultiplePatchTest(th) makeCommonFileForMultiplePatchTest(th)
th.writeF("/app/overlay/staging/deployment-patch1.yaml", ` th.WriteF("/app/overlay/staging/deployment-patch1.yaml", `
apiVersion: apps/v1beta2 apiVersion: apps/v1beta2
kind: Deployment kind: Deployment
metadata: metadata:
@@ -123,7 +125,7 @@ spec:
name: configmap-in-overlay name: configmap-in-overlay
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 apiVersion: apps/v1beta2
kind: Deployment kind: Deployment
metadata: metadata:
@@ -141,11 +143,11 @@ spec:
volumes: volumes:
- name: nginx-persistent-storage - name: nginx-persistent-storage
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
foo: bar foo: bar
@@ -244,9 +246,9 @@ spec:
} }
func TestMultiplePatchesWithConflict(t *testing.T) { func TestMultiplePatchesWithConflict(t *testing.T) {
th := NewKustTestHarness(t, "/app/overlay/staging") th := kusttest_test.NewKustTestHarness(t, "/app/overlay/staging")
makeCommonFileForMultiplePatchTest(th) makeCommonFileForMultiplePatchTest(th)
th.writeF("/app/overlay/staging/deployment-patch1.yaml", ` th.WriteF("/app/overlay/staging/deployment-patch1.yaml", `
apiVersion: apps/v1beta2 apiVersion: apps/v1beta2
kind: Deployment kind: Deployment
metadata: metadata:
@@ -268,7 +270,7 @@ spec:
name: configmap-in-overlay name: configmap-in-overlay
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 apiVersion: apps/v1beta2
kind: Deployment kind: Deployment
metadata: metadata:
@@ -282,7 +284,7 @@ spec:
- name: ENABLE_FEATURE_FOO - name: ENABLE_FEATURE_FOO
value: FALSE value: FALSE
`) `)
_, err := th.makeKustTarget().MakeCustomizedResMap() _, err := th.MakeKustTarget().MakeCustomizedResMap()
if err == nil { if err == nil {
t.Fatalf("expected conflict") t.Fatalf("expected conflict")
} }

View File

@@ -18,11 +18,13 @@ package target_test
import ( import (
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
func TestNamespacedGenerator(t *testing.T) { func TestNamespacedGenerator(t *testing.T) {
th := NewKustTestHarness(t, "/app") th := kusttest_test.NewKustTestHarness(t, "/app")
th.writeK("/app", ` th.WriteK("/app", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
configMapGenerator: configMapGenerator:
@@ -45,11 +47,11 @@ secretGenerator:
literals: literals:
- password.txt=anotherSecret - password.txt=anotherSecret
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
altGreeting: Good Morning from non-default namespace! altGreeting: Good Morning from non-default namespace!

View File

@@ -18,11 +18,13 @@ package target_test
import ( import (
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
func TestNullValues(t *testing.T) { func TestNullValues(t *testing.T) {
th := NewKustTestHarness(t, "/app") th := kusttest_test.NewKustTestHarness(t, "/app")
th.writeF("/app/deployment.yaml", ` th.WriteF("/app/deployment.yaml", `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
@@ -43,18 +45,18 @@ spec:
image: image image: image
name: example name: example
`) `)
th.writeF("/app/kustomization.yaml", ` th.WriteF("/app/kustomization.yaml", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
resources: resources:
- deployment.yaml - deployment.yaml
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:

View File

@@ -24,6 +24,7 @@ import (
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
"sigs.k8s.io/kustomize/k8sdeps/transformer" "sigs.k8s.io/kustomize/k8sdeps/transformer"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/kusttest"
"sigs.k8s.io/kustomize/pkg/loader" "sigs.k8s.io/kustomize/pkg/loader"
"sigs.k8s.io/kustomize/pkg/plugins" "sigs.k8s.io/kustomize/pkg/plugins"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
@@ -86,8 +87,8 @@ metadata:
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th := NewKustTestHarness(t, ".") th := kusttest_test.NewKustTestHarness(t, ".")
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: WorkDir kind: WorkDir
metadata: metadata:

View File

@@ -18,11 +18,13 @@ package target_test
import ( import (
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
func TestPruneConfigMap(t *testing.T) { func TestPruneConfigMap(t *testing.T) {
th := NewKustTestHarness(t, "/app/base") th := kusttest_test.NewKustTestHarness(t, "/app/base")
th.writeK("/app/base", ` th.WriteK("/app/base", `
resources: resources:
- deployment.yaml - deployment.yaml
- service.yaml - service.yaml
@@ -37,7 +39,7 @@ inventory:
namePrefix: my- namePrefix: my-
namespace: default namespace: default
`) `)
th.writeF("/app/base/deployment.yaml", ` th.WriteF("/app/base/deployment.yaml", `
apiVersion: apps/v1beta2 apiVersion: apps/v1beta2
kind: Deployment kind: Deployment
metadata: metadata:
@@ -74,7 +76,7 @@ spec:
- name: mysql-persistent-storage - name: mysql-persistent-storage
emptyDir: {} emptyDir: {}
`) `)
th.writeF("/app/base/service.yaml", ` th.WriteF("/app/base/service.yaml", `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@@ -87,7 +89,7 @@ spec:
selector: selector:
app: mysql app: mysql
`) `)
th.writeF("/app/base/secret.yaml", ` th.WriteF("/app/base/secret.yaml", `
apiVersion: v1 apiVersion: v1
kind: Secret kind: Secret
metadata: metadata:
@@ -99,12 +101,12 @@ data:
username: jingfang username: jingfang
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
//nolint //nolint
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: ConfigMap kind: ConfigMap
metadata: metadata:

View File

@@ -19,18 +19,20 @@ package target_test
import ( import (
"strings" "strings"
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
func writeCombinedOverlays(th *KustTestHarness) { func writeCombinedOverlays(th *kusttest_test.KustTestHarness) {
// Base // Base
th.writeK("/app/base", ` th.WriteK("/app/base", `
resources: resources:
- serviceaccount.yaml - serviceaccount.yaml
- rolebinding.yaml - rolebinding.yaml
namePrefix: base- namePrefix: base-
nameSuffix: -suffix nameSuffix: -suffix
`) `)
th.writeF("/app/base/rolebinding.yaml", ` th.WriteF("/app/base/rolebinding.yaml", `
apiVersion: rbac.authorization.k8s.io/v1beta1 apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding kind: RoleBinding
metadata: metadata:
@@ -43,7 +45,7 @@ subjects:
- kind: ServiceAccount - kind: ServiceAccount
name: serviceaccount name: serviceaccount
`) `)
th.writeF("/app/base/serviceaccount.yaml", ` th.WriteF("/app/base/serviceaccount.yaml", `
apiVersion: v1 apiVersion: v1
kind: ServiceAccount kind: ServiceAccount
metadata: metadata:
@@ -51,13 +53,13 @@ metadata:
`) `)
// Mid-level overlays // Mid-level overlays
th.writeK("/app/overlays/a", ` th.WriteK("/app/overlays/a", `
bases: bases:
- ../../base - ../../base
namePrefix: a- namePrefix: a-
nameSuffix: -suffixA nameSuffix: -suffixA
`) `)
th.writeK("/app/overlays/b", ` th.WriteK("/app/overlays/b", `
bases: bases:
- ../../base - ../../base
namePrefix: b- namePrefix: b-
@@ -65,7 +67,7 @@ nameSuffix: -suffixB
`) `)
// Top overlay, combining the mid-level overlays // Top overlay, combining the mid-level overlays
th.writeK("/app/combined", ` th.WriteK("/app/combined", `
bases: bases:
- ../overlays/a - ../overlays/a
- ../overlays/b - ../overlays/b
@@ -73,13 +75,13 @@ bases:
} }
func TestMultibasesNoConflict(t *testing.T) { func TestMultibasesNoConflict(t *testing.T) {
th := NewKustTestHarness(t, "/app/combined") th := kusttest_test.NewKustTestHarness(t, "/app/combined")
writeCombinedOverlays(th) writeCombinedOverlays(th)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Unexpected err: %v", err) t.Fatalf("Unexpected err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: ServiceAccount kind: ServiceAccount
metadata: metadata:
@@ -117,10 +119,10 @@ subjects:
} }
func TestMultibasesWithConflict(t *testing.T) { func TestMultibasesWithConflict(t *testing.T) {
th := NewKustTestHarness(t, "/app/combined") th := kusttest_test.NewKustTestHarness(t, "/app/combined")
writeCombinedOverlays(th) writeCombinedOverlays(th)
th.writeK("/app/overlays/a", ` th.WriteK("/app/overlays/a", `
bases: bases:
- ../../base - ../../base
namePrefix: a- namePrefix: a-
@@ -130,14 +132,14 @@ resources:
`) `)
// Expect an error because this resource in the overlay // Expect an error because this resource in the overlay
// matches a resource in the base. // matches a resource in the base.
th.writeF("/app/overlays/a/serviceaccount.yaml", ` th.WriteF("/app/overlays/a/serviceaccount.yaml", `
apiVersion: v1 apiVersion: v1
kind: ServiceAccount kind: ServiceAccount
metadata: metadata:
name: serviceaccount name: serviceaccount
`) `)
_, err := th.makeKustTarget().MakeCustomizedResMap() _, err := th.MakeKustTarget().MakeCustomizedResMap()
if err == nil { if err == nil {
t.Fatalf("Expected resource conflict.") t.Fatalf("Expected resource conflict.")
} }

View File

@@ -18,10 +18,11 @@ import (
"sigs.k8s.io/kustomize/internal/plugintest" "sigs.k8s.io/kustomize/internal/plugintest"
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
func writeDeployment(th *KustTestHarness, path string) { func writeDeployment(th *kusttest_test.KustTestHarness, path string) {
th.writeF(path, ` th.WriteF(path, `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
@@ -38,8 +39,8 @@ spec:
`) `)
} }
func writeStringPrefixer(th *KustTestHarness, path string) { func writeStringPrefixer(th *kusttest_test.KustTestHarness, path string) {
th.writeF(path, ` th.WriteF(path, `
apiVersion: someteam.example.com/v1 apiVersion: someteam.example.com/v1
kind: StringPrefixer kind: StringPrefixer
metadata: metadata:
@@ -47,8 +48,8 @@ metadata:
`) `)
} }
func writeDatePrefixer(th *KustTestHarness, path string) { func writeDatePrefixer(th *kusttest_test.KustTestHarness, path string) {
th.writeF(path, ` th.WriteF(path, `
apiVersion: someteam.example.com/v1 apiVersion: someteam.example.com/v1
kind: DatePrefixer kind: DatePrefixer
metadata: metadata:
@@ -66,9 +67,9 @@ func TestOrderedTransformers(t *testing.T) {
tc.BuildGoPlugin( tc.BuildGoPlugin(
"someteam.example.com", "v1", "DatePrefixer") "someteam.example.com", "v1", "DatePrefixer")
th := NewKustTestHarnessWithPluginConfig( th := kusttest_test.NewKustTestHarnessWithPluginConfig(
t, "/app", plugin.ActivePluginConfig()) t, "/app", plugin.ActivePluginConfig())
th.writeK("/app", ` th.WriteK("/app", `
resources: resources:
- deployment.yaml - deployment.yaml
transformers: transformers:
@@ -81,11 +82,11 @@ transformers:
writeDeployment(th, "/app/deployment.yaml") writeDeployment(th, "/app/deployment.yaml")
writeStringPrefixer(th, "/app/stringPrefixer.yaml") writeStringPrefixer(th, "/app/stringPrefixer.yaml")
writeDatePrefixer(th, "/app/datePrefixer.yaml") writeDatePrefixer(th, "/app/datePrefixer.yaml")
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
@@ -109,9 +110,9 @@ func TestSedTransformer(t *testing.T) {
tc.BuildExecPlugin( tc.BuildExecPlugin(
"someteam.example.com", "v1", "SedTransformer") "someteam.example.com", "v1", "SedTransformer")
th := NewKustTestHarnessWithPluginConfig( th := kusttest_test.NewKustTestHarnessWithPluginConfig(
t, "/app", plugin.ActivePluginConfig()) t, "/app", plugin.ActivePluginConfig())
th.writeK("/app", ` th.WriteK("/app", `
transformers: transformers:
- sed-transformer.yaml - sed-transformer.yaml
@@ -121,23 +122,23 @@ configMapGenerator:
- FOO=$FOO - FOO=$FOO
- BAR=$BAR - BAR=$BAR
`) `)
th.writeF("/app/sed-transformer.yaml", ` th.WriteF("/app/sed-transformer.yaml", `
apiVersion: someteam.example.com/v1 apiVersion: someteam.example.com/v1
kind: SedTransformer kind: SedTransformer
metadata: metadata:
name: some-random-name name: some-random-name
argsFromFile: sed-input.txt argsFromFile: sed-input.txt
`) `)
th.writeF("/app/sed-input.txt", ` th.WriteF("/app/sed-input.txt", `
s/$FOO/foo/g s/$FOO/foo/g
s/$BAR/bar/g s/$BAR/bar/g
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
BAR: bar BAR: bar
@@ -158,10 +159,10 @@ func TestTransformedTransformers(t *testing.T) {
tc.BuildGoPlugin( tc.BuildGoPlugin(
"someteam.example.com", "v1", "DatePrefixer") "someteam.example.com", "v1", "DatePrefixer")
th := NewKustTestHarnessWithPluginConfig( th := kusttest_test.NewKustTestHarnessWithPluginConfig(
t, "/app/overlay", plugin.ActivePluginConfig()) t, "/app/overlay", plugin.ActivePluginConfig())
th.writeK("/app/base", ` th.WriteK("/app/base", `
resources: resources:
- stringPrefixer.yaml - stringPrefixer.yaml
transformers: transformers:
@@ -170,7 +171,7 @@ transformers:
writeStringPrefixer(th, "/app/base/stringPrefixer.yaml") writeStringPrefixer(th, "/app/base/stringPrefixer.yaml")
writeDatePrefixer(th, "/app/base/datePrefixer.yaml") writeDatePrefixer(th, "/app/base/datePrefixer.yaml")
th.writeK("/app/overlay", ` th.WriteK("/app/overlay", `
resources: resources:
- deployment.yaml - deployment.yaml
transformers: transformers:
@@ -178,11 +179,11 @@ transformers:
`) `)
writeDeployment(th, "/app/overlay/deployment.yaml") writeDeployment(th, "/app/overlay/deployment.yaml")
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:

View File

@@ -18,10 +18,12 @@ package target_test
import ( import (
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
func makeTransfomersImageBase(th *KustTestHarness) { func makeTransfomersImageBase(th *kusttest_test.KustTestHarness) {
th.writeK("/app/base", ` th.WriteK("/app/base", `
resources: resources:
- deploy1.yaml - deploy1.yaml
- random.yaml - random.yaml
@@ -45,7 +47,7 @@ images:
newName: my-docker newName: my-docker
digest: sha256:25a0d4b4 digest: sha256:25a0d4b4
`) `)
th.writeF("/app/base/deploy1.yaml", ` th.WriteF("/app/base/deploy1.yaml", `
group: apps group: apps
apiVersion: v1 apiVersion: v1
kind: Deployment kind: Deployment
@@ -67,7 +69,7 @@ spec:
- name: postgresdb - name: postgresdb
image: postgres:1.8.0 image: postgres:1.8.0
`) `)
th.writeF("/app/base/random.yaml", ` th.WriteF("/app/base/random.yaml", `
kind: randomKind kind: randomKind
metadata: metadata:
name: random name: random
@@ -105,13 +107,13 @@ spec3:
} }
func TestTransfomersImageDefaultConfig(t *testing.T) { func TestTransfomersImageDefaultConfig(t *testing.T) {
th := NewKustTestHarness(t, "/app/base") th := kusttest_test.NewKustTestHarness(t, "/app/base")
makeTransfomersImageBase(th) makeTransfomersImageBase(th)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
group: apps group: apps
kind: Deployment kind: Deployment
@@ -169,8 +171,8 @@ spec3:
`) `)
} }
func makeTransfomersImageCustomBase(th *KustTestHarness) { func makeTransfomersImageCustomBase(th *kusttest_test.KustTestHarness) {
th.writeK("/app/base", ` th.WriteK("/app/base", `
resources: resources:
- custom.yaml - custom.yaml
configurations: configurations:
@@ -195,7 +197,7 @@ images:
newName: my-docker newName: my-docker
digest: sha256:25a0d4b4 digest: sha256:25a0d4b4
`) `)
th.writeF("/app/base/custom.yaml", ` th.WriteF("/app/base/custom.yaml", `
kind: customKind kind: customKind
metadata: metadata:
name: custom name: custom
@@ -230,7 +232,7 @@ spec3:
- name: my-cool-app - name: my-cool-app
image: gcr.io:8080/my-project/my-cool-app:latest 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: images:
- kind: Custom - kind: Custom
path: spec/template/spec/myContainers/image path: spec/template/spec/myContainers/image
@@ -241,13 +243,13 @@ images:
`) `)
} }
func TestTransfomersImageCustomConfig(t *testing.T) { func TestTransfomersImageCustomConfig(t *testing.T) {
th := NewKustTestHarness(t, "/app/base") th := kusttest_test.NewKustTestHarness(t, "/app/base")
makeTransfomersImageCustomBase(th) makeTransfomersImageCustomBase(th)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
kind: customKind kind: customKind
metadata: metadata:
name: custom name: custom
@@ -284,8 +286,8 @@ spec3:
`) `)
} }
func makeTransfomersImageKnativeBase(th *KustTestHarness) { func makeTransfomersImageKnativeBase(th *kusttest_test.KustTestHarness) {
th.writeK("/app/base", ` th.WriteK("/app/base", `
resources: resources:
- knative.yaml - knative.yaml
configurations: configurations:
@@ -294,7 +296,7 @@ images:
- name: solsa-echo - name: solsa-echo
newTag: foo newTag: foo
`) `)
th.writeF("/app/base/knative.yaml", ` th.WriteF("/app/base/knative.yaml", `
apiVersion: serving.knative.dev/v1alpha1 apiVersion: serving.knative.dev/v1alpha1
kind: Service kind: Service
metadata: metadata:
@@ -307,7 +309,7 @@ spec:
container: container:
image: solsa-echo image: solsa-echo
`) `)
th.writeF("/app/base/config/knative.yaml", ` th.WriteF("/app/base/config/knative.yaml", `
images: images:
- path: spec/runLatest/configuration/revisionTemplate/spec/container/image - path: spec/runLatest/configuration/revisionTemplate/spec/container/image
apiVersion: serving.knative.dev/v1alpha1 apiVersion: serving.knative.dev/v1alpha1
@@ -316,13 +318,13 @@ images:
} }
func TestTransfomersImageKnativeConfig(t *testing.T) { func TestTransfomersImageKnativeConfig(t *testing.T) {
th := NewKustTestHarness(t, "/app/base") th := kusttest_test.NewKustTestHarness(t, "/app/base")
makeTransfomersImageKnativeBase(th) makeTransfomersImageKnativeBase(th)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) 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
metadata: metadata:

View File

@@ -18,11 +18,13 @@ package target_test
import ( import (
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
) )
func TestVariableRef(t *testing.T) { func TestVariableRef(t *testing.T) {
th := NewKustTestHarness(t, "/app/overlay/staging") th := kusttest_test.NewKustTestHarness(t, "/app/overlay/staging")
th.writeK("/app/base", ` th.WriteK("/app/base", `
namePrefix: base- namePrefix: base-
resources: resources:
- cockroachdb-statefulset-secure.yaml - cockroachdb-statefulset-secure.yaml
@@ -62,7 +64,7 @@ vars:
apiVersion: v1 apiVersion: v1
fieldref: fieldref:
fieldpath: metadata.name`) fieldpath: metadata.name`)
th.writeF("/app/base/cronjob.yaml", ` th.WriteF("/app/base/cronjob.yaml", `
apiVersion: batch/v1beta1 apiVersion: batch/v1beta1
kind: CronJob kind: CronJob
metadata: metadata:
@@ -85,7 +87,7 @@ spec:
- name: CDB_PUBLIC_SVC - name: CDB_PUBLIC_SVC
value: "$(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 apiVersion: v1
kind: ServiceAccount kind: ServiceAccount
metadata: metadata:
@@ -320,16 +322,16 @@ spec:
requests: requests:
storage: 1Gi storage: 1Gi
`) `)
th.writeK("/app/overlay/staging", ` th.WriteK("/app/overlay/staging", `
namePrefix: dev- namePrefix: dev-
bases: bases:
- ../../base - ../../base
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: ServiceAccount kind: ServiceAccount
metadata: metadata:
@@ -577,8 +579,8 @@ spec:
} }
func TestVariableRefIngress(t *testing.T) { func TestVariableRefIngress(t *testing.T) {
th := NewKustTestHarness(t, "/app/overlay") th := kusttest_test.NewKustTestHarness(t, "/app/overlay")
th.writeK("/app/base", ` th.WriteK("/app/base", `
resources: resources:
- deployment.yaml - deployment.yaml
- ingress.yaml - ingress.yaml
@@ -593,7 +595,7 @@ vars:
fieldref: fieldref:
fieldpath: metadata.name fieldpath: metadata.name
`) `)
th.writeF("/app/base/deployment.yaml", ` th.WriteF("/app/base/deployment.yaml", `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
@@ -616,7 +618,7 @@ spec:
- name: http - name: http
containerPort: 80 containerPort: 80
`) `)
th.writeF("/app/base/ingress.yaml", ` th.WriteF("/app/base/ingress.yaml", `
apiVersion: extensions/v1beta1 apiVersion: extensions/v1beta1
kind: Ingress kind: Ingress
metadata: metadata:
@@ -637,7 +639,7 @@ spec:
- $(DEPLOYMENT_NAME).example.com - $(DEPLOYMENT_NAME).example.com
secretName: $(DEPLOYMENT_NAME).example.com-tls secretName: $(DEPLOYMENT_NAME).example.com-tls
`) `)
th.writeF("/app/base/service.yaml", ` th.WriteF("/app/base/service.yaml", `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@@ -651,16 +653,16 @@ spec:
protocol: TCP protocol: TCP
targetPort: http targetPort: http
`) `)
th.writeK("/app/overlay", ` th.WriteK("/app/overlay", `
nameprefix: kustomized- nameprefix: kustomized-
bases: bases:
- ../base - ../base
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@@ -719,8 +721,8 @@ spec:
} }
func TestVariableRefMounthPath(t *testing.T) { func TestVariableRefMounthPath(t *testing.T) {
th := NewKustTestHarness(t, "/app/base") th := kusttest_test.NewKustTestHarness(t, "/app/base")
th.writeK("/app/base", ` th.WriteK("/app/base", `
resources: resources:
- deployment.yaml - deployment.yaml
- namespace.yaml - namespace.yaml
@@ -733,7 +735,7 @@ vars:
name: my-namespace name: my-namespace
`) `)
th.writeF("/app/base/deployment.yaml", ` th.WriteF("/app/base/deployment.yaml", `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
@@ -754,18 +756,18 @@ vars:
- name: my-volume - name: my-volume
emptyDir: {} emptyDir: {}
`) `)
th.writeF("/app/base/namespace.yaml", ` th.WriteF("/app/base/namespace.yaml", `
apiVersion: v1 apiVersion: v1
kind: Namespace kind: Namespace
metadata: metadata:
name: my-namespace name: my-namespace
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: Namespace kind: Namespace
metadata: metadata:
@@ -794,8 +796,8 @@ spec:
} }
func TestVariableRefMaps(t *testing.T) { func TestVariableRefMaps(t *testing.T) {
th := NewKustTestHarness(t, "/app/base") th := kusttest_test.NewKustTestHarness(t, "/app/base")
th.writeK("/app/base", ` th.WriteK("/app/base", `
resources: resources:
- deployment.yaml - deployment.yaml
- namespace.yaml - namespace.yaml
@@ -806,7 +808,7 @@ vars:
kind: Namespace kind: Namespace
name: my-namespace name: my-namespace
`) `)
th.writeF("/app/base/deployment.yaml", ` th.WriteF("/app/base/deployment.yaml", `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
@@ -820,18 +822,18 @@ vars:
- name: app - name: app
image: busybox image: busybox
`) `)
th.writeF("/app/base/namespace.yaml", ` th.WriteF("/app/base/namespace.yaml", `
apiVersion: v1 apiVersion: v1
kind: Namespace kind: Namespace
metadata: metadata:
name: my-namespace name: my-namespace
`) `)
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: Namespace kind: Namespace
metadata: metadata: