Debian Codegraph @

plugins/ipam/static/static_test.go

raw symbols SCIP
1 // Copyright 2018 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 main
16
17 import (
18 "fmt"
19 "net"
20 "strings"
21
22 . "github.com/onsi/ginkgo/v2"
23 . "github.com/onsi/gomega"
24
25 "github.com/containernetworking/cni/pkg/skel"
26 "github.com/containernetworking/cni/pkg/types"
27 types100 "github.com/containernetworking/cni/pkg/types/100"
28 "github.com/containernetworking/plugins/pkg/testutils"
29 )
30
31 var _ = Describe("static Operations", func() {
32 for _, ver := range testutils.AllSpecVersions {
33 // Redefine ver inside for scope so real value is picked up by each dynamically defined It()
34 // See Gingkgo's "Patterns for dynamically generating tests" documentation.
35 ver := ver
36
37 It(fmt.Sprintf("[%s] allocates and releases addresses with ADD/DEL", ver), func() {
38 const ifname string = "eth0"
39 const nspath string = "/some/where"
40
41 conf := fmt.Sprintf(`{
42 "cniVersion": "%s",
43 "name": "mynet",
44 "type": "ipvlan",
45 "master": "foo0",
46 "ipam": {
47 "type": "static",
48 "addresses": [ {
49 "address": "10.10.0.1/24",
50 "gateway": "10.10.0.254"
51 },
52 {
53 "address": "3ffe:ffff:0:01ff::1/64",
54 "gateway": "3ffe:ffff:0::1"
55 }],
56 "routes": [
57 { "dst": "0.0.0.0/0" },
58 { "dst": "192.168.0.0/16", "gw": "10.10.5.1" },
59 { "dst": "3ffe:ffff:0:01ff::1/64" }],
60 "dns": {
61 "nameservers" : ["8.8.8.8"],
62 "domain": "example.com",
63 "search": [ "example.com" ]
64 }
65 }
66 }`, ver)
67
68 args := &skel.CmdArgs{
69 ContainerID: "dummy",
70 Netns: nspath,
71 IfName: ifname,
72 StdinData: []byte(conf),
73 }
74
75 // Allocate the IP
76 r, raw, err := testutils.CmdAddWithArgs(args, func() error {
77 return cmdAdd(args)
78 })
79 Expect(err).NotTo(HaveOccurred())
80 if testutils.SpecVersionHasIPVersion(ver) {
81 Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0))
82 }
83
84 result, err := types100.GetResult(r)
85 Expect(err).NotTo(HaveOccurred())
86
87 // Gomega is cranky about slices with different caps
88 Expect(*result.IPs[0]).To(Equal(
89 types100.IPConfig{
90 Address: mustCIDR("10.10.0.1/24"),
91 Gateway: net.ParseIP("10.10.0.254"),
92 }))
93
94 Expect(*result.IPs[1]).To(Equal(
95 types100.IPConfig{
96 Address: mustCIDR("3ffe:ffff:0:01ff::1/64"),
97 Gateway: net.ParseIP("3ffe:ffff:0::1"),
98 },
99 ))
100 Expect(result.IPs).To(HaveLen(2))
101
102 Expect(result.Routes).To(Equal([]*types.Route{
103 {Dst: mustCIDR("0.0.0.0/0")},
104 {Dst: mustCIDR("192.168.0.0/16"), GW: net.ParseIP("10.10.5.1")},
105 {Dst: mustCIDR("3ffe:ffff:0:01ff::1/64")},
106 }))
107
108 // Release the IP
109 err = testutils.CmdDelWithArgs(args, func() error {
110 return cmdDel(args)
111 })
112 Expect(err).NotTo(HaveOccurred())
113 })
114
115 It(fmt.Sprintf("[%s] doesn't error when passed an unknown ID on DEL", ver), func() {
116 const ifname string = "eth0"
117 const nspath string = "/some/where"
118
119 conf := fmt.Sprintf(`{
120 "cniVersion": "%s",
121 "name": "mynet",
122 "type": "ipvlan",
123 "master": "foo0",
124 "ipam": {
125 "type": "static",
126 "addresses": [ {
127 "address": "10.10.0.1/24",
128 "gateway": "10.10.0.254"
129 },
130 {
131 "address": "3ffe:ffff:0:01ff::1/64",
132 "gateway": "3ffe:ffff:0::1"
133 }],
134 "routes": [
135 { "dst": "0.0.0.0/0" },
136 { "dst": "192.168.0.0/16", "gw": "10.10.5.1" },
137 { "dst": "3ffe:ffff:0:01ff::1/64" }],
138 "dns": {
139 "nameservers" : ["8.8.8.8"],
140 "domain": "example.com",
141 "search": [ "example.com" ]
142 }
143 }
144 }`, ver)
145
146 args := &skel.CmdArgs{
147 ContainerID: "dummy",
148 Netns: nspath,
149 IfName: ifname,
150 StdinData: []byte(conf),
151 }
152
153 // Release the IP
154 err := testutils.CmdDelWithArgs(args, func() error {
155 return cmdDel(args)
156 })
157 Expect(err).NotTo(HaveOccurred())
158 })
159
160 It(fmt.Sprintf("[%s] allocates and releases addresses with ADD/DEL, with ENV variables", ver), func() {
161 const ifname string = "eth0"
162 const nspath string = "/some/where"
163
164 conf := fmt.Sprintf(`{
165 "cniVersion": "%s",
166 "name": "mynet",
167 "type": "ipvlan",
168 "master": "foo0",
169 "ipam": {
170 "type": "static",
171 "routes": [
172 { "dst": "0.0.0.0/0" },
173 { "dst": "192.168.0.0/16", "gw": "10.10.5.1" }],
174 "dns": {
175 "nameservers" : ["8.8.8.8"],
176 "domain": "example.com",
177 "search": [ "example.com" ]
178 }
179 }
180 }`, ver)
181
182 args := &skel.CmdArgs{
183 ContainerID: "dummy",
184 Netns: nspath,
185 IfName: ifname,
186 StdinData: []byte(conf),
187 Args: "IP=10.10.0.1/24;GATEWAY=10.10.0.254",
188 }
189
190 // Allocate the IP
191 r, raw, err := testutils.CmdAddWithArgs(args, func() error {
192 return cmdAdd(args)
193 })
194 Expect(err).NotTo(HaveOccurred())
195 if testutils.SpecVersionHasIPVersion(ver) {
196 Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0))
197 }
198
199 result, err := types100.GetResult(r)
200 Expect(err).NotTo(HaveOccurred())
201
202 // Gomega is cranky about slices with different caps
203 Expect(*result.IPs[0]).To(Equal(
204 types100.IPConfig{
205 Address: mustCIDR("10.10.0.1/24"),
206 Gateway: net.ParseIP("10.10.0.254"),
207 }))
208
209 Expect(result.IPs).To(HaveLen(1))
210
211 Expect(result.Routes).To(Equal([]*types.Route{
212 {Dst: mustCIDR("0.0.0.0/0")},
213 {Dst: mustCIDR("192.168.0.0/16"), GW: net.ParseIP("10.10.5.1")},
214 }))
215
216 // Release the IP
217 err = testutils.CmdDelWithArgs(args, func() error {
218 return cmdDel(args)
219 })
220 Expect(err).NotTo(HaveOccurred())
221 })
222
223 It(fmt.Sprintf("[%s] allocates and releases multiple addresses with ADD/DEL, with ENV variables", ver), func() {
224 const ifname string = "eth0"
225 const nspath string = "/some/where"
226
227 conf := fmt.Sprintf(`{
228 "cniVersion": "%s",
229 "name": "mynet",
230 "type": "ipvlan",
231 "master": "foo0",
232 "ipam": {
233 "type": "static"
234 }
235 }`, ver)
236
237 args := &skel.CmdArgs{
238 ContainerID: "dummy",
239 Netns: nspath,
240 IfName: ifname,
241 StdinData: []byte(conf),
242 Args: "IP=10.10.0.1/24,11.11.0.1/24;GATEWAY=10.10.0.254",
243 }
244
245 // Allocate the IP
246 r, raw, err := testutils.CmdAddWithArgs(args, func() error {
247 return cmdAdd(args)
248 })
249 if !testutils.SpecVersionHasMultipleIPs(ver) {
250 errStr := fmt.Sprintf("CNI version %s does not support more than 1 address per family", ver)
251 Expect(err).To(MatchError(errStr))
252 return
253 }
254
255 Expect(err).NotTo(HaveOccurred())
256 if testutils.SpecVersionHasIPVersion(ver) {
257 Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0))
258 }
259
260 result, err := types100.GetResult(r)
261 Expect(err).NotTo(HaveOccurred())
262
263 // Gomega is cranky about slices with different caps
264 Expect(*result.IPs[0]).To(Equal(
265 types100.IPConfig{
266 Address: mustCIDR("10.10.0.1/24"),
267 Gateway: net.ParseIP("10.10.0.254"),
268 }))
269 Expect(*result.IPs[1]).To(Equal(
270 types100.IPConfig{
271 Address: mustCIDR("11.11.0.1/24"),
272 Gateway: nil,
273 }))
274
275 Expect(result.IPs).To(HaveLen(2))
276
277 // Release the IP
278 err = testutils.CmdDelWithArgs(args, func() error {
279 return cmdDel(args)
280 })
281 Expect(err).NotTo(HaveOccurred())
282 })
283
284 It(fmt.Sprintf("[%s] allocates and releases multiple addresses with ADD/DEL, from RuntimeConfig", ver), func() {
285 const ifname string = "eth0"
286 const nspath string = "/some/where"
287
288 conf := fmt.Sprintf(`{
289 "cniVersion": "%s",
290 "name": "mynet",
291 "type": "ipvlan",
292 "master": "foo0",
293 "capabilities": {"ips": true},
294 "ipam": {
295 "type": "static",
296 "routes": [
297 { "dst": "0.0.0.0/0", "gw": "10.10.0.254" },
298 { "dst": "3ffe:ffff:0:01ff::1/64",
299 "gw": "3ffe:ffff:0::1" } ],
300 "dns": {
301 "nameservers" : ["8.8.8.8"],
302 "domain": "example.com",
303 "search": [ "example.com" ]
304 }
305 },
306 "RuntimeConfig": {
307 "ips" : ["10.10.0.1/24", "3ffe:ffff:0:01ff::1/64"]
308 }
309 }`, ver)
310
311 args := &skel.CmdArgs{
312 ContainerID: "dummy",
313 Netns: nspath,
314 IfName: ifname,
315 StdinData: []byte(conf),
316 }
317
318 // Allocate the IP
319 r, raw, err := testutils.CmdAddWithArgs(args, func() error {
320 return cmdAdd(args)
321 })
322 Expect(err).NotTo(HaveOccurred())
323 if testutils.SpecVersionHasIPVersion(ver) {
324 Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0))
325 }
326
327 result, err := types100.GetResult(r)
328 Expect(err).NotTo(HaveOccurred())
329
330 // Gomega is cranky about slices with different caps
331 Expect(*result.IPs[0]).To(Equal(
332 types100.IPConfig{
333 Address: mustCIDR("10.10.0.1/24"),
334 }))
335 Expect(*result.IPs[1]).To(Equal(
336 types100.IPConfig{
337 Address: mustCIDR("3ffe:ffff:0:01ff::1/64"),
338 },
339 ))
340 Expect(result.IPs).To(HaveLen(2))
341 Expect(result.Routes).To(Equal([]*types.Route{
342 {Dst: mustCIDR("0.0.0.0/0"), GW: net.ParseIP("10.10.0.254")},
343 {Dst: mustCIDR("3ffe:ffff:0:01ff::1/64"), GW: net.ParseIP("3ffe:ffff:0::1")},
344 }))
345
346 // Release the IP
347 err = testutils.CmdDelWithArgs(args, func() error {
348 return cmdDel(args)
349 })
350 Expect(err).NotTo(HaveOccurred())
351 })
352
353 It(fmt.Sprintf("[%s] allocates and releases multiple addresses with ADD/DEL, from args", ver), func() {
354 const ifname string = "eth0"
355 const nspath string = "/some/where"
356
357 conf := fmt.Sprintf(`{
358 "cniVersion": "%s",
359 "name": "mynet",
360 "type": "ipvlan",
361 "master": "foo0",
362 "ipam": {
363 "type": "static",
364 "routes": [
365 { "dst": "0.0.0.0/0", "gw": "10.10.0.254" },
366 { "dst": "3ffe:ffff:0:01ff::1/64",
367 "gw": "3ffe:ffff:0::1" } ],
368 "dns": {
369 "nameservers" : ["8.8.8.8"],
370 "domain": "example.com",
371 "search": [ "example.com" ]
372 }
373 },
374 "args": {
375 "cni": {
376 "ips" : ["10.10.0.1/24", "3ffe:ffff:0:01ff::1/64"]
377 }
378 }
379 }`, ver)
380
381 args := &skel.CmdArgs{
382 ContainerID: "dummy",
383 Netns: nspath,
384 IfName: ifname,
385 StdinData: []byte(conf),
386 }
387
388 // Allocate the IP
389 r, raw, err := testutils.CmdAddWithArgs(args, func() error {
390 return cmdAdd(args)
391 })
392 Expect(err).NotTo(HaveOccurred())
393 if testutils.SpecVersionHasIPVersion(ver) {
394 Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0))
395 }
396
397 result, err := types100.GetResult(r)
398 Expect(err).NotTo(HaveOccurred())
399
400 // Gomega is cranky about slices with different caps
401 Expect(*result.IPs[0]).To(Equal(
402 types100.IPConfig{
403 Address: mustCIDR("10.10.0.1/24"),
404 }))
405 Expect(*result.IPs[1]).To(Equal(
406 types100.IPConfig{
407 Address: mustCIDR("3ffe:ffff:0:01ff::1/64"),
408 },
409 ))
410 Expect(result.IPs).To(HaveLen(2))
411 Expect(result.Routes).To(Equal([]*types.Route{
412 {Dst: mustCIDR("0.0.0.0/0"), GW: net.ParseIP("10.10.0.254")},
413 {Dst: mustCIDR("3ffe:ffff:0:01ff::1/64"), GW: net.ParseIP("3ffe:ffff:0::1")},
414 }))
415
416 // Release the IP
417 err = testutils.CmdDelWithArgs(args, func() error {
418 return cmdDel(args)
419 })
420 Expect(err).NotTo(HaveOccurred())
421 })
422
423 It(fmt.Sprintf("[%s] allocates and releases multiple addresses with ADD/DEL, from RuntimeConfig/ARGS/CNI_ARGS", ver), func() {
424 const ifname string = "eth0"
425 const nspath string = "/some/where"
426
427 conf := fmt.Sprintf(`{
428 "cniVersion": "%s",
429 "name": "mynet",
430 "type": "ipvlan",
431 "master": "foo0",
432 "capabilities": {"ips": true},
433 "ipam": {
434 "type": "static",
435 "routes": [
436 { "dst": "0.0.0.0/0", "gw": "10.10.0.254" },
437 { "dst": "3ffe:ffff:0:01ff::1/64",
438 "gw": "3ffe:ffff:0::1" } ],
439 "dns": {
440 "nameservers" : ["8.8.8.8"],
441 "domain": "example.com",
442 "search": [ "example.com" ]
443 }
444 },
445 "RuntimeConfig": {
446 "ips" : ["10.10.0.1/24", "3ffe:ffff:0:01ff::1/64"]
447 },
448 "args": {
449 "cni": {
450 "ips" : ["10.10.0.2/24", "3ffe:ffff:0:01ff::2/64"]
451 }
452 }
453 }`, ver)
454
455 args := &skel.CmdArgs{
456 ContainerID: "dummy",
457 Netns: nspath,
458 IfName: ifname,
459 StdinData: []byte(conf),
460 Args: "IP=10.10.0.3/24,11.11.0.3/24;GATEWAY=10.10.0.254",
461 }
462
463 // Allocate the IP
464 r, raw, err := testutils.CmdAddWithArgs(args, func() error {
465 return cmdAdd(args)
466 })
467 Expect(err).NotTo(HaveOccurred())
468 if testutils.SpecVersionHasIPVersion(ver) {
469 Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0))
470 }
471
472 result, err := types100.GetResult(r)
473 Expect(err).NotTo(HaveOccurred())
474
475 // only addresses in runtimeConfig configured because of its priorities
476 Expect(*result.IPs[0]).To(Equal(
477 types100.IPConfig{
478 Address: mustCIDR("10.10.0.1/24"),
479 }))
480 Expect(*result.IPs[1]).To(Equal(
481 types100.IPConfig{
482 Address: mustCIDR("3ffe:ffff:0:01ff::1/64"),
483 },
484 ))
485 Expect(result.IPs).To(HaveLen(2))
486 Expect(result.Routes).To(Equal([]*types.Route{
487 {Dst: mustCIDR("0.0.0.0/0"), GW: net.ParseIP("10.10.0.254")},
488 {Dst: mustCIDR("3ffe:ffff:0:01ff::1/64"), GW: net.ParseIP("3ffe:ffff:0::1")},
489 }))
490
491 // Release the IP
492 err = testutils.CmdDelWithArgs(args, func() error {
493 return cmdDel(args)
494 })
495 Expect(err).NotTo(HaveOccurred())
496 })
497
498 It(fmt.Sprintf("[%s] is returning an error on missing ipam key when args are set", ver), func() {
499 const ifname string = "eth0"
500 const nspath string = "/some/where"
501 conf := fmt.Sprintf(`{
502 "cniVersion": "%s",
503 "name": "mynet",
504 "type": "ipvlan",
505 "master": "foo0"
506 }`, ver)
507
508 args := &skel.CmdArgs{
509 ContainerID: "dummy",
510 Netns: nspath,
511 IfName: ifname,
512 StdinData: []byte(conf),
513 Args: "IP=10.10.0.1/24;GATEWAY=10.10.0.254",
514 }
515
516 // Allocate the IP
517 _, _, err := testutils.CmdAddWithArgs(args, func() error {
518 return cmdAdd(args)
519 })
520 Expect(err).Should(MatchError("IPAM config missing 'ipam' key"))
521 })
522
523 It(fmt.Sprintf("[%s] is returning an error on missing ipam key when runtimeConfig is set", ver), func() {
524 const ifname string = "eth0"
525 const nspath string = "/some/where"
526 conf := fmt.Sprintf(`{
527 "cniVersion": "%s",
528 "name": "mynet",
529 "type": "ipvlan",
530 "master": "foo0",
531 "runtimeConfig": {
532 "ips": ["10.10.0.1/24"]
533 }
534 }`, ver)
535
536 args := &skel.CmdArgs{
537 ContainerID: "dummy",
538 Netns: nspath,
539 IfName: ifname,
540 StdinData: []byte(conf),
541 }
542
543 // Allocate the IP
544 _, _, err := testutils.CmdAddWithArgs(args, func() error {
545 return cmdAdd(args)
546 })
547 Expect(err).Should(MatchError("IPAM config missing 'ipam' key"))
548 })
549
550 It(fmt.Sprintf("[%s] errors when passed an invalid CIDR via ipam config", ver), func() {
551 const ifname string = "eth0"
552 const nspath string = "/some/where"
553 const ipStr string = "10.10.0.1"
554
555 conf := fmt.Sprintf(`{
556 "cniVersion": "%s",
557 "name": "mynet",
558 "type": "bridge",
559 "ipam": {
560 "type": "static",
561 "addresses": [ {
562 "address": "%s"
563 }]
564 }
565 }`, ver, ipStr)
566
567 args := &skel.CmdArgs{
568 ContainerID: "dummy",
569 Netns: nspath,
570 IfName: ifname,
571 StdinData: []byte(conf),
572 }
573
574 // Allocate the IP
575 _, _, err := testutils.CmdAddWithArgs(args, func() error {
576 return cmdAdd(args)
577 })
578 Expect(err).Should(MatchError(
579 fmt.Sprintf("the 'address' field is expected to be in CIDR notation, got: '%s'", ipStr)))
580 })
581
582 It(fmt.Sprintf("[%s] errors when passed an invalid CIDR via Args", ver), func() {
583 const ifname string = "eth0"
584 const nspath string = "/some/where"
585 const ipStr string = "10.10.0.1"
586
587 conf := fmt.Sprintf(`{
588 "cniVersion": "%s",
589 "name": "mynet",
590 "type": "bridge",
591 "ipam": {
592 "type": "static",
593 "routes": [{ "dst": "0.0.0.0/0" }]
594 }
595 }`, ver)
596
597 args := &skel.CmdArgs{
598 ContainerID: "dummy",
599 Netns: nspath,
600 IfName: ifname,
601 StdinData: []byte(conf),
602 Args: fmt.Sprintf("IP=%s", ipStr),
603 }
604
605 // Allocate the IP
606 _, _, err := testutils.CmdAddWithArgs(args, func() error {
607 return cmdAdd(args)
608 })
609 Expect(err).Should(MatchError(
610 fmt.Sprintf("the 'ip' field is expected to be in CIDR notation, got: '%s'", ipStr)))
611 })
612
613 It(fmt.Sprintf("[%s] errors when passed an invalid CIDR via CNI_ARGS", ver), func() {
614 const ifname string = "eth0"
615 const nspath string = "/some/where"
616 const ipStr string = "10.10.0.1"
617
618 conf := fmt.Sprintf(`{
619 "cniVersion": "%s",
620 "name": "mynet",
621 "type": "bridge",
622 "ipam": {
623 "type": "static",
624 "routes": [{ "dst": "0.0.0.0/0" }]
625 },
626 "args": {
627 "cni": {
628 "ips" : ["%s"]
629 }
630 }
631 }`, ver, ipStr)
632
633 args := &skel.CmdArgs{
634 ContainerID: "dummy",
635 Netns: nspath,
636 IfName: ifname,
637 StdinData: []byte(conf),
638 }
639
640 // Allocate the IP
641 _, _, err := testutils.CmdAddWithArgs(args, func() error {
642 return cmdAdd(args)
643 })
644 Expect(err).Should(MatchError(
645 fmt.Sprintf("an entry in the 'ips' field is NOT in CIDR notation, got: '%s'", ipStr)))
646 })
647
648 It(fmt.Sprintf("[%s] errors when passed an invalid CIDR via RuntimeConfig", ver), func() {
649 const ifname string = "eth0"
650 const nspath string = "/some/where"
651 const ipStr string = "10.10.0.1"
652
653 conf := fmt.Sprintf(`{
654 "cniVersion": "%s",
655 "name": "mynet",
656 "type": "bridge",
657 "ipam": {
658 "type": "static",
659 "routes": [{ "dst": "0.0.0.0/0" }]
660 },
661 "RuntimeConfig": {
662 "ips" : ["%s"]
663 }
664 }`, ver, ipStr)
665
666 args := &skel.CmdArgs{
667 ContainerID: "dummy",
668 Netns: nspath,
669 IfName: ifname,
670 StdinData: []byte(conf),
671 }
672
673 // Allocate the IP
674 _, _, err := testutils.CmdAddWithArgs(args, func() error {
675 return cmdAdd(args)
676 })
677 Expect(err).Should(MatchError(
678 fmt.Sprintf("an entry in the 'ips' field is NOT in CIDR notation, got: '%s'", ipStr)))
679 })
680 }
681 })
682
683 func mustCIDR(s string) net.IPNet {
684 ip, n, err := net.ParseCIDR(s)
685 n.IP = ip
686 if err != nil {
687 Fail(err.Error())
688 }
689
690 return *n
691 }