2015-08-27 17:17:41 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors 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-10-09 22:06:28 +00:00
|
|
|
package v1beta1
|
2015-08-27 17:17:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/v1"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2015-11-10 06:28:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/intstr"
|
2015-08-27 17:17:41 +00:00
|
|
|
)
|
|
|
|
|
2015-09-08 23:58:25 +00:00
|
|
|
func TestSetDefaultDaemonSet(t *testing.T) {
|
2015-08-27 17:17:41 +00:00
|
|
|
tests := []struct {
|
2015-09-08 23:58:25 +00:00
|
|
|
ds *DaemonSet
|
2015-08-27 17:17:41 +00:00
|
|
|
expectLabelsChange bool
|
|
|
|
}{
|
|
|
|
{
|
2015-09-08 23:58:25 +00:00
|
|
|
ds: &DaemonSet{
|
|
|
|
Spec: DaemonSetSpec{
|
2015-08-27 17:17:41 +00:00
|
|
|
Template: &v1.PodTemplateSpec{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectLabelsChange: true,
|
|
|
|
},
|
|
|
|
{
|
2015-09-08 23:58:25 +00:00
|
|
|
ds: &DaemonSet{
|
2015-08-27 17:17:41 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"bar": "foo",
|
|
|
|
},
|
|
|
|
},
|
2015-09-08 23:58:25 +00:00
|
|
|
Spec: DaemonSetSpec{
|
2015-08-27 17:17:41 +00:00
|
|
|
Template: &v1.PodTemplateSpec{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectLabelsChange: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2015-09-08 23:58:25 +00:00
|
|
|
ds := test.ds
|
|
|
|
obj2 := roundTrip(t, runtime.Object(ds))
|
|
|
|
ds2, ok := obj2.(*DaemonSet)
|
2015-08-27 17:17:41 +00:00
|
|
|
if !ok {
|
2015-09-08 23:58:25 +00:00
|
|
|
t.Errorf("unexpected object: %v", ds2)
|
2015-08-27 17:17:41 +00:00
|
|
|
t.FailNow()
|
|
|
|
}
|
2015-09-08 23:58:25 +00:00
|
|
|
if test.expectLabelsChange != reflect.DeepEqual(ds2.Labels, ds2.Spec.Template.Labels) {
|
2015-08-27 17:17:41 +00:00
|
|
|
if test.expectLabelsChange {
|
2015-09-08 23:58:25 +00:00
|
|
|
t.Errorf("expected: %v, got: %v", ds2.Spec.Template.Labels, ds2.Labels)
|
2015-08-27 17:17:41 +00:00
|
|
|
} else {
|
2015-09-08 23:58:25 +00:00
|
|
|
t.Errorf("unexpected equality: %v", ds.Labels)
|
2015-08-27 17:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 09:48:53 +00:00
|
|
|
func TestSetDefaultDeployment(t *testing.T) {
|
2015-11-10 06:28:45 +00:00
|
|
|
defaultIntOrString := intstr.FromInt(1)
|
|
|
|
differentIntOrString := intstr.FromInt(5)
|
2015-09-01 09:48:53 +00:00
|
|
|
deploymentLabelKey := "deployment.kubernetes.io/podTemplateHash"
|
2015-10-26 23:20:43 +00:00
|
|
|
period := int64(v1.DefaultTerminationGracePeriodSeconds)
|
|
|
|
defaultTemplate := v1.PodTemplateSpec{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
DNSPolicy: v1.DNSClusterFirst,
|
|
|
|
RestartPolicy: v1.RestartPolicyAlways,
|
|
|
|
SecurityContext: &v1.PodSecurityContext{},
|
|
|
|
TerminationGracePeriodSeconds: &period,
|
|
|
|
},
|
|
|
|
}
|
2015-09-01 09:48:53 +00:00
|
|
|
tests := []struct {
|
|
|
|
original *Deployment
|
|
|
|
expected *Deployment
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
original: &Deployment{},
|
|
|
|
expected: &Deployment{
|
|
|
|
Spec: DeploymentSpec{
|
2015-11-18 23:28:06 +00:00
|
|
|
Replicas: newInt32(1),
|
2015-09-01 09:48:53 +00:00
|
|
|
Strategy: DeploymentStrategy{
|
2015-09-15 21:46:41 +00:00
|
|
|
Type: RollingUpdateDeploymentStrategyType,
|
2015-09-01 09:48:53 +00:00
|
|
|
RollingUpdate: &RollingUpdateDeployment{
|
|
|
|
MaxSurge: &defaultIntOrString,
|
|
|
|
MaxUnavailable: &defaultIntOrString,
|
|
|
|
},
|
|
|
|
},
|
2015-10-26 23:20:43 +00:00
|
|
|
Template: defaultTemplate,
|
2015-09-01 09:48:53 +00:00
|
|
|
UniqueLabelKey: newString(deploymentLabelKey),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
original: &Deployment{
|
|
|
|
Spec: DeploymentSpec{
|
2015-11-18 23:28:06 +00:00
|
|
|
Replicas: newInt32(5),
|
2015-09-01 09:48:53 +00:00
|
|
|
Strategy: DeploymentStrategy{
|
|
|
|
RollingUpdate: &RollingUpdateDeployment{
|
|
|
|
MaxSurge: &differentIntOrString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: &Deployment{
|
|
|
|
Spec: DeploymentSpec{
|
2015-11-18 23:28:06 +00:00
|
|
|
Replicas: newInt32(5),
|
2015-09-01 09:48:53 +00:00
|
|
|
Strategy: DeploymentStrategy{
|
2015-09-15 21:46:41 +00:00
|
|
|
Type: RollingUpdateDeploymentStrategyType,
|
2015-09-01 09:48:53 +00:00
|
|
|
RollingUpdate: &RollingUpdateDeployment{
|
|
|
|
MaxSurge: &differentIntOrString,
|
|
|
|
MaxUnavailable: &defaultIntOrString,
|
|
|
|
},
|
|
|
|
},
|
2015-10-26 23:20:43 +00:00
|
|
|
Template: defaultTemplate,
|
2015-09-01 09:48:53 +00:00
|
|
|
UniqueLabelKey: newString(deploymentLabelKey),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
original: &Deployment{
|
|
|
|
Spec: DeploymentSpec{
|
2015-11-18 23:28:06 +00:00
|
|
|
Replicas: newInt32(5),
|
2015-09-01 09:48:53 +00:00
|
|
|
Strategy: DeploymentStrategy{
|
2015-09-15 21:46:41 +00:00
|
|
|
Type: RecreateDeploymentStrategyType,
|
2015-09-01 09:48:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: &Deployment{
|
|
|
|
Spec: DeploymentSpec{
|
2015-11-18 23:28:06 +00:00
|
|
|
Replicas: newInt32(5),
|
2015-09-01 09:48:53 +00:00
|
|
|
Strategy: DeploymentStrategy{
|
2015-09-15 21:46:41 +00:00
|
|
|
Type: RecreateDeploymentStrategyType,
|
2015-09-01 09:48:53 +00:00
|
|
|
},
|
2015-10-26 23:20:43 +00:00
|
|
|
Template: defaultTemplate,
|
2015-09-01 09:48:53 +00:00
|
|
|
UniqueLabelKey: newString(deploymentLabelKey),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
original: &Deployment{
|
|
|
|
Spec: DeploymentSpec{
|
2015-11-18 23:28:06 +00:00
|
|
|
Replicas: newInt32(5),
|
2015-09-01 09:48:53 +00:00
|
|
|
Strategy: DeploymentStrategy{
|
2015-09-15 21:46:41 +00:00
|
|
|
Type: RecreateDeploymentStrategyType,
|
2015-09-01 09:48:53 +00:00
|
|
|
},
|
|
|
|
UniqueLabelKey: newString("customDeploymentKey"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: &Deployment{
|
|
|
|
Spec: DeploymentSpec{
|
2015-11-18 23:28:06 +00:00
|
|
|
Replicas: newInt32(5),
|
2015-09-01 09:48:53 +00:00
|
|
|
Strategy: DeploymentStrategy{
|
2015-09-15 21:46:41 +00:00
|
|
|
Type: RecreateDeploymentStrategyType,
|
2015-09-01 09:48:53 +00:00
|
|
|
},
|
2015-10-26 23:20:43 +00:00
|
|
|
Template: defaultTemplate,
|
2015-09-01 09:48:53 +00:00
|
|
|
UniqueLabelKey: newString("customDeploymentKey"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
original := test.original
|
|
|
|
expected := test.expected
|
|
|
|
obj2 := roundTrip(t, runtime.Object(original))
|
|
|
|
got, ok := obj2.(*Deployment)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("unexpected object: %v", got)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got.Spec, expected.Spec) {
|
2015-10-26 23:20:43 +00:00
|
|
|
t.Errorf("got different than expected:\n\t%+v\ngot:\n\t%+v", got.Spec, expected.Spec)
|
2015-09-01 09:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 12:19:35 +00:00
|
|
|
func TestSetDefaultJob(t *testing.T) {
|
|
|
|
expected := &Job{
|
|
|
|
Spec: JobSpec{
|
2015-12-01 09:24:21 +00:00
|
|
|
Selector: &LabelSelector{
|
2015-10-14 18:03:59 +00:00
|
|
|
MatchLabels: map[string]string{"job": "selector"},
|
|
|
|
},
|
2015-11-18 23:28:06 +00:00
|
|
|
Completions: newInt32(1),
|
|
|
|
Parallelism: newInt32(1),
|
2015-08-27 12:19:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
tests := []*Job{
|
2015-09-21 10:53:25 +00:00
|
|
|
// selector set explicitly, completions and parallelism - default
|
|
|
|
{
|
|
|
|
Spec: JobSpec{
|
2015-12-01 09:24:21 +00:00
|
|
|
Selector: &LabelSelector{
|
2015-10-14 18:03:59 +00:00
|
|
|
MatchLabels: map[string]string{"job": "selector"},
|
|
|
|
},
|
2015-09-21 10:53:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// selector from template labels, completions and parallelism - default
|
|
|
|
{
|
|
|
|
Spec: JobSpec{
|
2015-09-25 19:07:06 +00:00
|
|
|
Template: v1.PodTemplateSpec{
|
2015-09-21 10:53:25 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Labels: map[string]string{"job": "selector"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// selector from template labels, completions set explicitly, parallelism - default
|
2015-08-27 12:19:35 +00:00
|
|
|
{
|
|
|
|
Spec: JobSpec{
|
2015-11-18 23:28:06 +00:00
|
|
|
Completions: newInt32(1),
|
2015-09-25 19:07:06 +00:00
|
|
|
Template: v1.PodTemplateSpec{
|
2015-09-21 10:53:25 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Labels: map[string]string{"job": "selector"},
|
|
|
|
},
|
|
|
|
},
|
2015-08-27 12:19:35 +00:00
|
|
|
},
|
|
|
|
},
|
2015-09-21 10:53:25 +00:00
|
|
|
// selector from template labels, completions - default, parallelism set explicitly
|
2015-08-27 12:19:35 +00:00
|
|
|
{
|
|
|
|
Spec: JobSpec{
|
2015-11-18 23:28:06 +00:00
|
|
|
Parallelism: newInt32(1),
|
2015-09-25 19:07:06 +00:00
|
|
|
Template: v1.PodTemplateSpec{
|
2015-09-21 10:53:25 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Labels: map[string]string{"job": "selector"},
|
|
|
|
},
|
|
|
|
},
|
2015-08-27 12:19:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, original := range tests {
|
|
|
|
obj2 := roundTrip(t, runtime.Object(original))
|
|
|
|
got, ok := obj2.(*Job)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("unexpected object: %v", got)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if *got.Spec.Completions != *expected.Spec.Completions {
|
|
|
|
t.Errorf("got different completions than expected: %d %d", *got.Spec.Completions, *expected.Spec.Completions)
|
|
|
|
}
|
|
|
|
if *got.Spec.Parallelism != *expected.Spec.Parallelism {
|
|
|
|
t.Errorf("got different parallelism than expected: %d %d", *got.Spec.Parallelism, *expected.Spec.Parallelism)
|
|
|
|
}
|
2015-09-21 10:53:25 +00:00
|
|
|
if !reflect.DeepEqual(got.Spec.Selector, expected.Spec.Selector) {
|
|
|
|
t.Errorf("got different selectors %#v %#v", got.Spec.Selector, expected.Spec.Selector)
|
|
|
|
}
|
2015-08-27 12:19:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 17:17:41 +00:00
|
|
|
func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
|
2015-09-17 05:15:05 +00:00
|
|
|
data, err := Codec.Encode(obj)
|
2015-08-27 17:17:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v\n %#v", err, obj)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
obj2, err := api.Codec.Decode(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
|
|
|
|
err = api.Scheme.Convert(obj2, obj3)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v\nSource: %#v", err, obj2)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return obj3
|
|
|
|
}
|
2015-09-01 09:48:53 +00:00
|
|
|
|
2015-11-18 23:28:06 +00:00
|
|
|
func newInt32(val int32) *int32 {
|
2015-11-18 18:24:54 +00:00
|
|
|
p := new(int32)
|
2015-11-18 23:28:06 +00:00
|
|
|
*p = val
|
2015-09-01 09:48:53 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func newString(val string) *string {
|
|
|
|
p := new(string)
|
|
|
|
*p = val
|
|
|
|
return p
|
|
|
|
}
|