195 lines
5.4 KiB
Go
195 lines
5.4 KiB
Go
/*
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
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 v1_test
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
"k8s.io/api/core/v1"
|
|
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
"k8s.io/kubernetes/pkg/api"
|
|
_ "k8s.io/kubernetes/pkg/api/install"
|
|
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
|
_ "k8s.io/kubernetes/pkg/apis/apps/install"
|
|
. "k8s.io/kubernetes/pkg/apis/apps/v1"
|
|
)
|
|
|
|
func TestSetDefaultDaemonSetSpec(t *testing.T) {
|
|
defaultLabels := map[string]string{"foo": "bar"}
|
|
maxUnavailable := intstr.FromInt(1)
|
|
period := int64(v1.DefaultTerminationGracePeriodSeconds)
|
|
defaultTemplate := v1.PodTemplateSpec{
|
|
Spec: v1.PodSpec{
|
|
DNSPolicy: v1.DNSClusterFirst,
|
|
RestartPolicy: v1.RestartPolicyAlways,
|
|
SecurityContext: &v1.PodSecurityContext{},
|
|
TerminationGracePeriodSeconds: &period,
|
|
SchedulerName: api.DefaultSchedulerName,
|
|
},
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Labels: defaultLabels,
|
|
},
|
|
}
|
|
templateNoLabel := v1.PodTemplateSpec{
|
|
Spec: v1.PodSpec{
|
|
DNSPolicy: v1.DNSClusterFirst,
|
|
RestartPolicy: v1.RestartPolicyAlways,
|
|
SecurityContext: &v1.PodSecurityContext{},
|
|
TerminationGracePeriodSeconds: &period,
|
|
SchedulerName: api.DefaultSchedulerName,
|
|
},
|
|
}
|
|
tests := []struct {
|
|
original *appsv1.DaemonSet
|
|
expected *appsv1.DaemonSet
|
|
}{
|
|
{ // Labels change/defaulting test.
|
|
original: &appsv1.DaemonSet{
|
|
Spec: appsv1.DaemonSetSpec{
|
|
Template: defaultTemplate,
|
|
},
|
|
},
|
|
expected: &appsv1.DaemonSet{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Labels: defaultLabels,
|
|
},
|
|
Spec: appsv1.DaemonSetSpec{
|
|
Template: defaultTemplate,
|
|
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
|
|
Type: appsv1.RollingUpdateDaemonSetStrategyType,
|
|
RollingUpdate: &appsv1.RollingUpdateDaemonSet{
|
|
MaxUnavailable: &maxUnavailable,
|
|
},
|
|
},
|
|
RevisionHistoryLimit: newInt32(10),
|
|
},
|
|
},
|
|
},
|
|
{ // Labels change/defaulting test.
|
|
original: &appsv1.DaemonSet{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Labels: map[string]string{
|
|
"bar": "foo",
|
|
},
|
|
},
|
|
Spec: appsv1.DaemonSetSpec{
|
|
Template: defaultTemplate,
|
|
RevisionHistoryLimit: newInt32(1),
|
|
},
|
|
},
|
|
expected: &appsv1.DaemonSet{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Labels: map[string]string{
|
|
"bar": "foo",
|
|
},
|
|
},
|
|
Spec: appsv1.DaemonSetSpec{
|
|
Template: defaultTemplate,
|
|
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
|
|
Type: appsv1.RollingUpdateDaemonSetStrategyType,
|
|
RollingUpdate: &appsv1.RollingUpdateDaemonSet{
|
|
MaxUnavailable: &maxUnavailable,
|
|
},
|
|
},
|
|
RevisionHistoryLimit: newInt32(1),
|
|
},
|
|
},
|
|
},
|
|
{ // OnDeleteDaemonSetStrategyType update strategy.
|
|
original: &appsv1.DaemonSet{
|
|
Spec: appsv1.DaemonSetSpec{
|
|
Template: templateNoLabel,
|
|
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
|
|
Type: appsv1.OnDeleteDaemonSetStrategyType,
|
|
},
|
|
},
|
|
},
|
|
expected: &appsv1.DaemonSet{
|
|
Spec: appsv1.DaemonSetSpec{
|
|
Template: templateNoLabel,
|
|
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
|
|
Type: appsv1.OnDeleteDaemonSetStrategyType,
|
|
},
|
|
RevisionHistoryLimit: newInt32(10),
|
|
},
|
|
},
|
|
},
|
|
{ // Custom unique label key.
|
|
original: &appsv1.DaemonSet{
|
|
Spec: appsv1.DaemonSetSpec{},
|
|
},
|
|
expected: &appsv1.DaemonSet{
|
|
Spec: appsv1.DaemonSetSpec{
|
|
Template: templateNoLabel,
|
|
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
|
|
Type: appsv1.RollingUpdateDaemonSetStrategyType,
|
|
RollingUpdate: &appsv1.RollingUpdateDaemonSet{
|
|
MaxUnavailable: &maxUnavailable,
|
|
},
|
|
},
|
|
RevisionHistoryLimit: newInt32(10),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for i, test := range tests {
|
|
original := test.original
|
|
expected := test.expected
|
|
obj2 := roundTrip(t, runtime.Object(original))
|
|
got, ok := obj2.(*appsv1.DaemonSet)
|
|
if !ok {
|
|
t.Errorf("(%d) unexpected object: %v", i, got)
|
|
t.FailNow()
|
|
}
|
|
if !apiequality.Semantic.DeepEqual(got.Spec, expected.Spec) {
|
|
t.Errorf("(%d) got different than expected\ngot:\n\t%+v\nexpected:\n\t%+v", i, got.Spec, expected.Spec)
|
|
}
|
|
}
|
|
}
|
|
|
|
func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
|
|
data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj)
|
|
if err != nil {
|
|
t.Errorf("%v\n %#v", err, obj)
|
|
return nil
|
|
}
|
|
obj2, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), 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 = legacyscheme.Scheme.Convert(obj2, obj3, nil)
|
|
if err != nil {
|
|
t.Errorf("%v\nSource: %#v", err, obj2)
|
|
return nil
|
|
}
|
|
return obj3
|
|
}
|
|
|
|
func newInt32(val int32) *int32 {
|
|
p := new(int32)
|
|
*p = val
|
|
return p
|
|
}
|