Skip to content

Commit c7129bd

Browse files
committed
Add tests for targetRefGroup
1 parent 48ec86b commit c7129bd

File tree

1 file changed

+86
-14
lines changed

1 file changed

+86
-14
lines changed

tests/cel/policies/clientsettingspolicies/targetRef_test.go

Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ func TestClientSettingsPoliciesTargetRef(t *testing.T) {
1313
"GRPCRoute": true,
1414
}
1515

16-
testInvalidTargetRefs(t, allowedKinds)
1716
testValidTargetRefs(t, allowedKinds)
17+
testInvalidTargetRefs(t, allowedKinds)
1818
}
1919

20-
func testInvalidTargetRefs(t *testing.T, allowedKinds map[string]bool) {
20+
func TestClientSettingsPoliciesTargetRefGroup(t *testing.T) {
21+
testValidTargetRefGroup(t)
22+
testInvalidTargetRefGroup(t)
23+
}
24+
25+
func testValidTargetRefs(t *testing.T, allowedKinds map[string]bool) {
2126
t.Helper()
2227

2328
tests := []struct {
@@ -29,15 +34,23 @@ func testInvalidTargetRefs(t *testing.T, allowedKinds map[string]bool) {
2934
name: "Validate TargetRef is of an allowed kind",
3035
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
3136
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
32-
Kind: "InvalidKind",
37+
Kind: "Gateway",
3338
Group: "gateway.networking.k8s.io",
3439
},
3540
},
3641
{
3742
name: "Validate TargetRef is of an allowed kind",
3843
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
3944
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
40-
Kind: "TCPRoute",
45+
Kind: "HTTPRoute",
46+
Group: "gateway.networking.k8s.io",
47+
},
48+
},
49+
{
50+
name: "Validate TargetRef is of an allowed kind",
51+
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
52+
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
53+
Kind: "GRPCRoute",
4154
Group: "gateway.networking.k8s.io",
4255
},
4356
},
@@ -48,15 +61,15 @@ func testInvalidTargetRefs(t *testing.T, allowedKinds map[string]bool) {
4861
if _, ok := allowedKinds[string(tt.targetRef.Kind)]; !ok {
4962
gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'"
5063

51-
if tt.wantErrors != gotError {
64+
if tt.wantErrors == gotError {
5265
t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors)
5366
}
5467
}
5568
})
5669
}
5770
}
5871

59-
func testValidTargetRefs(t *testing.T, allowedKinds map[string]bool) {
72+
func testInvalidTargetRefs(t *testing.T, allowedKinds map[string]bool) {
6073
t.Helper()
6174

6275
tests := []struct {
@@ -68,32 +81,54 @@ func testValidTargetRefs(t *testing.T, allowedKinds map[string]bool) {
6881
name: "Validate TargetRef is of an allowed kind",
6982
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
7083
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
71-
Kind: "Gateway",
84+
Kind: "InvalidKind",
7285
Group: "gateway.networking.k8s.io",
7386
},
7487
},
7588
{
7689
name: "Validate TargetRef is of an allowed kind",
7790
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
7891
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
79-
Kind: "HTTPRoute",
92+
Kind: "TCPRoute",
8093
Group: "gateway.networking.k8s.io",
8194
},
8295
},
96+
}
97+
98+
for _, tt := range tests {
99+
t.Run(tt.name, func(t *testing.T) {
100+
if _, ok := allowedKinds[string(tt.targetRef.Kind)]; !ok {
101+
gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'"
102+
103+
if tt.wantErrors != gotError {
104+
t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors)
105+
}
106+
}
107+
})
108+
}
109+
}
110+
111+
func testValidTargetRefGroup(t *testing.T) {
112+
t.Helper()
113+
114+
tests := []struct {
115+
name string
116+
wantErrors string
117+
targetRefGroup gatewayv1alpha2.LocalPolicyTargetReference
118+
}{
83119
{
84-
name: "Validate TargetRef is of an allowed kind",
85-
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
86-
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
87-
Kind: "GRPCRoute",
120+
name: "Validate TargetRef group is gateway.networking.k8s.io",
121+
wantErrors: "TargetRef Group must be gateway.networking.k8s.io",
122+
targetRefGroup: gatewayv1alpha2.LocalPolicyTargetReference{
88123
Group: "gateway.networking.k8s.io",
89124
},
90125
},
91126
}
92127

93128
for _, tt := range tests {
94129
t.Run(tt.name, func(t *testing.T) {
95-
if _, ok := allowedKinds[string(tt.targetRef.Kind)]; !ok {
96-
gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'"
130+
if tt.targetRefGroup.Group != "gateway.networking.k8s.io" {
131+
gotError := "TargetRef Group must be gateway.networking.k8s.io"
97132

98133
if tt.wantErrors == gotError {
99134
t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors)
@@ -102,3 +137,40 @@ func testValidTargetRefs(t *testing.T, allowedKinds map[string]bool) {
102137
})
103138
}
104139
}
140+
141+
func testInvalidTargetRefGroup(t *testing.T) {
142+
t.Helper()
143+
144+
tests := []struct {
145+
name string
146+
wantErrors string
147+
targetRefGroup gatewayv1alpha2.LocalPolicyTargetReference
148+
}{
149+
{
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: "invalid.networking.k8s.io",
154+
},
155+
},
156+
{
157+
name: "Validate TargetRef is of an allowed kind",
158+
wantErrors: "TargetRef Group must be gateway.networking.k8s.io",
159+
targetRefGroup: gatewayv1alpha2.LocalPolicyTargetReference{
160+
Group: "discovery.k8s.io/v1",
161+
},
162+
},
163+
}
164+
165+
for _, tt := range tests {
166+
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"
169+
170+
if tt.wantErrors != gotError {
171+
t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors)
172+
}
173+
}
174+
})
175+
}
176+
}

0 commit comments

Comments
 (0)