2015-01-15 03:29:13 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-02-05 00:14:48 +00:00
|
|
|
package util
|
2015-01-15 03:29:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMerge(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
obj runtime.Object
|
|
|
|
fragment string
|
|
|
|
expected runtime.Object
|
|
|
|
expectErr bool
|
2015-01-31 18:59:08 +00:00
|
|
|
kind string
|
2015-01-15 03:29:13 +00:00
|
|
|
}{
|
|
|
|
{
|
2015-01-31 18:59:08 +00:00
|
|
|
kind: "Pod",
|
2015-01-15 03:29:13 +00:00
|
|
|
obj: &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
2015-01-31 18:59:08 +00:00
|
|
|
fragment: `{ "apiVersion": "v1beta1" }`,
|
2015-01-15 03:29:13 +00:00
|
|
|
expected: &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
2015-01-26 17:52:50 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
RestartPolicy: api.RestartPolicy{
|
|
|
|
Always: &api.RestartPolicyAlways{},
|
|
|
|
},
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
|
|
},
|
2015-01-15 03:29:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-01-31 18:59:08 +00:00
|
|
|
kind: "Pod",
|
2015-01-15 03:29:13 +00:00
|
|
|
obj: &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
2015-01-31 18:59:08 +00:00
|
|
|
fragment: `{ "apiVersion": "v1beta1", "id": "baz", "desiredState": { "host": "bar" } }`,
|
2015-01-15 03:29:13 +00:00
|
|
|
expected: &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Host: "bar",
|
2015-01-26 17:52:50 +00:00
|
|
|
RestartPolicy: api.RestartPolicy{
|
|
|
|
Always: &api.RestartPolicyAlways{},
|
|
|
|
},
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2015-01-15 03:29:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-01-31 18:59:08 +00:00
|
|
|
kind: "Pod",
|
2015-01-15 03:29:13 +00:00
|
|
|
obj: &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
2015-01-31 18:59:08 +00:00
|
|
|
fragment: `{ "apiVersion": "v1beta3", "spec": { "volumes": [ {"name": "v1"}, {"name": "v2"} ] } }`,
|
2015-01-15 03:29:13 +00:00
|
|
|
expected: &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Volumes: []api.Volume{
|
|
|
|
{
|
2015-01-26 17:52:50 +00:00
|
|
|
Name: "v1",
|
2015-02-20 06:27:27 +00:00
|
|
|
Source: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}},
|
2015-01-15 03:29:13 +00:00
|
|
|
},
|
|
|
|
{
|
2015-01-26 17:52:50 +00:00
|
|
|
Name: "v2",
|
2015-02-20 06:27:27 +00:00
|
|
|
Source: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}},
|
2015-01-15 03:29:13 +00:00
|
|
|
},
|
|
|
|
},
|
2015-01-26 17:52:50 +00:00
|
|
|
RestartPolicy: api.RestartPolicy{
|
|
|
|
Always: &api.RestartPolicyAlways{},
|
|
|
|
},
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2015-01-15 03:29:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-01-31 18:59:08 +00:00
|
|
|
kind: "Pod",
|
2015-01-15 03:29:13 +00:00
|
|
|
obj: &api.Pod{},
|
|
|
|
fragment: "invalid json",
|
|
|
|
expected: &api.Pod{},
|
|
|
|
expectErr: true,
|
|
|
|
},
|
2015-01-31 18:59:08 +00:00
|
|
|
{
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-02-25 18:06:18 +00:00
|
|
|
{
|
|
|
|
kind: "Service",
|
|
|
|
obj: &api.Service{},
|
|
|
|
fragment: `{ "apiVersion": "badVersion" }`,
|
|
|
|
expectErr: true,
|
|
|
|
},
|
2015-01-31 18:59:08 +00:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-01-15 03:29:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-26 17:52:50 +00:00
|
|
|
for i, test := range tests {
|
2015-01-31 18:59:08 +00:00
|
|
|
out, err := Merge(test.obj, test.fragment, test.kind)
|
2015-01-15 03:29:13 +00:00
|
|
|
if !test.expectErr {
|
|
|
|
if err != nil {
|
2015-01-31 18:59:08 +00:00
|
|
|
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)
|
2015-01-15 03:29:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if test.expectErr && err == nil {
|
2015-01-31 18:59:08 +00:00
|
|
|
t.Errorf("testcase[%d], unexpected non-error", i)
|
2015-01-15 03:29:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|