Skip to content

Commit

Permalink
Consolidate test cases to reduce duplication
Browse files Browse the repository at this point in the history
Signed-off-by: Hui Kang <hui.kang@salesforce.com>
  • Loading branch information
Hui Kang committed Aug 22, 2021
1 parent 4e4484b commit 0cfdf29
Showing 1 changed file with 47 additions and 68 deletions.
115 changes: 47 additions & 68 deletions rollout/bluegreen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,86 +114,65 @@ func TestBlueGreenSetPreviewService(t *testing.T) {
f.verifyPatchedService(servicePatch, rsPodHash, "")
}

// TestBlueGreenProgressDeadlineAbort tests aborting an update if it is timeout
func TestBlueGreenProgressDeadlineAbort(t *testing.T) {
f := newFixture(t)
defer f.Close()

r := newBlueGreenRollout("foo", 1, nil, "active", "preview")
progressDeadlineSeconds := int32(1)
r.Spec.ProgressDeadlineSeconds = &progressDeadlineSeconds
r.Spec.ProgressDeadlineAbort = true

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

rs := newReplicaSetWithStatus(r, 1, 1)
r.Status.UpdatedReplicas = 1
r.Status.ReadyReplicas = 1
r.Status.AvailableReplicas = 1

progressingTimeoutCond := conditions.NewRolloutCondition(v1alpha1.RolloutProgressing, corev1.ConditionTrue, conditions.TimedOutReason, conditions.TimedOutReason)
conditions.SetRolloutCondition(&r.Status, *progressingTimeoutCond)

rsPodHash := rs.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]
r.Status.BlueGreen.ActiveSelector = rsPodHash
r.Status.BlueGreen.PreviewSelector = rsPodHash

previewSvc := newService("preview", 80, nil, r)
selector := map[string]string{v1alpha1.DefaultRolloutUniqueLabelKey: rsPodHash}
activeSvc := newService("active", 80, selector, r)
f.kubeobjects = append(f.kubeobjects, previewSvc, activeSvc)
f.serviceLister = append(f.serviceLister, previewSvc, activeSvc)

f.kubeobjects = append(f.kubeobjects, rs)
f.replicaSetLister = append(f.replicaSetLister, rs)

f.expectPatchServiceAction(previewSvc, rsPodHash)
patchIndex := f.expectPatchRolloutAction(r)
f.run(getKey(r, t))

f.verifyPatchedRolloutAborted(patchIndex, "foo-"+rsPodHash)
}
// Two cases to be tested:
// 1. the rollout is making progress, but timeout just happens
// 2. the rollout is not making progress due to timeout and the rollout spec
// is changed to set ProgressDeadlineAbort
tests := []bool{true, false}

var runRolloutProgressDeadlineAbort func(isTimeout bool)
runRolloutProgressDeadlineAbort = func(isTimeout bool) {
f := newFixture(t)
defer f.Close()

func TestBlueGreenProgressDeadlineAbortAfterDegraded(t *testing.T) {
f := newFixture(t)
defer f.Close()
r := newBlueGreenRollout("foo", 1, nil, "active", "preview")
progressDeadlineSeconds := int32(1)
r.Spec.ProgressDeadlineSeconds = &progressDeadlineSeconds
r.Spec.ProgressDeadlineAbort = true

r := newBlueGreenRollout("foo", 1, nil, "active", "preview")
progressDeadlineSeconds := int32(1)
r.Spec.ProgressDeadlineSeconds = &progressDeadlineSeconds
r.Spec.ProgressDeadlineAbort = true
f.rolloutLister = append(f.rolloutLister, r)
f.objects = append(f.objects, r)

f.rolloutLister = append(f.rolloutLister, r)
f.objects = append(f.objects, r)
rs := newReplicaSetWithStatus(r, 1, 1)
r.Status.UpdatedReplicas = 1
r.Status.ReadyReplicas = 1
r.Status.AvailableReplicas = 1

rs := newReplicaSetWithStatus(r, 1, 1)
r.Status.UpdatedReplicas = 1
r.Status.ReadyReplicas = 1
r.Status.AvailableReplicas = 1
rsPodHash := rs.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]

rsPodHash := rs.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]
var progressingTimeoutCond *v1alpha1.RolloutCondition
if isTimeout {
msg := fmt.Sprintf("ReplicaSet %q has timed out progressing.", "foo-"+rsPodHash)
progressingTimeoutCond = conditions.NewRolloutCondition(v1alpha1.RolloutProgressing, corev1.ConditionFalse, conditions.TimedOutReason, msg)
} else {
progressingTimeoutCond = conditions.NewRolloutCondition(v1alpha1.RolloutProgressing, corev1.ConditionTrue, conditions.TimedOutReason, conditions.TimedOutReason)
}
conditions.SetRolloutCondition(&r.Status, *progressingTimeoutCond)

msg := fmt.Sprintf("ReplicaSet %q has timed out progressing.", "foo-"+rsPodHash)
progressingTimeoutCond := conditions.NewRolloutCondition(v1alpha1.RolloutProgressing, corev1.ConditionFalse, conditions.TimedOutReason, msg)
conditions.SetRolloutCondition(&r.Status, *progressingTimeoutCond)
r.Status.BlueGreen.ActiveSelector = rsPodHash
r.Status.BlueGreen.PreviewSelector = rsPodHash

r.Status.BlueGreen.ActiveSelector = rsPodHash
r.Status.BlueGreen.PreviewSelector = rsPodHash
previewSvc := newService("preview", 80, nil, r)
selector := map[string]string{v1alpha1.DefaultRolloutUniqueLabelKey: rsPodHash}
activeSvc := newService("active", 80, selector, r)
f.kubeobjects = append(f.kubeobjects, previewSvc, activeSvc)
f.serviceLister = append(f.serviceLister, previewSvc, activeSvc)

previewSvc := newService("preview", 80, nil, r)
selector := map[string]string{v1alpha1.DefaultRolloutUniqueLabelKey: rsPodHash}
activeSvc := newService("active", 80, selector, r)
f.kubeobjects = append(f.kubeobjects, previewSvc, activeSvc)
f.serviceLister = append(f.serviceLister, previewSvc, activeSvc)
f.kubeobjects = append(f.kubeobjects, rs)
f.replicaSetLister = append(f.replicaSetLister, rs)

f.kubeobjects = append(f.kubeobjects, rs)
f.replicaSetLister = append(f.replicaSetLister, rs)
f.expectPatchServiceAction(previewSvc, rsPodHash)
patchIndex := f.expectPatchRolloutAction(r)
f.run(getKey(r, t))

f.expectPatchServiceAction(previewSvc, rsPodHash)
patchIndex := f.expectPatchRolloutAction(r)
f.run(getKey(r, t))
f.verifyPatchedRolloutAborted(patchIndex, "foo-"+rsPodHash)
}

f.verifyPatchedRolloutAborted(patchIndex, "foo-"+rsPodHash)
for _, tc := range tests {
runRolloutProgressDeadlineAbort(tc)
}
}

//TestSetServiceManagedBy ensures the managed by annotation is set in the service is set
Expand Down

0 comments on commit 0cfdf29

Please sign in to comment.