Skip to content

Commit 34c150a

Browse files
committed
Add tests for RequestTimeout CEL validation
1 parent 966d874 commit 34c150a

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

tests/cel/clientsettingspolicy_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
1111

1212
ngfAPIv1alpha1 "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha1"
13+
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/helpers"
1314
"github.com/nginx/nginx-gateway-fabric/v2/tests/framework"
1415
)
1516

@@ -129,6 +130,66 @@ func TestClientSettingsPoliciesTargetRefGroup(t *testing.T) {
129130
}
130131
}
131132

133+
func TestClientSettingsPoliciesKeepAliveTimeout(t *testing.T) {
134+
t.Parallel()
135+
g := NewWithT(t)
136+
k8sClient, err := getKubernetesClient(t)
137+
g.Expect(err).ToNot(HaveOccurred())
138+
tests := []struct {
139+
policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec
140+
name string
141+
wantErrors []string
142+
}{
143+
{
144+
name: "Validate KeepAliveTimeout is not set",
145+
policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
146+
TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{
147+
Kind: gatewayKind,
148+
Group: gatewayGroup,
149+
},
150+
KeepAlive: nil,
151+
},
152+
},
153+
{
154+
name: "Validate KeepAlive is set",
155+
policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
156+
TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{
157+
Kind: gatewayKind,
158+
Group: gatewayGroup,
159+
},
160+
KeepAlive: &ngfAPIv1alpha1.ClientKeepAlive{
161+
Timeout: &ngfAPIv1alpha1.ClientKeepAliveTimeout{
162+
Server: helpers.GetPointer[ngfAPIv1alpha1.Duration]("5s"),
163+
Header: helpers.GetPointer[ngfAPIv1alpha1.Duration]("2s"),
164+
},
165+
},
166+
},
167+
},
168+
{
169+
name: "Validate Header cannot be set without Server",
170+
wantErrors: []string{expectedHeaderWithoutServerError},
171+
policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
172+
TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{
173+
Kind: gatewayKind,
174+
Group: gatewayGroup,
175+
},
176+
KeepAlive: &ngfAPIv1alpha1.ClientKeepAlive{
177+
Timeout: &ngfAPIv1alpha1.ClientKeepAliveTimeout{
178+
Header: helpers.GetPointer[ngfAPIv1alpha1.Duration]("2s"),
179+
},
180+
},
181+
},
182+
},
183+
}
184+
185+
for _, tt := range tests {
186+
t.Run(tt.name, func(t *testing.T) {
187+
t.Parallel()
188+
validateClientSettingsPolicy(t, tt, g, k8sClient)
189+
})
190+
}
191+
}
192+
132193
func validateClientSettingsPolicy(t *testing.T, tt struct {
133194
policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec
134195
name string

tests/cel/common.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ const (
2828
)
2929

3030
const (
31-
expectedTargetRefKindError = `TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute`
32-
expectedTargetRefGroupError = `TargetRef Group must be gateway.networking.k8s.io.`
31+
expectedTargetRefKindError = `TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute`
32+
expectedTargetRefGroupError = `TargetRef Group must be gateway.networking.k8s.io.`
33+
expectedHeaderWithoutServerError = `header can only be specified if server is specified`
3334
)
3435

3536
const (

0 commit comments

Comments
 (0)