1
16
17 package validation
18
19 import (
20 "strings"
21 "testing"
22
23 "k8s.io/apimachinery/pkg/util/validation/field"
24
25 gatewayv1b1 "sigs.k8s.io/gateway-api/apis/v1beta1"
26 )
27
28 var path = field.Path{}
29
30 func TestValidateParentRefs(t *testing.T) {
31 namespace := gatewayv1b1.Namespace("example-namespace")
32 kind := gatewayv1b1.Kind("Gateway")
33 sectionA := gatewayv1b1.SectionName("Section A")
34 sectionB := gatewayv1b1.SectionName("Section B")
35 sectionC := gatewayv1b1.SectionName("Section C")
36
37 tests := []struct {
38 name string
39 parentRefs []gatewayv1b1.ParentReference
40 err string
41 }{{
42 name: "valid ParentRefs includes 1 reference",
43 parentRefs: []gatewayv1b1.ParentReference{
44 {
45 Name: "example",
46 Namespace: &namespace,
47 Kind: &kind,
48 SectionName: §ionA,
49 },
50 },
51 err: "",
52 }, {
53 name: "valid ParentRefs includes 2 references",
54 parentRefs: []gatewayv1b1.ParentReference{
55 {
56 Name: "example",
57 Namespace: &namespace,
58 Kind: &kind,
59 SectionName: §ionA,
60 },
61 {
62 Name: "example",
63 Namespace: &namespace,
64 Kind: &kind,
65 SectionName: §ionB,
66 },
67 },
68 err: "",
69 }, {
70 name: "valid ParentRefs when different references have the same section name",
71 parentRefs: []gatewayv1b1.ParentReference{
72 {
73 Name: "example A",
74 Namespace: &namespace,
75 Kind: &kind,
76 SectionName: §ionA,
77 },
78 {
79 Name: "example B",
80 Namespace: &namespace,
81 Kind: &kind,
82 SectionName: §ionA,
83 },
84 },
85 err: "",
86 }, {
87 name: "valid ParentRefs includes more references to the same parent",
88 parentRefs: []gatewayv1b1.ParentReference{
89 {
90 Name: "example",
91 Namespace: &namespace,
92 Kind: &kind,
93 SectionName: §ionA,
94 },
95 {
96 Name: "example",
97 Namespace: &namespace,
98 Kind: &kind,
99 SectionName: §ionB,
100 },
101 {
102 Name: "example",
103 Namespace: &namespace,
104 Kind: &kind,
105 SectionName: §ionC,
106 },
107 },
108 err: "",
109 }, {
110 name: "invalid ParentRefs due to the same section names to the same parentRefs",
111 parentRefs: []gatewayv1b1.ParentReference{
112 {
113 Name: "example",
114 Namespace: &namespace,
115 Kind: &kind,
116 SectionName: §ionA,
117 },
118 {
119 Name: "example",
120 Namespace: &namespace,
121 Kind: &kind,
122 SectionName: §ionA,
123 },
124 },
125 err: "must be unique when ParentRefs",
126 }, {
127 name: "invalid ParentRefs due to section names not set to the same ParentRefs",
128 parentRefs: []gatewayv1b1.ParentReference{
129 {
130 Name: "example",
131 },
132 {
133 Name: "example",
134 },
135 },
136 err: "sectionNames or ports must be specified",
137 }, {
138 name: "invalid ParentRefs due to more same section names to the same ParentRefs",
139 parentRefs: []gatewayv1b1.ParentReference{
140 {
141 Name: "example",
142 Namespace: &namespace,
143 SectionName: §ionA,
144 },
145 {
146 Name: "example",
147 Namespace: &namespace,
148 SectionName: nil,
149 },
150 {
151 Name: "example",
152 Namespace: &namespace,
153 SectionName: §ionB,
154 },
155 {
156 Name: "example",
157 Namespace: &namespace,
158 SectionName: §ionA,
159 },
160 },
161 err: "sectionNames or ports must be specified",
162 }, {
163 name: "invalid ParentRefs when one ParentRef section name not set to the same ParentRefs",
164 parentRefs: []gatewayv1b1.ParentReference{
165 {
166 Name: "example",
167 Namespace: &namespace,
168 SectionName: nil,
169 },
170 {
171 Name: "example",
172 Namespace: &namespace,
173 SectionName: §ionA,
174 },
175 },
176 err: "sectionNames or ports must be specified",
177 }, {
178 name: "invalid ParentRefs when next ParentRef section name not set to the same ParentRefs",
179 parentRefs: []gatewayv1b1.ParentReference{
180 {
181 Name: "example",
182 Namespace: &namespace,
183 SectionName: §ionA,
184 },
185 {
186 Name: "example",
187 Namespace: &namespace,
188 SectionName: nil,
189 },
190 },
191 err: "sectionNames or ports must be specified",
192 }, {
193 name: "valid ParentRefs with multiple port references to the same parent",
194 parentRefs: []gatewayv1b1.ParentReference{
195 {
196 Name: "example",
197 Namespace: &namespace,
198 Port: ptrTo(gatewayv1b1.PortNumber(80)),
199 },
200 {
201 Name: "example",
202 Namespace: &namespace,
203 Port: ptrTo(gatewayv1b1.PortNumber(81)),
204 },
205 },
206 err: "",
207 }, {
208 name: "valid ParentRefs with multiple mixed references to the same parent",
209 parentRefs: []gatewayv1b1.ParentReference{
210 {
211 Name: "example",
212 Namespace: &namespace,
213 Port: ptrTo(gatewayv1b1.PortNumber(80)),
214 },
215 {
216 Name: "example",
217 Namespace: &namespace,
218 SectionName: §ionA,
219 },
220 },
221 err: "",
222 }, {
223 name: "invalid ParentRefs due to same port references to the same parent",
224 parentRefs: []gatewayv1b1.ParentReference{
225 {
226 Name: "example",
227 Namespace: &namespace,
228 Port: ptrTo(gatewayv1b1.PortNumber(80)),
229 },
230 {
231 Name: "example",
232 Namespace: &namespace,
233 Port: ptrTo(gatewayv1b1.PortNumber(80)),
234 },
235 },
236 err: "port: Invalid value: 80: must be unique when ParentRefs",
237 }, {
238 name: "invalid ParentRefs due to mixed port references to the same parent",
239 parentRefs: []gatewayv1b1.ParentReference{
240 {
241 Name: "example",
242 Namespace: &namespace,
243 Port: ptrTo(gatewayv1b1.PortNumber(80)),
244 },
245 {
246 Name: "example",
247 Namespace: &namespace,
248 Port: nil,
249 },
250 },
251 err: "Required value: sectionNames or ports must be specified",
252 }, {
253 name: "valid ParentRefs with multiple same port references to different section of a parent",
254 parentRefs: []gatewayv1b1.ParentReference{
255 {
256 Name: "example",
257 Namespace: &namespace,
258 Port: ptrTo(gatewayv1b1.PortNumber(80)),
259 SectionName: §ionA,
260 },
261 {
262 Name: "example",
263 Namespace: &namespace,
264 Port: ptrTo(gatewayv1b1.PortNumber(80)),
265 SectionName: §ionB,
266 },
267 },
268 err: "",
269 }}
270
271 for _, tc := range tests {
272 t.Run(tc.name, func(t *testing.T) {
273 spec := gatewayv1b1.CommonRouteSpec{
274 ParentRefs: tc.parentRefs,
275 }
276 errs := ValidateParentRefs(spec.ParentRefs, path.Child("spec"))
277 if tc.err == "" {
278 if len(errs) != 0 {
279 t.Errorf("got %d errors, want none: %s", len(errs), errs)
280 }
281 } else {
282 if errs == nil {
283 t.Errorf("got no errors, want %q", tc.err)
284 } else if !strings.Contains(errs.ToAggregate().Error(), tc.err) {
285 t.Errorf("got %d errors, want %q: %s", len(errs), tc.err, errs)
286 }
287 }
288 })
289 }
290 }
291
View as plain text