Skip to content

Commit

Permalink
test(replicaset): add test case for IsActive function
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Chen <[email protected]>
  • Loading branch information
amazingandyyy authored and yohanb committed Aug 16, 2023
1 parent e6e61c0 commit c4940f1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
3 changes: 1 addition & 2 deletions rollout/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,7 @@ func (c *rolloutContext) syncRolloutStatusCanary() error {
c.recorder.Eventf(c.rollout, record.EventOptions{EventReason: "SkipSteps"}, "Rollback to stable ReplicaSets")
newStatus.CurrentStepIndex = &stepCount
} else if c.isRollbackWithinWindow() && replicasetutil.IsActive(c.newRS) {
// Else if we get here we detected that we are within the rollback window
// we can skip steps and move back to the active ReplicaSet
// Else if we get here we detected that we are within the rollback window we can skip steps and move back to the active ReplicaSet
c.recorder.Eventf(c.rollout, record.EventOptions{EventReason: "SkipSteps"}, "Rollback to active ReplicaSets within RollbackWindow")
newStatus.CurrentStepIndex = &stepCount
}
Expand Down
55 changes: 25 additions & 30 deletions rollout/canary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,50 +854,45 @@ func TestRollBackToActiveReplicaSetWithinWindow(t *testing.T) {
SetWeight: int32Ptr(10),
}}

r1 := newCanaryRollout("foo", 10, int32Ptr(1), steps, int32Ptr(0), intstr.FromInt(1), intstr.FromInt(0))
r1.Spec.RollbackWindow = &v1alpha1.RollbackWindowSpec{
Revisions: 1,
}

rs1 := newReplicaSetWithStatus(r1, 10, 10)
rs1.CreationTimestamp = metav1.Time{Time: time.Now().Add(-10 * time.Minute)}
r1 := newCanaryRollout("foo", 1, nil, steps, int32Ptr(0), intstr.FromInt(1), intstr.FromInt(0))
r2 := bumpVersion(r1)
rs2 := newReplicaSetWithStatus(r2, 10, 10)
rs2.CreationTimestamp = metav1.Time{Time: time.Now().Add(-1 * time.Minute)}

r2.Spec.Template = r1.Spec.Template
// For the fast rollback to work, we need to:
// 1. Have the previous revision be active (i.e. not scaled down)
// 2. Be in rollback window (within window revisions and previous creation timestamp)
rs1 := newReplicaSetWithStatus(r1, 1, 1)
rs2 := newReplicaSetWithStatus(r2, 1, 1)
r2.Spec.RollbackWindow = &v1alpha1.RollbackWindowSpec{Revisions: 1}
rs1.CreationTimestamp = timeutil.MetaTime(time.Now().Add(-1 * time.Hour))
rs2.CreationTimestamp = timeutil.MetaNow()

rs2PodHash := rs2.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]
f.kubeobjects = append(f.kubeobjects, rs1, rs2)
f.replicaSetLister = append(f.replicaSetLister, rs1, rs2)
f.serviceLister = append(f.serviceLister)

r2 = updateCanaryRolloutStatus(r2, rs2PodHash, 10, 10, 10, false)
// Switch back to version 1
r2.Spec.Template = r1.Spec.Template

rs1PodHash := rs1.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]
rs2PodHash := rs2.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]

// Since old replicaset is still active, expect twice the number of replicas
r2 = updateCanaryRolloutStatus(r2, rs2PodHash, 2, 2, 2, false)

f.rolloutLister = append(f.rolloutLister, r2)
f.objects = append(f.objects, r2)

updatedRSIndex := f.expectUpdateReplicaSetAction(rs1)
f.expectUpdateReplicaSetAction(rs1)
patchIndex := f.expectPatchRolloutAction(r2)
f.expectUpdateReplicaSetAction(rs1)
rolloutPatchIndex := f.expectPatchRolloutAction(r2)
f.run(getKey(r2, t))

expectedRS1 := rs1.DeepCopy()
expectedRS1.Annotations[annotations.RevisionAnnotation] = "3"
expectedRS1.Annotations[annotations.RevisionHistoryAnnotation] = "1"
firstUpdatedRS1 := f.getUpdatedReplicaSet(updatedRSIndex)
assert.Equal(t, expectedRS1, firstUpdatedRS1)
expectedStepIndex := len(steps)
patch := f.getPatchedRolloutWithoutConditions(rolloutPatchIndex)

expectedPatchWithoutSub := `{
"status":{
"currentPodHash": "%s",
"currentStepIndex":1,
"conditions": %s
}
}`
newConditions := generateConditionsPatch(true, conditions.ReplicaSetUpdatedReason, rs1, false, "", true)
expectedPatch := fmt.Sprintf(expectedPatchWithoutSub, hash.ComputePodTemplateHash(&r2.Spec.Template, r2.Status.CollisionCount), newConditions)
patch := f.getPatchedRollout(patchIndex)
assert.Equal(t, calculatePatch(r2, expectedPatch), patch)
// Assert current pod hash is the old replicaset and steps were skipped
assert.Regexp(t, fmt.Sprintf(`"currentPodHash":"%s"`, rs1PodHash), patch)
assert.Regexp(t, fmt.Sprintf(`"currentStepIndex":%d`, expectedStepIndex), patch)
}

func TestGradualShiftToNewStable(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions utils/replicaset/replicaset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ func TestFindOldReplicaSets(t *testing.T) {
}
}

func TestIsActive(t *testing.T) {
rs1 := generateRS(generateRollout("foo"))
*(rs1.Spec.Replicas) = 1

rs2 := generateRS(generateRollout("foo"))
*(rs2.Spec.Replicas) = 0

assert.False(t, IsActive(nil))
assert.True(t, IsActive(&rs1))
assert.False(t, IsActive(&rs2))
}

func TestGetReplicaCountForReplicaSets(t *testing.T) {
rs1 := generateRS(generateRollout("foo"))
*(rs1.Spec.Replicas) = 1
Expand Down
5 changes: 5 additions & 0 deletions utils/time/now.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ var Now = time.Now
var MetaNow = func() metav1.Time {
return metav1.Time{Time: Now()}
}

// MetaTime is a wrapper around metav1.Time and used to override behavior in tests.
var MetaTime = func(time time.Time) metav1.Time {
return metav1.Time{Time: time}
}

0 comments on commit c4940f1

Please sign in to comment.