func TestDeploymentController_reconcileOldRCs(t *testing.T) { tests := []struct { deploymentReplicas int maxUnavailable intstr.IntOrString readyPods int oldReplicas int scaleExpected bool expectedOldReplicas int }{ { deploymentReplicas: 10, maxUnavailable: intstr.FromInt(0), readyPods: 10, oldReplicas: 10, scaleExpected: false, }, { deploymentReplicas: 10, maxUnavailable: intstr.FromInt(2), readyPods: 10, oldReplicas: 10, scaleExpected: true, expectedOldReplicas: 8, }, { deploymentReplicas: 10, maxUnavailable: intstr.FromInt(2), readyPods: 8, oldReplicas: 10, scaleExpected: false, }, { deploymentReplicas: 10, maxUnavailable: intstr.FromInt(2), readyPods: 10, oldReplicas: 0, scaleExpected: false, }, } for i, test := range tests { t.Logf("executing scenario %d", i) oldRc := rc("foo-v2", test.oldReplicas) allRcs := []*api.ReplicationController{oldRc} oldRcs := []*api.ReplicationController{oldRc} deployment := deployment("foo", test.deploymentReplicas, intstr.FromInt(0), test.maxUnavailable) fake := &testclient.Fake{} fake.AddReactor("list", "pods", func(action testclient.Action) (handled bool, ret runtime.Object, err error) { switch action.(type) { case testclient.ListAction: podList := &api.PodList{} for podIndex := 0; podIndex < test.readyPods; podIndex++ { podList.Items = append(podList.Items, api.Pod{ ObjectMeta: api.ObjectMeta{ Name: fmt.Sprintf("%s-pod-%d", oldRc.Name, podIndex), }, Status: api.PodStatus{ Conditions: []api.PodCondition{ { Type: api.PodReady, Status: api.ConditionTrue, }, }, }, }) } return true, podList, nil } return false, nil, nil }) controller := &DeploymentController{ client: fake, eventRecorder: &record.FakeRecorder{}, } scaled, err := controller.reconcileOldRCs(allRcs, oldRcs, nil, deployment, false) if err != nil { t.Errorf("unexpected error: %v", err) continue } if !test.scaleExpected { if scaled { t.Errorf("unexpected scaling: %v", fake.Actions()) } continue } if test.scaleExpected && !scaled { t.Errorf("expected scaling to occur; actions: %v", fake.Actions()) continue } // There are both list and update actions logged, so extract the update // action for verification. var updateAction testclient.UpdateAction for _, action := range fake.Actions() { switch a := action.(type) { case testclient.UpdateAction: if updateAction != nil { t.Errorf("expected only 1 update action; had %v and found %v", updateAction, a) } else { updateAction = a } } } if updateAction == nil { t.Errorf("expected an update action") continue } updated := updateAction.GetObject().(*api.ReplicationController) if e, a := test.expectedOldReplicas, updated.Spec.Replicas; e != a { t.Errorf("expected update to %d replicas, got %d", e, a) } } }
func TestDeploymentController_reconcileOldRCs(t *testing.T) { tests := []struct { deploymentReplicas int maxUnavailable intstr.IntOrString oldReplicas int newReplicas int readyPodsFromOldRC int readyPodsFromNewRC int scaleExpected bool expectedOldReplicas int }{ { deploymentReplicas: 10, maxUnavailable: intstr.FromInt(0), oldReplicas: 10, newReplicas: 0, readyPodsFromOldRC: 10, readyPodsFromNewRC: 0, scaleExpected: false, }, { deploymentReplicas: 10, maxUnavailable: intstr.FromInt(2), oldReplicas: 10, newReplicas: 0, readyPodsFromOldRC: 10, readyPodsFromNewRC: 0, scaleExpected: true, expectedOldReplicas: 8, }, { // expect unhealthy replicas from old rcs been cleaned up deploymentReplicas: 10, maxUnavailable: intstr.FromInt(2), oldReplicas: 10, newReplicas: 0, readyPodsFromOldRC: 8, readyPodsFromNewRC: 0, scaleExpected: true, expectedOldReplicas: 8, }, { // expect 1 unhealthy replica from old rcs been cleaned up, and 1 ready pod been scaled down deploymentReplicas: 10, maxUnavailable: intstr.FromInt(2), oldReplicas: 10, newReplicas: 0, readyPodsFromOldRC: 9, readyPodsFromNewRC: 0, scaleExpected: true, expectedOldReplicas: 8, }, { // the unavailable pods from the newRC would not make us scale down old RCs in a further step deploymentReplicas: 10, maxUnavailable: intstr.FromInt(2), oldReplicas: 8, newReplicas: 2, readyPodsFromOldRC: 8, readyPodsFromNewRC: 0, scaleExpected: false, }, } for i, test := range tests { t.Logf("executing scenario %d", i) newSelector := map[string]string{"foo": "new"} oldSelector := map[string]string{"foo": "old"} newRc := rc("foo-new", test.newReplicas, newSelector) oldRc := rc("foo-old", test.oldReplicas, oldSelector) oldRCs := []*api.ReplicationController{oldRc} allRCs := []*api.ReplicationController{oldRc, newRc} deployment := deployment("foo", test.deploymentReplicas, intstr.FromInt(0), test.maxUnavailable) fakeClientset := fake.Clientset{} fakeClientset.AddReactor("list", "pods", func(action core.Action) (handled bool, ret runtime.Object, err error) { switch action.(type) { case core.ListAction: podList := &api.PodList{} for podIndex := 0; podIndex < test.readyPodsFromOldRC; podIndex++ { podList.Items = append(podList.Items, api.Pod{ ObjectMeta: api.ObjectMeta{ Name: fmt.Sprintf("%s-oldReadyPod-%d", oldRc.Name, podIndex), Labels: oldSelector, }, Status: api.PodStatus{ Conditions: []api.PodCondition{ { Type: api.PodReady, Status: api.ConditionTrue, }, }, }, }) } for podIndex := 0; podIndex < test.oldReplicas-test.readyPodsFromOldRC; podIndex++ { podList.Items = append(podList.Items, api.Pod{ ObjectMeta: api.ObjectMeta{ Name: fmt.Sprintf("%s-oldUnhealthyPod-%d", oldRc.Name, podIndex), Labels: oldSelector, }, Status: api.PodStatus{ Conditions: []api.PodCondition{ { Type: api.PodReady, Status: api.ConditionFalse, }, }, }, }) } for podIndex := 0; podIndex < test.readyPodsFromNewRC; podIndex++ { podList.Items = append(podList.Items, api.Pod{ ObjectMeta: api.ObjectMeta{ Name: fmt.Sprintf("%s-newReadyPod-%d", oldRc.Name, podIndex), Labels: newSelector, }, Status: api.PodStatus{ Conditions: []api.PodCondition{ { Type: api.PodReady, Status: api.ConditionTrue, }, }, }, }) } for podIndex := 0; podIndex < test.oldReplicas-test.readyPodsFromOldRC; podIndex++ { podList.Items = append(podList.Items, api.Pod{ ObjectMeta: api.ObjectMeta{ Name: fmt.Sprintf("%s-newUnhealthyPod-%d", oldRc.Name, podIndex), Labels: newSelector, }, Status: api.PodStatus{ Conditions: []api.PodCondition{ { Type: api.PodReady, Status: api.ConditionFalse, }, }, }, }) } return true, podList, nil } return false, nil, nil }) controller := &DeploymentController{ client: &fakeClientset, eventRecorder: &record.FakeRecorder{}, } scaled, err := controller.reconcileOldRCs(allRCs, oldRCs, newRc, deployment, false) if err != nil { t.Errorf("unexpected error: %v", err) continue } if !test.scaleExpected && scaled { t.Errorf("unexpected scaling: %v", fakeClientset.Actions()) } if test.scaleExpected && !scaled { t.Errorf("expected scaling to occur") continue } continue } }