Skip to content

Commit

Permalink
unit test for the max weight in canary rollout.
Browse files Browse the repository at this point in the history
Signed-off-by: Liming Liu <[email protected]>
  • Loading branch information
andyliuliming committed Dec 19, 2023
1 parent b0d72fa commit c327863
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions rollout/canary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/argoproj/argo-rollouts/utils/annotations"
"github.com/argoproj/argo-rollouts/utils/conditions"
"github.com/argoproj/argo-rollouts/utils/hash"
ingressutil "github.com/argoproj/argo-rollouts/utils/ingress"
logutil "github.com/argoproj/argo-rollouts/utils/log"
"github.com/argoproj/argo-rollouts/utils/record"
replicasetutil "github.com/argoproj/argo-rollouts/utils/replicaset"
Expand Down Expand Up @@ -549,6 +550,92 @@ func TestCanaryRolloutCreateFirstReplicasetWithSteps(t *testing.T) {
assert.JSONEq(t, calculatePatch(r, expectedPatch), patch)
}

func TestCanaryRolloutWithMaxWeightInTrafficRouting(t *testing.T) {
testCases := []struct {
name string
maxWeight *int32
setWeight int32
expectedCreatedReplicas int32
expectedUpdatedReplicas int32
}{
{
name: "max weight 100",
maxWeight: int32Ptr(100),
setWeight: 10,
expectedCreatedReplicas: 0,
expectedUpdatedReplicas: 1,
},
{
name: "max weight 1000",
maxWeight: int32Ptr(1000),
setWeight: 200,
expectedCreatedReplicas: 0,
expectedUpdatedReplicas: 2,
},
}

for _, tc := range testCases {
f := newFixture(t)
defer f.Close()
steps := []v1alpha1.CanaryStep{{
SetWeight: int32Ptr(tc.setWeight),
}}
r1 := newCanaryRollout("foo", 10, nil, steps, int32Ptr(0), intstr.FromInt(1), intstr.FromInt(0))

canarySVCName := "canary"
stableSVCName := "stable"

ingressName := "ingress"
r1.Spec.Strategy.Canary.TrafficRouting = &v1alpha1.RolloutTrafficRouting{
MaxTrafficWeight: tc.maxWeight,
Nginx: &v1alpha1.NginxTrafficRouting{
StableIngress: ingressName,
},
}
r1.Spec.Strategy.Canary.StableService = stableSVCName
r1.Spec.Strategy.Canary.CanaryService = canarySVCName
r1.Status.StableRS = "895c6c4f9"
r2 := bumpVersion(r1)

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

rs1 := newReplicaSetWithStatus(r1, 10, 10)
rs2 := newReplicaSetWithStatus(r2, 1, 0)

stableSvc := newService(stableSVCName, 80,
map[string]string{v1alpha1.DefaultRolloutUniqueLabelKey: rs1.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]}, r1)

canarySvc := newService(canarySVCName, 80,
map[string]string{v1alpha1.DefaultRolloutUniqueLabelKey: rs2.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]}, r1)
f.replicaSetLister = append(f.replicaSetLister, rs1)

ing := newIngress(ingressName, canarySvc, stableSvc)
ing.Spec.Rules[0].HTTP.Paths[0].Backend.ServiceName = stableSVCName
f.kubeobjects = append(f.kubeobjects, rs1, canarySvc, stableSvc, ing)
f.serviceLister = append(f.serviceLister, canarySvc, stableSvc)
f.ingressLister = append(f.ingressLister, ingressutil.NewLegacyIngress(ing))

createdRSIndex := f.expectCreateReplicaSetAction(rs2)
updatedRSIndex := f.expectUpdateReplicaSetAction(rs2)
updatedRolloutIndex := f.expectUpdateRolloutStatusAction(r2)
f.expectPatchRolloutAction(r2)
f.run(getKey(r2, t))

createdRS := f.getCreatedReplicaSet(createdRSIndex)
assert.Equal(t, int32(tc.expectedCreatedReplicas), *createdRS.Spec.Replicas)

Check failure on line 626 in rollout/canary_test.go

View workflow job for this annotation

GitHub Actions / Lint Go code

unnecessary conversion (unconvert)
updatedRS := f.getUpdatedReplicaSet(updatedRSIndex)
assert.Equal(t, int32(tc.expectedUpdatedReplicas), *updatedRS.Spec.Replicas)

Check failure on line 628 in rollout/canary_test.go

View workflow job for this annotation

GitHub Actions / Lint Go code

unnecessary conversion (unconvert)

updatedRollout := f.getUpdatedRollout(updatedRolloutIndex)
progressingCondition := conditions.GetRolloutCondition(updatedRollout.Status, v1alpha1.RolloutProgressing)
assert.NotNil(t, progressingCondition)
assert.Equal(t, conditions.NewReplicaSetReason, progressingCondition.Reason)
assert.Equal(t, corev1.ConditionTrue, progressingCondition.Status)
assert.Equal(t, fmt.Sprintf(conditions.NewReplicaSetMessage, createdRS.Name), progressingCondition.Message)
}

}
func TestCanaryRolloutCreateNewReplicaWithCorrectWeight(t *testing.T) {
f := newFixture(t)
defer f.Close()
Expand Down

0 comments on commit c327863

Please sign in to comment.