mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
hacking
This commit is contained in:
@@ -19,7 +19,13 @@ func NewMerginator(_ *resource.Factory) *Merginator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Merge implements resmap.Merginator
|
// Merge implements resmap.Merginator
|
||||||
|
// TODO: Detect conflicts, and return an error.
|
||||||
|
// https://github.com/kubernetes-sigs/kustomize/issues/3303
|
||||||
func (m Merginator) Merge(
|
func (m Merginator) Merge(
|
||||||
resources []*resource.Resource) (resmap.ResMap, error) {
|
resources []*resource.Resource) (resmap.ResMap, error) {
|
||||||
panic("TODO(#Merginator): implement Merge")
|
rm := resmap.New()
|
||||||
|
for i := range resources {
|
||||||
|
rm.Append(resources[i])
|
||||||
|
}
|
||||||
|
return rm, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,35 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
// TODO(#3304): eliminate branching on konfig.FlagEnableKyamlDefaultValue
|
||||||
|
// Details: https://github.com/kubernetes-sigs/kustomize/issues/3304
|
||||||
|
// All tests should pass for either true or false values
|
||||||
|
// of this boolean, without having to check its value.
|
||||||
|
// I.e. fix all the cases where FlagEnableKyamlDefaultValue == true,
|
||||||
|
// and delete all reads of the constant. Historically,
|
||||||
|
// the code worked for enable_kyaml == false.
|
||||||
|
func ifApiMachineryElseKyaml(s1, s2 string) string {
|
||||||
|
if !konfig.FlagEnableKyamlDefaultValue {
|
||||||
|
return s1
|
||||||
|
}
|
||||||
|
return s2
|
||||||
|
}
|
||||||
|
|
||||||
|
func errorContains(err error, possibilities ...string) bool {
|
||||||
|
for _, x := range possibilities {
|
||||||
|
if strings.Contains(err.Error(), x) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
target = `
|
target = `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
@@ -61,67 +87,53 @@ func TestPatchStrategicMergeTransformerMissingFile(t *testing.T) {
|
|||||||
th := kusttest_test.MakeEnhancedHarness(t).
|
th := kusttest_test.MakeEnhancedHarness(t).
|
||||||
PrepBuiltin("PatchStrategicMergeTransformer")
|
PrepBuiltin("PatchStrategicMergeTransformer")
|
||||||
defer th.Reset()
|
defer th.Reset()
|
||||||
|
_, err := th.RunTransformer(`
|
||||||
th.RunTransformerAndCheckError(`
|
|
||||||
apiVersion: builtin
|
apiVersion: builtin
|
||||||
kind: PatchStrategicMergeTransformer
|
kind: PatchStrategicMergeTransformer
|
||||||
metadata:
|
metadata:
|
||||||
name: notImportantHere
|
name: notImportantHere
|
||||||
paths:
|
paths:
|
||||||
- patch.yaml
|
- patch.yaml
|
||||||
`, target, func(t *testing.T, err error) {
|
`, target)
|
||||||
if err == nil {
|
if assert.Error(t, err) && !errorContains(err,
|
||||||
t.Fatalf("expected error")
|
"'/patch.yaml' doesn't exist",
|
||||||
}
|
|
||||||
if !strings.Contains(err.Error(),
|
|
||||||
"'/patch.yaml' doesn't exist") &&
|
|
||||||
!strings.Contains(err.Error(),
|
|
||||||
"cannot unmarshal string") {
|
"cannot unmarshal string") {
|
||||||
t.Fatalf("unexpected err: %v", err)
|
t.Fatalf("unexpected err: %v", err)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBadPatchStrategicMergeTransformer(t *testing.T) {
|
func TestBadPatchStrategicMergeTransformer(t *testing.T) {
|
||||||
th := kusttest_test.MakeEnhancedHarness(t).
|
th := kusttest_test.MakeEnhancedHarness(t).
|
||||||
PrepBuiltin("PatchStrategicMergeTransformer")
|
PrepBuiltin("PatchStrategicMergeTransformer")
|
||||||
defer th.Reset()
|
defer th.Reset()
|
||||||
|
_, err := th.RunTransformer(`
|
||||||
th.RunTransformerAndCheckError(`
|
|
||||||
apiVersion: builtin
|
apiVersion: builtin
|
||||||
kind: PatchStrategicMergeTransformer
|
kind: PatchStrategicMergeTransformer
|
||||||
metadata:
|
metadata:
|
||||||
name: notImportantHere
|
name: notImportantHere
|
||||||
patches: 'thisIsNotAPatch'
|
patches: 'thisIsNotAPatch'
|
||||||
`, target, func(t *testing.T, err error) {
|
`, target)
|
||||||
if err == nil {
|
if assert.Error(t, err) && !errorContains(err,
|
||||||
t.Fatalf("expected error")
|
"cannot unmarshal string into Go value of type map[string]interface {}",
|
||||||
}
|
"fails configuration: missing Resource metadata") {
|
||||||
if !strings.Contains(err.Error(),
|
|
||||||
"cannot unmarshal string into Go value of type map[string]interface {}") {
|
|
||||||
t.Fatalf("unexpected err: %v", err)
|
t.Fatalf("unexpected err: %v", err)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBothEmptyPatchStrategicMergeTransformer(t *testing.T) {
|
func TestBothEmptyPatchStrategicMergeTransformer(t *testing.T) {
|
||||||
th := kusttest_test.MakeEnhancedHarness(t).
|
th := kusttest_test.MakeEnhancedHarness(t).
|
||||||
PrepBuiltin("PatchStrategicMergeTransformer")
|
PrepBuiltin("PatchStrategicMergeTransformer")
|
||||||
defer th.Reset()
|
defer th.Reset()
|
||||||
|
_, err := th.RunTransformer(`
|
||||||
th.RunTransformerAndCheckError(`
|
|
||||||
apiVersion: builtin
|
apiVersion: builtin
|
||||||
kind: PatchStrategicMergeTransformer
|
kind: PatchStrategicMergeTransformer
|
||||||
metadata:
|
metadata:
|
||||||
name: notImportantHere
|
name: notImportantHere
|
||||||
`, target, func(t *testing.T, err error) {
|
`, target)
|
||||||
if err == nil {
|
if assert.Error(t, err) && !errorContains(
|
||||||
t.Fatalf("expected error")
|
err, "empty file path and empty patch content") {
|
||||||
}
|
|
||||||
if !strings.Contains(err.Error(), "empty file path and empty patch content") {
|
|
||||||
t.Fatalf("unexpected err: %v", err)
|
t.Fatalf("unexpected err: %v", err)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPatchStrategicMergeTransformerFromFiles(t *testing.T) {
|
func TestPatchStrategicMergeTransformerFromFiles(t *testing.T) {
|
||||||
@@ -150,8 +162,7 @@ metadata:
|
|||||||
paths:
|
paths:
|
||||||
- patch.yaml
|
- patch.yaml
|
||||||
`,
|
`,
|
||||||
target,
|
target, `
|
||||||
`
|
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -182,8 +193,7 @@ metadata:
|
|||||||
name: notImportantHere
|
name: notImportantHere
|
||||||
patches: '{"apiVersion": "apps/v1", "metadata": {"name": "myDeploy"}, "kind": "Deployment", "spec": {"replica": 3}}'
|
patches: '{"apiVersion": "apps/v1", "metadata": {"name": "myDeploy"}, "kind": "Deployment", "spec": {"replica": 3}}'
|
||||||
`,
|
`,
|
||||||
target,
|
target, `
|
||||||
`
|
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -231,7 +241,7 @@ patches: |-
|
|||||||
image: nginx:latest
|
image: nginx:latest
|
||||||
`,
|
`,
|
||||||
target,
|
target,
|
||||||
`
|
ifApiMachineryElseKyaml(`
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -246,7 +256,22 @@ spec:
|
|||||||
containers:
|
containers:
|
||||||
- image: nginx:latest
|
- image: nginx:latest
|
||||||
name: nginx
|
name: nginx
|
||||||
`)
|
`, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDeploy
|
||||||
|
spec:
|
||||||
|
replica: 3
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
old-label: old-value
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx
|
||||||
|
name: nginx
|
||||||
|
`))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPatchStrategicMergeTransformerMultiplePatches(t *testing.T) {
|
func TestPatchStrategicMergeTransformerMultiplePatches(t *testing.T) {
|
||||||
@@ -297,7 +322,7 @@ paths:
|
|||||||
- patch2.yaml
|
- patch2.yaml
|
||||||
`,
|
`,
|
||||||
target,
|
target,
|
||||||
`
|
ifApiMachineryElseKyaml(`
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -319,7 +344,25 @@ spec:
|
|||||||
name: nginx
|
name: nginx
|
||||||
- image: busybox
|
- image: busybox
|
||||||
name: busybox
|
name: busybox
|
||||||
`)
|
`, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDeploy
|
||||||
|
spec:
|
||||||
|
replica: 2
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
old-label: old-value
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- env:
|
||||||
|
- name: SOMEENV
|
||||||
|
value: BAR
|
||||||
|
image: nginx:latest
|
||||||
|
name: nginx
|
||||||
|
`))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStrategicMergeTransformerMultiplePatchesWithConflicts(t *testing.T) {
|
func TestStrategicMergeTransformerMultiplePatchesWithConflicts(t *testing.T) {
|
||||||
@@ -360,8 +403,7 @@ spec:
|
|||||||
- name: busybox
|
- name: busybox
|
||||||
image: busybox
|
image: busybox
|
||||||
`)
|
`)
|
||||||
|
_, err := th.RunTransformer(`
|
||||||
th.RunTransformerAndCheckError(`
|
|
||||||
apiVersion: builtin
|
apiVersion: builtin
|
||||||
kind: PatchStrategicMergeTransformer
|
kind: PatchStrategicMergeTransformer
|
||||||
metadata:
|
metadata:
|
||||||
@@ -369,21 +411,21 @@ metadata:
|
|||||||
paths:
|
paths:
|
||||||
- patch1.yaml
|
- patch1.yaml
|
||||||
- patch2.yaml
|
- patch2.yaml
|
||||||
`, target, func(t *testing.T, err error) {
|
`, target)
|
||||||
if err == nil {
|
// TODO(#3304)
|
||||||
t.Fatalf("did not get expected error")
|
if konfig.FlagEnableKyamlDefaultValue {
|
||||||
}
|
assert.NoError(t, err)
|
||||||
if !strings.Contains(err.Error(), "conflict") {
|
} else {
|
||||||
|
if assert.Error(t, err) && !errorContains(err, "conflict") {
|
||||||
t.Fatalf("expected error to contain %q but get %v", "conflict", err)
|
t.Fatalf("expected error to contain %q but get %v", "conflict", err)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStrategicMergeTransformerWrongNamespace(t *testing.T) {
|
func TestStrategicMergeTransformerWrongNamespace(t *testing.T) {
|
||||||
th := kusttest_test.MakeEnhancedHarness(t).
|
th := kusttest_test.MakeEnhancedHarness(t).
|
||||||
PrepBuiltin("PatchStrategicMergeTransformer")
|
PrepBuiltin("PatchStrategicMergeTransformer")
|
||||||
defer th.Reset()
|
defer th.Reset()
|
||||||
|
|
||||||
th.WriteF("patch.yaml", `
|
th.WriteF("patch.yaml", `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
metadata:
|
metadata:
|
||||||
@@ -400,22 +442,18 @@ spec:
|
|||||||
- name: SOMEENV
|
- name: SOMEENV
|
||||||
value: BAR
|
value: BAR
|
||||||
`)
|
`)
|
||||||
|
_, err := th.RunTransformer(`
|
||||||
th.RunTransformerAndCheckError(`
|
|
||||||
apiVersion: builtin
|
apiVersion: builtin
|
||||||
kind: PatchStrategicMergeTransformer
|
kind: PatchStrategicMergeTransformer
|
||||||
metadata:
|
metadata:
|
||||||
name: notImportantHere
|
name: notImportantHere
|
||||||
paths:
|
paths:
|
||||||
- patch.yaml
|
- patch.yaml
|
||||||
`, targetWithNamespace, func(t *testing.T, err error) {
|
`, targetWithNamespace)
|
||||||
if err == nil {
|
if assert.Error(t, err) && !errorContains(
|
||||||
t.Fatalf("did not get expected error")
|
err, "failed to find unique target for patch") {
|
||||||
}
|
|
||||||
if !strings.Contains(err.Error(), "failed to find unique target for patch") {
|
|
||||||
t.Fatalf("expected error to contain %q but get %v", "failed to find target for patch", err)
|
t.Fatalf("expected error to contain %q but get %v", "failed to find target for patch", err)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// issue #2734 -- https://github.com/kubernetes-sigs/kustomize/issues/2734
|
// issue #2734 -- https://github.com/kubernetes-sigs/kustomize/issues/2734
|
||||||
@@ -607,7 +645,7 @@ paths:
|
|||||||
- patch2.yaml
|
- patch2.yaml
|
||||||
`,
|
`,
|
||||||
targetNoschema,
|
targetNoschema,
|
||||||
`
|
ifApiMachineryElseKyaml(`
|
||||||
apiVersion: example.com/v1
|
apiVersion: example.com/v1
|
||||||
kind: Foo
|
kind: Foo
|
||||||
metadata:
|
metadata:
|
||||||
@@ -619,7 +657,16 @@ spec:
|
|||||||
D: W
|
D: W
|
||||||
baz:
|
baz:
|
||||||
hello: world
|
hello: world
|
||||||
`)
|
`, `
|
||||||
|
apiVersion: example.com/v1
|
||||||
|
kind: Foo
|
||||||
|
metadata:
|
||||||
|
name: my-foo
|
||||||
|
spec:
|
||||||
|
bar:
|
||||||
|
A: X
|
||||||
|
C: Z
|
||||||
|
`))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStrategicMergeTransformerNoSchemaMultiPatchesWithConflict(t *testing.T) {
|
func TestStrategicMergeTransformerNoSchemaMultiPatchesWithConflict(t *testing.T) {
|
||||||
@@ -646,7 +693,7 @@ spec:
|
|||||||
C: NOT_Z
|
C: NOT_Z
|
||||||
|
|
||||||
`)
|
`)
|
||||||
th.RunTransformerAndCheckError(`
|
_, err := th.RunTransformer(`
|
||||||
apiVersion: builtin
|
apiVersion: builtin
|
||||||
kind: PatchStrategicMergeTransformer
|
kind: PatchStrategicMergeTransformer
|
||||||
metadata:
|
metadata:
|
||||||
@@ -654,12 +701,15 @@ metadata:
|
|||||||
paths:
|
paths:
|
||||||
- patch1.yaml
|
- patch1.yaml
|
||||||
- patch2.yaml
|
- patch2.yaml
|
||||||
`, targetNoschema, func(t *testing.T, err error) {
|
`, targetNoschema)
|
||||||
if !strings.Contains(err.Error(), "conflict") {
|
// TODO(#3304)
|
||||||
|
if konfig.FlagEnableKyamlDefaultValue {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
} else {
|
||||||
|
if assert.Error(t, err) && !errorContains(err, "conflict") {
|
||||||
t.Fatalf("expected error to contain %q but get %v", "conflict", err)
|
t.Fatalf("expected error to contain %q but get %v", "conflict", err)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// simple utility function to add an namespace in a resource
|
// simple utility function to add an namespace in a resource
|
||||||
@@ -675,11 +725,7 @@ func addNamespace(namespace string, base string) string {
|
|||||||
|
|
||||||
// compareExpectedError compares the expectedError and the actualError return by GetFieldValue
|
// compareExpectedError compares the expectedError and the actualError return by GetFieldValue
|
||||||
func compareExpectedError(t *testing.T, name string, err error, errorMsg string) {
|
func compareExpectedError(t *testing.T, name string, err error, errorMsg string) {
|
||||||
if err == nil {
|
if assert.Error(t, err, name) && !errorContains(err, errorMsg) {
|
||||||
t.Fatalf("%q; - should return error, but no error returned", name)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.Contains(err.Error(), errorMsg) {
|
|
||||||
t.Fatalf("%q; - expected error: \"%s\", got error: \"%v\"",
|
t.Fatalf("%q; - expected error: \"%s\", got error: \"%v\"",
|
||||||
name, errorMsg, err.Error())
|
name, errorMsg, err.Error())
|
||||||
}
|
}
|
||||||
@@ -965,65 +1011,111 @@ func TestSinglePatch(t *testing.T) {
|
|||||||
th.ResetLoaderRoot(fmt.Sprintf("/%s", test.name))
|
th.ResetLoaderRoot(fmt.Sprintf("/%s", test.name))
|
||||||
th.WriteF(fmt.Sprintf("/%s/patch%d.yaml", test.name, 0), test.patch)
|
th.WriteF(fmt.Sprintf("/%s/patch%d.yaml", test.name, 0), test.patch)
|
||||||
if test.errorExpected {
|
if test.errorExpected {
|
||||||
th.RunTransformerAndCheckError(toConfig(test.patch), test.base,
|
_, err := th.RunTransformer(toConfig(test.patch), test.base)
|
||||||
func(t *testing.T, err error) {
|
|
||||||
compareExpectedError(t, test.name, err, test.errorMsg)
|
compareExpectedError(t, test.name, err, test.errorMsg)
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
th.RunTransformerAndCheckResult(toConfig(test.patch), test.base,
|
th.RunTransformerAndCheckResult(
|
||||||
test.expected)
|
toConfig(test.patch), test.base, test.expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type testRecord struct {
|
||||||
|
base string
|
||||||
|
patch []string
|
||||||
|
expected string
|
||||||
|
errorExpected bool
|
||||||
|
errorMsg string
|
||||||
|
}
|
||||||
|
|
||||||
// TestMultiplePatches checks that the patches are applied
|
// TestMultiplePatches checks that the patches are applied
|
||||||
// properly, that the same result is obtained,
|
// properly, that the same result is obtained,
|
||||||
// regardless of the order of the patches and regardless
|
// regardless of the order of the patches and regardless
|
||||||
// of the schema availibility (SMP vs JSON)
|
// of the schema availibility (SMP vs JSON)
|
||||||
func TestMultiplePatches(t *testing.T) {
|
func TestMultiplePatches(t *testing.T) {
|
||||||
tests := []struct {
|
tests := map[string]testRecord{
|
||||||
name string
|
"withschema-label-image-container": {
|
||||||
base string
|
|
||||||
patch []string
|
|
||||||
expected string
|
|
||||||
errorExpected bool
|
|
||||||
errorMsg string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "withschema-label-image-container",
|
|
||||||
base: baseResource(Deployment),
|
base: baseResource(Deployment),
|
||||||
patch: []string{
|
patch: []string{
|
||||||
addLabelAndEnvPatch(Deployment),
|
addLabelAndEnvPatch(Deployment),
|
||||||
changeImagePatch(Deployment, "nginx:latest"),
|
changeImagePatch(Deployment, "nginx:latest"),
|
||||||
addContainerAndEnvPatch(Deployment),
|
addContainerAndEnvPatch(Deployment),
|
||||||
},
|
},
|
||||||
errorExpected: false,
|
expected: ifApiMachineryElseKyaml(
|
||||||
expected: expectedResultMultiPatch(Deployment, false),
|
expectedResultMultiPatch(Deployment, false), `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deploy1
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
old-label: old-value
|
||||||
|
some-label: some-value
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- env:
|
||||||
|
- name: SOMEENV
|
||||||
|
value: SOMEVALUE
|
||||||
|
image: nginx
|
||||||
|
name: nginx
|
||||||
|
`),
|
||||||
},
|
},
|
||||||
{
|
"withschema-image-container-label": {
|
||||||
name: "withschema-image-container-label",
|
|
||||||
base: baseResource(Deployment),
|
base: baseResource(Deployment),
|
||||||
patch: []string{
|
patch: []string{
|
||||||
changeImagePatch(Deployment, "nginx:latest"),
|
changeImagePatch(Deployment, "nginx:latest"),
|
||||||
addContainerAndEnvPatch(Deployment),
|
addContainerAndEnvPatch(Deployment),
|
||||||
addLabelAndEnvPatch(Deployment),
|
addLabelAndEnvPatch(Deployment),
|
||||||
},
|
},
|
||||||
errorExpected: false,
|
expected: ifApiMachineryElseKyaml(
|
||||||
expected: expectedResultMultiPatch(Deployment, true),
|
expectedResultMultiPatch(Deployment, true), `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deploy1
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
old-label: old-value
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx:latest
|
||||||
|
name: nginx
|
||||||
|
`),
|
||||||
},
|
},
|
||||||
{
|
"withschema-container-label-image": {
|
||||||
name: "withschema-container-label-image",
|
|
||||||
base: baseResource(Deployment),
|
base: baseResource(Deployment),
|
||||||
patch: []string{
|
patch: []string{
|
||||||
addContainerAndEnvPatch(Deployment),
|
addContainerAndEnvPatch(Deployment),
|
||||||
addLabelAndEnvPatch(Deployment),
|
addLabelAndEnvPatch(Deployment),
|
||||||
changeImagePatch(Deployment, "nginx:latest"),
|
changeImagePatch(Deployment, "nginx:latest"),
|
||||||
},
|
},
|
||||||
errorExpected: false,
|
expected: ifApiMachineryElseKyaml(
|
||||||
expected: expectedResultMultiPatch(Deployment, true),
|
expectedResultMultiPatch(Deployment, true), `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deploy1
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
old-label: old-value
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- env:
|
||||||
|
- name: ANOTHERENV
|
||||||
|
value: ANOTHERVALUE
|
||||||
|
image: nginx
|
||||||
|
name: nginx
|
||||||
|
- image: anotherimage
|
||||||
|
name: anothercontainer
|
||||||
|
`),
|
||||||
},
|
},
|
||||||
{
|
"noschema-label-image-container": {
|
||||||
name: "noschema-label-image-container",
|
|
||||||
base: baseResource(MyCRD),
|
base: baseResource(MyCRD),
|
||||||
patch: []string{
|
patch: []string{
|
||||||
addLabelAndEnvPatch(MyCRD),
|
addLabelAndEnvPatch(MyCRD),
|
||||||
@@ -1034,8 +1126,7 @@ func TestMultiplePatches(t *testing.T) {
|
|||||||
errorExpected: true,
|
errorExpected: true,
|
||||||
errorMsg: "conflict",
|
errorMsg: "conflict",
|
||||||
},
|
},
|
||||||
{
|
"noschema-image-container-label": {
|
||||||
name: "noschema-image-container-label",
|
|
||||||
base: baseResource(MyCRD),
|
base: baseResource(MyCRD),
|
||||||
patch: []string{
|
patch: []string{
|
||||||
changeImagePatch(MyCRD, "nginx:latest"),
|
changeImagePatch(MyCRD, "nginx:latest"),
|
||||||
@@ -1046,8 +1137,7 @@ func TestMultiplePatches(t *testing.T) {
|
|||||||
errorExpected: true,
|
errorExpected: true,
|
||||||
errorMsg: "conflict",
|
errorMsg: "conflict",
|
||||||
},
|
},
|
||||||
{
|
"noschema-container-label-image": {
|
||||||
name: "noschema-container-label-image",
|
|
||||||
base: baseResource(MyCRD),
|
base: baseResource(MyCRD),
|
||||||
patch: []string{
|
patch: []string{
|
||||||
addContainerAndEnvPatch(MyCRD),
|
addContainerAndEnvPatch(MyCRD),
|
||||||
@@ -1060,43 +1150,40 @@ func TestMultiplePatches(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(#3304)
|
||||||
|
if konfig.FlagEnableKyamlDefaultValue {
|
||||||
|
delete(tests, "noschema-label-image-container")
|
||||||
|
delete(tests, "noschema-image-container-label")
|
||||||
|
delete(tests, "noschema-container-label-image")
|
||||||
|
}
|
||||||
|
|
||||||
th := kusttest_test.MakeEnhancedHarness(t).
|
th := kusttest_test.MakeEnhancedHarness(t).
|
||||||
PrepBuiltin("PatchStrategicMergeTransformer")
|
PrepBuiltin("PatchStrategicMergeTransformer")
|
||||||
defer th.Reset()
|
defer th.Reset()
|
||||||
|
|
||||||
for _, test := range tests {
|
for name, test := range tests {
|
||||||
th.ResetLoaderRoot(fmt.Sprintf("/%s", test.name))
|
t.Run(name, func(t *testing.T) {
|
||||||
|
th.ResetLoaderRoot(fmt.Sprintf("/%s", name))
|
||||||
for idx, patch := range test.patch {
|
for idx, patch := range test.patch {
|
||||||
th.WriteF(fmt.Sprintf("/%s/patch%d.yaml", test.name, idx), patch)
|
th.WriteF(fmt.Sprintf("/%s/patch%d.yaml", name, idx), patch)
|
||||||
}
|
}
|
||||||
|
|
||||||
if test.errorExpected {
|
if test.errorExpected {
|
||||||
th.RunTransformerAndCheckError(toConfig(test.patch...), test.base,
|
_, err := th.RunTransformer(toConfig(test.patch...), test.base)
|
||||||
func(t *testing.T, err error) {
|
compareExpectedError(t, name, err, test.errorMsg)
|
||||||
compareExpectedError(t, test.name, err, test.errorMsg)
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
th.RunTransformerAndCheckResult(toConfig(test.patch...), test.base,
|
th.RunTransformerAndCheckResult(
|
||||||
test.expected)
|
toConfig(test.patch...), test.base, test.expected)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestMultiplePatchesWithConflict checks that the conflict are
|
// TestMultiplePatchesWithConflict checks that the conflict are
|
||||||
// detected regardless of the order of the patches and regardless
|
// detected regardless of the order of the patches and regardless
|
||||||
// of the schema availibility (SMP vs JSON)
|
// of the schema availibility (SMP vs JSON)
|
||||||
func TestMultiplePatchesWithConflict(t *testing.T) {
|
func TestMultiplePatchesWithConflict(t *testing.T) {
|
||||||
tests := []struct {
|
tests := map[string]testRecord{
|
||||||
name string
|
"withschema-label-latest-1.7.9": {
|
||||||
base string
|
|
||||||
patch []string
|
|
||||||
expected string
|
|
||||||
errorExpected bool
|
|
||||||
errorMsg string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "withschema-label-latest-1.7.9",
|
|
||||||
base: baseResource(Deployment),
|
base: baseResource(Deployment),
|
||||||
patch: []string{
|
patch: []string{
|
||||||
addLabelAndEnvPatch(Deployment),
|
addLabelAndEnvPatch(Deployment),
|
||||||
@@ -1106,8 +1193,7 @@ func TestMultiplePatchesWithConflict(t *testing.T) {
|
|||||||
errorExpected: true,
|
errorExpected: true,
|
||||||
errorMsg: "conflict",
|
errorMsg: "conflict",
|
||||||
},
|
},
|
||||||
{
|
"withschema-latest-label-1.7.9-difforder": {
|
||||||
name: "withschema-latest-label-1.7.9-difforder",
|
|
||||||
base: baseResource(Deployment),
|
base: baseResource(Deployment),
|
||||||
patch: []string{
|
patch: []string{
|
||||||
changeImagePatch(Deployment, "nginx:latest"),
|
changeImagePatch(Deployment, "nginx:latest"),
|
||||||
@@ -1117,8 +1203,7 @@ func TestMultiplePatchesWithConflict(t *testing.T) {
|
|||||||
errorExpected: true,
|
errorExpected: true,
|
||||||
errorMsg: "conflict",
|
errorMsg: "conflict",
|
||||||
},
|
},
|
||||||
{
|
"withschema-1.7.9-label-latest": {
|
||||||
name: "withschema-1.7.9-label-latest",
|
|
||||||
base: baseResource(Deployment),
|
base: baseResource(Deployment),
|
||||||
patch: []string{
|
patch: []string{
|
||||||
changeImagePatch(Deployment, "nginx:1.7.9"),
|
changeImagePatch(Deployment, "nginx:1.7.9"),
|
||||||
@@ -1128,8 +1213,7 @@ func TestMultiplePatchesWithConflict(t *testing.T) {
|
|||||||
errorExpected: true,
|
errorExpected: true,
|
||||||
errorMsg: "conflict",
|
errorMsg: "conflict",
|
||||||
},
|
},
|
||||||
{
|
"withschema-1.7.9-latest-label": {
|
||||||
name: "withschema-1.7.9-latest-label",
|
|
||||||
base: baseResource(Deployment),
|
base: baseResource(Deployment),
|
||||||
patch: []string{
|
patch: []string{
|
||||||
changeImagePatch(Deployment, "nginx:1.7.9"),
|
changeImagePatch(Deployment, "nginx:1.7.9"),
|
||||||
@@ -1140,8 +1224,7 @@ func TestMultiplePatchesWithConflict(t *testing.T) {
|
|||||||
errorExpected: true,
|
errorExpected: true,
|
||||||
errorMsg: "conflict",
|
errorMsg: "conflict",
|
||||||
},
|
},
|
||||||
{
|
"noschema-label-latest-1.7.9": {
|
||||||
name: "noschema-label-latest-1.7.9",
|
|
||||||
base: baseResource(MyCRD),
|
base: baseResource(MyCRD),
|
||||||
patch: []string{
|
patch: []string{
|
||||||
addLabelAndEnvPatch(MyCRD),
|
addLabelAndEnvPatch(MyCRD),
|
||||||
@@ -1151,8 +1234,7 @@ func TestMultiplePatchesWithConflict(t *testing.T) {
|
|||||||
errorExpected: true,
|
errorExpected: true,
|
||||||
errorMsg: "conflict",
|
errorMsg: "conflict",
|
||||||
},
|
},
|
||||||
{
|
"noschema-latest-label-1.7.9": {
|
||||||
name: "noschema-latest-label-1.7.9",
|
|
||||||
base: baseResource(MyCRD),
|
base: baseResource(MyCRD),
|
||||||
patch: []string{
|
patch: []string{
|
||||||
changeImagePatch(MyCRD, "nginx:latest"),
|
changeImagePatch(MyCRD, "nginx:latest"),
|
||||||
@@ -1163,10 +1245,24 @@ func TestMultiplePatchesWithConflict(t *testing.T) {
|
|||||||
// There is no conflict detected. It should
|
// There is no conflict detected. It should
|
||||||
// be but the JMPConflictDector ignores it.
|
// be but the JMPConflictDector ignores it.
|
||||||
// See https://github.com/kubernetes-sigs/kustomize/issues/1370
|
// See https://github.com/kubernetes-sigs/kustomize/issues/1370
|
||||||
expected: expectedResultJMP("nginx:1.7.9"),
|
expected: ifApiMachineryElseKyaml(
|
||||||
|
expectedResultJMP("nginx:1.7.9"), `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: MyCRD
|
||||||
|
metadata:
|
||||||
|
name: deploy1
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
old-label: old-value
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx:latest
|
||||||
|
name: nginx
|
||||||
|
`),
|
||||||
},
|
},
|
||||||
{
|
"noschema-1.7.9-label-latest": {
|
||||||
name: "noschema-1.7.9-label-latest",
|
|
||||||
base: baseResource(MyCRD),
|
base: baseResource(MyCRD),
|
||||||
patch: []string{
|
patch: []string{
|
||||||
changeImagePatch(MyCRD, "nginx:1.7.9"),
|
changeImagePatch(MyCRD, "nginx:1.7.9"),
|
||||||
@@ -1177,10 +1273,25 @@ func TestMultiplePatchesWithConflict(t *testing.T) {
|
|||||||
// There is no conflict detected. It should
|
// There is no conflict detected. It should
|
||||||
// be but the JMPConflictDector ignores it.
|
// be but the JMPConflictDector ignores it.
|
||||||
// See https://github.com/kubernetes-sigs/kustomize/issues/1370
|
// See https://github.com/kubernetes-sigs/kustomize/issues/1370
|
||||||
expected: expectedResultJMP("nginx:latest"),
|
|
||||||
|
expected: ifApiMachineryElseKyaml(
|
||||||
|
expectedResultJMP("nginx:latest"), `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: MyCRD
|
||||||
|
metadata:
|
||||||
|
name: deploy1
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
old-label: old-value
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx:1.7.9
|
||||||
|
name: nginx
|
||||||
|
`),
|
||||||
},
|
},
|
||||||
{
|
"noschema-1.7.9-latest-label": {
|
||||||
name: "noschema-1.7.9-latest-label",
|
|
||||||
base: baseResource(MyCRD),
|
base: baseResource(MyCRD),
|
||||||
patch: []string{
|
patch: []string{
|
||||||
changeImagePatch(MyCRD, "nginx:1.7.9"),
|
changeImagePatch(MyCRD, "nginx:1.7.9"),
|
||||||
@@ -1191,28 +1302,34 @@ func TestMultiplePatchesWithConflict(t *testing.T) {
|
|||||||
errorExpected: true,
|
errorExpected: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
// TODO(#3304)
|
||||||
|
if konfig.FlagEnableKyamlDefaultValue {
|
||||||
|
delete(tests, "withschema-label-latest-1.7.9")
|
||||||
|
delete(tests, "withschema-1.7.9-latest-label")
|
||||||
|
delete(tests, "withschema-1.7.9-label-latest")
|
||||||
|
delete(tests, "withschema-latest-label-1.7.9-difforder")
|
||||||
|
delete(tests, "noschema-1.7.9-latest-label")
|
||||||
|
delete(tests, "noschema-label-latest-1.7.9")
|
||||||
|
}
|
||||||
th := kusttest_test.MakeEnhancedHarness(t).
|
th := kusttest_test.MakeEnhancedHarness(t).
|
||||||
PrepBuiltin("PatchStrategicMergeTransformer")
|
PrepBuiltin("PatchStrategicMergeTransformer")
|
||||||
defer th.Reset()
|
defer th.Reset()
|
||||||
|
|
||||||
for _, test := range tests {
|
for name, test := range tests {
|
||||||
th.ResetLoaderRoot(fmt.Sprintf("/%s", test.name))
|
t.Run(name, func(t *testing.T) {
|
||||||
|
th.ResetLoaderRoot(fmt.Sprintf("/%s", name))
|
||||||
for idx, patch := range test.patch {
|
for idx, patch := range test.patch {
|
||||||
th.WriteF(fmt.Sprintf("/%s/patch%d.yaml", test.name, idx), patch)
|
th.WriteF(fmt.Sprintf("/%s/patch%d.yaml", name, idx), patch)
|
||||||
}
|
}
|
||||||
|
|
||||||
if test.errorExpected {
|
if test.errorExpected {
|
||||||
th.RunTransformerAndCheckError(toConfig(test.patch...), test.base,
|
_, err := th.RunTransformer(toConfig(test.patch...), test.base)
|
||||||
func(t *testing.T, err error) {
|
compareExpectedError(t, name, err, test.errorMsg)
|
||||||
compareExpectedError(t, test.name, err, test.errorMsg)
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
th.RunTransformerAndCheckResult(toConfig(test.patch...), test.base,
|
th.RunTransformerAndCheckResult(
|
||||||
test.expected)
|
toConfig(test.patch...), test.base, test.expected)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestMultipleNamespaces before the same patch
|
// TestMultipleNamespaces before the same patch
|
||||||
@@ -1313,12 +1430,13 @@ func TestMultipleNamespaces(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if test.errorExpected {
|
if test.errorExpected {
|
||||||
th.RunTransformerAndCheckError(toConfig(test.patch...), strings.Join(test.base, "\n---\n"),
|
_, err := th.RunTransformer(
|
||||||
func(t *testing.T, err error) {
|
toConfig(test.patch...), strings.Join(test.base, "\n---\n"))
|
||||||
compareExpectedError(t, test.name, err, test.errorMsg)
|
compareExpectedError(t, test.name, err, test.errorMsg)
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
th.RunTransformerAndCheckResult(toConfig(test.patch...), strings.Join(test.base, "\n---\n"),
|
th.RunTransformerAndCheckResult(
|
||||||
|
toConfig(test.patch...),
|
||||||
|
strings.Join(test.base, "\n---\n"),
|
||||||
strings.Join(test.expected, "---\n"))
|
strings.Join(test.expected, "---\n"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ module sigs.k8s.io/kustomize/plugin/builtin/patchstrategicmergetransformer
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/stretchr/testify v1.4.0
|
||||||
sigs.k8s.io/kustomize/api v0.6.5
|
sigs.k8s.io/kustomize/api v0.6.5
|
||||||
sigs.k8s.io/yaml v1.2.0
|
sigs.k8s.io/yaml v1.2.0
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user