Debian Codegraph @

plugins/ipam/host-local/backend/allocator/config_test.go

raw symbols SCIP
1 // Copyright 2016 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package allocator
16
17 import (
18 "net"
19
20 . "github.com/onsi/ginkgo/v2"
21 . "github.com/onsi/gomega"
22
23 "github.com/containernetworking/cni/pkg/types"
24 )
25
26 var _ = Describe("IPAM config", func() {
27 It("Should parse an old-style config", func() {
28 input := `{
29 "cniVersion": "0.3.1",
30 "name": "mynet",
31 "type": "ipvlan",
32 "master": "foo0",
33 "ipam": {
34 "type": "host-local",
35 "subnet": "10.1.2.0/24",
36 "rangeStart": "10.1.2.9",
37 "rangeEnd": "10.1.2.20",
38 "gateway": "10.1.2.30"
39 }
40 }`
41 conf, version, err := LoadIPAMConfig([]byte(input), "")
42 Expect(err).NotTo(HaveOccurred())
43 Expect(version).Should(Equal("0.3.1"))
44
45 Expect(conf).To(Equal(&IPAMConfig{
46 Name: "mynet",
47 Type: "host-local",
48 Ranges: []RangeSet{
49 {
50 {
51 RangeStart: net.IP{10, 1, 2, 9},
52 RangeEnd: net.IP{10, 1, 2, 20},
53 Gateway: net.IP{10, 1, 2, 30},
54 Subnet: types.IPNet{
55 IP: net.IP{10, 1, 2, 0},
56 Mask: net.CIDRMask(24, 32),
57 },
58 },
59 },
60 },
61 }))
62 })
63
64 It("Should parse a new-style config", func() {
65 input := `{
66 "cniVersion": "0.3.1",
67 "name": "mynet",
68 "type": "ipvlan",
69 "master": "foo0",
70 "ipam": {
71 "type": "host-local",
72 "ranges": [
73 [
74 {
75 "subnet": "10.1.2.0/24",
76 "rangeStart": "10.1.2.9",
77 "rangeEnd": "10.1.2.20",
78 "gateway": "10.1.2.30"
79 },
80 {
81 "subnet": "10.1.4.0/24"
82 }
83 ],
84 [{
85 "subnet": "11.1.2.0/24",
86 "rangeStart": "11.1.2.9",
87 "rangeEnd": "11.1.2.20",
88 "gateway": "11.1.2.30"
89 }]
90 ]
91 }
92 }`
93 conf, version, err := LoadIPAMConfig([]byte(input), "")
94 Expect(err).NotTo(HaveOccurred())
95 Expect(version).Should(Equal("0.3.1"))
96
97 Expect(conf).To(Equal(&IPAMConfig{
98 Name: "mynet",
99 Type: "host-local",
100 Ranges: []RangeSet{
101 {
102 {
103 RangeStart: net.IP{10, 1, 2, 9},
104 RangeEnd: net.IP{10, 1, 2, 20},
105 Gateway: net.IP{10, 1, 2, 30},
106 Subnet: types.IPNet{
107 IP: net.IP{10, 1, 2, 0},
108 Mask: net.CIDRMask(24, 32),
109 },
110 },
111 {
112 RangeStart: net.IP{10, 1, 4, 1},
113 RangeEnd: net.IP{10, 1, 4, 254},
114 Gateway: net.IP{10, 1, 4, 1},
115 Subnet: types.IPNet{
116 IP: net.IP{10, 1, 4, 0},
117 Mask: net.CIDRMask(24, 32),
118 },
119 },
120 },
121 {
122 {
123 RangeStart: net.IP{11, 1, 2, 9},
124 RangeEnd: net.IP{11, 1, 2, 20},
125 Gateway: net.IP{11, 1, 2, 30},
126 Subnet: types.IPNet{
127 IP: net.IP{11, 1, 2, 0},
128 Mask: net.CIDRMask(24, 32),
129 },
130 },
131 },
132 },
133 }))
134 })
135
136 It("Should parse a mixed config with runtime args", func() {
137 input := `{
138 "cniVersion": "0.3.1",
139 "name": "mynet",
140 "type": "ipvlan",
141 "master": "foo0",
142 "runtimeConfig": {
143 "irrelevant": "a",
144 "ipRanges": [
145 [{ "subnet": "12.1.3.0/24" }]
146 ]
147 },
148 "ipam": {
149 "type": "host-local",
150 "subnet": "10.1.2.0/24",
151 "rangeStart": "10.1.2.9",
152 "rangeEnd": "10.1.2.20",
153 "gateway": "10.1.2.30",
154 "ranges": [[
155 {
156 "subnet": "11.1.2.0/24",
157 "rangeStart": "11.1.2.9",
158 "rangeEnd": "11.1.2.20",
159 "gateway": "11.1.2.30"
160 }
161 ]]
162 }
163 }`
164 conf, version, err := LoadIPAMConfig([]byte(input), "")
165 Expect(err).NotTo(HaveOccurred())
166 Expect(version).Should(Equal("0.3.1"))
167
168 Expect(conf).To(Equal(&IPAMConfig{
169 Name: "mynet",
170 Type: "host-local",
171 Ranges: []RangeSet{
172 { // The RuntimeConfig should always be first
173 {
174 RangeStart: net.IP{12, 1, 3, 1},
175 RangeEnd: net.IP{12, 1, 3, 254},
176 Gateway: net.IP{12, 1, 3, 1},
177 Subnet: types.IPNet{
178 IP: net.IP{12, 1, 3, 0},
179 Mask: net.CIDRMask(24, 32),
180 },
181 },
182 },
183 {
184 {
185 RangeStart: net.IP{10, 1, 2, 9},
186 RangeEnd: net.IP{10, 1, 2, 20},
187 Gateway: net.IP{10, 1, 2, 30},
188 Subnet: types.IPNet{
189 IP: net.IP{10, 1, 2, 0},
190 Mask: net.CIDRMask(24, 32),
191 },
192 },
193 },
194 {
195 {
196 RangeStart: net.IP{11, 1, 2, 9},
197 RangeEnd: net.IP{11, 1, 2, 20},
198 Gateway: net.IP{11, 1, 2, 30},
199 Subnet: types.IPNet{
200 IP: net.IP{11, 1, 2, 0},
201 Mask: net.CIDRMask(24, 32),
202 },
203 },
204 },
205 },
206 }))
207 })
208
209 Context("Should parse CNI_ARGS env", func() {
210 It("without prefix", func() {
211 input := `{
212 "cniVersion": "0.3.1",
213 "name": "mynet",
214 "type": "ipvlan",
215 "master": "foo0",
216 "ipam": {
217 "type": "host-local",
218 "ranges": [[
219 {
220 "subnet": "10.1.2.0/24",
221 "rangeStart": "10.1.2.9",
222 "rangeEnd": "10.1.2.20",
223 "gateway": "10.1.2.30"
224 }
225 ]]
226 }
227 }`
228
229 envArgs := "IP=10.1.2.10"
230
231 conf, _, err := LoadIPAMConfig([]byte(input), envArgs)
232 Expect(err).NotTo(HaveOccurred())
233 Expect(conf.IPArgs).To(Equal([]net.IP{{10, 1, 2, 10}}))
234 })
235
236 It("with prefix", func() {
237 input := `{
238 "cniVersion": "0.3.1",
239 "name": "mynet",
240 "type": "ipvlan",
241 "master": "foo0",
242 "ipam": {
243 "type": "host-local",
244 "ranges": [[
245 {
246 "subnet": "10.1.2.0/24",
247 "rangeStart": "10.1.2.9",
248 "rangeEnd": "10.1.2.20",
249 "gateway": "10.1.2.30"
250 }
251 ]]
252 }
253 }`
254
255 envArgs := "IP=10.1.2.11/24"
256
257 conf, _, err := LoadIPAMConfig([]byte(input), envArgs)
258 Expect(err).NotTo(HaveOccurred())
259 Expect(conf.IPArgs).To(Equal([]net.IP{{10, 1, 2, 11}}))
260 })
261 })
262
263 Context("Should parse config args", func() {
264 It("without prefix", func() {
265 input := `{
266 "cniVersion": "0.3.1",
267 "name": "mynet",
268 "type": "ipvlan",
269 "master": "foo0",
270 "args": {
271 "cni": {
272 "ips": [ "10.1.2.11", "11.11.11.11", "2001:db8:1::11"]
273 }
274 },
275 "ipam": {
276 "type": "host-local",
277 "ranges": [
278 [{
279 "subnet": "10.1.2.0/24",
280 "rangeStart": "10.1.2.9",
281 "rangeEnd": "10.1.2.20",
282 "gateway": "10.1.2.30"
283 }],
284 [{
285 "subnet": "11.1.2.0/24",
286 "rangeStart": "11.1.2.9",
287 "rangeEnd": "11.1.2.20",
288 "gateway": "11.1.2.30"
289 }],
290 [{
291 "subnet": "2001:db8:1::/64"
292 }]
293 ]
294 }
295 }`
296
297 envArgs := "IP=10.1.2.10"
298
299 conf, _, err := LoadIPAMConfig([]byte(input), envArgs)
300 Expect(err).NotTo(HaveOccurred())
301 Expect(conf.IPArgs).To(Equal([]net.IP{
302 {10, 1, 2, 10},
303 {10, 1, 2, 11},
304 {11, 11, 11, 11},
305 net.ParseIP("2001:db8:1::11"),
306 }))
307 })
308
309 It("with prefix", func() {
310 input := `{
311 "cniVersion": "0.3.1",
312 "name": "mynet",
313 "type": "ipvlan",
314 "master": "foo0",
315 "args": {
316 "cni": {
317 "ips": [ "10.1.2.11/24", "11.11.11.11/24", "2001:db8:1::11/64"]
318 }
319 },
320 "ipam": {
321 "type": "host-local",
322 "ranges": [
323 [{
324 "subnet": "10.1.2.0/24",
325 "rangeStart": "10.1.2.9",
326 "rangeEnd": "10.1.2.20",
327 "gateway": "10.1.2.30"
328 }],
329 [{
330 "subnet": "11.1.2.0/24",
331 "rangeStart": "11.1.2.9",
332 "rangeEnd": "11.1.2.20",
333 "gateway": "11.1.2.30"
334 }],
335 [{
336 "subnet": "2001:db8:1::/64"
337 }]
338 ]
339 }
340 }`
341
342 envArgs := "IP=10.1.2.10/24"
343
344 conf, _, err := LoadIPAMConfig([]byte(input), envArgs)
345 Expect(err).NotTo(HaveOccurred())
346 Expect(conf.IPArgs).To(Equal([]net.IP{
347 {10, 1, 2, 10},
348 {10, 1, 2, 11},
349 {11, 11, 11, 11},
350 net.ParseIP("2001:db8:1::11"),
351 }))
352 })
353 })
354
355 It("Should detect overlap between rangesets", func() {
356 input := `{
357 "cniVersion": "0.3.1",
358 "name": "mynet",
359 "type": "ipvlan",
360 "master": "foo0",
361 "ipam": {
362 "type": "host-local",
363 "ranges": [
364 [
365 {"subnet": "10.1.2.0/24"},
366 {"subnet": "10.2.2.0/24"}
367 ],
368 [
369 { "subnet": "10.1.4.0/24"},
370 { "subnet": "10.1.6.0/24"},
371 { "subnet": "10.1.8.0/24"},
372 { "subnet": "10.1.2.0/24"}
373 ]
374 ]
375 }
376 }`
377 _, _, err := LoadIPAMConfig([]byte(input), "")
378 Expect(err).To(MatchError("range set 0 overlaps with 1"))
379 })
380
381 It("Should detect overlap within rangeset", func() {
382 input := `{
383 "cniVersion": "0.3.1",
384 "name": "mynet",
385 "type": "ipvlan",
386 "master": "foo0",
387 "ipam": {
388 "type": "host-local",
389 "ranges": [
390 [
391 { "subnet": "10.1.0.0/22" },
392 { "subnet": "10.1.2.0/24" }
393 ]
394 ]
395 }
396 }`
397 _, _, err := LoadIPAMConfig([]byte(input), "")
398 Expect(err).To(MatchError("invalid range set 0: subnets 10.1.0.1-10.1.3.254 and 10.1.2.1-10.1.2.254 overlap"))
399 })
400
401 It("should error on rangesets with different families", func() {
402 input := `{
403 "cniVersion": "0.3.1",
404 "name": "mynet",
405 "type": "ipvlan",
406 "master": "foo0",
407 "ipam": {
408 "type": "host-local",
409 "ranges": [
410 [
411 { "subnet": "10.1.0.0/22" },
412 { "subnet": "2001:db8:5::/64" }
413 ]
414 ]
415 }
416 }`
417 _, _, err := LoadIPAMConfig([]byte(input), "")
418 Expect(err).To(MatchError("invalid range set 0: mixed address families"))
419 })
420
421 It("Should should error on too many ranges", func() {
422 input := `{
423 "cniVersion": "0.2.0",
424 "name": "mynet",
425 "type": "ipvlan",
426 "master": "foo0",
427 "ipam": {
428 "type": "host-local",
429 "ranges": [
430 [{"subnet": "10.1.2.0/24"}],
431 [{"subnet": "11.1.2.0/24"}]
432 ]
433 }
434 }`
435 _, _, err := LoadIPAMConfig([]byte(input), "")
436 Expect(err).To(MatchError("CNI version 0.2.0 does not support more than 1 address per family"))
437 })
438
439 It("Should allow one v4 and v6 range for 0.2.0", func() {
440 input := `{
441 "cniVersion": "0.2.0",
442 "name": "mynet",
443 "type": "ipvlan",
444 "master": "foo0",
445 "ipam": {
446 "type": "host-local",
447 "ranges": [
448 [{"subnet": "10.1.2.0/24"}],
449 [{"subnet": "2001:db8:1::/48"}]
450 ]
451 }
452 }`
453 _, _, err := LoadIPAMConfig([]byte(input), "")
454 Expect(err).NotTo(HaveOccurred())
455 })
456
457 It("Should parse custom IPs from runtime configuration", func() {
458 input := `{
459 "cniVersion": "0.3.1",
460 "name": "mynet",
461 "type": "ipvlan",
462 "master": "foo0",
463 "runtimeConfig": {
464 "ips": ["192.168.0.1", "192.168.0.5/24", "2001:db8::1/64"]
465 },
466 "ipam": {
467 "type": "host-local",
468 "subnet": "10.1.2.0/24"
469 }
470 }`
471 conf, version, err := LoadIPAMConfig([]byte(input), "")
472 Expect(err).NotTo(HaveOccurred())
473 Expect(version).Should(Equal("0.3.1"))
474
475 Expect(conf.IPArgs).To(Equal([]net.IP{
476 net.IPv4(192, 168, 0, 1).To4(),
477 net.IPv4(192, 168, 0, 5).To4(),
478 net.ParseIP("2001:db8::1"),
479 }))
480 })
481 })