From 2fb4be46a8e4af50cac92221ef97a94fe5ef1ed8 Mon Sep 17 00:00:00 2001 From: Hui Kang Date: Fri, 19 Nov 2021 00:02:42 -0500 Subject: [PATCH] Add unit test Signed-off-by: Hui Kang --- rollout/bluegreen_test.go | 55 ++++++++++++++++++++++++++++++++++++++ rollout/controller_test.go | 3 +++ 2 files changed, 58 insertions(+) diff --git a/rollout/bluegreen_test.go b/rollout/bluegreen_test.go index a83b9071c2..2670b5cc38 100644 --- a/rollout/bluegreen_test.go +++ b/rollout/bluegreen_test.go @@ -37,6 +37,61 @@ func newBlueGreenRollout(name string, replicas int, revisionHistoryLimit *int32, return rollout } +func TestBlueGreenComplateRolloutRestart(t *testing.T) { + f := newFixture(t) + defer f.Close() + + r := newBlueGreenRollout("foo", 1, nil, "active", "preview") + r.Status.Conditions = []v1alpha1.RolloutCondition{} + + completedCond := conditions.NewRolloutCondition(v1alpha1.RolloutCompleted, corev1.ConditionTrue, conditions.RolloutCompletedReason, conditions.RolloutCompletedReason) + conditions.SetRolloutCondition(&r.Status, *completedCond) + + f.rolloutLister = append(f.rolloutLister, r) + f.objects = append(f.objects, r) + previewSvc := newService("preview", 80, nil, r) + activeSvc := newService("active", 80, nil, r) + f.kubeobjects = append(f.kubeobjects, previewSvc, activeSvc) + f.serviceLister = append(f.serviceLister, activeSvc, previewSvc) + + rs := newReplicaSet(r, 1) + rsPodHash := rs.Labels[v1alpha1.DefaultRolloutUniqueLabelKey] + // generatedConditions := generateConditionsPatch(false, conditions.ReplicaSetUpdatedReason, rs, false, "") + generatedConditions := generateConditionsPatchWithComplete(false, conditions.ReplicaSetNotAvailableReason, rs, false, "", false) + + f.expectCreateReplicaSetAction(rs) + servicePatchIndex := f.expectPatchServiceAction(previewSvc, rsPodHash) + f.expectUpdateReplicaSetAction(rs) // scale up RS + updatedRolloutIndex := f.expectUpdateRolloutStatusAction(r) + expectedPatchWithoutSubs := `{ + "status":{ + "blueGreen" : { + "previewSelector": "%s" + }, + "conditions": %s, + "selector": "foo=bar", + "stableRS": "%s", + "phase": "Progressing", + "message": "more replicas need to be updated" + } + }` + expectedPatch := calculatePatch(r, fmt.Sprintf(expectedPatchWithoutSubs, rsPodHash, generatedConditions, rsPodHash)) + patchRolloutIndex := f.expectPatchRolloutActionWithPatch(r, expectedPatch) + f.run(getKey(r, t)) + + f.verifyPatchedService(servicePatchIndex, rsPodHash, "") + + updatedRollout := f.getUpdatedRollout(updatedRolloutIndex) + updatedProgressingCondition := conditions.GetRolloutCondition(updatedRollout.Status, v1alpha1.RolloutProgressing) + assert.NotNil(t, updatedProgressingCondition) + assert.Equal(t, conditions.NewReplicaSetReason, updatedProgressingCondition.Reason) + assert.Equal(t, corev1.ConditionTrue, updatedProgressingCondition.Status) + assert.Equal(t, fmt.Sprintf(conditions.NewReplicaSetMessage, rs.Name), updatedProgressingCondition.Message) + + patch := f.getPatchedRollout(patchRolloutIndex) + assert.Equal(t, expectedPatch, patch) +} + func TestBlueGreenCreatesReplicaSet(t *testing.T) { f := newFixture(t) defer f.Close() diff --git a/rollout/controller_test.go b/rollout/controller_test.go index d58904e779..7d545d4e79 100644 --- a/rollout/controller_test.go +++ b/rollout/controller_test.go @@ -229,6 +229,9 @@ func newProgressingCondition(reason string, resourceObj runtime.Object, optional if reason == conditions.NewRSAvailableReason { msg = fmt.Sprintf(conditions.ReplicaSetCompletedMessage, resource.Name) } + if reason == conditions.ReplicaSetNotAvailableReason { + msg = conditions.NotAvailableMessage + } case *v1alpha1.Rollout: if reason == conditions.ReplicaSetUpdatedReason { msg = fmt.Sprintf(conditions.RolloutProgressingMessage, resource.Name)