Skip to content

Commit f22269a

Browse files
committed
Update tests to create a ClientSettingsPolicy resource during tests
1 parent d921ed2 commit f22269a

File tree

1 file changed

+66
-18
lines changed

1 file changed

+66
-18
lines changed

tests/cel/clientsettingspolicy_test.go

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,44 @@ package cel
33
import (
44
"testing"
55

6+
ngfAPIv1alpha1 "github.com/nginx/nginx-gateway-fabric/apis/v1alpha1"
67
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
78
)
89

910
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,
1424
}
1525

1626
testValidTargetRefKind(t, allowedKinds)
1727
testInvalidTargetRefKind(t, allowedKinds)
1828
}
1929

2030
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)
2341
}
2442

25-
func testValidTargetRefKind(t *testing.T, allowedKinds map[string]bool) {
43+
func testValidTargetRefKind(t *testing.T, allowedKinds map[gatewayv1alpha2.LocalPolicyTargetReference]bool) {
2644
t.Helper()
2745

2846
tests := []struct {
@@ -58,7 +76,14 @@ func testValidTargetRefKind(t *testing.T, allowedKinds map[string]bool) {
5876

5977
for _, tt := range tests {
6078
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 {
6287
gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'"
6388

6489
if tt.wantErrors == gotError {
@@ -69,7 +94,7 @@ func testValidTargetRefKind(t *testing.T, allowedKinds map[string]bool) {
6994
}
7095
}
7196

72-
func testInvalidTargetRefKind(t *testing.T, allowedKinds map[string]bool) {
97+
func testInvalidTargetRefKind(t *testing.T, allowedKinds map[gatewayv1alpha2.LocalPolicyTargetReference]bool) {
7398
t.Helper()
7499

75100
tests := []struct {
@@ -97,7 +122,14 @@ func testInvalidTargetRefKind(t *testing.T, allowedKinds map[string]bool) {
97122

98123
for _, tt := range tests {
99124
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 {
101133
gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'"
102134

103135
if tt.wantErrors != gotError {
@@ -108,7 +140,7 @@ func testInvalidTargetRefKind(t *testing.T, allowedKinds map[string]bool) {
108140
}
109141
}
110142

111-
func testValidTargetRefGroup(t *testing.T) {
143+
func testValidTargetRefGroup(t *testing.T, allowedGroups map[gatewayv1alpha2.LocalPolicyTargetReference]bool) {
112144
t.Helper()
113145

114146
tests := []struct {
@@ -127,7 +159,14 @@ func testValidTargetRefGroup(t *testing.T) {
127159

128160
for _, tt := range tests {
129161
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 {
131170
gotError := "TargetRef Group must be gateway.networking.k8s.io"
132171

133172
if tt.wantErrors == gotError {
@@ -138,7 +177,7 @@ func testValidTargetRefGroup(t *testing.T) {
138177
}
139178
}
140179

141-
func testInvalidTargetRefGroup(t *testing.T) {
180+
func testInvalidTargetRefGroup(t *testing.T, allowedGroups map[gatewayv1alpha2.LocalPolicyTargetReference]bool) {
142181
t.Helper()
143182

144183
tests := []struct {
@@ -164,13 +203,22 @@ func testInvalidTargetRefGroup(t *testing.T) {
164203

165204
for _, tt := range tests {
166205
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+
}
169213

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+
}
172220
}
173-
}
221+
})
174222
})
175223
}
176224
}

0 commit comments

Comments
 (0)