mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-10 08:20:59 +00:00
Merge pull request #655 from monopole/makeTestHarness
Make KustTarget test harness to reduce boilerplate.
This commit is contained in:
@@ -18,8 +18,6 @@ package target
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
|
||||
)
|
||||
|
||||
// TODO(monopole): Add a feature test example covering secret generation.
|
||||
@@ -37,8 +35,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(t *testing.T, ldr loadertest.FakeLoader) {
|
||||
writeK(t, ldr, "/app/base", `
|
||||
func writeMediumBase(th *KustTestHarness) {
|
||||
th.writeK("/app/base", `
|
||||
namePrefix: baseprefix-
|
||||
commonLabels:
|
||||
foo: bar
|
||||
@@ -48,7 +46,7 @@ resources:
|
||||
- deployment/deployment.yaml
|
||||
- service/service.yaml
|
||||
`)
|
||||
writeF(t, ldr, "/app/base/service/service.yaml", `
|
||||
th.writeF("/app/base/service/service.yaml", `
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
@@ -61,7 +59,7 @@ spec:
|
||||
selector:
|
||||
app: mungebot
|
||||
`)
|
||||
writeF(t, ldr, "/app/base/deployment/deployment.yaml", `
|
||||
th.writeF("/app/base/deployment/deployment.yaml", `
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
@@ -87,20 +85,13 @@ spec:
|
||||
}
|
||||
|
||||
func TestMediumBase(t *testing.T) {
|
||||
ldr := loadertest.NewFakeLoader("/app/base")
|
||||
writeMediumBase(t, ldr)
|
||||
m, err := makeKustTarget(t, ldr).MakeCustomizedResMap()
|
||||
th := NewKustTestHarness(t, "/app/base")
|
||||
writeMediumBase(th)
|
||||
m, err := th.makeKustTarget().MakeCustomizedResMap()
|
||||
if err != nil {
|
||||
t.Fatalf("Err: %v", err)
|
||||
}
|
||||
if m == nil {
|
||||
t.Fatalf("Empty map.")
|
||||
}
|
||||
s, err := m.EncodeAsYaml()
|
||||
if err != nil {
|
||||
t.Fatalf("Err: %v", err)
|
||||
}
|
||||
assertExpectedEqualsActual(t, s, `apiVersion: v1
|
||||
th.assertActualEqualsExpected(m, `apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
annotations:
|
||||
@@ -150,9 +141,9 @@ spec:
|
||||
}
|
||||
|
||||
func TestMediumOverlay(t *testing.T) {
|
||||
ldr := loadertest.NewFakeLoader("/app/overlay")
|
||||
writeMediumBase(t, ldr)
|
||||
writeK(t, ldr, "/app/overlay", `
|
||||
th := NewKustTestHarness(t, "/app/overlay")
|
||||
writeMediumBase(th)
|
||||
th.writeK("/app/overlay", `
|
||||
namePrefix: test-infra-
|
||||
commonLabels:
|
||||
app: mungebot
|
||||
@@ -174,15 +165,15 @@ imageTags:
|
||||
- name: nginx
|
||||
newTag: 1.8.0`)
|
||||
|
||||
writeF(t, ldr, "/app/overlay/configmap/app.env", `
|
||||
th.writeF("/app/overlay/configmap/app.env", `
|
||||
DB_USERNAME=admin
|
||||
DB_PASSWORD=somepw
|
||||
`)
|
||||
writeF(t, ldr, "/app/overlay/configmap/app-init.ini", `
|
||||
th.writeF("/app/overlay/configmap/app-init.ini", `
|
||||
FOO=bar
|
||||
BAR=baz
|
||||
`)
|
||||
writeF(t, ldr, "/app/overlay/deployment/deployment.yaml", `
|
||||
th.writeF("/app/overlay/deployment/deployment.yaml", `
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
@@ -215,15 +206,11 @@ spec:
|
||||
name: app-env
|
||||
name: app-env
|
||||
`)
|
||||
m, err := makeKustTarget(t, ldr).MakeCustomizedResMap()
|
||||
m, err := th.makeKustTarget().MakeCustomizedResMap()
|
||||
if err != nil {
|
||||
t.Fatalf("Err: %v", err)
|
||||
}
|
||||
s, err := m.EncodeAsYaml()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected err: %v", err)
|
||||
}
|
||||
assertExpectedEqualsActual(t, s, `apiVersion: v1
|
||||
th.assertActualEqualsExpected(m, `apiVersion: v1
|
||||
data:
|
||||
app-init.ini: |2
|
||||
|
||||
|
||||
@@ -18,12 +18,10 @@ package target
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
|
||||
)
|
||||
|
||||
func writeSmallBase(t *testing.T, ldr loadertest.FakeLoader) {
|
||||
writeK(t, ldr, "/app/base", `
|
||||
func writeSmallBase(th *KustTestHarness) {
|
||||
th.writeK("/app/base", `
|
||||
namePrefix: a-
|
||||
commonLabels:
|
||||
app: myApp
|
||||
@@ -31,7 +29,7 @@ resources:
|
||||
- deployment.yaml
|
||||
- service.yaml
|
||||
`)
|
||||
writeF(t, ldr, "/app/base/service.yaml", `
|
||||
th.writeF("/app/base/service.yaml", `
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
@@ -42,7 +40,7 @@ spec:
|
||||
ports:
|
||||
- port: 7002
|
||||
`)
|
||||
writeF(t, ldr, "/app/base/deployment.yaml", `
|
||||
th.writeF("/app/base/deployment.yaml", `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
@@ -60,20 +58,13 @@ spec:
|
||||
}
|
||||
|
||||
func TestSmallBase(t *testing.T) {
|
||||
ldr := loadertest.NewFakeLoader("/app/base")
|
||||
writeSmallBase(t, ldr)
|
||||
m, err := makeKustTarget(t, ldr).MakeCustomizedResMap()
|
||||
th := NewKustTestHarness(t, "/app/base")
|
||||
writeSmallBase(th)
|
||||
m, err := th.makeKustTarget().MakeCustomizedResMap()
|
||||
if err != nil {
|
||||
t.Fatalf("Err: %v", err)
|
||||
}
|
||||
if m == nil {
|
||||
t.Fatalf("Empty map.")
|
||||
}
|
||||
s, err := m.EncodeAsYaml()
|
||||
if err != nil {
|
||||
t.Fatalf("Err: %v", err)
|
||||
}
|
||||
assertExpectedEqualsActual(t, s, `apiVersion: v1
|
||||
th.assertActualEqualsExpected(m, `apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
labels:
|
||||
@@ -109,9 +100,9 @@ spec:
|
||||
}
|
||||
|
||||
func TestSmallOverlay(t *testing.T) {
|
||||
ldr := loadertest.NewFakeLoader("/app/overlay")
|
||||
writeSmallBase(t, ldr)
|
||||
writeK(t, ldr, "/app/overlay", `
|
||||
th := NewKustTestHarness(t, "/app/overlay")
|
||||
writeSmallBase(th)
|
||||
th.writeK("/app/overlay", `
|
||||
namePrefix: b-
|
||||
commonLabels:
|
||||
env: prod
|
||||
@@ -123,15 +114,15 @@ imageTags:
|
||||
- name: whatever
|
||||
newTag: 1.8.0`)
|
||||
|
||||
writeF(t, ldr, "/app/overlay/configmap/app.env", `
|
||||
th.writeF("/app/overlay/configmap/app.env", `
|
||||
DB_USERNAME=admin
|
||||
DB_PASSWORD=somepw
|
||||
`)
|
||||
writeF(t, ldr, "/app/overlay/configmap/app-init.ini", `
|
||||
th.writeF("/app/overlay/configmap/app-init.ini", `
|
||||
FOO=bar
|
||||
BAR=baz
|
||||
`)
|
||||
writeF(t, ldr, "/app/overlay/deployment/deployment.yaml", `
|
||||
th.writeF("/app/overlay/deployment/deployment.yaml", `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
@@ -139,15 +130,11 @@ metadata:
|
||||
spec:
|
||||
replicas: 1000
|
||||
`)
|
||||
m, err := makeKustTarget(t, ldr).MakeCustomizedResMap()
|
||||
m, err := th.makeKustTarget().MakeCustomizedResMap()
|
||||
if err != nil {
|
||||
t.Fatalf("Err: %v", err)
|
||||
}
|
||||
s, err := m.EncodeAsYaml()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected err: %v", err)
|
||||
}
|
||||
assertExpectedEqualsActual(t, s, `apiVersion: v1
|
||||
th.assertActualEqualsExpected(m, `apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
labels:
|
||||
|
||||
@@ -23,10 +23,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
|
||||
"sigs.k8s.io/kustomize/pkg/gvk"
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
|
||||
"sigs.k8s.io/kustomize/pkg/resid"
|
||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||
"sigs.k8s.io/kustomize/pkg/resource"
|
||||
@@ -91,27 +89,17 @@ metadata:
|
||||
]`
|
||||
)
|
||||
|
||||
var rf = resmap.NewFactory(resource.NewFactory(
|
||||
kunstruct.NewKunstructuredFactoryImpl()))
|
||||
|
||||
var deploy = gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"}
|
||||
var cmap = gvk.Gvk{Version: "v1", Kind: "ConfigMap"}
|
||||
var secret = gvk.Gvk{Version: "v1", Kind: "Secret"}
|
||||
var ns = gvk.Gvk{Version: "v1", Kind: "Namespace"}
|
||||
|
||||
func makeALoader(t *testing.T) ifc.Loader {
|
||||
ldr := loadertest.NewFakeLoader("/testpath")
|
||||
writeK(t, ldr, "/testpath/", kustomizationContent1)
|
||||
writeF(t, ldr, "/testpath/deployment.yaml", deploymentContent)
|
||||
writeF(t, ldr, "/testpath/namespace.yaml", namespaceContent)
|
||||
writeF(t, ldr, "/testpath/jsonpatch.json", jsonpatchContent)
|
||||
return ldr
|
||||
}
|
||||
|
||||
func TestResources1(t *testing.T) {
|
||||
th := NewKustTestHarness(t, "/whatever")
|
||||
th.writeK("/whatever/", kustomizationContent1)
|
||||
th.writeF("/whatever/deployment.yaml", deploymentContent)
|
||||
th.writeF("/whatever/namespace.yaml", namespaceContent)
|
||||
th.writeF("/whatever/jsonpatch.json", jsonpatchContent)
|
||||
|
||||
expected := resmap.ResMap{
|
||||
resid.NewResIdWithPrefixSuffixNamespace(
|
||||
deploy, "dply1", "foo-", "-bar", "ns1"): rf.RF().FromMap(
|
||||
gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"},
|
||||
"dply1", "foo-", "-bar", "ns1"): th.fromMap(
|
||||
map[string]interface{}{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
@@ -145,7 +133,8 @@ func TestResources1(t *testing.T) {
|
||||
},
|
||||
}),
|
||||
resid.NewResIdWithPrefixSuffixNamespace(
|
||||
cmap, "literalConfigMap", "foo-", "-bar", "ns1"): rf.RF().FromMap(
|
||||
gvk.Gvk{Version: "v1", Kind: "ConfigMap"},
|
||||
"literalConfigMap", "foo-", "-bar", "ns1"): th.fromMap(
|
||||
map[string]interface{}{
|
||||
"apiVersion": "v1",
|
||||
"kind": "ConfigMap",
|
||||
@@ -165,7 +154,8 @@ func TestResources1(t *testing.T) {
|
||||
},
|
||||
}).SetBehavior(ifc.BehaviorCreate),
|
||||
resid.NewResIdWithPrefixSuffixNamespace(
|
||||
secret, "secret", "foo-", "-bar", "ns1"): rf.RF().FromMap(
|
||||
gvk.Gvk{Version: "v1", Kind: "Secret"},
|
||||
"secret", "foo-", "-bar", "ns1"): th.fromMap(
|
||||
map[string]interface{}{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Secret",
|
||||
@@ -186,7 +176,8 @@ func TestResources1(t *testing.T) {
|
||||
},
|
||||
}).SetBehavior(ifc.BehaviorCreate),
|
||||
resid.NewResIdWithPrefixSuffixNamespace(
|
||||
ns, "ns1", "foo-", "-bar", ""): rf.RF().FromMap(
|
||||
gvk.Gvk{Version: "v1", Kind: "Namespace"},
|
||||
"ns1", "foo-", "-bar", ""): th.fromMap(
|
||||
map[string]interface{}{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Namespace",
|
||||
@@ -201,8 +192,7 @@ func TestResources1(t *testing.T) {
|
||||
},
|
||||
}),
|
||||
}
|
||||
actual, err := makeKustTarget(
|
||||
t, makeALoader(t)).MakeCustomizedResMap()
|
||||
actual, err := th.makeKustTarget().MakeCustomizedResMap()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected Resources error %v", err)
|
||||
}
|
||||
@@ -214,9 +204,9 @@ func TestResources1(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestResourceNotFound(t *testing.T) {
|
||||
l := loadertest.NewFakeLoader("/testpath")
|
||||
writeK(t, l, "/testpath", kustomizationContent1)
|
||||
_, err := makeKustTarget(t, l).MakeCustomizedResMap()
|
||||
th := NewKustTestHarness(t, "/whatever")
|
||||
th.writeK("/whatever", kustomizationContent1)
|
||||
_, err := th.makeKustTarget().MakeCustomizedResMap()
|
||||
if err == nil {
|
||||
t.Fatalf("Didn't get the expected error for an unknown resource")
|
||||
}
|
||||
@@ -226,9 +216,9 @@ func TestResourceNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSecretTimeout(t *testing.T) {
|
||||
l := loadertest.NewFakeLoader("/testpath")
|
||||
writeK(t, l, "/testpath", kustomizationContent2)
|
||||
_, err := makeKustTarget(t, l).MakeCustomizedResMap()
|
||||
th := NewKustTestHarness(t, "/whatever")
|
||||
th.writeK("/whatever", kustomizationContent2)
|
||||
_, err := th.makeKustTarget().MakeCustomizedResMap()
|
||||
if err == nil {
|
||||
t.Fatalf("Didn't get the expected error for an unknown resource")
|
||||
}
|
||||
@@ -247,8 +237,13 @@ func findSecret(m resmap.ResMap) *resource.Resource {
|
||||
}
|
||||
|
||||
func TestDisableNameSuffixHash(t *testing.T) {
|
||||
kt := makeKustTarget(t, makeALoader(t))
|
||||
th := NewKustTestHarness(t, "/whatever")
|
||||
th.writeK("/whatever/", kustomizationContent1)
|
||||
th.writeF("/whatever/deployment.yaml", deploymentContent)
|
||||
th.writeF("/whatever/namespace.yaml", namespaceContent)
|
||||
th.writeF("/whatever/jsonpatch.json", jsonpatchContent)
|
||||
|
||||
kt := th.makeKustTarget()
|
||||
m, err := kt.MakeCustomizedResMap()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected Resources error %v", err)
|
||||
@@ -277,28 +272,25 @@ func TestDisableNameSuffixHash(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestIssue596AllowDirectoriesThatAreSubstringsOfEachOther(t *testing.T) {
|
||||
ldr := loadertest.NewFakeLoader(
|
||||
"/app/overlays/aws-sandbox2.us-east-1")
|
||||
writeK(t, ldr, "/app/base", "")
|
||||
writeK(t, ldr, "/app/overlays/aws", `
|
||||
th := NewKustTestHarness(t, "/app/overlays/aws-sandbox2.us-east-1")
|
||||
th.writeK("/app/base", "")
|
||||
th.writeK("/app/overlays/aws", `
|
||||
bases:
|
||||
- ../../base
|
||||
`)
|
||||
writeK(t, ldr, "/app/overlays/aws-nonprod", `
|
||||
th.writeK("/app/overlays/aws-nonprod", `
|
||||
bases:
|
||||
- ../aws
|
||||
`)
|
||||
writeK(t, ldr, "/app/overlays/aws-sandbox2.us-east-1", `
|
||||
th.writeK("/app/overlays/aws-sandbox2.us-east-1", `
|
||||
bases:
|
||||
- ../aws-nonprod
|
||||
`)
|
||||
m, err := makeKustTarget(t, ldr).MakeCustomizedResMap()
|
||||
m, err := th.makeKustTarget().MakeCustomizedResMap()
|
||||
if err != nil {
|
||||
t.Fatalf("Err: %v", err)
|
||||
}
|
||||
if m == nil {
|
||||
t.Fatalf("Empty map.")
|
||||
}
|
||||
th.assertActualEqualsExpected(m, "")
|
||||
}
|
||||
|
||||
// To simplify tests, these vars specified in alphabetical order.
|
||||
@@ -336,9 +328,8 @@ var someVars = []types.Var{
|
||||
}
|
||||
|
||||
func TestGetAllVarsSimple(t *testing.T) {
|
||||
ldr := loadertest.NewFakeLoader(
|
||||
"/app")
|
||||
writeK(t, ldr, "/app", `
|
||||
th := NewKustTestHarness(t, "/app")
|
||||
th.writeK("/app", `
|
||||
vars:
|
||||
- name: AWARD
|
||||
objref:
|
||||
@@ -353,7 +344,7 @@ vars:
|
||||
name: heron
|
||||
apiVersion: v300
|
||||
`)
|
||||
vars, err := makeKustTarget(t, ldr).getAllVars()
|
||||
vars, err := th.makeKustTarget().getAllVars()
|
||||
if err != nil {
|
||||
t.Fatalf("Err: %v", err)
|
||||
}
|
||||
@@ -376,9 +367,8 @@ func sortedKeys(m map[string]types.Var) (result []string) {
|
||||
}
|
||||
|
||||
func TestGetAllVarsNested(t *testing.T) {
|
||||
ldr := loadertest.NewFakeLoader(
|
||||
"/app/overlays/o2")
|
||||
writeK(t, ldr, "/app/base", `
|
||||
th := NewKustTestHarness(t, "/app/overlays/o2")
|
||||
th.writeK("/app/base", `
|
||||
vars:
|
||||
- name: AWARD
|
||||
objref:
|
||||
@@ -393,7 +383,7 @@ vars:
|
||||
name: heron
|
||||
apiVersion: v300
|
||||
`)
|
||||
writeK(t, ldr, "/app/overlays/o1", `
|
||||
th.writeK("/app/overlays/o1", `
|
||||
vars:
|
||||
- name: FRUIT
|
||||
objref:
|
||||
@@ -402,7 +392,7 @@ vars:
|
||||
bases:
|
||||
- ../../base
|
||||
`)
|
||||
writeK(t, ldr, "/app/overlays/o2", `
|
||||
th.writeK("/app/overlays/o2", `
|
||||
vars:
|
||||
- name: VEGETABLE
|
||||
objref:
|
||||
@@ -411,7 +401,7 @@ vars:
|
||||
bases:
|
||||
- ../o1
|
||||
`)
|
||||
vars, err := makeKustTarget(t, ldr).getAllVars()
|
||||
vars, err := th.makeKustTarget().getAllVars()
|
||||
if err != nil {
|
||||
t.Fatalf("Err: %v", err)
|
||||
}
|
||||
@@ -426,9 +416,8 @@ bases:
|
||||
}
|
||||
|
||||
func TestVarCollisionsForbidden(t *testing.T) {
|
||||
ldr := loadertest.NewFakeLoader(
|
||||
"/app/overlays/o2")
|
||||
writeK(t, ldr, "/app/base", `
|
||||
th := NewKustTestHarness(t, "/app/overlays/o2")
|
||||
th.writeK("/app/base", `
|
||||
vars:
|
||||
- name: AWARD
|
||||
objref:
|
||||
@@ -443,7 +432,7 @@ vars:
|
||||
name: heron
|
||||
apiVersion: v300
|
||||
`)
|
||||
writeK(t, ldr, "/app/overlays/o1", `
|
||||
th.writeK("/app/overlays/o1", `
|
||||
vars:
|
||||
- name: AWARD
|
||||
objref:
|
||||
@@ -452,7 +441,7 @@ vars:
|
||||
bases:
|
||||
- ../../base
|
||||
`)
|
||||
writeK(t, ldr, "/app/overlays/o2", `
|
||||
th.writeK("/app/overlays/o2", `
|
||||
vars:
|
||||
- name: VEGETABLE
|
||||
objref:
|
||||
@@ -461,7 +450,7 @@ vars:
|
||||
bases:
|
||||
- ../o1
|
||||
`)
|
||||
_, err := makeKustTarget(t, ldr).getAllVars()
|
||||
_, err := th.makeKustTarget().getAllVars()
|
||||
if err == nil {
|
||||
t.Fatalf("expected var collision")
|
||||
}
|
||||
|
||||
@@ -19,20 +19,18 @@ package target
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
|
||||
)
|
||||
|
||||
func writeCombinedOverlays(t *testing.T, ldr loadertest.FakeLoader) {
|
||||
func writeCombinedOverlays(th *KustTestHarness) {
|
||||
// Base
|
||||
writeK(t, ldr, "/app/base", `
|
||||
th.writeK("/app/base", `
|
||||
resources:
|
||||
- serviceaccount.yaml
|
||||
- rolebinding.yaml
|
||||
namePrefix: base-
|
||||
nameSuffix: -suffix
|
||||
`)
|
||||
writeF(t, ldr, "/app/base/rolebinding.yaml", `
|
||||
th.writeF("/app/base/rolebinding.yaml", `
|
||||
apiVersion: rbac.authorization.k8s.io/v1beta1
|
||||
kind: RoleBinding
|
||||
metadata:
|
||||
@@ -45,7 +43,7 @@ subjects:
|
||||
- kind: ServiceAccount
|
||||
name: serviceaccount
|
||||
`)
|
||||
writeF(t, ldr, "/app/base/serviceaccount.yaml", `
|
||||
th.writeF("/app/base/serviceaccount.yaml", `
|
||||
apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
@@ -53,13 +51,13 @@ metadata:
|
||||
`)
|
||||
|
||||
// Mid-level overlays
|
||||
writeK(t, ldr, "/app/overlays/a", `
|
||||
th.writeK("/app/overlays/a", `
|
||||
bases:
|
||||
- ../../base
|
||||
namePrefix: a-
|
||||
nameSuffix: -suffixA
|
||||
`)
|
||||
writeK(t, ldr, "/app/overlays/b", `
|
||||
th.writeK("/app/overlays/b", `
|
||||
bases:
|
||||
- ../../base
|
||||
namePrefix: b-
|
||||
@@ -67,7 +65,7 @@ nameSuffix: -suffixB
|
||||
`)
|
||||
|
||||
// Top overlay, combining the mid-level overlays
|
||||
writeK(t, ldr, "/app/combined", `
|
||||
th.writeK("/app/combined", `
|
||||
bases:
|
||||
- ../overlays/a
|
||||
- ../overlays/b
|
||||
@@ -75,17 +73,13 @@ bases:
|
||||
}
|
||||
|
||||
func TestMultibasesNoConflict(t *testing.T) {
|
||||
ldr := loadertest.NewFakeLoader("/app/combined")
|
||||
writeCombinedOverlays(t, ldr)
|
||||
m, err := makeKustTarget(t, ldr).MakeCustomizedResMap()
|
||||
th := NewKustTestHarness(t, "/app/combined")
|
||||
writeCombinedOverlays(th)
|
||||
m, err := th.makeKustTarget().MakeCustomizedResMap()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected err: %v", err)
|
||||
}
|
||||
s, err := m.EncodeAsYaml()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected err: %v", err)
|
||||
}
|
||||
assertExpectedEqualsActual(t, s, `apiVersion: v1
|
||||
th.assertActualEqualsExpected(m, `apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
name: a-base-serviceaccount-suffix-suffixA
|
||||
@@ -122,10 +116,10 @@ subjects:
|
||||
}
|
||||
|
||||
func TestMultibasesWithConflict(t *testing.T) {
|
||||
ldr := loadertest.NewFakeLoader("/app/combined")
|
||||
writeCombinedOverlays(t, ldr)
|
||||
th := NewKustTestHarness(t, "/app/combined")
|
||||
writeCombinedOverlays(th)
|
||||
|
||||
writeK(t, ldr, "/app/overlays/a", `
|
||||
th.writeK("/app/overlays/a", `
|
||||
bases:
|
||||
- ../../base
|
||||
namePrefix: a-
|
||||
@@ -135,14 +129,14 @@ resources:
|
||||
`)
|
||||
// Expect an error because this resource in the overlay
|
||||
// matches a resource in the base.
|
||||
writeF(t, ldr, "/app/overlays/a/serviceaccount.yaml", `
|
||||
th.writeF("/app/overlays/a/serviceaccount.yaml", `
|
||||
apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
name: serviceaccount
|
||||
`)
|
||||
|
||||
_, err := makeKustTarget(t, ldr).MakeCustomizedResMap()
|
||||
_, err := th.makeKustTarget().MakeCustomizedResMap()
|
||||
if err == nil {
|
||||
t.Fatalf("Expected resource conflict.")
|
||||
}
|
||||
|
||||
@@ -24,14 +24,30 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
|
||||
"sigs.k8s.io/kustomize/k8sdeps/transformer"
|
||||
"sigs.k8s.io/kustomize/pkg/constants"
|
||||
"sigs.k8s.io/kustomize/pkg/fs"
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
|
||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||
"sigs.k8s.io/kustomize/pkg/resource"
|
||||
)
|
||||
|
||||
func makeKustTarget(t *testing.T, l ifc.Loader) *KustTarget {
|
||||
type KustTestHarness struct {
|
||||
t *testing.T
|
||||
rf *resmap.Factory
|
||||
ldr loadertest.FakeLoader
|
||||
}
|
||||
|
||||
func NewKustTestHarness(t *testing.T, path string) *KustTestHarness {
|
||||
return &KustTestHarness{
|
||||
t: t,
|
||||
rf: resmap.NewFactory(resource.NewFactory(
|
||||
kunstruct.NewKunstructuredFactoryImpl())),
|
||||
ldr: loadertest.NewFakeLoader(path)}
|
||||
}
|
||||
|
||||
func (th *KustTestHarness) makeKustTarget() *KustTarget {
|
||||
// Warning: the following filesystem - a fake - must be rooted at /.
|
||||
// This fs root is used as the working directory for the shell spawned by
|
||||
// the secretgenerator, and has nothing to do with the filesystem used
|
||||
@@ -42,29 +58,31 @@ func makeKustTarget(t *testing.T, l ifc.Loader) *KustTarget {
|
||||
fakeFs := fs.MakeFakeFS()
|
||||
fakeFs.Mkdir("/")
|
||||
kt, err := NewKustTarget(
|
||||
l, fakeFs, rf, transformer.NewFactoryImpl())
|
||||
th.ldr, fakeFs, th.rf, transformer.NewFactoryImpl())
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected construction error %v", err)
|
||||
th.t.Fatalf("Unexpected construction error %v", err)
|
||||
}
|
||||
return kt
|
||||
}
|
||||
|
||||
func writeF(
|
||||
t *testing.T, ldr loadertest.FakeLoader, dir string, content string) {
|
||||
err := ldr.AddFile(dir, []byte(content))
|
||||
func (th *KustTestHarness) writeF(dir string, content string) {
|
||||
err := th.ldr.AddFile(dir, []byte(content))
|
||||
if err != nil {
|
||||
t.Fatalf("failed write to %s; %v", dir, err)
|
||||
th.t.Fatalf("failed write to %s; %v", dir, err)
|
||||
}
|
||||
}
|
||||
|
||||
func writeK(
|
||||
t *testing.T, ldr loadertest.FakeLoader, dir string, content string) {
|
||||
writeF(t, ldr, filepath.Join(dir, constants.KustomizationFileName), `
|
||||
func (th *KustTestHarness) writeK(dir string, content string) {
|
||||
th.writeF(filepath.Join(dir, constants.KustomizationFileName), `
|
||||
apiVersion: v1beta1
|
||||
kind: Kustomization
|
||||
`+content)
|
||||
}
|
||||
|
||||
func (th *KustTestHarness) fromMap(m map[string]interface{}) *resource.Resource {
|
||||
return th.rf.RF().FromMap(m)
|
||||
}
|
||||
|
||||
func tabToSpace(input string) string {
|
||||
var result []string
|
||||
for _, i := range input {
|
||||
@@ -98,21 +116,30 @@ func hint(a, b string) string {
|
||||
}
|
||||
|
||||
// Pretty printing of file differences.
|
||||
func assertExpectedEqualsActual(t *testing.T, actual []byte, expected string) {
|
||||
if expected == string(actual) {
|
||||
return
|
||||
func (th *KustTestHarness) assertActualEqualsExpected(
|
||||
m resmap.ResMap, expected string) {
|
||||
if m == nil {
|
||||
th.t.Fatalf("Map should not be nil.")
|
||||
}
|
||||
actual, err := m.EncodeAsYaml()
|
||||
if err != nil {
|
||||
th.t.Fatalf("Unexpected err: %v", err)
|
||||
}
|
||||
if string(actual) != expected {
|
||||
th.reportDiffAndFail(actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func (th *KustTestHarness) reportDiffAndFail(actual []byte, expected string) {
|
||||
sE, maxLen := convertToArray(expected)
|
||||
sA, _ := convertToArray(string(actual))
|
||||
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++ {
|
||||
@@ -127,5 +154,5 @@ func assertExpectedEqualsActual(t *testing.T, actual []byte, expected string) {
|
||||
fmt.Printf(format, "X", sE[i], "")
|
||||
}
|
||||
}
|
||||
t.Fatalf("Expected not equal to actual")
|
||||
th.t.Fatalf("Expected not equal to actual")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user