Skip to content

Commit

Permalink
unit test for the max weight ingress.
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 aeb6a5b commit b0d72fa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rollout/trafficrouting/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func (r *Reconciler) buildCanaryIngress(stableIngress *networkingv1.Ingress, nam
desiredCanaryIngress.Annotations[fmt.Sprintf("%s/canary", annotationPrefix)] = "true"
desiredCanaryIngress.Annotations[fmt.Sprintf("%s/canary-weight", annotationPrefix)] = fmt.Sprintf("%d", desiredWeight)

if r.cfg.Rollout.Spec.Strategy.Canary.TrafficRouting.MaxTrafficWeight != nil {
weightTotal := *r.cfg.Rollout.Spec.Strategy.Canary.TrafficRouting.MaxTrafficWeight
desiredCanaryIngress.Annotations[fmt.Sprintf("%s/canary-weight-total", annotationPrefix)] = fmt.Sprintf("%d", weightTotal)
}

Check warning on line 139 in rollout/trafficrouting/nginx/nginx.go

View check run for this annotation

Codecov / codecov/patch

rollout/trafficrouting/nginx/nginx.go#L137-L139

Added lines #L137 - L139 were not covered by tests
return ingressutil.NewIngress(desiredCanaryIngress), nil
}

Expand Down
46 changes: 46 additions & 0 deletions rollout/trafficrouting/nginx/nginx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,52 @@ func TestCanaryIngressAdditionalAnnotations(t *testing.T) {
}
}

func TestCanaryIngressMaxWeightInTrafficRouting(t *testing.T) {
maxWeights := []*int32{nil, pointer.Int32(1000)}
for _, maxWeight := range maxWeights {
tests := generateMultiIngressTestData()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r := Reconciler{
cfg: ReconcilerConfig{
Rollout: fakeRollout(stableService, canaryService, test.singleIngress, test.multiIngress),
},
}
r.cfg.Rollout.Spec.Strategy.Canary.TrafficRouting.MaxTrafficWeight = maxWeight
for _, ing := range test.ingresses {
stable := extensionsIngress(ing, 80, stableService)
canary := extensionsIngress("canary-ingress", 80, canaryService)
canary.SetAnnotations(map[string]string{
"nginx.ingress.kubernetes.io/canary": "true",
"nginx.ingress.kubernetes.io/canary-weight": "10",
})
stableIngress := ingressutil.NewLegacyIngress(stable)
canaryIngress := ingressutil.NewLegacyIngress(canary)

desiredCanaryIngress, err := r.canaryIngress(stableIngress, ingressutil.GetCanaryIngressName(r.cfg.Rollout.GetName(), ing), 15)
assert.Nil(t, err, "No error returned when calling canaryIngress")

checkBackendService(t, desiredCanaryIngress, canaryService)

patch, modified, err := ingressutil.BuildIngressPatch(canaryIngress.Mode(), canaryIngress, desiredCanaryIngress,
ingressutil.WithAnnotations(), ingressutil.WithLabels(), ingressutil.WithSpec())
assert.Nil(t, err, "compareCanaryIngresses returns no error")
assert.True(t, modified, "compareCanaryIngresses returns modified=true")
if maxWeight == nil {
assert.Equal(t,
"{\"metadata\":{\"annotations\":{\"nginx.ingress.kubernetes.io/canary-weight\":\"15\"}}}", string(patch), "compareCanaryIngresses returns expected patch")
} else {
assert.Equal(t,
fmt.Sprintf("{\"metadata\":{\"annotations\":{\"nginx.ingress.kubernetes.io/canary-weight\":\"15\",\"nginx.ingress.kubernetes.io/canary-weight-total\":\"%d\"}}}", *maxWeight),
string(patch), "compareCanaryIngresses returns expected patch")
}
}
})
}
}

}

func TestReconciler_canaryIngress(t *testing.T) {
tests := generateMultiIngressTestData()
for _, test := range tests {
Expand Down

0 comments on commit b0d72fa

Please sign in to comment.