Skip to content

Commit dbb5b5c

Browse files
committed
Add CEL validation test for targetRef in ClientSettingsPolicy
1 parent e88fbeb commit dbb5b5c

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package clientsettingspolicies
2+
3+
import (
4+
"testing"
5+
6+
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
7+
)
8+
9+
func TestClientSettingsPoliciesTargetRef(t *testing.T) {
10+
11+
allowedKinds := map[string]bool{
12+
"Gateway": true,
13+
"HTTPRoute": true,
14+
"GRPCRoute": true,
15+
}
16+
17+
testInvalidTargetRefs(t, allowedKinds)
18+
testValidTargetRefs(t, allowedKinds)
19+
}
20+
21+
func testInvalidTargetRefs(t *testing.T, allowedKinds map[string]bool) {
22+
t.Helper()
23+
24+
tests := []struct {
25+
name string
26+
wantErrors string
27+
targetRef gatewayv1alpha2.LocalPolicyTargetReference
28+
}{
29+
{
30+
name: "Validate TargetRef is of an allowed kind",
31+
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
32+
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
33+
Kind: "InvalidKind",
34+
Group: "gateway.networking.k8s.io",
35+
},
36+
},
37+
{
38+
name: "Validate TargetRef is of an allowed kind",
39+
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
40+
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
41+
Kind: "TCPRoute",
42+
Group: "gateway.networking.k8s.io",
43+
},
44+
},
45+
}
46+
47+
for _, tt := range tests {
48+
t.Run(tt.name, func(t *testing.T) {
49+
50+
if _, ok := allowedKinds[string(tt.targetRef.Kind)]; !ok {
51+
gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'"
52+
53+
if tt.wantErrors != gotError {
54+
t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors)
55+
}
56+
}
57+
})
58+
}
59+
}
60+
61+
func testValidTargetRefs(t *testing.T, allowedKinds map[string]bool) {
62+
t.Helper()
63+
64+
tests := []struct {
65+
name string
66+
wantErrors string
67+
targetRef gatewayv1alpha2.LocalPolicyTargetReference
68+
}{
69+
{
70+
name: "Validate TargetRef is of an allowed kind",
71+
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
72+
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
73+
Kind: "Gateway",
74+
Group: "gateway.networking.k8s.io",
75+
},
76+
},
77+
{
78+
name: "Validate TargetRef is of an allowed kind",
79+
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
80+
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
81+
Kind: "HTTPRoute",
82+
Group: "gateway.networking.k8s.io",
83+
},
84+
},
85+
{
86+
name: "Validate TargetRef is of an allowed kind",
87+
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
88+
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
89+
Kind: "GRPCRoute",
90+
Group: "gateway.networking.k8s.io",
91+
},
92+
},
93+
}
94+
95+
for _, tt := range tests {
96+
t.Run(tt.name, func(t *testing.T) {
97+
98+
if _, ok := allowedKinds[string(tt.targetRef.Kind)]; !ok {
99+
gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'"
100+
101+
if tt.wantErrors == gotError {
102+
t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors)
103+
}
104+
}
105+
})
106+
}
107+
}

tests/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.24.2
55
replace github.com/nginx/nginx-gateway-fabric => ../
66

77
require (
8-
github.com/nginx/nginx-gateway-fabric v0.0.0
8+
github.com/nginx/nginx-gateway-fabric v1.6.2
99
github.com/onsi/ginkgo/v2 v2.23.4
1010
github.com/onsi/gomega v1.37.0
1111
github.com/prometheus/client_golang v1.22.0

0 commit comments

Comments
 (0)