2015-11-11 23:22:57 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-11-11 23:22:57 +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.
|
|
|
|
*/
|
|
|
|
|
2016-07-05 07:29:09 +00:00
|
|
|
package util
|
2015-11-11 23:22:57 +00:00
|
|
|
|
|
|
|
import (
|
2016-03-01 01:57:35 +00:00
|
|
|
"fmt"
|
2015-12-16 06:59:36 +00:00
|
|
|
"reflect"
|
2015-11-11 23:22:57 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-06-02 07:31:15 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2015-11-11 23:22:57 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-11-18 20:50:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/v1"
|
|
|
|
extensions "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
|
2016-12-14 01:18:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset/fake"
|
2016-02-11 18:57:42 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/testing/core"
|
2016-06-02 07:31:15 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/intstr"
|
2015-11-11 23:22:57 +00:00
|
|
|
)
|
|
|
|
|
2016-02-11 18:57:42 +00:00
|
|
|
func addListRSReactor(fakeClient *fake.Clientset, obj runtime.Object) *fake.Clientset {
|
|
|
|
fakeClient.AddReactor("list", "replicasets", func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
|
|
|
return true, obj, nil
|
|
|
|
})
|
|
|
|
return fakeClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func addListPodsReactor(fakeClient *fake.Clientset, obj runtime.Object) *fake.Clientset {
|
|
|
|
fakeClient.AddReactor("list", "pods", func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
|
|
|
return true, obj, nil
|
|
|
|
})
|
|
|
|
return fakeClient
|
|
|
|
}
|
|
|
|
|
2016-03-01 01:57:35 +00:00
|
|
|
func addGetRSReactor(fakeClient *fake.Clientset, obj runtime.Object) *fake.Clientset {
|
|
|
|
rsList, ok := obj.(*extensions.ReplicaSetList)
|
|
|
|
fakeClient.AddReactor("get", "replicasets", func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
2016-10-13 18:52:57 +00:00
|
|
|
name := action.(core.GetAction).GetName()
|
2016-03-01 01:57:35 +00:00
|
|
|
if ok {
|
|
|
|
for _, rs := range rsList.Items {
|
|
|
|
if rs.Name == name {
|
|
|
|
return true, &rs, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil, fmt.Errorf("could not find the requested replica set: %s", name)
|
|
|
|
|
|
|
|
})
|
|
|
|
return fakeClient
|
|
|
|
}
|
|
|
|
|
2016-02-11 18:57:42 +00:00
|
|
|
func addUpdateRSReactor(fakeClient *fake.Clientset) *fake.Clientset {
|
|
|
|
fakeClient.AddReactor("update", "replicasets", func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
2016-10-13 18:52:57 +00:00
|
|
|
obj := action.(core.UpdateAction).GetObject().(*extensions.ReplicaSet)
|
2016-02-11 18:57:42 +00:00
|
|
|
return true, obj, nil
|
|
|
|
})
|
|
|
|
return fakeClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func addUpdatePodsReactor(fakeClient *fake.Clientset) *fake.Clientset {
|
|
|
|
fakeClient.AddReactor("update", "pods", func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
2016-11-18 20:50:17 +00:00
|
|
|
obj := action.(core.UpdateAction).GetObject().(*v1.Pod)
|
2016-02-11 18:57:42 +00:00
|
|
|
return true, obj, nil
|
|
|
|
})
|
|
|
|
return fakeClient
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
func newPod(now time.Time, ready bool, beforeSec int) v1.Pod {
|
|
|
|
conditionStatus := v1.ConditionFalse
|
2015-11-11 23:22:57 +00:00
|
|
|
if ready {
|
2016-11-18 20:50:17 +00:00
|
|
|
conditionStatus = v1.ConditionTrue
|
2015-11-11 23:22:57 +00:00
|
|
|
}
|
2016-11-18 20:50:17 +00:00
|
|
|
return v1.Pod{
|
|
|
|
Status: v1.PodStatus{
|
|
|
|
Conditions: []v1.PodCondition{
|
2015-11-11 23:22:57 +00:00
|
|
|
{
|
2016-11-18 20:50:17 +00:00
|
|
|
Type: v1.PodReady,
|
2016-12-03 18:57:26 +00:00
|
|
|
LastTransitionTime: metav1.NewTime(now.Add(-1 * time.Duration(beforeSec) * time.Second)),
|
2015-11-11 23:22:57 +00:00
|
|
|
Status: conditionStatus,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:40:18 +00:00
|
|
|
// generatePodFromRS creates a pod, with the input ReplicaSet's selector and its template
|
2016-11-18 20:50:17 +00:00
|
|
|
func generatePodFromRS(rs extensions.ReplicaSet) v1.Pod {
|
|
|
|
return v1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-01-20 00:40:18 +00:00
|
|
|
Labels: rs.Labels,
|
2015-12-16 06:59:36 +00:00
|
|
|
},
|
2016-01-20 00:40:18 +00:00
|
|
|
Spec: rs.Spec.Template.Spec,
|
2015-12-16 06:59:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
func generatePod(labels map[string]string, image string) v1.Pod {
|
|
|
|
return v1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-12-16 06:59:36 +00:00
|
|
|
Labels: labels,
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2015-12-16 06:59:36 +00:00
|
|
|
{
|
|
|
|
Name: image,
|
|
|
|
Image: image,
|
2016-11-18 20:50:17 +00:00
|
|
|
ImagePullPolicy: v1.PullAlways,
|
|
|
|
TerminationMessagePath: v1.TerminationMessagePathDefault,
|
2015-12-16 06:59:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:40:18 +00:00
|
|
|
func generateRSWithLabel(labels map[string]string, image string) extensions.ReplicaSet {
|
|
|
|
return extensions.ReplicaSet{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-11-18 20:50:17 +00:00
|
|
|
Name: v1.SimpleNameGenerator.GenerateName("replicaset"),
|
2015-12-16 06:59:36 +00:00
|
|
|
Labels: labels,
|
|
|
|
},
|
2016-01-20 00:40:18 +00:00
|
|
|
Spec: extensions.ReplicaSetSpec{
|
2016-11-18 20:50:17 +00:00
|
|
|
Replicas: func(i int32) *int32 { return &i }(1),
|
2016-12-03 18:57:26 +00:00
|
|
|
Selector: &metav1.LabelSelector{MatchLabels: labels},
|
2016-11-18 20:50:17 +00:00
|
|
|
Template: v1.PodTemplateSpec{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-06-03 17:53:14 +00:00
|
|
|
Labels: labels,
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2015-12-16 06:59:36 +00:00
|
|
|
{
|
|
|
|
Name: image,
|
|
|
|
Image: image,
|
2016-11-18 20:50:17 +00:00
|
|
|
ImagePullPolicy: v1.PullAlways,
|
|
|
|
TerminationMessagePath: v1.TerminationMessagePathDefault,
|
2015-12-16 06:59:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:40:18 +00:00
|
|
|
// generateRS creates a replica set, with the input deployment's template as its template
|
|
|
|
func generateRS(deployment extensions.Deployment) extensions.ReplicaSet {
|
2016-02-28 02:13:32 +00:00
|
|
|
template := GetNewReplicaSetTemplate(&deployment)
|
2016-01-20 00:40:18 +00:00
|
|
|
return extensions.ReplicaSet{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-11-18 20:50:17 +00:00
|
|
|
Name: v1.SimpleNameGenerator.GenerateName("replicaset"),
|
2015-12-11 00:37:03 +00:00
|
|
|
Labels: template.Labels,
|
|
|
|
},
|
2016-01-20 00:40:18 +00:00
|
|
|
Spec: extensions.ReplicaSetSpec{
|
2016-11-18 20:50:17 +00:00
|
|
|
Replicas: func() *int32 { i := int32(0); return &i }(),
|
2016-03-09 21:11:13 +00:00
|
|
|
Template: template,
|
2016-12-03 18:57:26 +00:00
|
|
|
Selector: &metav1.LabelSelector{MatchLabels: template.Labels},
|
2015-12-11 00:37:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 06:59:36 +00:00
|
|
|
// generateDeployment creates a deployment, with the input image as its template
|
2015-12-11 00:37:03 +00:00
|
|
|
func generateDeployment(image string) extensions.Deployment {
|
|
|
|
podLabels := map[string]string{"name": image}
|
|
|
|
terminationSec := int64(30)
|
|
|
|
return extensions.Deployment{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-01-04 15:44:13 +00:00
|
|
|
Name: image,
|
|
|
|
Annotations: make(map[string]string),
|
2015-12-11 00:37:03 +00:00
|
|
|
},
|
|
|
|
Spec: extensions.DeploymentSpec{
|
2016-11-18 20:50:17 +00:00
|
|
|
Replicas: func(i int32) *int32 { return &i }(1),
|
2016-12-03 18:57:26 +00:00
|
|
|
Selector: &metav1.LabelSelector{MatchLabels: podLabels},
|
2016-11-18 20:50:17 +00:00
|
|
|
Template: v1.PodTemplateSpec{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-12-11 00:37:03 +00:00
|
|
|
Labels: podLabels,
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2015-12-11 00:37:03 +00:00
|
|
|
{
|
|
|
|
Name: image,
|
|
|
|
Image: image,
|
2016-11-18 20:50:17 +00:00
|
|
|
ImagePullPolicy: v1.PullAlways,
|
|
|
|
TerminationMessagePath: v1.TerminationMessagePathDefault,
|
2015-12-11 00:37:03 +00:00
|
|
|
},
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
DNSPolicy: v1.DNSClusterFirst,
|
2015-12-11 00:37:03 +00:00
|
|
|
TerminationGracePeriodSeconds: &terminationSec,
|
2016-11-18 20:50:17 +00:00
|
|
|
RestartPolicy: v1.RestartPolicyAlways,
|
|
|
|
SecurityContext: &v1.PodSecurityContext{},
|
2015-12-11 00:37:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetNewRC(t *testing.T) {
|
|
|
|
newDeployment := generateDeployment("nginx")
|
2016-01-20 00:40:18 +00:00
|
|
|
newRC := generateRS(newDeployment)
|
2015-12-11 00:37:03 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
test string
|
2016-02-22 18:33:59 +00:00
|
|
|
objs []runtime.Object
|
2016-01-20 00:40:18 +00:00
|
|
|
expected *extensions.ReplicaSet
|
2015-12-11 00:37:03 +00:00
|
|
|
}{
|
|
|
|
{
|
2016-01-20 00:40:18 +00:00
|
|
|
"No new ReplicaSet",
|
2016-02-22 18:33:59 +00:00
|
|
|
[]runtime.Object{
|
2016-11-18 20:50:17 +00:00
|
|
|
&v1.PodList{},
|
2016-02-22 18:33:59 +00:00
|
|
|
&extensions.ReplicaSetList{
|
|
|
|
Items: []extensions.ReplicaSet{
|
|
|
|
generateRS(generateDeployment("foo")),
|
|
|
|
generateRS(generateDeployment("bar")),
|
|
|
|
},
|
2015-12-11 00:37:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2016-01-20 00:40:18 +00:00
|
|
|
"Has new ReplicaSet",
|
2016-02-22 18:33:59 +00:00
|
|
|
[]runtime.Object{
|
2016-11-18 20:50:17 +00:00
|
|
|
&v1.PodList{},
|
2016-02-22 18:33:59 +00:00
|
|
|
&extensions.ReplicaSetList{
|
|
|
|
Items: []extensions.ReplicaSet{
|
|
|
|
generateRS(generateDeployment("foo")),
|
|
|
|
generateRS(generateDeployment("bar")),
|
|
|
|
generateRS(generateDeployment("abc")),
|
|
|
|
newRC,
|
|
|
|
generateRS(generateDeployment("xyz")),
|
|
|
|
},
|
2015-12-11 00:37:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&newRC,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-02-22 18:33:59 +00:00
|
|
|
fakeClient := &fake.Clientset{}
|
|
|
|
fakeClient = addListPodsReactor(fakeClient, test.objs[0])
|
|
|
|
fakeClient = addListRSReactor(fakeClient, test.objs[1])
|
|
|
|
fakeClient = addUpdatePodsReactor(fakeClient)
|
|
|
|
fakeClient = addUpdateRSReactor(fakeClient)
|
2016-02-28 02:13:32 +00:00
|
|
|
rs, err := GetNewReplicaSet(&newDeployment, fakeClient)
|
2015-12-11 00:37:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("In test case %s, got unexpected error %v", test.test, err)
|
|
|
|
}
|
2016-11-19 23:32:10 +00:00
|
|
|
if !api.Semantic.DeepEqual(rs, test.expected) {
|
2016-06-14 12:04:38 +00:00
|
|
|
t.Errorf("In test case %s, expected %#v, got %#v", test.test, test.expected, rs)
|
2015-12-11 00:37:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-16 06:59:36 +00:00
|
|
|
|
|
|
|
func TestGetOldRCs(t *testing.T) {
|
|
|
|
newDeployment := generateDeployment("nginx")
|
2016-01-20 00:40:18 +00:00
|
|
|
newRS := generateRS(newDeployment)
|
2016-11-18 20:50:17 +00:00
|
|
|
newRS.Status.FullyLabeledReplicas = *(newRS.Spec.Replicas)
|
2016-01-20 00:40:18 +00:00
|
|
|
newPod := generatePodFromRS(newRS)
|
2015-12-16 06:59:36 +00:00
|
|
|
|
2016-01-20 00:40:18 +00:00
|
|
|
// create 2 old deployments and related replica sets/pods, with the same labels but different template
|
2015-12-16 06:59:36 +00:00
|
|
|
oldDeployment := generateDeployment("nginx")
|
|
|
|
oldDeployment.Spec.Template.Spec.Containers[0].Name = "nginx-old-1"
|
2016-01-20 00:40:18 +00:00
|
|
|
oldRS := generateRS(oldDeployment)
|
2016-11-18 20:50:17 +00:00
|
|
|
oldRS.Status.FullyLabeledReplicas = *(oldRS.Spec.Replicas)
|
2016-01-20 00:40:18 +00:00
|
|
|
oldPod := generatePodFromRS(oldRS)
|
2015-12-16 06:59:36 +00:00
|
|
|
oldDeployment2 := generateDeployment("nginx")
|
|
|
|
oldDeployment2.Spec.Template.Spec.Containers[0].Name = "nginx-old-2"
|
2016-01-20 00:40:18 +00:00
|
|
|
oldRS2 := generateRS(oldDeployment2)
|
2016-11-18 20:50:17 +00:00
|
|
|
oldRS2.Status.FullyLabeledReplicas = *(oldRS2.Spec.Replicas)
|
2016-01-20 00:40:18 +00:00
|
|
|
oldPod2 := generatePodFromRS(oldRS2)
|
2015-12-16 06:59:36 +00:00
|
|
|
|
2016-01-20 00:40:18 +00:00
|
|
|
// create 1 ReplicaSet that existed before the deployment, with the same labels as the deployment
|
|
|
|
existedPod := generatePod(newDeployment.Spec.Template.Labels, "foo")
|
|
|
|
existedRS := generateRSWithLabel(newDeployment.Spec.Template.Labels, "foo")
|
2016-11-18 20:50:17 +00:00
|
|
|
existedRS.Status.FullyLabeledReplicas = *(existedRS.Spec.Replicas)
|
2015-12-16 06:59:36 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
test string
|
|
|
|
objs []runtime.Object
|
2016-01-20 00:40:18 +00:00
|
|
|
expected []*extensions.ReplicaSet
|
2015-12-16 06:59:36 +00:00
|
|
|
}{
|
|
|
|
{
|
2016-01-20 00:40:18 +00:00
|
|
|
"No old ReplicaSets",
|
2015-12-16 06:59:36 +00:00
|
|
|
[]runtime.Object{
|
2016-11-18 20:50:17 +00:00
|
|
|
&v1.PodList{
|
|
|
|
Items: []v1.Pod{
|
2016-01-20 00:40:18 +00:00
|
|
|
generatePod(newDeployment.Spec.Template.Labels, "foo"),
|
|
|
|
generatePod(newDeployment.Spec.Template.Labels, "bar"),
|
2015-12-16 06:59:36 +00:00
|
|
|
newPod,
|
|
|
|
},
|
|
|
|
},
|
2016-01-20 00:40:18 +00:00
|
|
|
&extensions.ReplicaSetList{
|
|
|
|
Items: []extensions.ReplicaSet{
|
|
|
|
generateRS(generateDeployment("foo")),
|
|
|
|
newRS,
|
|
|
|
generateRS(generateDeployment("bar")),
|
2015-12-16 06:59:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-01-20 00:40:18 +00:00
|
|
|
[]*extensions.ReplicaSet{},
|
2015-12-16 06:59:36 +00:00
|
|
|
},
|
|
|
|
{
|
2016-01-20 00:40:18 +00:00
|
|
|
"Has old ReplicaSet",
|
2015-12-16 06:59:36 +00:00
|
|
|
[]runtime.Object{
|
2016-11-18 20:50:17 +00:00
|
|
|
&v1.PodList{
|
|
|
|
Items: []v1.Pod{
|
2015-12-16 06:59:36 +00:00
|
|
|
oldPod,
|
|
|
|
oldPod2,
|
|
|
|
generatePod(map[string]string{"name": "bar"}, "bar"),
|
|
|
|
generatePod(map[string]string{"name": "xyz"}, "xyz"),
|
|
|
|
existedPod,
|
2016-01-20 00:40:18 +00:00
|
|
|
generatePod(newDeployment.Spec.Template.Labels, "abc"),
|
2015-12-16 06:59:36 +00:00
|
|
|
},
|
|
|
|
},
|
2016-01-20 00:40:18 +00:00
|
|
|
&extensions.ReplicaSetList{
|
|
|
|
Items: []extensions.ReplicaSet{
|
|
|
|
oldRS2,
|
|
|
|
oldRS,
|
|
|
|
existedRS,
|
|
|
|
newRS,
|
|
|
|
generateRSWithLabel(map[string]string{"name": "xyz"}, "xyz"),
|
|
|
|
generateRSWithLabel(map[string]string{"name": "bar"}, "bar"),
|
2015-12-16 06:59:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-01-20 00:40:18 +00:00
|
|
|
[]*extensions.ReplicaSet{&oldRS, &oldRS2, &existedRS},
|
2015-12-16 06:59:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-02-11 18:57:42 +00:00
|
|
|
fakeClient := &fake.Clientset{}
|
|
|
|
fakeClient = addListPodsReactor(fakeClient, test.objs[0])
|
|
|
|
fakeClient = addListRSReactor(fakeClient, test.objs[1])
|
2016-03-01 01:57:35 +00:00
|
|
|
fakeClient = addGetRSReactor(fakeClient, test.objs[1])
|
2016-02-11 18:57:42 +00:00
|
|
|
fakeClient = addUpdatePodsReactor(fakeClient)
|
|
|
|
fakeClient = addUpdateRSReactor(fakeClient)
|
2016-02-28 02:13:32 +00:00
|
|
|
rss, _, err := GetOldReplicaSets(&newDeployment, fakeClient)
|
2015-12-16 06:59:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("In test case %s, got unexpected error %v", test.test, err)
|
|
|
|
}
|
2016-01-20 00:40:18 +00:00
|
|
|
if !equal(rss, test.expected) {
|
2016-02-11 18:57:42 +00:00
|
|
|
t.Errorf("In test case %q, expected:", test.test)
|
|
|
|
for _, rs := range test.expected {
|
2016-06-14 12:04:38 +00:00
|
|
|
t.Errorf("rs = %#v", rs)
|
2016-02-11 18:57:42 +00:00
|
|
|
}
|
|
|
|
t.Errorf("In test case %q, got:", test.test)
|
|
|
|
for _, rs := range rss {
|
2016-06-14 12:04:38 +00:00
|
|
|
t.Errorf("rs = %#v", rs)
|
2016-02-11 18:57:42 +00:00
|
|
|
}
|
2015-12-16 06:59:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
func generatePodTemplateSpec(name, nodeName string, annotations, labels map[string]string) v1.PodTemplateSpec {
|
|
|
|
return v1.PodTemplateSpec{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-06-03 17:53:14 +00:00
|
|
|
Name: name,
|
|
|
|
Annotations: annotations,
|
|
|
|
Labels: labels,
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Spec: v1.PodSpec{
|
2016-06-03 17:53:14 +00:00
|
|
|
NodeName: nodeName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEqualIgnoreHash(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
test string
|
2016-11-18 20:50:17 +00:00
|
|
|
former, latter v1.PodTemplateSpec
|
2016-06-03 17:53:14 +00:00
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"Same spec, same labels",
|
|
|
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-1", "something": "else"}),
|
|
|
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-1", "something": "else"}),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Same spec, only pod-template-hash label value is different",
|
|
|
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-1", "something": "else"}),
|
|
|
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-2", "something": "else"}),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Same spec, the former doesn't have pod-template-hash label",
|
|
|
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{"something": "else"}),
|
|
|
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-2", "something": "else"}),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Same spec, the label is different, and the pod-template-hash label value is the same",
|
|
|
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-1"}),
|
|
|
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-1", "something": "else"}),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Different spec, same labels",
|
|
|
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{"former": "value"}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-1", "something": "else"}),
|
|
|
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{"latter": "value"}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-1", "something": "else"}),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Different spec, different pod-template-hash label value",
|
|
|
|
generatePodTemplateSpec("foo-1", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-1", "something": "else"}),
|
|
|
|
generatePodTemplateSpec("foo-2", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-2", "something": "else"}),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Different spec, the former doesn't have pod-template-hash label",
|
|
|
|
generatePodTemplateSpec("foo-1", "foo-node-1", map[string]string{}, map[string]string{"something": "else"}),
|
|
|
|
generatePodTemplateSpec("foo-2", "foo-node-2", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-2", "something": "else"}),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Different spec, different labels",
|
|
|
|
generatePodTemplateSpec("foo", "foo-node-1", map[string]string{}, map[string]string{"something": "else"}),
|
|
|
|
generatePodTemplateSpec("foo", "foo-node-2", map[string]string{}, map[string]string{"nothing": "else"}),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-12-04 20:51:44 +00:00
|
|
|
runTest := func(t1, t2 *v1.PodTemplateSpec, reversed bool) {
|
2016-08-03 18:19:12 +00:00
|
|
|
// Set up
|
|
|
|
t1Copy, err := api.Scheme.DeepCopy(t1)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed setting up the test: %v", err)
|
|
|
|
}
|
|
|
|
t2Copy, err := api.Scheme.DeepCopy(t2)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed setting up the test: %v", err)
|
|
|
|
}
|
|
|
|
reverseString := ""
|
|
|
|
if reversed {
|
|
|
|
reverseString = " (reverse order)"
|
|
|
|
}
|
|
|
|
// Run
|
2016-12-01 09:10:30 +00:00
|
|
|
equal := EqualIgnoreHash(*t1, *t2)
|
2016-08-03 18:19:12 +00:00
|
|
|
if equal != test.expected {
|
|
|
|
t.Errorf("In test case %q%s, expected %v", test.test, reverseString, test.expected)
|
|
|
|
}
|
|
|
|
if t1.Labels == nil || t2.Labels == nil {
|
|
|
|
t.Errorf("In test case %q%s, unexpected labels becomes nil", test.test, reverseString)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(t1, t1Copy) || !reflect.DeepEqual(t2, t2Copy) {
|
|
|
|
t.Errorf("In test case %q%s, unexpected input template modified", test.test, reverseString)
|
|
|
|
}
|
2016-06-03 17:53:14 +00:00
|
|
|
}
|
2016-12-04 20:51:44 +00:00
|
|
|
runTest(&test.former, &test.latter, false)
|
2016-08-03 18:19:12 +00:00
|
|
|
// Test the same case in reverse order
|
2016-12-04 20:51:44 +00:00
|
|
|
runTest(&test.latter, &test.former, true)
|
2016-06-03 17:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindNewReplicaSet(t *testing.T) {
|
|
|
|
deployment := generateDeployment("nginx")
|
|
|
|
newRS := generateRS(deployment)
|
|
|
|
newRS.Labels[extensions.DefaultDeploymentUniqueLabelKey] = "different-hash"
|
|
|
|
oldDeployment := generateDeployment("nginx")
|
|
|
|
oldDeployment.Spec.Template.Spec.Containers[0].Name = "nginx-old-1"
|
|
|
|
oldRS := generateRS(oldDeployment)
|
2016-11-18 20:50:17 +00:00
|
|
|
oldRS.Status.FullyLabeledReplicas = *(oldRS.Spec.Replicas)
|
2016-06-03 17:53:14 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
test string
|
|
|
|
deployment extensions.Deployment
|
2016-10-04 17:23:27 +00:00
|
|
|
rsList []*extensions.ReplicaSet
|
2016-06-03 17:53:14 +00:00
|
|
|
expected *extensions.ReplicaSet
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
test: "Get new ReplicaSet with the same spec but different pod-template-hash value",
|
|
|
|
deployment: deployment,
|
2016-10-04 17:23:27 +00:00
|
|
|
rsList: []*extensions.ReplicaSet{&newRS, &oldRS},
|
2016-06-03 17:53:14 +00:00
|
|
|
expected: &newRS,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "Get nil new ReplicaSet",
|
|
|
|
deployment: deployment,
|
2016-10-04 17:23:27 +00:00
|
|
|
rsList: []*extensions.ReplicaSet{&oldRS},
|
2016-06-03 17:53:14 +00:00
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
if rs, err := FindNewReplicaSet(&test.deployment, test.rsList); !reflect.DeepEqual(rs, test.expected) || err != nil {
|
|
|
|
t.Errorf("In test case %q, expected %#v, got %#v: %v", test.test, test.expected, rs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindOldReplicaSets(t *testing.T) {
|
|
|
|
deployment := generateDeployment("nginx")
|
|
|
|
newRS := generateRS(deployment)
|
|
|
|
newRS.Labels[extensions.DefaultDeploymentUniqueLabelKey] = "different-hash"
|
|
|
|
oldDeployment := generateDeployment("nginx")
|
|
|
|
oldDeployment.Spec.Template.Spec.Containers[0].Name = "nginx-old-1"
|
|
|
|
oldRS := generateRS(oldDeployment)
|
2016-11-18 20:50:17 +00:00
|
|
|
oldRS.Status.FullyLabeledReplicas = *(oldRS.Spec.Replicas)
|
2016-06-03 17:53:14 +00:00
|
|
|
newPod := generatePodFromRS(newRS)
|
|
|
|
oldPod := generatePodFromRS(oldRS)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
test string
|
|
|
|
deployment extensions.Deployment
|
2016-10-04 17:23:27 +00:00
|
|
|
rsList []*extensions.ReplicaSet
|
2016-11-18 20:50:17 +00:00
|
|
|
podList *v1.PodList
|
2016-06-03 17:53:14 +00:00
|
|
|
expected []*extensions.ReplicaSet
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
test: "Get old ReplicaSets",
|
|
|
|
deployment: deployment,
|
2016-10-04 17:23:27 +00:00
|
|
|
rsList: []*extensions.ReplicaSet{&newRS, &oldRS},
|
2016-11-18 20:50:17 +00:00
|
|
|
podList: &v1.PodList{
|
|
|
|
Items: []v1.Pod{
|
2016-06-03 17:53:14 +00:00
|
|
|
newPod,
|
|
|
|
oldPod,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []*extensions.ReplicaSet{&oldRS},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "Get old ReplicaSets with no new ReplicaSet",
|
|
|
|
deployment: deployment,
|
2016-10-04 17:23:27 +00:00
|
|
|
rsList: []*extensions.ReplicaSet{&oldRS},
|
2016-11-18 20:50:17 +00:00
|
|
|
podList: &v1.PodList{
|
|
|
|
Items: []v1.Pod{
|
2016-06-03 17:53:14 +00:00
|
|
|
oldPod,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []*extensions.ReplicaSet{&oldRS},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "Get empty old ReplicaSets",
|
|
|
|
deployment: deployment,
|
2016-10-04 17:23:27 +00:00
|
|
|
rsList: []*extensions.ReplicaSet{&newRS},
|
2016-11-18 20:50:17 +00:00
|
|
|
podList: &v1.PodList{
|
|
|
|
Items: []v1.Pod{
|
2016-06-03 17:53:14 +00:00
|
|
|
newPod,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []*extensions.ReplicaSet{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
if old, _, err := FindOldReplicaSets(&test.deployment, test.rsList, test.podList); !reflect.DeepEqual(old, test.expected) || err != nil {
|
|
|
|
t.Errorf("In test case %q, expected %#v, got %#v: %v", test.test, test.expected, old, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:40:18 +00:00
|
|
|
// equal compares the equality of two ReplicaSet slices regardless of their ordering
|
|
|
|
func equal(rss1, rss2 []*extensions.ReplicaSet) bool {
|
|
|
|
if reflect.DeepEqual(rss1, rss2) {
|
2015-12-16 06:59:36 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-01-20 00:40:18 +00:00
|
|
|
if rss1 == nil || rss2 == nil || len(rss1) != len(rss2) {
|
2015-12-16 06:59:36 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
count := 0
|
2016-01-20 00:40:18 +00:00
|
|
|
for _, rs1 := range rss1 {
|
|
|
|
for _, rs2 := range rss2 {
|
|
|
|
if reflect.DeepEqual(rs1, rs2) {
|
2015-12-16 06:59:36 +00:00
|
|
|
count++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:40:18 +00:00
|
|
|
return count == len(rss1)
|
2015-12-16 06:59:36 +00:00
|
|
|
}
|
2016-06-02 07:31:15 +00:00
|
|
|
|
|
|
|
func TestGetReplicaCountForReplicaSets(t *testing.T) {
|
|
|
|
rs1 := generateRS(generateDeployment("foo"))
|
2016-11-18 20:50:17 +00:00
|
|
|
*(rs1.Spec.Replicas) = 1
|
2016-06-02 07:31:15 +00:00
|
|
|
rs1.Status.Replicas = 2
|
|
|
|
rs2 := generateRS(generateDeployment("bar"))
|
2016-11-18 20:50:17 +00:00
|
|
|
*(rs2.Spec.Replicas) = 2
|
2016-06-02 07:31:15 +00:00
|
|
|
rs2.Status.Replicas = 3
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
test string
|
|
|
|
sets []*extensions.ReplicaSet
|
|
|
|
expectedCount int32
|
|
|
|
expectedActual int32
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"1:2 Replicas",
|
|
|
|
[]*extensions.ReplicaSet{&rs1},
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"3:5 Replicas",
|
|
|
|
[]*extensions.ReplicaSet{&rs1, &rs2},
|
|
|
|
3,
|
|
|
|
5,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
rs := GetReplicaCountForReplicaSets(test.sets)
|
|
|
|
if rs != test.expectedCount {
|
|
|
|
t.Errorf("In test case %s, expectedCount %+v, got %+v", test.test, test.expectedCount, rs)
|
|
|
|
}
|
|
|
|
rs = GetActualReplicaCountForReplicaSets(test.sets)
|
|
|
|
if rs != test.expectedActual {
|
|
|
|
t.Errorf("In test case %s, expectedActual %+v, got %+v", test.test, test.expectedActual, rs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolveFenceposts(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
maxSurge string
|
|
|
|
maxUnavailable string
|
|
|
|
desired int32
|
|
|
|
expectSurge int32
|
|
|
|
expectUnavailable int32
|
|
|
|
expectError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
maxSurge: "0%",
|
|
|
|
maxUnavailable: "0%",
|
|
|
|
desired: 0,
|
|
|
|
expectSurge: 0,
|
|
|
|
expectUnavailable: 1,
|
|
|
|
expectError: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
maxSurge: "39%",
|
|
|
|
maxUnavailable: "39%",
|
|
|
|
desired: 10,
|
|
|
|
expectSurge: 4,
|
|
|
|
expectUnavailable: 3,
|
|
|
|
expectError: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
maxSurge: "oops",
|
|
|
|
maxUnavailable: "39%",
|
|
|
|
desired: 10,
|
|
|
|
expectSurge: 0,
|
|
|
|
expectUnavailable: 0,
|
|
|
|
expectError: "invalid value for IntOrString: invalid value \"oops\": strconv.ParseInt: parsing \"oops\": invalid syntax",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
maxSurge: "55%",
|
|
|
|
maxUnavailable: "urg",
|
|
|
|
desired: 10,
|
|
|
|
expectSurge: 0,
|
|
|
|
expectUnavailable: 0,
|
|
|
|
expectError: "invalid value for IntOrString: invalid value \"urg\": strconv.ParseInt: parsing \"urg\": invalid syntax",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for num, test := range tests {
|
|
|
|
maxSurge := intstr.FromString(test.maxSurge)
|
|
|
|
maxUnavail := intstr.FromString(test.maxUnavailable)
|
|
|
|
surge, unavail, err := ResolveFenceposts(&maxSurge, &maxUnavail, test.desired)
|
|
|
|
if err != nil {
|
|
|
|
if test.expectError == "" {
|
|
|
|
t.Errorf("unexpected error %v", err)
|
|
|
|
} else {
|
|
|
|
assert := assert.New(t)
|
|
|
|
assert.EqualError(err, test.expectError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == nil && test.expectError != "" {
|
|
|
|
t.Errorf("missing error %v", test.expectError)
|
|
|
|
}
|
|
|
|
if surge != test.expectSurge || unavail != test.expectUnavailable {
|
|
|
|
t.Errorf("#%v got %v:%v, want %v:%v", num, surge, unavail, test.expectSurge, test.expectUnavailable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRSNewReplicas(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
test string
|
|
|
|
strategyType extensions.DeploymentStrategyType
|
|
|
|
depReplicas int32
|
|
|
|
newRSReplicas int32
|
|
|
|
maxSurge int
|
|
|
|
expected int32
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"can not scale up - to newRSReplicas",
|
|
|
|
extensions.RollingUpdateDeploymentStrategyType,
|
|
|
|
1, 5, 1, 5,
|
|
|
|
},
|
|
|
|
{
|
2016-09-15 15:57:53 +00:00
|
|
|
"scale up - to depReplicas",
|
2016-06-02 07:31:15 +00:00
|
|
|
extensions.RollingUpdateDeploymentStrategyType,
|
|
|
|
6, 2, 10, 6,
|
|
|
|
},
|
|
|
|
{
|
2016-09-15 15:57:53 +00:00
|
|
|
"recreate - to depReplicas",
|
2016-06-02 07:31:15 +00:00
|
|
|
extensions.RecreateDeploymentStrategyType,
|
|
|
|
3, 1, 1, 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
newDeployment := generateDeployment("nginx")
|
|
|
|
newRC := generateRS(newDeployment)
|
|
|
|
rs5 := generateRS(newDeployment)
|
2016-11-18 20:50:17 +00:00
|
|
|
*(rs5.Spec.Replicas) = 5
|
2016-06-02 07:31:15 +00:00
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-11-18 20:50:17 +00:00
|
|
|
*(newDeployment.Spec.Replicas) = test.depReplicas
|
2016-06-02 07:31:15 +00:00
|
|
|
newDeployment.Spec.Strategy = extensions.DeploymentStrategy{Type: test.strategyType}
|
|
|
|
newDeployment.Spec.Strategy.RollingUpdate = &extensions.RollingUpdateDeployment{
|
2016-11-18 20:50:17 +00:00
|
|
|
MaxUnavailable: func(i int) *intstr.IntOrString { x := intstr.FromInt(i); return &x }(1),
|
|
|
|
MaxSurge: func(i int) *intstr.IntOrString { x := intstr.FromInt(i); return &x }(test.maxSurge),
|
2016-06-02 07:31:15 +00:00
|
|
|
}
|
2016-11-18 20:50:17 +00:00
|
|
|
*(newRC.Spec.Replicas) = test.newRSReplicas
|
2016-06-02 07:31:15 +00:00
|
|
|
rs, err := NewRSNewReplicas(&newDeployment, []*extensions.ReplicaSet{&rs5}, &newRC)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("In test case %s, got unexpected error %v", test.test, err)
|
|
|
|
}
|
|
|
|
if rs != test.expected {
|
|
|
|
t.Errorf("In test case %s, expected %+v, got %+v", test.test, test.expected, rs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 15:57:53 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
condProgressing = func() extensions.DeploymentCondition {
|
|
|
|
return extensions.DeploymentCondition{
|
|
|
|
Type: extensions.DeploymentProgressing,
|
2016-11-18 20:50:17 +00:00
|
|
|
Status: v1.ConditionFalse,
|
2016-09-15 15:57:53 +00:00
|
|
|
Reason: "ForSomeReason",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
condProgressing2 = func() extensions.DeploymentCondition {
|
|
|
|
return extensions.DeploymentCondition{
|
|
|
|
Type: extensions.DeploymentProgressing,
|
2016-11-18 20:50:17 +00:00
|
|
|
Status: v1.ConditionTrue,
|
2016-09-15 15:57:53 +00:00
|
|
|
Reason: "BecauseItIs",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
condAvailable = func() extensions.DeploymentCondition {
|
|
|
|
return extensions.DeploymentCondition{
|
|
|
|
Type: extensions.DeploymentAvailable,
|
2016-11-18 20:50:17 +00:00
|
|
|
Status: v1.ConditionTrue,
|
2016-09-15 15:57:53 +00:00
|
|
|
Reason: "AwesomeController",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status = func() *extensions.DeploymentStatus {
|
|
|
|
return &extensions.DeploymentStatus{
|
|
|
|
Conditions: []extensions.DeploymentCondition{condProgressing(), condAvailable()},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetCondition(t *testing.T) {
|
|
|
|
exampleStatus := status()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
status extensions.DeploymentStatus
|
|
|
|
condType extensions.DeploymentConditionType
|
2016-11-18 20:50:17 +00:00
|
|
|
condStatus v1.ConditionStatus
|
2016-09-15 15:57:53 +00:00
|
|
|
condReason string
|
|
|
|
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "condition exists",
|
|
|
|
|
|
|
|
status: *exampleStatus,
|
|
|
|
condType: extensions.DeploymentAvailable,
|
|
|
|
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "condition does not exist",
|
|
|
|
|
|
|
|
status: *exampleStatus,
|
|
|
|
condType: extensions.DeploymentReplicaFailure,
|
|
|
|
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
cond := GetDeploymentCondition(test.status, test.condType)
|
|
|
|
exists := cond != nil
|
|
|
|
if exists != test.expected {
|
|
|
|
t.Errorf("%s: expected condition to exist: %t, got: %t", test.name, test.expected, exists)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetCondition(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
status *extensions.DeploymentStatus
|
|
|
|
cond extensions.DeploymentCondition
|
|
|
|
|
|
|
|
expectedStatus *extensions.DeploymentStatus
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "set for the first time",
|
|
|
|
|
|
|
|
status: &extensions.DeploymentStatus{},
|
|
|
|
cond: condAvailable(),
|
|
|
|
|
|
|
|
expectedStatus: &extensions.DeploymentStatus{Conditions: []extensions.DeploymentCondition{condAvailable()}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple set",
|
|
|
|
|
|
|
|
status: &extensions.DeploymentStatus{Conditions: []extensions.DeploymentCondition{condProgressing()}},
|
|
|
|
cond: condAvailable(),
|
|
|
|
|
|
|
|
expectedStatus: status(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "overwrite",
|
|
|
|
|
|
|
|
status: &extensions.DeploymentStatus{Conditions: []extensions.DeploymentCondition{condProgressing()}},
|
|
|
|
cond: condProgressing2(),
|
|
|
|
|
|
|
|
expectedStatus: &extensions.DeploymentStatus{Conditions: []extensions.DeploymentCondition{condProgressing2()}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
SetDeploymentCondition(test.status, test.cond)
|
|
|
|
if !reflect.DeepEqual(test.status, test.expectedStatus) {
|
|
|
|
t.Errorf("%s: expected status: %v, got: %v", test.name, test.expectedStatus, test.status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveCondition(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
status *extensions.DeploymentStatus
|
|
|
|
condType extensions.DeploymentConditionType
|
|
|
|
|
|
|
|
expectedStatus *extensions.DeploymentStatus
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "remove from empty status",
|
|
|
|
|
|
|
|
status: &extensions.DeploymentStatus{},
|
|
|
|
condType: extensions.DeploymentProgressing,
|
|
|
|
|
|
|
|
expectedStatus: &extensions.DeploymentStatus{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple remove",
|
|
|
|
|
|
|
|
status: &extensions.DeploymentStatus{Conditions: []extensions.DeploymentCondition{condProgressing()}},
|
|
|
|
condType: extensions.DeploymentProgressing,
|
|
|
|
|
|
|
|
expectedStatus: &extensions.DeploymentStatus{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "doesn't remove anything",
|
|
|
|
|
|
|
|
status: status(),
|
|
|
|
condType: extensions.DeploymentReplicaFailure,
|
|
|
|
|
|
|
|
expectedStatus: status(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
RemoveDeploymentCondition(test.status, test.condType)
|
|
|
|
if !reflect.DeepEqual(test.status, test.expectedStatus) {
|
|
|
|
t.Errorf("%s: expected status: %v, got: %v", test.name, test.expectedStatus, test.status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeploymentComplete(t *testing.T) {
|
|
|
|
deployment := func(desired, current, updated, available, maxUnavailable int32) *extensions.Deployment {
|
|
|
|
return &extensions.Deployment{
|
|
|
|
Spec: extensions.DeploymentSpec{
|
2016-11-18 20:50:17 +00:00
|
|
|
Replicas: &desired,
|
2016-09-15 15:57:53 +00:00
|
|
|
Strategy: extensions.DeploymentStrategy{
|
|
|
|
RollingUpdate: &extensions.RollingUpdateDeployment{
|
2016-11-18 20:50:17 +00:00
|
|
|
MaxUnavailable: func(i int) *intstr.IntOrString { x := intstr.FromInt(i); return &x }(int(maxUnavailable)),
|
|
|
|
MaxSurge: func() *intstr.IntOrString { x := intstr.FromInt(0); return &x }(),
|
2016-09-15 15:57:53 +00:00
|
|
|
},
|
|
|
|
Type: extensions.RollingUpdateDeploymentStrategyType,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Status: extensions.DeploymentStatus{
|
|
|
|
Replicas: current,
|
|
|
|
UpdatedReplicas: updated,
|
|
|
|
AvailableReplicas: available,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
d *extensions.Deployment
|
|
|
|
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "complete",
|
|
|
|
|
|
|
|
d: deployment(5, 5, 5, 4, 1),
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not complete",
|
|
|
|
|
|
|
|
d: deployment(5, 5, 5, 3, 1),
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "complete #2",
|
|
|
|
|
|
|
|
d: deployment(5, 5, 5, 5, 0),
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not complete #2",
|
|
|
|
|
|
|
|
d: deployment(5, 5, 4, 5, 0),
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Log(test.name)
|
|
|
|
|
|
|
|
if got, exp := DeploymentComplete(test.d, &test.d.Status), test.expected; got != exp {
|
|
|
|
t.Errorf("expected complete: %t, got: %t", exp, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeploymentProgressing(t *testing.T) {
|
|
|
|
deployment := func(current, updated int32) *extensions.Deployment {
|
|
|
|
return &extensions.Deployment{
|
|
|
|
Status: extensions.DeploymentStatus{
|
|
|
|
Replicas: current,
|
|
|
|
UpdatedReplicas: updated,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newStatus := func(current, updated int32) extensions.DeploymentStatus {
|
|
|
|
return extensions.DeploymentStatus{
|
|
|
|
Replicas: current,
|
|
|
|
UpdatedReplicas: updated,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
d *extensions.Deployment
|
|
|
|
newStatus extensions.DeploymentStatus
|
|
|
|
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "progressing",
|
|
|
|
|
|
|
|
d: deployment(10, 4),
|
|
|
|
newStatus: newStatus(10, 6),
|
|
|
|
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not progressing",
|
|
|
|
|
|
|
|
d: deployment(10, 4),
|
|
|
|
newStatus: newStatus(10, 4),
|
|
|
|
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "progressing #2",
|
|
|
|
|
|
|
|
d: deployment(10, 4),
|
|
|
|
newStatus: newStatus(8, 4),
|
|
|
|
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not progressing #2",
|
|
|
|
|
|
|
|
d: deployment(10, 7),
|
|
|
|
newStatus: newStatus(10, 6),
|
|
|
|
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "progressing #3",
|
|
|
|
|
|
|
|
d: deployment(10, 4),
|
|
|
|
newStatus: newStatus(8, 8),
|
|
|
|
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not progressing #2",
|
|
|
|
|
|
|
|
d: deployment(10, 7),
|
|
|
|
newStatus: newStatus(10, 7),
|
|
|
|
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Log(test.name)
|
|
|
|
|
|
|
|
if got, exp := DeploymentProgressing(test.d, &test.newStatus), test.expected; got != exp {
|
|
|
|
t.Errorf("expected progressing: %t, got: %t", exp, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeploymentTimedOut(t *testing.T) {
|
|
|
|
var (
|
|
|
|
null *int32
|
|
|
|
ten = int32(10)
|
|
|
|
)
|
|
|
|
|
|
|
|
timeFn := func(min, sec int) time.Time {
|
|
|
|
return time.Date(2016, 1, 1, 0, min, sec, 0, time.UTC)
|
|
|
|
}
|
2016-11-18 20:50:17 +00:00
|
|
|
deployment := func(condType extensions.DeploymentConditionType, status v1.ConditionStatus, pds *int32, from time.Time) extensions.Deployment {
|
2016-09-15 15:57:53 +00:00
|
|
|
return extensions.Deployment{
|
|
|
|
Spec: extensions.DeploymentSpec{
|
|
|
|
ProgressDeadlineSeconds: pds,
|
|
|
|
},
|
|
|
|
Status: extensions.DeploymentStatus{
|
|
|
|
Conditions: []extensions.DeploymentCondition{
|
|
|
|
{
|
2016-11-08 10:41:53 +00:00
|
|
|
Type: condType,
|
|
|
|
Status: status,
|
2016-12-03 18:57:26 +00:00
|
|
|
LastUpdateTime: metav1.Time{Time: from},
|
2016-09-15 15:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
d extensions.Deployment
|
|
|
|
nowFn func() time.Time
|
|
|
|
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no progressDeadlineSeconds specified - no timeout",
|
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
d: deployment(extensions.DeploymentProgressing, v1.ConditionTrue, null, timeFn(1, 9)),
|
2016-09-15 15:57:53 +00:00
|
|
|
nowFn: func() time.Time { return timeFn(1, 20) },
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "progressDeadlineSeconds: 10s, now - started => 00:01:20 - 00:01:09 => 11s",
|
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
d: deployment(extensions.DeploymentProgressing, v1.ConditionTrue, &ten, timeFn(1, 9)),
|
2016-09-15 15:57:53 +00:00
|
|
|
nowFn: func() time.Time { return timeFn(1, 20) },
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "progressDeadlineSeconds: 10s, now - started => 00:01:20 - 00:01:11 => 9s",
|
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
d: deployment(extensions.DeploymentProgressing, v1.ConditionTrue, &ten, timeFn(1, 11)),
|
2016-09-15 15:57:53 +00:00
|
|
|
nowFn: func() time.Time { return timeFn(1, 20) },
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Log(test.name)
|
|
|
|
|
|
|
|
nowFn = test.nowFn
|
|
|
|
if got, exp := DeploymentTimedOut(&test.d, &test.d.Status), test.expected; got != exp {
|
|
|
|
t.Errorf("expected timeout: %t, got: %t", exp, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-04 15:44:13 +00:00
|
|
|
|
|
|
|
func TestSelectorUpdatedBefore(t *testing.T) {
|
|
|
|
now := metav1.Now()
|
|
|
|
later := metav1.Time{Time: now.Add(time.Minute)}
|
|
|
|
selectorUpdated := metav1.Time{Time: later.Add(time.Minute)}
|
|
|
|
selectorUpdatedLater := metav1.Time{Time: selectorUpdated.Add(time.Minute)}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
d1 extensions.Deployment
|
|
|
|
creationTimestamp1 *metav1.Time
|
|
|
|
selectorUpdated1 *metav1.Time
|
|
|
|
|
|
|
|
d2 extensions.Deployment
|
|
|
|
creationTimestamp2 *metav1.Time
|
|
|
|
selectorUpdated2 *metav1.Time
|
|
|
|
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "d1 created before d2",
|
|
|
|
|
|
|
|
d1: generateDeployment("foo"),
|
|
|
|
creationTimestamp1: &now,
|
|
|
|
|
|
|
|
d2: generateDeployment("bar"),
|
|
|
|
creationTimestamp2: &later,
|
|
|
|
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "d1 created after d2",
|
|
|
|
|
|
|
|
d1: generateDeployment("foo"),
|
|
|
|
creationTimestamp1: &later,
|
|
|
|
|
|
|
|
d2: generateDeployment("bar"),
|
|
|
|
creationTimestamp2: &now,
|
|
|
|
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Think of the following scenario:
|
|
|
|
// d1 is created first, d2 is created after and its selector overlaps
|
|
|
|
// with d1. d2 is marked as overlapping correctly. If d1's selector is
|
|
|
|
// updated and continues to overlap with the selector of d2 then d1 is
|
|
|
|
// now marked overlapping and d2 is cleaned up. Proved by the following
|
|
|
|
// test case. Callers of SelectorUpdatedBefore should first check for
|
|
|
|
// the existence of the overlapping annotation in any of the two deployments
|
|
|
|
// prior to comparing their timestamps and as a matter of fact this is
|
|
|
|
// now handled in `(dc *DeploymentController) handleOverlap`.
|
|
|
|
name: "d1 created before d2 but updated its selector afterwards",
|
|
|
|
|
|
|
|
d1: generateDeployment("foo"),
|
|
|
|
creationTimestamp1: &now,
|
|
|
|
selectorUpdated1: &selectorUpdated,
|
|
|
|
|
|
|
|
d2: generateDeployment("bar"),
|
|
|
|
creationTimestamp2: &later,
|
|
|
|
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "d1 selector is older than d2",
|
|
|
|
|
|
|
|
d1: generateDeployment("foo"),
|
|
|
|
selectorUpdated1: &selectorUpdated,
|
|
|
|
|
|
|
|
d2: generateDeployment("bar"),
|
|
|
|
selectorUpdated2: &selectorUpdatedLater,
|
|
|
|
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "d1 selector is younger than d2",
|
|
|
|
|
|
|
|
d1: generateDeployment("foo"),
|
|
|
|
selectorUpdated1: &selectorUpdatedLater,
|
|
|
|
|
|
|
|
d2: generateDeployment("bar"),
|
|
|
|
selectorUpdated2: &selectorUpdated,
|
|
|
|
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Logf("running scenario %q", test.name)
|
|
|
|
|
|
|
|
if test.creationTimestamp1 != nil {
|
|
|
|
test.d1.CreationTimestamp = *test.creationTimestamp1
|
|
|
|
}
|
|
|
|
if test.creationTimestamp2 != nil {
|
|
|
|
test.d2.CreationTimestamp = *test.creationTimestamp2
|
|
|
|
}
|
|
|
|
if test.selectorUpdated1 != nil {
|
|
|
|
test.d1.Annotations[SelectorUpdateAnnotation] = test.selectorUpdated1.Format(time.RFC3339)
|
|
|
|
}
|
|
|
|
if test.selectorUpdated2 != nil {
|
|
|
|
test.d2.Annotations[SelectorUpdateAnnotation] = test.selectorUpdated2.Format(time.RFC3339)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got := SelectorUpdatedBefore(&test.d1, &test.d2); got != test.expected {
|
|
|
|
t.Errorf("expected d1 selector to be updated before d2: %t, got: %t", test.expected, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|