From 4bce52a5f6ec0073e83deb13b6dab615aedc8d7b Mon Sep 17 00:00:00 2001 From: Yu-Ju Hong Date: Wed, 11 Feb 2015 16:07:17 -0800 Subject: [PATCH] Add defaulting tests for API v1beta2 and v1beta3 This commit also add more tests in v1beta1/defaults_test.go --- pkg/api/v1beta1/defaults_test.go | 31 ++++++++++- pkg/api/v1beta2/defaults_test.go | 94 ++++++++++++++++++++++++++++++++ pkg/api/v1beta3/defaults_test.go | 94 ++++++++++++++++++++++++++++++++ 3 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 pkg/api/v1beta2/defaults_test.go create mode 100644 pkg/api/v1beta3/defaults_test.go diff --git a/pkg/api/v1beta1/defaults_test.go b/pkg/api/v1beta1/defaults_test.go index 26eae9dac67..9115978d62f 100644 --- a/pkg/api/v1beta1/defaults_test.go +++ b/pkg/api/v1beta1/defaults_test.go @@ -53,6 +53,8 @@ func TestSetDefaultService(t *testing.T) { func TestSetDefaulPodSpec(t *testing.T) { bp := ¤t.BoundPod{} + bp.Spec.Volumes = []current.Volume{{}} + obj2 := roundTrip(t, runtime.Object(bp)) bp2 := obj2.(*current.BoundPod) if bp2.Spec.DNSPolicy != current.DNSClusterFirst { @@ -60,6 +62,33 @@ func TestSetDefaulPodSpec(t *testing.T) { } policy := bp2.Spec.RestartPolicy if policy.Never != nil || policy.OnFailure != nil || policy.Always == nil { - t.Errorf("Expected only policy.Aways is set, got: %s", policy) + t.Errorf("Expected only policy.Always is set, got: %s", policy) + } + vsource := bp2.Spec.Volumes[0].Source + if vsource.EmptyDir == nil { + t.Errorf("Expected non-empty volume is set, got: %s", vsource.EmptyDir) + } +} + +func TestSetDefaultContainer(t *testing.T) { + bp := ¤t.BoundPod{} + bp.Spec.Containers = []current.Container{{}} + bp.Spec.Containers[0].Ports = []current.Port{{}} + + obj2 := roundTrip(t, runtime.Object(bp)) + bp2 := obj2.(*current.BoundPod) + + container := bp2.Spec.Containers[0] + if container.TerminationMessagePath != current.TerminationMessagePathDefault { + t.Errorf("Expected termination message path: %s, got: %s", + current.TerminationMessagePathDefault, container.TerminationMessagePath) + } + if container.ImagePullPolicy != current.PullIfNotPresent { + t.Errorf("Expected image pull policy: %s, got: %s", + current.PullIfNotPresent, container.ImagePullPolicy) + } + if container.Ports[0].Protocol != current.ProtocolTCP { + t.Errorf("Expected protocol: %s, got: %s", + current.ProtocolTCP, container.Ports[0].Protocol) } } diff --git a/pkg/api/v1beta2/defaults_test.go b/pkg/api/v1beta2/defaults_test.go new file mode 100644 index 00000000000..5808860ae6e --- /dev/null +++ b/pkg/api/v1beta2/defaults_test.go @@ -0,0 +1,94 @@ +/* +Copyright 2015 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. +*/ + +package v1beta2_test + +import ( + "reflect" + "testing" + + current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta2" + "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" +) + +func roundTrip(t *testing.T, obj runtime.Object) runtime.Object { + data, err := current.Codec.Encode(obj) + if err != nil { + t.Errorf("%v\n %#v", err, obj) + return nil + } + obj2 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object) + err = current.Codec.DecodeInto(data, obj2) + if err != nil { + t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj) + return nil + } + return obj2 +} + +func TestSetDefaultService(t *testing.T) { + svc := ¤t.Service{} + obj2 := roundTrip(t, runtime.Object(svc)) + svc2 := obj2.(*current.Service) + if svc2.Protocol != current.ProtocolTCP { + t.Errorf("Expected default protocol :%s, got: %s", current.ProtocolTCP, svc2.Protocol) + } + if svc2.SessionAffinity != current.AffinityTypeNone { + t.Errorf("Expected default sesseion affinity type:%s, got: %s", current.AffinityTypeNone, svc2.SessionAffinity) + } +} + +func TestSetDefaulPodSpec(t *testing.T) { + bp := ¤t.BoundPod{} + bp.Spec.Volumes = []current.Volume{{}} + + obj2 := roundTrip(t, runtime.Object(bp)) + bp2 := obj2.(*current.BoundPod) + if bp2.Spec.DNSPolicy != current.DNSClusterFirst { + t.Errorf("Expected default dns policy :%s, got: %s", current.DNSClusterFirst, bp2.Spec.DNSPolicy) + } + policy := bp2.Spec.RestartPolicy + if policy.Never != nil || policy.OnFailure != nil || policy.Always == nil { + t.Errorf("Expected only policy.Always is set, got: %s", policy) + } + vsource := bp2.Spec.Volumes[0].Source + if vsource.EmptyDir == nil { + t.Errorf("Expected non-empty volume is set, got: %s", vsource.EmptyDir) + } +} + +func TestSetDefaultContainer(t *testing.T) { + bp := ¤t.BoundPod{} + bp.Spec.Containers = []current.Container{{}} + bp.Spec.Containers[0].Ports = []current.Port{{}} + + obj2 := roundTrip(t, runtime.Object(bp)) + bp2 := obj2.(*current.BoundPod) + + container := bp2.Spec.Containers[0] + if container.TerminationMessagePath != current.TerminationMessagePathDefault { + t.Errorf("Expected termination message path: %s, got: %s", + current.TerminationMessagePathDefault, container.TerminationMessagePath) + } + if container.ImagePullPolicy != current.PullIfNotPresent { + t.Errorf("Expected image pull policy: %s, got: %s", + current.PullIfNotPresent, container.ImagePullPolicy) + } + if container.Ports[0].Protocol != current.ProtocolTCP { + t.Errorf("Expected protocol: %s, got: %s", + current.ProtocolTCP, container.Ports[0].Protocol) + } +} diff --git a/pkg/api/v1beta3/defaults_test.go b/pkg/api/v1beta3/defaults_test.go new file mode 100644 index 00000000000..49152fedd85 --- /dev/null +++ b/pkg/api/v1beta3/defaults_test.go @@ -0,0 +1,94 @@ +/* +Copyright 2015 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. +*/ + +package v1beta3_test + +import ( + "reflect" + "testing" + + current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta3" + "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" +) + +func roundTrip(t *testing.T, obj runtime.Object) runtime.Object { + data, err := current.Codec.Encode(obj) + if err != nil { + t.Errorf("%v\n %#v", err, obj) + return nil + } + obj2 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object) + err = current.Codec.DecodeInto(data, obj2) + if err != nil { + t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj) + return nil + } + return obj2 +} + +func TestSetDefaultService(t *testing.T) { + svc := ¤t.Service{} + obj2 := roundTrip(t, runtime.Object(svc)) + svc2 := obj2.(*current.Service) + if svc2.Spec.Protocol != current.ProtocolTCP { + t.Errorf("Expected default protocol :%s, got: %s", current.ProtocolTCP, svc2.Spec.Protocol) + } + if svc2.Spec.SessionAffinity != current.AffinityTypeNone { + t.Errorf("Expected default sesseion affinity type:%s, got: %s", current.AffinityTypeNone, svc2.Spec.SessionAffinity) + } +} + +func TestSetDefaulPodSpec(t *testing.T) { + bp := ¤t.BoundPod{} + bp.Spec.Volumes = []current.Volume{{}} + + obj2 := roundTrip(t, runtime.Object(bp)) + bp2 := obj2.(*current.BoundPod) + if bp2.Spec.DNSPolicy != current.DNSClusterFirst { + t.Errorf("Expected default dns policy :%s, got: %s", current.DNSClusterFirst, bp2.Spec.DNSPolicy) + } + policy := bp2.Spec.RestartPolicy + if policy.Never != nil || policy.OnFailure != nil || policy.Always == nil { + t.Errorf("Expected only policy.Always is set, got: %s", policy) + } + vsource := bp2.Spec.Volumes[0].Source + if vsource.EmptyDir == nil { + t.Errorf("Expected non-empty volume is set, got: %s", vsource.EmptyDir) + } +} + +func TestSetDefaultContainer(t *testing.T) { + bp := ¤t.BoundPod{} + bp.Spec.Containers = []current.Container{{}} + bp.Spec.Containers[0].Ports = []current.Port{{}} + + obj2 := roundTrip(t, runtime.Object(bp)) + bp2 := obj2.(*current.BoundPod) + + container := bp2.Spec.Containers[0] + if container.TerminationMessagePath != current.TerminationMessagePathDefault { + t.Errorf("Expected termination message path: %s, got: %s", + current.TerminationMessagePathDefault, container.TerminationMessagePath) + } + if container.ImagePullPolicy != current.PullIfNotPresent { + t.Errorf("Expected image pull policy: %s, got: %s", + current.PullIfNotPresent, container.ImagePullPolicy) + } + if container.Ports[0].Protocol != current.ProtocolTCP { + t.Errorf("Expected protocol: %s, got: %s", + current.ProtocolTCP, container.Ports[0].Protocol) + } +}