Update tests for PatchTransformer

This commit is contained in:
Morten Torkildsen
2020-04-10 16:12:53 -07:00
parent 6aabe72fce
commit e7e7648c55

View File

@@ -69,20 +69,21 @@ func TestPatchTransformerMissingFile(t *testing.T) {
PrepBuiltin("PatchTransformer") PrepBuiltin("PatchTransformer")
defer th.Reset() defer th.Reset()
_, err := th.RunTransformer(` th.RunTransformerAndCheckError(`
apiVersion: builtin apiVersion: builtin
kind: PatchTransformer kind: PatchTransformer
metadata: metadata:
name: notImportantHere name: notImportantHere
path: patch.yaml path: patch.yaml
`, target) `, target, func(t *testing.T, err error) {
if err == nil { if err == nil {
t.Fatalf("expected error") t.Fatalf("expected error")
} }
if !strings.Contains(err.Error(), if !strings.Contains(err.Error(),
"'/patch.yaml' doesn't exist") { "'/patch.yaml' doesn't exist") {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
})
} }
func TestPatchTransformerBadPatch(t *testing.T) { func TestPatchTransformerBadPatch(t *testing.T) {
@@ -90,20 +91,21 @@ func TestPatchTransformerBadPatch(t *testing.T) {
PrepBuiltin("PatchTransformer") PrepBuiltin("PatchTransformer")
defer th.Reset() defer th.Reset()
_, err := th.RunTransformer(` th.RunTransformerAndCheckError(`
apiVersion: builtin apiVersion: builtin
kind: PatchTransformer kind: PatchTransformer
metadata: metadata:
name: notImportantHere name: notImportantHere
patch: "thisIsNotAPatch" patch: "thisIsNotAPatch"
`, target) `, target, func(t *testing.T, err error) {
if err == nil { if err == nil {
t.Fatalf("expected error") t.Fatalf("expected error")
} }
if !strings.Contains(err.Error(), if !strings.Contains(err.Error(),
"unable to get either a Strategic Merge Patch or JSON patch 6902 from") { "unable to get either a Strategic Merge Patch or JSON patch 6902 from") {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
})
} }
func TestPatchTransformerMissingSelector(t *testing.T) { func TestPatchTransformerMissingSelector(t *testing.T) {
@@ -111,20 +113,21 @@ func TestPatchTransformerMissingSelector(t *testing.T) {
PrepBuiltin("PatchTransformer") PrepBuiltin("PatchTransformer")
defer th.Reset() defer th.Reset()
_, err := th.RunTransformer(` th.RunTransformerAndCheckError(`
apiVersion: builtin apiVersion: builtin
kind: PatchTransformer kind: PatchTransformer
metadata: metadata:
name: notImportantHere name: notImportantHere
patch: '[{"op": "add", "path": "/spec/template/spec/dnsPolicy", "value": "ClusterFirst"}]' patch: '[{"op": "add", "path": "/spec/template/spec/dnsPolicy", "value": "ClusterFirst"}]'
`, target) `, target, func(t *testing.T, err error) {
if err == nil { if err == nil {
t.Fatalf("expected error") t.Fatalf("expected error")
} }
if !strings.Contains(err.Error(), if !strings.Contains(err.Error(),
"must specify a target for patch") { "must specify a target for patch") {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
})
} }
func TestPatchTransformerBothEmptyPathAndPatch(t *testing.T) { func TestPatchTransformerBothEmptyPathAndPatch(t *testing.T) {
@@ -132,18 +135,19 @@ func TestPatchTransformerBothEmptyPathAndPatch(t *testing.T) {
PrepBuiltin("PatchTransformer") PrepBuiltin("PatchTransformer")
defer th.Reset() defer th.Reset()
_, err := th.RunTransformer(` th.RunTransformerAndCheckError(`
apiVersion: builtin apiVersion: builtin
kind: PatchTransformer kind: PatchTransformer
metadata: metadata:
name: notImportantHere name: notImportantHere
`, target) `, target, func(t *testing.T, err error) {
if err == nil { if err == nil {
t.Fatalf("expected error") t.Fatalf("expected error")
} }
if !strings.Contains(err.Error(), "must specify one of patch and path in") { if !strings.Contains(err.Error(), "must specify one of patch and path in") {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
})
} }
func TestPatchTransformerBothNonEmptyPathAndPatch(t *testing.T) { func TestPatchTransformerBothNonEmptyPathAndPatch(t *testing.T) {
@@ -151,20 +155,21 @@ func TestPatchTransformerBothNonEmptyPathAndPatch(t *testing.T) {
PrepBuiltin("PatchTransformer") PrepBuiltin("PatchTransformer")
defer th.Reset() defer th.Reset()
_, err := th.RunTransformer(` th.RunTransformerAndCheckError(`
apiVersion: builtin apiVersion: builtin
kind: PatchTransformer kind: PatchTransformer
metadata: metadata:
name: notImportantHere name: notImportantHere
Path: patch.yaml Path: patch.yaml
Patch: "something" Patch: "something"
`, target) `, target, func(t *testing.T, err error) {
if err == nil { if err == nil {
t.Fatalf("expected error") t.Fatalf("expected error")
} }
if !strings.Contains(err.Error(), "patch and path can't be set at the same time") { if !strings.Contains(err.Error(), "patch and path can't be set at the same time") {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
})
} }
func TestPatchTransformerFromFiles(t *testing.T) { func TestPatchTransformerFromFiles(t *testing.T) {
@@ -181,7 +186,7 @@ spec:
replica: 3 replica: 3
`) `)
rm := th.LoadAndRunTransformer(` th.RunTransformerAndCheckResult(`
apiVersion: builtin apiVersion: builtin
kind: PatchTransformer kind: PatchTransformer
metadata: metadata:
@@ -189,9 +194,9 @@ metadata:
path: patch.yaml path: patch.yaml
target: target:
name: .*Deploy name: .*Deploy
`, target) `,
target,
th.AssertActualEqualsExpected(rm, ` `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
@@ -250,7 +255,7 @@ func TestPatchTransformerWithInline(t *testing.T) {
PrepBuiltin("PatchTransformer") PrepBuiltin("PatchTransformer")
defer th.Reset() defer th.Reset()
rm := th.LoadAndRunTransformer(` th.RunTransformerAndCheckResult(`
apiVersion: builtin apiVersion: builtin
kind: PatchTransformer kind: PatchTransformer
metadata: metadata:
@@ -259,9 +264,8 @@ patch: '[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "va
target: target:
name: .*Deploy name: .*Deploy
kind: Deployment kind: Deployment
`, target) `, target,
`
th.AssertActualEqualsExpected(rm, `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata: