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 <[email protected]>
  • Loading branch information
kahirokunn committed Dec 16, 2024
1 parent e9997a3 commit 5931ff6
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions pkg/router/gateway_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,67 @@ 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,
},
},
},
},
}

name := v1.ObjectName("canary")
unmanagedSvcNamespace := "kube-system"
namespace := v1.Namespace(unmanagedSvcNamespace)
port := v1.PortNumber(30080)
objRef := v1.BackendObjectReference{
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)

err = router.Reconcile(canary)
require.NoError(t, err)

referenceGrant, err := router.gatewayAPIClient.GatewayapiV1beta1().ReferenceGrants(unmanagedSvcNamespace).Get(context.TODO(), canary.Name, metav1.GetOptions{})
require.NoError(t, err)
assert.Equal(t, unmanagedSvcNamespace, string(referenceGrant.Namespace))
assert.Equal(t, "HTTPRoute", string(referenceGrant.Spec.From[0].Kind))
assert.Equal(t, canary.Namespace, string(referenceGrant.Spec.From[0].Namespace))
assert.Equal(t, "Service", string(referenceGrant.Spec.To[0].Kind))
assert.Equal(t, "", string(referenceGrant.Spec.To[0].Group))
assert.Equal(t, string(name), string(*referenceGrant.Spec.To[0].Name))
})
}

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

0 comments on commit 5931ff6

Please sign in to comment.