func TestUpdate_assignOriginalAnnotation(t *testing.T) { oldRc := oldRc(1, 1) delete(oldRc.Annotations, originalReplicasAnnotation) newRc := newRc(1, 1) var updatedOldRc *api.ReplicationController fake := &testclient.Fake{} fake.AddReactor("*", "*", func(action testclient.Action) (handled bool, ret runtime.Object, err error) { switch a := action.(type) { case testclient.GetAction: return true, oldRc, nil case testclient.UpdateAction: updatedOldRc = a.GetObject().(*api.ReplicationController) return true, updatedOldRc, nil } return false, nil, nil }) updater := &RollingUpdater{ c: fake, ns: "default", scaleAndWait: func(rc *api.ReplicationController, retry *RetryParams, wait *RetryParams) (*api.ReplicationController, error) { return rc, nil }, getOrCreateTargetController: func(controller *api.ReplicationController, sourceId string) (*api.ReplicationController, bool, error) { return newRc, false, nil }, cleanup: func(oldRc, newRc *api.ReplicationController, config *RollingUpdaterConfig) error { return nil }, getReadyPods: func(oldRc, newRc *api.ReplicationController) (int32, int32, error) { return 1, 1, nil }, } var buffer bytes.Buffer config := &RollingUpdaterConfig{ Out: &buffer, OldRc: oldRc, NewRc: newRc, UpdatePeriod: 0, Interval: time.Millisecond, Timeout: time.Millisecond, CleanupPolicy: DeleteRollingUpdateCleanupPolicy, MaxUnavailable: intstr.FromString("100%"), } err := updater.Update(config) if err != nil { t.Fatalf("unexpected error: %v", err) } if updatedOldRc == nil { t.Fatalf("expected rc to be updated") } if e, a := "1", updatedOldRc.Annotations[originalReplicasAnnotation]; e != a { t.Fatalf("expected annotation value %s, got %s", e, a) } }
func TestRollingUpdater_multipleContainersInPod(t *testing.T) { tests := []struct { oldRc *api.ReplicationController newRc *api.ReplicationController container string image string deploymentKey string }{ { oldRc: &api.ReplicationController{ ObjectMeta: api.ObjectMeta{ Name: "foo", }, Spec: api.ReplicationControllerSpec{ Selector: map[string]string{ "dk": "old", }, Template: &api.PodTemplateSpec{ ObjectMeta: api.ObjectMeta{ Labels: map[string]string{ "dk": "old", }, }, Spec: api.PodSpec{ Containers: []api.Container{ { Name: "container1", Image: "image1", }, { Name: "container2", Image: "image2", }, }, }, }, }, }, newRc: &api.ReplicationController{ ObjectMeta: api.ObjectMeta{ Name: "foo", }, Spec: api.ReplicationControllerSpec{ Selector: map[string]string{ "dk": "old", }, Template: &api.PodTemplateSpec{ ObjectMeta: api.ObjectMeta{ Labels: map[string]string{ "dk": "old", }, }, Spec: api.PodSpec{ Containers: []api.Container{ { Name: "container1", Image: "newimage", }, { Name: "container2", Image: "image2", }, }, }, }, }, }, container: "container1", image: "newimage", deploymentKey: "dk", }, { oldRc: &api.ReplicationController{ ObjectMeta: api.ObjectMeta{ Name: "bar", }, Spec: api.ReplicationControllerSpec{ Selector: map[string]string{ "dk": "old", }, Template: &api.PodTemplateSpec{ ObjectMeta: api.ObjectMeta{ Labels: map[string]string{ "dk": "old", }, }, Spec: api.PodSpec{ Containers: []api.Container{ { Name: "container1", Image: "image1", }, }, }, }, }, }, newRc: &api.ReplicationController{ ObjectMeta: api.ObjectMeta{ Name: "bar", }, Spec: api.ReplicationControllerSpec{ Selector: map[string]string{ "dk": "old", }, Template: &api.PodTemplateSpec{ ObjectMeta: api.ObjectMeta{ Labels: map[string]string{ "dk": "old", }, }, Spec: api.PodSpec{ Containers: []api.Container{ { Name: "container1", Image: "newimage", }, }, }, }, }, }, container: "container1", image: "newimage", deploymentKey: "dk", }, } for _, test := range tests { fake := &testclient.Fake{} fake.AddReactor("*", "*", func(action testclient.Action) (handled bool, ret runtime.Object, err error) { switch action.(type) { case testclient.GetAction: return true, test.oldRc, nil } return false, nil, nil }) codec := testapi.Default.Codec() deploymentHash, err := api.HashObject(test.newRc, codec) if err != nil { t.Errorf("unexpected error: %v", err) } test.newRc.Spec.Selector[test.deploymentKey] = deploymentHash test.newRc.Spec.Template.Labels[test.deploymentKey] = deploymentHash test.newRc.Name = fmt.Sprintf("%s-%s", test.newRc.Name, deploymentHash) config := &NewControllerConfig{ OldName: test.oldRc.ObjectMeta.Name, NewName: test.newRc.ObjectMeta.Name, Image: test.image, Container: test.container, DeploymentKey: test.deploymentKey, } updatedRc, err := CreateNewControllerFromCurrentController(fake, codec, config) if err != nil { t.Errorf("unexpected error: %v", err) } if !reflect.DeepEqual(updatedRc, test.newRc) { t.Errorf("expected:\n%#v\ngot:\n%#v\n", test.newRc, updatedRc) } } }