Merge pull request #2253 from khrisrichardson/issues/2252

#2252: convert tests to exercise openapi and inference
This commit is contained in:
Kubernetes Prow Robot
2020-03-03 21:07:47 -08:00
committed by GitHub
4 changed files with 727 additions and 337 deletions

View File

@@ -6,142 +6,211 @@ package merge2_test
var elementTestCases = []testCase{
{description: `merge Element -- keep field in dest`,
source: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
`,
dest: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v0
command: ['run.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v0
command: ['run.sh']
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
command: ['run.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
command: ['run.sh']
`,
},
{description: `merge Element -- add field to dest`,
source: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
command: ['run.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
command: ['run.sh']
`,
dest: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v0
spec:
template:
spec:
containers:
- name: foo
image: foo:v0
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
command: ['run.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
command: ['run.sh']
`,
},
{description: `merge Element -- add list, empty in dest`,
source: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
command: ['run.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
command: ['run.sh']
`,
dest: `
apiVersion: apps/v1
kind: Deployment
items: []
spec:
template:
spec:
containers: []
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
command: ['run.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
command: ['run.sh']
`,
},
{description: `merge Element -- add list, missing from dest`,
source: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
command: ['run.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
command: ['run.sh']
`,
dest: `
apiVersion: apps/v1
kind: Deployment
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
command: ['run.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
command: ['run.sh']
`,
},
{description: `merge Element -- add Element first`,
source: `
apiVersion: apps/v1
kind: Deployment
items:
- name: bar
image: bar:v1
command: ['run2.sh']
- name: foo
image: foo:v1
spec:
template:
spec:
containers:
- name: bar
image: bar:v1
command: ['run2.sh']
- name: foo
image: foo:v1
`,
dest: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v0
spec:
template:
spec:
containers:
- name: foo
image: foo:v0
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
`,
},
{description: `merge Element -- add Element second`,
source: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
`,
dest: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v0
spec:
template:
spec:
containers:
- name: foo
image: foo:v0
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
`,
},
@@ -150,25 +219,34 @@ items:
//
{description: `keep list -- list missing from src`,
source: `
apiVersion: apps/v1
kind: Deployment
`,
dest: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
`,
},
@@ -177,28 +255,40 @@ items:
//
{description: `keep Element -- element missing in src`,
source: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
`,
dest: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v0
- name: bar
image: bar:v1
command: ['run2.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v0
- name: bar
image: bar:v1
command: ['run2.sh']
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
`,
},
@@ -207,26 +297,38 @@ items:
//
{description: `keep element -- empty list in src`,
source: `
apiVersion: apps/v1
kind: Deployment
items: {}
spec:
template:
spec:
containers: {}
`,
dest: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
`,
},
@@ -235,46 +337,62 @@ items:
//
{description: `remove Element -- null in src`,
source: `
apiVersion: apps/v1
kind: Deployment
items: null
spec:
template:
spec:
containers: null
`,
dest: `
apiVersion: apps/v1
kind: Deployment
items:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
`,
expected: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec: {}
`,
},
//
// Test Case
//
{description: `no infer merge keys no merge'`,
{description: `infer merge keys merge'`,
source: `
apiVersion: custom
kind: Deployment
containers:
- name: foo
command: ['run2.sh']
`,
dest: `
apiVersion: custom
kind: Deployment
containers:
- name: foo
image: foo:bar
`,
expected: `
apiVersion: custom
kind: Deployment
containers:
- name: foo
image: foo:bar
command: ['run2.sh']
`,
noInfer: true,
infer: true,
},
//
@@ -282,8 +400,8 @@ containers:
//
{description: `no infer merge keys merge using schema`,
source: `
kind: Deployment
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
@@ -292,8 +410,8 @@ spec:
command: ['run2.sh']
`,
dest: `
kind: Deployment
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
@@ -302,8 +420,8 @@ spec:
image: foo:bar
`,
expected: `
kind: Deployment
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
@@ -312,7 +430,7 @@ spec:
image: foo:bar
command: ['run2.sh']
`,
noInfer: true,
infer: false,
},
//
@@ -320,24 +438,27 @@ spec:
//
{description: `no infer merge keys merge using explicit schema as line comment'`,
source: `
apiVersion: custom
kind: Deployment
containers:
- name: foo
command: ['run2.sh']
`,
dest: `
apiVersion: custom
kind: Deployment
containers: # {"items":{"$ref": "#/definitions/io.k8s.api.core.v1.Container"},"type":"array","x-kubernetes-patch-merge-key":"name","x-kubernetes-patch-strategy": "merge"}
- name: foo # hell ow
image: foo:bar
`,
expected: `
apiVersion: custom
kind: Deployment
containers: # {"items":{"$ref": "#/definitions/io.k8s.api.core.v1.Container"},"type":"array","x-kubernetes-patch-merge-key":"name","x-kubernetes-patch-strategy": "merge"}
- name: foo
image: foo:bar
command: ['run2.sh']
`,
noInfer: true,
infer: false,
},
}

View File

@@ -20,7 +20,7 @@ func TestMerge(t *testing.T) {
for j := range testCases[i] {
tc := testCases[i][j]
t.Run(tc.description, func(t *testing.T) {
actual, err := MergeStrings(tc.source, tc.dest, !tc.noInfer)
actual, err := MergeStrings(tc.source, tc.dest, tc.infer)
if !assert.NoError(t, err, tc.description) {
t.FailNow()
}
@@ -47,5 +47,5 @@ type testCase struct {
source string
dest string
expected string
noInfer bool
infer bool
}

File diff suppressed because it is too large Load Diff

View File

@@ -18,7 +18,7 @@ func TestMerge(t *testing.T) {
for j := range testCases[i] {
tc := testCases[i][j]
t.Run(tc.description, func(t *testing.T) {
actual, err := MergeStrings(tc.local, tc.origin, tc.update, !tc.noInfer)
actual, err := MergeStrings(tc.local, tc.origin, tc.update, tc.infer)
if tc.err == nil {
if !assert.NoError(t, err, tc.description) {
t.FailNow()
@@ -47,5 +47,5 @@ type testCase struct {
local string
expected string
err error
noInfer bool
infer bool
}