From c3278634b8529288efbafed06c198d819f22b7b0 Mon Sep 17 00:00:00 2001 From: Liming Liu Date: Tue, 19 Dec 2023 16:30:06 +0000 Subject: [PATCH] unit test for the max weight in canary rollout. Signed-off-by: Liming Liu --- rollout/canary_test.go | 87 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/rollout/canary_test.go b/rollout/canary_test.go index 6fc3dda93c..7d4d926db7 100644 --- a/rollout/canary_test.go +++ b/rollout/canary_test.go @@ -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" @@ -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) + updatedRS := f.getUpdatedReplicaSet(updatedRSIndex) + assert.Equal(t, int32(tc.expectedUpdatedReplicas), *updatedRS.Spec.Replicas) + + 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()