2016-12-09 16:16:00 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 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 deployment
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/api/core/v1"
|
|
|
|
extensions "k8s.io/api/extensions/v1beta1"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2017-04-02 15:59:30 +00:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2017-06-23 20:56:37 +00:00
|
|
|
"k8s.io/client-go/informers"
|
|
|
|
"k8s.io/client-go/kubernetes/fake"
|
2017-01-30 18:39:54 +00:00
|
|
|
"k8s.io/client-go/tools/record"
|
2016-12-09 16:16:00 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestScaleDownOldReplicaSets(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
oldRSSizes []int
|
|
|
|
d *extensions.Deployment
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
oldRSSizes: []int{3},
|
|
|
|
d: newDeployment("foo", 3, nil, nil, nil, map[string]string{"foo": "bar"}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range tests {
|
|
|
|
t.Logf("running scenario %d", i)
|
|
|
|
test := tests[i]
|
|
|
|
|
|
|
|
var oldRSs []*extensions.ReplicaSet
|
|
|
|
var expected []runtime.Object
|
|
|
|
|
|
|
|
for n, size := range test.oldRSSizes {
|
|
|
|
rs := newReplicaSet(test.d, fmt.Sprintf("%s-%d", test.d.Name, n), size)
|
|
|
|
oldRSs = append(oldRSs, rs)
|
|
|
|
|
2017-08-15 12:14:21 +00:00
|
|
|
rsCopy := rs.DeepCopy()
|
2016-12-09 16:16:00 +00:00
|
|
|
|
|
|
|
zero := int32(0)
|
|
|
|
rsCopy.Spec.Replicas = &zero
|
|
|
|
expected = append(expected, rsCopy)
|
|
|
|
|
|
|
|
if *(oldRSs[n].Spec.Replicas) == *(expected[n].(*extensions.ReplicaSet).Spec.Replicas) {
|
|
|
|
t.Errorf("broken test - original and expected RS have the same size")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kc := fake.NewSimpleClientset(expected...)
|
2017-02-08 21:18:21 +00:00
|
|
|
informers := informers.NewSharedInformerFactory(kc, controller.NoResyncPeriodFunc())
|
2017-02-06 18:35:50 +00:00
|
|
|
c := NewDeploymentController(informers.Extensions().V1beta1().Deployments(), informers.Extensions().V1beta1().ReplicaSets(), informers.Core().V1().Pods(), kc)
|
2017-01-30 18:39:54 +00:00
|
|
|
c.eventRecorder = &record.FakeRecorder{}
|
2016-12-09 16:16:00 +00:00
|
|
|
|
|
|
|
c.scaleDownOldReplicaSetsForRecreate(oldRSs, test.d)
|
|
|
|
for j := range oldRSs {
|
|
|
|
rs := oldRSs[j]
|
|
|
|
|
|
|
|
if *rs.Spec.Replicas != 0 {
|
|
|
|
t.Errorf("rs %q has non-zero replicas", rs.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-02 15:59:30 +00:00
|
|
|
|
|
|
|
func TestOldPodsRunning(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
newRS *extensions.ReplicaSet
|
|
|
|
oldRSs []*extensions.ReplicaSet
|
|
|
|
podMap map[types.UID]*v1.PodList
|
|
|
|
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no old RSs",
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "old RSs with running pods",
|
|
|
|
oldRSs: []*extensions.ReplicaSet{rsWithUID("some-uid"), rsWithUID("other-uid")},
|
|
|
|
podMap: podMapWithUIDs([]string{"some-uid", "other-uid"}),
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "old RSs without pods but with non-zero status replicas",
|
|
|
|
oldRSs: []*extensions.ReplicaSet{newRSWithStatus("rs-blabla", 0, 1, nil)},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "old RSs without pods or non-zero status replicas",
|
|
|
|
oldRSs: []*extensions.ReplicaSet{newRSWithStatus("rs-blabla", 0, 0, nil)},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
if expected, got := test.expected, oldPodsRunning(test.newRS, test.oldRSs, test.podMap); expected != got {
|
|
|
|
t.Errorf("%s: expected %t, got %t", test.name, expected, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func rsWithUID(uid string) *extensions.ReplicaSet {
|
|
|
|
d := newDeployment("foo", 1, nil, nil, nil, map[string]string{"foo": "bar"})
|
|
|
|
rs := newReplicaSet(d, fmt.Sprintf("foo-%s", uid), 0)
|
|
|
|
rs.UID = types.UID(uid)
|
|
|
|
return rs
|
|
|
|
}
|
|
|
|
|
|
|
|
func podMapWithUIDs(uids []string) map[types.UID]*v1.PodList {
|
|
|
|
podMap := make(map[types.UID]*v1.PodList)
|
|
|
|
for _, uid := range uids {
|
|
|
|
podMap[types.UID(uid)] = &v1.PodList{
|
|
|
|
Items: []v1.Pod{
|
|
|
|
{ /* supposedly a pod */ },
|
|
|
|
{ /* supposedly another pod pod */ },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return podMap
|
|
|
|
}
|