Skip to content

Commit

Permalink
test(gateway-api): add tests for custom backend filters
Browse files Browse the repository at this point in the history
Signed-off-by: kahirokunn <okinakahiro@gmail.com>
  • Loading branch information
kahirokunn committed Dec 16, 2024
1 parent 7eb0708 commit f89feb6
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions pkg/router/gateway_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,56 @@ func TestGatewayAPIRouter_Routes(t *testing.T) {
assert.Len(t, hr.Spec.Rules, 1)
assert.Len(t, hr.Spec.Rules[0].Filters, 0)
})

t.Run("custom backend filters", func(t *testing.T) {
canary := mocks.canary.DeepCopy()
primaryHostName := v1.PreciseHostname("primary.example.com")
canary.Spec.Service.Primary = &flaggerv1.CustomBackend{
Backend: &flaggerv1.HTTPBackendRefTemplate{
Filters: []v1.HTTPRouteFilter{
{
Type: v1.HTTPRouteFilterURLRewrite,
URLRewrite: &v1.HTTPURLRewriteFilter{
Hostname: &primaryHostName,
},
},
},
},
}

kind := v1.Kind("Service")
name := v1.ObjectName("canary")
namespace := v1.Namespace("kube-system")
port := v1.PortNumber(30080)
objRef := v1.BackendObjectReference{
Kind: &kind,
Name: name,
Namespace: &namespace,
Port: &port,
}

canary.Spec.Service.Canary = &flaggerv1.CustomBackend{
Backend: &flaggerv1.HTTPBackendRefTemplate{
BackendObjectReference: &objRef,
},
}
err = router.SetRoutes(canary, 50, 50, false)
require.NoError(t, err)

httpRoute, err := router.gatewayAPIClient.GatewayapiV1().HTTPRoutes("default").Get(context.TODO(), "podinfo", metav1.GetOptions{})
require.NoError(t, err)

primary := httpRoute.Spec.Rules[0].BackendRefs[0]
assert.Equal(t, int32(50), *primary.Weight)

canaryBackend := httpRoute.Spec.Rules[0].BackendRefs[1]
assert.Equal(t, canaryBackend.Name, name)
assert.Equal(t, canaryBackend.Namespace, &namespace)
assert.Equal(t, canaryBackend.Port, &port)

primaryBackend := httpRoute.Spec.Rules[0].BackendRefs[0].Filters[0].URLRewrite
assert.Equal(t, primaryBackend.Hostname, &primaryHostName)
})
}

func TestGatewayAPIRouter_getSessionAffinityRouteRules(t *testing.T) {
Expand Down

0 comments on commit f89feb6

Please sign in to comment.