Skip to content

Commit 206b84e

Browse files
authored
Add CEL validation test for timeout in ClientSettingsPolicy (#3695)
1 parent 1dd7381 commit 206b84e

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

tests/cel/clientsettingspolicy_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ 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

1617
func TestClientSettingsPoliciesTargetRefKind(t *testing.T) {
1718
t.Parallel()
1819
g := NewWithT(t)
20+
1921
k8sClient, err := getKubernetesClient(t)
2022
g.Expect(err).ToNot(HaveOccurred())
23+
2124
tests := []struct {
2225
policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec
2326
name string
@@ -83,8 +86,10 @@ func TestClientSettingsPoliciesTargetRefKind(t *testing.T) {
8386
func TestClientSettingsPoliciesTargetRefGroup(t *testing.T) {
8487
t.Parallel()
8588
g := NewWithT(t)
89+
8690
k8sClient, err := getKubernetesClient(t)
8791
g.Expect(err).ToNot(HaveOccurred())
92+
8893
tests := []struct {
8994
policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec
9095
name string
@@ -129,6 +134,68 @@ func TestClientSettingsPoliciesTargetRefGroup(t *testing.T) {
129134
}
130135
}
131136

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