Skip to content

Commit cd9bada

Browse files
committed
Update TargetRegGroup tests
1 parent d664f35 commit cd9bada

File tree

1 file changed

+52
-55
lines changed

1 file changed

+52
-55
lines changed

tests/cel/clientsettingspolicy_test.go

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,12 @@ func TestClientSettingsPoliciesTargetRefKind(t *testing.T) {
2525
}
2626

2727
func TestClientSettingsPoliciesTargetRefGroup(t *testing.T) {
28-
// Test valid and invalid TargetRef Group
29-
30-
allowedGroups := map[gatewayv1alpha2.LocalPolicyTargetReference]bool{
31-
{
32-
Group: "gateway.networking.k8s.io",
33-
}: true,
34-
}
35-
36-
testValidTargetRefGroup(t, allowedGroups)
37-
testInvalidTargetRefGroup(t, allowedGroups)
28+
// Test valid TargetRef Group
29+
// Valid group is: "gateway.networking.k8s.io"
30+
testValidTargetRefGroup(t)
31+
// Invalid groups should return an error
32+
// Examples of invalid groups: "invalid.networking.k8s.io", "discovery.k8s.io/v1"
33+
testInvalidTargetRefGroup(t)
3834
}
3935

4036
func testValidTargetRefKind(t *testing.T) {
@@ -138,19 +134,22 @@ func testInvalidTargetRefKind(t *testing.T) {
138134
}
139135
}
140136

141-
func testValidTargetRefGroup(t *testing.T, allowedGroups map[gatewayv1alpha2.LocalPolicyTargetReference]bool) {
137+
func testValidTargetRefGroup(t *testing.T) {
142138
t.Helper()
143139

144140
tests := []struct {
145-
name string
146-
wantErrors string
147-
targetRefGroup gatewayv1alpha2.LocalPolicyTargetReference
141+
name string
142+
wantErrors []string
143+
policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec
148144
}{
149145
{
150-
name: "Validate TargetRef group is gateway.networking.k8s.io",
151-
wantErrors: "TargetRef Group must be gateway.networking.k8s.io",
152-
targetRefGroup: gatewayv1alpha2.LocalPolicyTargetReference{
153-
Group: "gateway.networking.k8s.io",
146+
name: "Validate gateway.networking.k8s.io TargetRef Group is allowed",
147+
wantErrors: []string{"TargetRef Group must be gateway.networking.k8s.io."},
148+
policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
149+
TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{
150+
Kind: "Gateway",
151+
Group: "gateway.networking.k8s.io",
152+
},
154153
},
155154
},
156155
}
@@ -159,64 +158,62 @@ func testValidTargetRefGroup(t *testing.T, allowedGroups map[gatewayv1alpha2.Loc
159158
t.Run(tt.name, func(t *testing.T) {
160159
// Create a ClientSettingsPolicy with the targetRef from the test case.
161160
clientSettingsPolicy := &ngfAPIv1alpha1.ClientSettingsPolicy{
161+
ObjectMeta: metav1.ObjectMeta{
162+
Name: "test-policy",
163+
Namespace: "default",
164+
},
162165
Spec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
163-
TargetRef: tt.targetRefGroup,
166+
TargetRef: tt.policySpec.TargetRef,
164167
},
165168
}
166-
167-
if _, ok := allowedGroups[clientSettingsPolicy.Spec.TargetRef]; !ok {
168-
gotError := "TargetRef Group must be gateway.networking.k8s.io"
169-
170-
if tt.wantErrors == gotError {
171-
t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors)
172-
}
173-
}
169+
validateClientSettingsPolicy(t, clientSettingsPolicy, tt.wantErrors)
174170
})
175171
}
176172
}
177173

178-
func testInvalidTargetRefGroup(t *testing.T, allowedGroups map[gatewayv1alpha2.LocalPolicyTargetReference]bool) {
174+
func testInvalidTargetRefGroup(t *testing.T) {
179175
t.Helper()
180176

181177
tests := []struct {
182-
name string
183-
wantErrors string
184-
targetRefGroup gatewayv1alpha2.LocalPolicyTargetReference
178+
name string
179+
wantErrors []string
180+
policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec
185181
}{
186182
{
187-
name: "Validate TargetRef group is gateway.networking.k8s.io",
188-
wantErrors: "TargetRef Group must be gateway.networking.k8s.io",
189-
targetRefGroup: gatewayv1alpha2.LocalPolicyTargetReference{
190-
Group: "invalid.networking.k8s.io",
183+
name: "Validate invalid.networking.k8s.io TargetRef Group is not allowed",
184+
wantErrors: []string{"TargetRef Group must be gateway.networking.k8s.io."},
185+
policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
186+
TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{
187+
Kind: "Gateway",
188+
Group: "invalid.networking.k8s.io",
189+
},
191190
},
192191
},
193192
{
194-
name: "Validate TargetRef is of an allowed kind",
195-
wantErrors: "TargetRef Group must be gateway.networking.k8s.io",
196-
targetRefGroup: gatewayv1alpha2.LocalPolicyTargetReference{
197-
Group: "discovery.k8s.io/v1",
193+
name: "Validate discovery.k8s.io/v1 TargetRef Group is not allowed",
194+
wantErrors: []string{"TargetRef Group must be gateway.networking.k8s.io."},
195+
policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
196+
TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{
197+
Kind: "Gateway",
198+
Group: "discovery.k8s.io/v1",
199+
},
198200
},
199201
},
200202
}
201203

202204
for _, tt := range tests {
203205
t.Run(tt.name, func(t *testing.T) {
204-
t.Run(tt.name, func(t *testing.T) {
205-
// Create a ClientSettingsPolicy with the targetRef from the test case.
206-
clientSettingsPolicy := &ngfAPIv1alpha1.ClientSettingsPolicy{
207-
Spec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
208-
TargetRef: tt.targetRefGroup,
209-
},
210-
}
211-
212-
if _, ok := allowedGroups[clientSettingsPolicy.Spec.TargetRef]; !ok {
213-
gotError := "TargetRef Group must be gateway.networking.k8s.io"
214-
215-
if tt.wantErrors != gotError {
216-
t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors)
217-
}
218-
}
219-
})
206+
// Create a ClientSettingsPolicy with the targetRef from the test case.
207+
clientSettingsPolicy := &ngfAPIv1alpha1.ClientSettingsPolicy{
208+
ObjectMeta: metav1.ObjectMeta{
209+
Name: "test-policy",
210+
Namespace: "default",
211+
},
212+
Spec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
213+
TargetRef: tt.policySpec.TargetRef,
214+
},
215+
}
216+
validateClientSettingsPolicy(t, clientSettingsPolicy, tt.wantErrors)
220217
})
221218
}
222219
}

0 commit comments

Comments
 (0)