added previously failing unit test and fix using pkg/util/merge.go instead of mergo to apply patch.

This commit is contained in:
Mike Danese
2015-01-31 10:59:08 -08:00
parent 3039f7846c
commit a4eca69e6c
5 changed files with 95 additions and 27 deletions

View File

@@ -30,14 +30,16 @@ func TestMerge(t *testing.T) {
fragment string
expected runtime.Object
expectErr bool
kind string
}{
{
kind: "Pod",
obj: &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "foo",
},
},
fragment: "{ \"apiVersion\": \"v1beta1\" }",
fragment: `{ "apiVersion": "v1beta1" }`,
expected: &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "foo",
@@ -51,12 +53,13 @@ func TestMerge(t *testing.T) {
},
},
{
kind: "Pod",
obj: &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "foo",
},
},
fragment: "{ \"apiVersion\": \"v1beta1\", \"id\": \"baz\", \"desiredState\": { \"host\": \"bar\" } }",
fragment: `{ "apiVersion": "v1beta1", "id": "baz", "desiredState": { "host": "bar" } }`,
expected: &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "baz",
@@ -71,12 +74,13 @@ func TestMerge(t *testing.T) {
},
},
{
kind: "Pod",
obj: &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "foo",
},
},
fragment: "{ \"apiVersion\": \"v1beta3\", \"spec\": { \"volumes\": [ {\"name\": \"v1\"}, {\"name\": \"v2\"} ] } }",
fragment: `{ "apiVersion": "v1beta3", "spec": { "volumes": [ {"name": "v1"}, {"name": "v2"} ] } }`,
expected: &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "foo",
@@ -100,24 +104,81 @@ func TestMerge(t *testing.T) {
},
},
{
kind: "Pod",
obj: &api.Pod{},
fragment: "invalid json",
expected: &api.Pod{},
expectErr: true,
},
{
kind: "Pod",
obj: &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "foo",
},
},
fragment: `{ "apiVersion": "v1beta1", "id": null}`,
expected: &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "",
},
Spec: api.PodSpec{
RestartPolicy: api.RestartPolicy{
Always: &api.RestartPolicyAlways{},
},
DNSPolicy: api.DNSClusterFirst,
},
},
},
{
kind: "Service",
obj: &api.Service{
Spec: api.ServiceSpec{
Port: 10,
},
},
fragment: `{ "apiVersion": "v1beta1", "port": 0 }`,
expected: &api.Service{
Spec: api.ServiceSpec{
Port: 0,
Protocol: "TCP",
SessionAffinity: "None",
},
},
},
{
kind: "Service",
obj: &api.Service{
Spec: api.ServiceSpec{
Selector: map[string]string{
"version": "v1",
},
},
},
fragment: `{ "apiVersion": "v1beta1", "selector": { "version": "v2" } }`,
expected: &api.Service{
Spec: api.ServiceSpec{
Protocol: "TCP",
SessionAffinity: "None",
Selector: map[string]string{
"version": "v2",
},
},
},
},
}
for i, test := range tests {
err := Merge(test.obj, test.fragment, "Pod")
out, err := Merge(test.obj, test.fragment, test.kind)
if !test.expectErr {
if err != nil {
t.Errorf("unexpected error: %v", err)
} else if !reflect.DeepEqual(test.obj, test.expected) {
t.Errorf("\n\ntestcase[%d]\nexpected:\n%v\nsaw:\n%v", i, test.expected, test.obj)
t.Errorf("testcase[%d], unexpected error: %v", i, err)
} else if !reflect.DeepEqual(out, test.expected) {
t.Errorf("\n\ntestcase[%d]\nexpected:\n%+v\nsaw:\n%+v", i, test.expected, out)
}
}
if test.expectErr && err == nil {
t.Errorf("unexpected non-error")
t.Errorf("testcase[%d], unexpected non-error", i)
}
}