@@ -3,26 +3,44 @@ package cel
3
3
import (
4
4
"testing"
5
5
6
+ ngfAPIv1alpha1 "github.com/nginx/nginx-gateway-fabric/apis/v1alpha1"
6
7
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
7
8
)
8
9
9
10
func TestClientSettingsPoliciesTargetRefKind (t * testing.T ) {
10
- allowedKinds := map [string ]bool {
11
- "Gateway" : true ,
12
- "HTTPRoute" : true ,
13
- "GRPCRoute" : true ,
11
+ allowedKinds := map [gatewayv1alpha2.LocalPolicyTargetReference ]bool {
12
+ {
13
+ Kind : "Gateway" ,
14
+ Group : "gateway.networking.k8s.io" ,
15
+ }: true ,
16
+ {
17
+ Kind : "HTTPRoute" ,
18
+ Group : "gateway.networking.k8s.io" ,
19
+ }: true ,
20
+ {
21
+ Kind : "GRPCRoute" ,
22
+ Group : "gateway.networking.k8s.io" ,
23
+ }: true ,
14
24
}
15
25
16
26
testValidTargetRefKind (t , allowedKinds )
17
27
testInvalidTargetRefKind (t , allowedKinds )
18
28
}
19
29
20
30
func TestClientSettingsPoliciesTargetRefGroup (t * testing.T ) {
21
- testValidTargetRefGroup (t )
22
- testInvalidTargetRefGroup (t )
31
+ // Test valid and invalid TargetRef Group
32
+
33
+ allowedGroups := map [gatewayv1alpha2.LocalPolicyTargetReference ]bool {
34
+ {
35
+ Group : "gateway.networking.k8s.io" ,
36
+ }: true ,
37
+ }
38
+
39
+ testValidTargetRefGroup (t , allowedGroups )
40
+ testInvalidTargetRefGroup (t , allowedGroups )
23
41
}
24
42
25
- func testValidTargetRefKind (t * testing.T , allowedKinds map [string ]bool ) {
43
+ func testValidTargetRefKind (t * testing.T , allowedKinds map [gatewayv1alpha2. LocalPolicyTargetReference ]bool ) {
26
44
t .Helper ()
27
45
28
46
tests := []struct {
@@ -58,7 +76,14 @@ func testValidTargetRefKind(t *testing.T, allowedKinds map[string]bool) {
58
76
59
77
for _ , tt := range tests {
60
78
t .Run (tt .name , func (t * testing.T ) {
61
- if _ , ok := allowedKinds [string (tt .targetRef .Kind )]; ! ok {
79
+ // Create a ClientSettingsPolicy with the targetRef from the test case.
80
+ clientSettingsPolicy := & ngfAPIv1alpha1.ClientSettingsPolicy {
81
+ Spec : ngfAPIv1alpha1.ClientSettingsPolicySpec {
82
+ TargetRef : tt .targetRef ,
83
+ },
84
+ }
85
+
86
+ if _ , ok := allowedKinds [clientSettingsPolicy .Spec .TargetRef ]; ! ok {
62
87
gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'"
63
88
64
89
if tt .wantErrors == gotError {
@@ -69,7 +94,7 @@ func testValidTargetRefKind(t *testing.T, allowedKinds map[string]bool) {
69
94
}
70
95
}
71
96
72
- func testInvalidTargetRefKind (t * testing.T , allowedKinds map [string ]bool ) {
97
+ func testInvalidTargetRefKind (t * testing.T , allowedKinds map [gatewayv1alpha2. LocalPolicyTargetReference ]bool ) {
73
98
t .Helper ()
74
99
75
100
tests := []struct {
@@ -97,7 +122,14 @@ func testInvalidTargetRefKind(t *testing.T, allowedKinds map[string]bool) {
97
122
98
123
for _ , tt := range tests {
99
124
t .Run (tt .name , func (t * testing.T ) {
100
- if _ , ok := allowedKinds [string (tt .targetRef .Kind )]; ! ok {
125
+ // Create a ClientSettingsPolicy with the targetRef from the test case.
126
+ clientSettingsPolicy := & ngfAPIv1alpha1.ClientSettingsPolicy {
127
+ Spec : ngfAPIv1alpha1.ClientSettingsPolicySpec {
128
+ TargetRef : tt .targetRef ,
129
+ },
130
+ }
131
+
132
+ if _ , ok := allowedKinds [clientSettingsPolicy .Spec .TargetRef ]; ! ok {
101
133
gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'"
102
134
103
135
if tt .wantErrors != gotError {
@@ -108,7 +140,7 @@ func testInvalidTargetRefKind(t *testing.T, allowedKinds map[string]bool) {
108
140
}
109
141
}
110
142
111
- func testValidTargetRefGroup (t * testing.T ) {
143
+ func testValidTargetRefGroup (t * testing.T , allowedGroups map [gatewayv1alpha2. LocalPolicyTargetReference ] bool ) {
112
144
t .Helper ()
113
145
114
146
tests := []struct {
@@ -127,7 +159,14 @@ func testValidTargetRefGroup(t *testing.T) {
127
159
128
160
for _ , tt := range tests {
129
161
t .Run (tt .name , func (t * testing.T ) {
130
- if tt .targetRefGroup .Group != "gateway.networking.k8s.io" {
162
+ // Create a ClientSettingsPolicy with the targetRef from the test case.
163
+ clientSettingsPolicy := & ngfAPIv1alpha1.ClientSettingsPolicy {
164
+ Spec : ngfAPIv1alpha1.ClientSettingsPolicySpec {
165
+ TargetRef : tt .targetRefGroup ,
166
+ },
167
+ }
168
+
169
+ if _ , ok := allowedGroups [clientSettingsPolicy .Spec .TargetRef ]; ! ok {
131
170
gotError := "TargetRef Group must be gateway.networking.k8s.io"
132
171
133
172
if tt .wantErrors == gotError {
@@ -138,7 +177,7 @@ func testValidTargetRefGroup(t *testing.T) {
138
177
}
139
178
}
140
179
141
- func testInvalidTargetRefGroup (t * testing.T ) {
180
+ func testInvalidTargetRefGroup (t * testing.T , allowedGroups map [gatewayv1alpha2. LocalPolicyTargetReference ] bool ) {
142
181
t .Helper ()
143
182
144
183
tests := []struct {
@@ -164,13 +203,22 @@ func testInvalidTargetRefGroup(t *testing.T) {
164
203
165
204
for _ , tt := range tests {
166
205
t .Run (tt .name , func (t * testing.T ) {
167
- if tt .targetRefGroup .Group != "gateway.networking.k8s.io" {
168
- gotError := "TargetRef Group must be gateway.networking.k8s.io"
206
+ t .Run (tt .name , func (t * testing.T ) {
207
+ // Create a ClientSettingsPolicy with the targetRef from the test case.
208
+ clientSettingsPolicy := & ngfAPIv1alpha1.ClientSettingsPolicy {
209
+ Spec : ngfAPIv1alpha1.ClientSettingsPolicySpec {
210
+ TargetRef : tt .targetRefGroup ,
211
+ },
212
+ }
169
213
170
- if tt .wantErrors != gotError {
171
- t .Errorf ("Test %s failed: got error %q, want %q" , tt .name , gotError , tt .wantErrors )
214
+ if _ , ok := allowedGroups [clientSettingsPolicy .Spec .TargetRef ]; ! ok {
215
+ gotError := "TargetRef Group must be gateway.networking.k8s.io"
216
+
217
+ if tt .wantErrors != gotError {
218
+ t .Errorf ("Test %s failed: got error %q, want %q" , tt .name , gotError , tt .wantErrors )
219
+ }
172
220
}
173
- }
221
+ })
174
222
})
175
223
}
176
224
}
0 commit comments