2015-10-29 16:34:59 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-10-29 16:34:59 +00:00
|
|
|
|
|
|
|
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 deployment
|
|
|
|
|
|
|
|
import (
|
2016-02-09 16:53:43 +00:00
|
|
|
"reflect"
|
2015-10-29 16:34:59 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-01-17 03:38:19 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2017-08-15 23:31:59 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
2017-01-17 10:38:25 +00:00
|
|
|
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
2017-11-08 22:34:54 +00:00
|
|
|
api "k8s.io/kubernetes/pkg/apis/core"
|
2015-10-29 16:34:59 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
|
|
|
)
|
|
|
|
|
2017-08-15 23:31:59 +00:00
|
|
|
const (
|
|
|
|
fakeImageName = "fake-name"
|
|
|
|
fakeImage = "fakeimage"
|
|
|
|
deploymentName = "test-deployment"
|
|
|
|
namespace = "test-namespace"
|
|
|
|
)
|
|
|
|
|
2016-02-09 16:53:43 +00:00
|
|
|
func TestStatusUpdates(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
old runtime.Object
|
|
|
|
obj runtime.Object
|
|
|
|
expected runtime.Object
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
old: newDeployment(map[string]string{"test": "label"}, map[string]string{"test": "annotation"}),
|
|
|
|
obj: newDeployment(map[string]string{"test": "label", "sneaky": "label"}, map[string]string{"test": "annotation"}),
|
|
|
|
expected: newDeployment(map[string]string{"test": "label"}, map[string]string{"test": "annotation"}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
old: newDeployment(map[string]string{"test": "label"}, map[string]string{"test": "annotation"}),
|
|
|
|
obj: newDeployment(map[string]string{"test": "label"}, map[string]string{"test": "annotation", "sneaky": "annotation"}),
|
|
|
|
expected: newDeployment(map[string]string{"test": "label"}, map[string]string{"test": "annotation", "sneaky": "annotation"}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2017-01-02 14:07:36 +00:00
|
|
|
deploymentStatusStrategy{}.PrepareForUpdate(genericapirequest.NewContext(), test.obj, test.old)
|
2016-02-09 16:53:43 +00:00
|
|
|
if !reflect.DeepEqual(test.expected, test.obj) {
|
|
|
|
t.Errorf("Unexpected object mismatch! Expected:\n%#v\ngot:\n%#v", test.expected, test.obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDeployment(labels, annotations map[string]string) *extensions.Deployment {
|
|
|
|
return &extensions.Deployment{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-02-09 16:53:43 +00:00
|
|
|
Name: "test",
|
|
|
|
Labels: labels,
|
|
|
|
Annotations: annotations,
|
|
|
|
},
|
|
|
|
Spec: extensions.DeploymentSpec{
|
|
|
|
Replicas: 1,
|
|
|
|
Strategy: extensions.DeploymentStrategy{
|
|
|
|
Type: extensions.RecreateDeploymentStrategyType,
|
|
|
|
},
|
|
|
|
Template: api.PodTemplateSpec{
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Image: "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2017-08-15 23:31:59 +00:00
|
|
|
|
|
|
|
func TestSelectorImmutability(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
requestInfo genericapirequest.RequestInfo
|
|
|
|
oldSelectorLabels map[string]string
|
|
|
|
newSelectorLabels map[string]string
|
|
|
|
expectedErrorList field.ErrorList
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
genericapirequest.RequestInfo{
|
|
|
|
APIGroup: "apps",
|
|
|
|
APIVersion: "v1beta2",
|
|
|
|
Resource: "deployments",
|
|
|
|
},
|
|
|
|
map[string]string{"a": "b"},
|
|
|
|
map[string]string{"c": "d"},
|
|
|
|
field.ErrorList{
|
|
|
|
&field.Error{
|
|
|
|
Type: field.ErrorTypeInvalid,
|
|
|
|
Field: field.NewPath("spec").Child("selector").String(),
|
|
|
|
BadValue: &metav1.LabelSelector{
|
|
|
|
MatchLabels: map[string]string{"c": "d"},
|
|
|
|
MatchExpressions: []metav1.LabelSelectorRequirement{},
|
|
|
|
},
|
|
|
|
Detail: "field is immutable",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
genericapirequest.RequestInfo{
|
|
|
|
APIGroup: "apps",
|
|
|
|
APIVersion: "v1beta1",
|
|
|
|
Resource: "deployments",
|
|
|
|
},
|
|
|
|
map[string]string{"a": "b"},
|
|
|
|
map[string]string{"c": "d"},
|
|
|
|
field.ErrorList{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
genericapirequest.RequestInfo{
|
|
|
|
APIGroup: "extensions",
|
|
|
|
APIVersion: "v1beta1",
|
|
|
|
},
|
|
|
|
map[string]string{"a": "b"},
|
|
|
|
map[string]string{"c": "d"},
|
|
|
|
field.ErrorList{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
oldDeployment := newDeploymentWithSelectorLabels(test.oldSelectorLabels)
|
|
|
|
newDeployment := newDeploymentWithSelectorLabels(test.newSelectorLabels)
|
|
|
|
context := genericapirequest.NewContext()
|
|
|
|
context = genericapirequest.WithRequestInfo(context, &test.requestInfo)
|
|
|
|
errorList := deploymentStrategy{}.ValidateUpdate(context, newDeployment, oldDeployment)
|
|
|
|
if len(test.expectedErrorList) == 0 && len(errorList) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(test.expectedErrorList, errorList) {
|
|
|
|
t.Errorf("Unexpected error list, expected: %v, actual: %v", test.expectedErrorList, errorList)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDeploymentWithSelectorLabels(selectorLabels map[string]string) *extensions.Deployment {
|
|
|
|
return &extensions.Deployment{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: deploymentName,
|
|
|
|
Namespace: namespace,
|
|
|
|
ResourceVersion: "1",
|
|
|
|
},
|
|
|
|
Spec: extensions.DeploymentSpec{
|
|
|
|
Selector: &metav1.LabelSelector{
|
|
|
|
MatchLabels: selectorLabels,
|
|
|
|
MatchExpressions: []metav1.LabelSelectorRequirement{},
|
|
|
|
},
|
|
|
|
Strategy: extensions.DeploymentStrategy{
|
|
|
|
Type: extensions.RollingUpdateDeploymentStrategyType,
|
|
|
|
RollingUpdate: &extensions.RollingUpdateDeployment{
|
|
|
|
MaxSurge: intstr.FromInt(1),
|
|
|
|
MaxUnavailable: intstr.FromInt(1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Template: api.PodTemplateSpec{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Labels: selectorLabels,
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSDefault,
|
|
|
|
Containers: []api.Container{{Name: fakeImageName, Image: fakeImage, ImagePullPolicy: api.PullNever, TerminationMessagePolicy: api.TerminationMessageReadFile}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|