Debian Codegraph @

pkg/skel/skel_test.go

raw
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 skel
16
17 import (
18 "bytes"
19 "errors"
20 "fmt"
21 "strings"
22
23 . "github.com/onsi/ginkgo/v2"
24 . "github.com/onsi/gomega"
25
26 "github.com/containernetworking/cni/pkg/types"
27 "github.com/containernetworking/cni/pkg/version"
28 )
29
30 type fakeCmd struct {
31 CallCount int
32 Returns struct {
33 Error error
34 }
35 Received struct {
36 CmdArgs *CmdArgs
37 }
38 }
39
40 func (c *fakeCmd) Func(args *CmdArgs) error {
41 c.CallCount++
42 c.Received.CmdArgs = args
43 return c.Returns.Error
44 }
45
46 var _ = Describe("dispatching to the correct callback", func() {
47 var (
48 environment map[string]string
49 stdinData string
50 stdout, stderr *bytes.Buffer
51 cmdAdd, cmdCheck, cmdDel, cmdGC *fakeCmd
52 dispatch *dispatcher
53 expectedCmdArgs *CmdArgs
54 versionInfo version.PluginInfo
55 funcs CNIFuncs
56 )
57
58 BeforeEach(func() {
59 environment = map[string]string{
60 "CNI_COMMAND": "ADD",
61 "CNI_CONTAINERID": "some-container-id",
62 "CNI_NETNS": "/some/netns/path",
63 "CNI_IFNAME": "eth0",
64 "CNI_ARGS": "some;extra;args",
65 "CNI_PATH": "/some/cni/path",
66 }
67
68 stdinData = `{ "name":"skel-test", "some": "config", "cniVersion": "9.8.7" }`
69 stdout = &bytes.Buffer{}
70 stderr = &bytes.Buffer{}
71 versionInfo = version.PluginSupports("9.8.7", "10.0.0")
72 dispatch = &dispatcher{
73 Getenv: func(key string) string { return environment[key] },
74 Stdin: strings.NewReader(stdinData),
75 Stdout: stdout,
76 Stderr: stderr,
77 }
78 cmdAdd = &fakeCmd{}
79 cmdCheck = &fakeCmd{}
80 cmdDel = &fakeCmd{}
81 cmdGC = &fakeCmd{}
82 funcs = CNIFuncs{
83 Add: cmdAdd.Func,
84 Del: cmdDel.Func,
85 Check: cmdCheck.Func,
86 GC: cmdGC.Func,
87 }
88
89 expectedCmdArgs = &CmdArgs{
90 ContainerID: "some-container-id",
91 Netns: "/some/netns/path",
92 IfName: "eth0",
93 Args: "some;extra;args",
94 Path: "/some/cni/path",
95 StdinData: []byte(stdinData),
96 }
97 })
98
99 envVarChecker := func(envVar string, isRequired bool) {
100 delete(environment, envVar)
101
102 err := dispatch.pluginMain(funcs, versionInfo, "")
103 if isRequired {
104 Expect(err).To(Equal(&types.Error{
105 Code: types.ErrInvalidEnvironmentVariables,
106 Msg: "required env variables [" + envVar + "] missing",
107 }))
108 } else {
109 Expect(err).NotTo(HaveOccurred())
110 }
111 }
112
113 Context("when the CNI_COMMAND is ADD", func() {
114 expectedCmdArgs = &CmdArgs{
115 ContainerID: "some-container-id",
116 Netns: "/some/netns/path",
117 IfName: "eth0",
118 Args: "some;extra;args",
119 Path: "/some/cni/path",
120 StdinData: []byte(stdinData),
121 }
122
123 It("extracts env vars and stdin data and calls cmdAdd", func() {
124 err := dispatch.pluginMain(funcs, versionInfo, "")
125
126 Expect(err).NotTo(HaveOccurred())
127 Expect(cmdAdd.CallCount).To(Equal(1))
128 Expect(cmdCheck.CallCount).To(Equal(0))
129 Expect(cmdDel.CallCount).To(Equal(0))
130 Expect(cmdAdd.Received.CmdArgs).To(Equal(expectedCmdArgs))
131 })
132
133 It("returns an error when containerID has invalid characters", func() {
134 environment["CNI_CONTAINERID"] = "some-%%container-id"
135 err := dispatch.pluginMain(funcs, versionInfo, "")
136 Expect(err).To(HaveOccurred())
137 Expect(err).To(Equal(&types.Error{
138 Code: types.ErrInvalidEnvironmentVariables,
139 Msg: "invalid characters in containerID",
140 Details: "some-%%container-id",
141 }))
142 })
143
144 Context("return errors when interface name is invalid", func() {
145 It("interface name is too long", func() {
146 environment["CNI_IFNAME"] = "1234567890123456"
147
148 err := dispatch.pluginMain(funcs, versionInfo, "")
149 Expect(err).To(HaveOccurred())
150 Expect(err).To(Equal(&types.Error{
151 Code: types.ErrInvalidEnvironmentVariables,
152 Msg: "interface name is too long",
153 Details: "interface name should be less than 16 characters",
154 }))
155 })
156
157 It("interface name is .", func() {
158 environment["CNI_IFNAME"] = "."
159
160 err := dispatch.pluginMain(funcs, versionInfo, "")
161 Expect(err).To(HaveOccurred())
162 Expect(err).To(Equal(&types.Error{
163 Code: types.ErrInvalidEnvironmentVariables,
164 Msg: "interface name is . or ..",
165 Details: "",
166 }))
167 })
168
169 It("interface name is ..", func() {
170 environment["CNI_IFNAME"] = ".."
171
172 err := dispatch.pluginMain(funcs, versionInfo, "")
173 Expect(err).To(HaveOccurred())
174 Expect(err).To(Equal(&types.Error{
175 Code: types.ErrInvalidEnvironmentVariables,
176 Msg: "interface name is . or ..",
177 Details: "",
178 }))
179 })
180
181 It("interface name contains invalid characters /", func() {
182 environment["CNI_IFNAME"] = "test/test"
183
184 err := dispatch.pluginMain(funcs, versionInfo, "")
185 Expect(err).To(HaveOccurred())
186 Expect(err).To(Equal(&types.Error{
187 Code: types.ErrInvalidEnvironmentVariables,
188 Msg: "interface name contains / or : or whitespace characters",
189 Details: "",
190 }))
191 })
192
193 It("interface name contains invalid characters :", func() {
194 environment["CNI_IFNAME"] = "test:test"
195
196 err := dispatch.pluginMain(funcs, versionInfo, "")
197 Expect(err).To(HaveOccurred())
198 Expect(err).To(Equal(&types.Error{
199 Code: types.ErrInvalidEnvironmentVariables,
200 Msg: "interface name contains / or : or whitespace characters",
201 Details: "",
202 }))
203 })
204
205 It("interface name contains invalid characters whitespace", func() {
206 environment["CNI_IFNAME"] = "test test"
207
208 err := dispatch.pluginMain(funcs, versionInfo, "")
209 Expect(err).To(HaveOccurred())
210 Expect(err).To(Equal(&types.Error{
211 Code: types.ErrInvalidEnvironmentVariables,
212 Msg: "interface name contains / or : or whitespace characters",
213 Details: "",
214 }))
215 })
216 })
217
218 It("does not call cmdCheck or cmdDel", func() {
219 err := dispatch.pluginMain(funcs, versionInfo, "")
220
221 Expect(err).NotTo(HaveOccurred())
222 Expect(cmdCheck.CallCount).To(Equal(0))
223 Expect(cmdDel.CallCount).To(Equal(0))
224 })
225
226 DescribeTable("required / optional env vars", envVarChecker,
227 Entry("command", "CNI_COMMAND", true),
228 Entry("container id", "CNI_CONTAINERID", true),
229 Entry("net ns", "CNI_NETNS", true),
230 Entry("if name", "CNI_IFNAME", true),
231 Entry("args", "CNI_ARGS", false),
232 Entry("path", "CNI_PATH", true),
233 )
234
235 Context("when multiple required env vars are missing", func() {
236 BeforeEach(func() {
237 delete(environment, "CNI_NETNS")
238 delete(environment, "CNI_IFNAME")
239 delete(environment, "CNI_PATH")
240 })
241
242 It("reports that all of them are missing, not just the first", func() {
243 err := dispatch.pluginMain(funcs, versionInfo, "")
244 Expect(err).To(HaveOccurred())
245
246 Expect(err).To(Equal(&types.Error{
247 Code: types.ErrInvalidEnvironmentVariables,
248 Msg: "required env variables [CNI_NETNS,CNI_IFNAME,CNI_PATH] missing",
249 }))
250 })
251 })
252
253 Context("when the stdin data is missing the required cniVersion config", func() {
254 BeforeEach(func() {
255 dispatch.Stdin = strings.NewReader(`{ "name": "skel-test", "some": "config" }`)
256 })
257
258 Context("when the plugin supports version 0.1.0", func() {
259 BeforeEach(func() {
260 versionInfo = version.PluginSupports("0.1.0")
261 expectedCmdArgs.StdinData = []byte(`{ "name": "skel-test", "some": "config" }`)
262 })
263
264 It("infers the config is 0.1.0 and calls the cmdAdd callback", func() {
265 err := dispatch.pluginMain(funcs, versionInfo, "")
266 Expect(err).NotTo(HaveOccurred())
267
268 Expect(cmdAdd.CallCount).To(Equal(1))
269 Expect(cmdAdd.Received.CmdArgs).To(Equal(expectedCmdArgs))
270 })
271 })
272
273 Context("when the plugin does not support 0.1.0", func() {
274 BeforeEach(func() {
275 versionInfo = version.PluginSupports("4.3.2")
276 })
277
278 It("immediately returns a useful error", func() {
279 err := dispatch.pluginMain(funcs, versionInfo, "")
280 Expect(err.Code).To(Equal(types.ErrIncompatibleCNIVersion)) // see https://github.com/containernetworking/cni/blob/main/SPEC.md#well-known-error-codes
281 Expect(err.Msg).To(Equal("incompatible CNI versions"))
282 Expect(err.Details).To(Equal(`config is "0.1.0", plugin supports ["4.3.2"]`))
283 })
284
285 It("does not call either callback", func() {
286 err := dispatch.pluginMain(funcs, versionInfo, "")
287 Expect(err).To(HaveOccurred())
288 Expect(cmdAdd.CallCount).To(Equal(0))
289 Expect(cmdCheck.CallCount).To(Equal(0))
290 Expect(cmdDel.CallCount).To(Equal(0))
291 })
292 })
293 })
294 })
295
296 Context("when the CNI_COMMAND is CHECK", func() {
297 BeforeEach(func() {
298 environment["CNI_COMMAND"] = "CHECK"
299 })
300
301 It("extracts env vars and stdin data and calls cmdCheck", func() {
302 err := dispatch.pluginMain(funcs, versionInfo, "")
303
304 Expect(err).NotTo(HaveOccurred())
305 Expect(cmdAdd.CallCount).To(Equal(0))
306 Expect(cmdCheck.CallCount).To(Equal(1))
307 Expect(cmdDel.CallCount).To(Equal(0))
308 Expect(cmdCheck.Received.CmdArgs).To(Equal(expectedCmdArgs))
309 })
310
311 It("does not call cmdAdd or cmdDel", func() {
312 err := dispatch.pluginMain(funcs, versionInfo, "")
313
314 Expect(err).NotTo(HaveOccurred())
315 Expect(cmdAdd.CallCount).To(Equal(0))
316 Expect(cmdDel.CallCount).To(Equal(0))
317 })
318
319 DescribeTable("required / optional env vars", envVarChecker,
320 Entry("command", "CNI_COMMAND", true),
321 Entry("container id", "CNI_CONTAINERID", true),
322 Entry("net ns", "CNI_NETNS", true),
323 Entry("if name", "CNI_IFNAME", true),
324 Entry("args", "CNI_ARGS", false),
325 Entry("path", "CNI_PATH", true),
326 )
327
328 Context("when multiple required env vars are missing", func() {
329 BeforeEach(func() {
330 delete(environment, "CNI_NETNS")
331 delete(environment, "CNI_IFNAME")
332 delete(environment, "CNI_PATH")
333 })
334
335 It("reports that all of them are missing, not just the first", func() {
336 err := dispatch.pluginMain(funcs, versionInfo, "")
337 Expect(err).To(HaveOccurred())
338
339 Expect(err).To(Equal(&types.Error{
340 Code: types.ErrInvalidEnvironmentVariables,
341 Msg: "required env variables [CNI_NETNS,CNI_IFNAME,CNI_PATH] missing",
342 }))
343 })
344 })
345
346 Context("when cniVersion is less than 0.4.0", func() {
347 It("immediately returns a useful error", func() {
348 dispatch.Stdin = strings.NewReader(`{ "name": "skel-test", "cniVersion": "0.3.0", "some": "config" }`)
349 err := dispatch.pluginMain(funcs, versionInfo, "")
350 Expect(err.Code).To(Equal(types.ErrIncompatibleCNIVersion)) // see https://github.com/containernetworking/cni/blob/main/SPEC.md#well-known-error-codes
351 Expect(err.Msg).To(Equal("config version does not allow CHECK"))
352 Expect(cmdAdd.CallCount).To(Equal(0))
353 Expect(cmdCheck.CallCount).To(Equal(0))
354 Expect(cmdDel.CallCount).To(Equal(0))
355 })
356 })
357
358 Context("when plugin does not support 0.4.0", func() {
359 It("immediately returns a useful error", func() {
360 dispatch.Stdin = strings.NewReader(`{ "name": "skel-test", "cniVersion": "0.4.0", "some": "config" }`)
361 versionInfo = version.PluginSupports("0.1.0", "0.2.0", "0.3.0")
362 err := dispatch.pluginMain(funcs, versionInfo, "")
363 Expect(err.Code).To(Equal(types.ErrIncompatibleCNIVersion)) // see https://github.com/containernetworking/cni/blob/main/SPEC.md#well-known-error-codes
364 Expect(err.Msg).To(Equal("plugin version does not allow CHECK"))
365 Expect(cmdAdd.CallCount).To(Equal(0))
366 Expect(cmdCheck.CallCount).To(Equal(0))
367 Expect(cmdDel.CallCount).To(Equal(0))
368 })
369 })
370
371 Context("when the config has a bad version", func() {
372 It("immediately returns a useful error", func() {
373 dispatch.Stdin = strings.NewReader(`{ "cniVersion": "adsfsadf", "some": "config", "name": "test" }`)
374 versionInfo = version.PluginSupports("0.1.0", "0.2.0", "0.3.0")
375 err := dispatch.pluginMain(funcs, versionInfo, "")
376 Expect(err.Code).To(Equal(types.ErrDecodingFailure))
377 Expect(cmdAdd.CallCount).To(Equal(0))
378 Expect(cmdCheck.CallCount).To(Equal(0))
379 Expect(cmdDel.CallCount).To(Equal(0))
380 })
381 })
382
383 Context("when the config has a bad name", func() {
384 It("immediately returns invalid network config", func() {
385 dispatch.Stdin = strings.NewReader(`{ "cniVersion": "0.4.0", "some": "config", "name": "te%%st" }`)
386 versionInfo = version.PluginSupports("0.1.0", "0.2.0", "0.3.0", "0.4.0")
387 err := dispatch.pluginMain(funcs, versionInfo, "")
388 Expect(err.Code).To(Equal(types.ErrInvalidNetworkConfig))
389 Expect(cmdAdd.CallCount).To(Equal(0))
390 Expect(cmdCheck.CallCount).To(Equal(0))
391 Expect(cmdDel.CallCount).To(Equal(0))
392 })
393 })
394
395 Context("when the plugin has a bad version", func() {
396 It("immediately returns a useful error", func() {
397 dispatch.Stdin = strings.NewReader(`{ "cniVersion": "0.4.0", "some": "config", "name": "test" }`)
398 versionInfo = version.PluginSupports("0.1.0", "0.2.0", "adsfasdf")
399 err := dispatch.pluginMain(funcs, versionInfo, "")
400 Expect(err.Code).To(Equal(types.ErrDecodingFailure))
401 Expect(cmdAdd.CallCount).To(Equal(0))
402 Expect(cmdCheck.CallCount).To(Equal(0))
403 Expect(cmdDel.CallCount).To(Equal(0))
404 })
405 })
406 })
407
408 Context("when the CNI_COMMAND is GC", func() {
409 BeforeEach(func() {
410 environment["CNI_COMMAND"] = "GC"
411 delete(environment, "CNI_NETNS")
412 delete(environment, "CNI_IFNAME")
413 delete(environment, "CNI_CONTAINERID")
414 delete(environment, "CNI_ARGS")
415
416 expectedCmdArgs = &CmdArgs{
417 Path: "/some/cni/path",
418 StdinData: []byte(stdinData),
419 }
420
421 dispatch = &dispatcher{
422 Getenv: func(key string) string {
423 return environment[key]
424 },
425 Stdin: strings.NewReader(stdinData),
426 Stdout: stdout,
427 Stderr: stderr,
428 }
429 })
430
431 It("extracts env vars and stdin data and calls cmdGC", func() {
432 err := dispatch.pluginMain(funcs, versionInfo, "")
433
434 Expect(err).NotTo(HaveOccurred())
435 Expect(cmdAdd.CallCount).To(Equal(0))
436 Expect(cmdCheck.CallCount).To(Equal(0))
437 Expect(cmdDel.CallCount).To(Equal(0))
438 Expect(cmdGC.CallCount).To(Equal(1))
439 Expect(cmdGC.Received.CmdArgs).To(Equal(expectedCmdArgs))
440 })
441
442 It("does not call cmdAdd or cmdDel", func() {
443 err := dispatch.pluginMain(funcs, versionInfo, "")
444
445 Expect(err).NotTo(HaveOccurred())
446 Expect(cmdAdd.CallCount).To(Equal(0))
447 Expect(cmdDel.CallCount).To(Equal(0))
448 })
449
450 DescribeTable("required / optional env vars", envVarChecker,
451 Entry("command", "CNI_COMMAND", true),
452 Entry("container id", "CNI_CONTAINERID", false),
453 Entry("net ns", "CNI_NETNS", false),
454 Entry("if name", "CNI_IFNAME", false),
455 Entry("args", "CNI_ARGS", false),
456 Entry("path", "CNI_PATH", true),
457 )
458
459 Context("when multiple required env vars are missing", func() {
460 BeforeEach(func() {
461 delete(environment, "CNI_PATH")
462 })
463
464 It("reports that all of them are missing, not just the first", func() {
465 err := dispatch.pluginMain(funcs, versionInfo, "")
466 Expect(err).To(HaveOccurred())
467
468 Expect(err).To(Equal(&types.Error{
469 Code: types.ErrInvalidEnvironmentVariables,
470 Msg: "required env variables [CNI_PATH] missing",
471 }))
472 })
473 })
474
475 Context("when cniVersion is less than 1.1.0", func() {
476 It("immediately returns a useful error", func() {
477 dispatch.Stdin = strings.NewReader(`{ "name": "skel-test", "cniVersion": "0.3.0", "some": "config" }`)
478 err := dispatch.pluginMain(funcs, versionInfo, "")
479 Expect(err.Code).To(Equal(types.ErrIncompatibleCNIVersion)) // see https://github.com/containernetworking/cni/blob/main/SPEC.md#well-known-error-codes
480 Expect(err.Msg).To(Equal("config version does not allow GC"))
481 Expect(cmdGC.CallCount).To(Equal(0))
482 })
483 })
484
485 Context("when plugin does not support 1.1.0", func() {
486 It("immediately returns a useful error", func() {
487 dispatch.Stdin = strings.NewReader(`{ "name": "skel-test", "cniVersion": "1.1.0", "some": "config" }`)
488 versionInfo = version.PluginSupports("0.1.0", "0.2.0", "0.3.0")
489 err := dispatch.pluginMain(funcs, versionInfo, "")
490 Expect(err.Code).To(Equal(types.ErrIncompatibleCNIVersion)) // see https://github.com/containernetworking/cni/blob/main/SPEC.md#well-known-error-codes
491 Expect(err.Msg).To(Equal("plugin version does not allow GC"))
492 Expect(cmdGC.CallCount).To(Equal(0))
493 })
494 })
495
496 Context("when the config has a bad version", func() {
497 It("immediately returns a useful error", func() {
498 dispatch.Stdin = strings.NewReader(`{ "cniVersion": "adsfsadf", "some": "config", "name": "test" }`)
499 versionInfo = version.PluginSupports("1.1.0")
500 err := dispatch.pluginMain(funcs, versionInfo, "")
501 Expect(err.Code).To(Equal(types.ErrDecodingFailure))
502 Expect(cmdGC.CallCount).To(Equal(0))
503 })
504 })
505
506 Context("when the config has a bad name", func() {
507 It("immediately returns invalid network config", func() {
508 dispatch.Stdin = strings.NewReader(`{ "cniVersion": "0.4.0", "some": "config", "name": "te%%st" }`)
509 versionInfo = version.PluginSupports("1.1.0")
510 err := dispatch.pluginMain(funcs, versionInfo, "")
511 Expect(err.Code).To(Equal(types.ErrInvalidNetworkConfig))
512 Expect(cmdGC.CallCount).To(Equal(0))
513 })
514 })
515
516 Context("when the plugin has a bad version", func() {
517 It("immediately returns a useful error", func() {
518 dispatch.Stdin = strings.NewReader(`{ "cniVersion": "1.1.0", "some": "config", "name": "test" }`)
519 versionInfo = version.PluginSupports("0.1.0", "0.2.0", "adsfasdf")
520 err := dispatch.pluginMain(funcs, versionInfo, "")
521 Expect(err.Code).To(Equal(types.ErrDecodingFailure))
522 Expect(cmdGC.CallCount).To(Equal(0))
523 })
524 })
525 })
526
527 Context("when the CNI_COMMAND is DEL", func() {
528 BeforeEach(func() {
529 environment["CNI_COMMAND"] = "DEL"
530 })
531
532 It("calls cmdDel with the env vars and stdin data", func() {
533 err := dispatch.pluginMain(funcs, versionInfo, "")
534
535 Expect(err).NotTo(HaveOccurred())
536 Expect(cmdDel.CallCount).To(Equal(1))
537 Expect(cmdDel.Received.CmdArgs).To(Equal(expectedCmdArgs))
538 })
539
540 It("does not call cmdAdd", func() {
541 err := dispatch.pluginMain(funcs, versionInfo, "")
542
543 Expect(err).NotTo(HaveOccurred())
544 Expect(cmdAdd.CallCount).To(Equal(0))
545 })
546
547 DescribeTable("required / optional env vars", envVarChecker,
548 Entry("command", "CNI_COMMAND", true),
549 Entry("container id", "CNI_CONTAINERID", true),
550 Entry("net ns", "CNI_NETNS", false),
551 Entry("if name", "CNI_IFNAME", true),
552 Entry("args", "CNI_ARGS", false),
553 Entry("path", "CNI_PATH", true),
554 )
555 })
556
557 Context("when the CNI_COMMAND is VERSION", func() {
558 BeforeEach(func() {
559 environment["CNI_COMMAND"] = "VERSION"
560 })
561
562 It("prints the version to stdout", func() {
563 err := dispatch.pluginMain(funcs, versionInfo, "")
564
565 Expect(err).NotTo(HaveOccurred())
566 Expect(stdout).To(MatchJSON(fmt.Sprintf(`{
567 "cniVersion": "%s",
568 "supportedVersions": ["9.8.7", "10.0.0"]
569 }`, version.Current())))
570 })
571
572 It("does not call cmdAdd or cmdDel", func() {
573 err := dispatch.pluginMain(funcs, versionInfo, "")
574
575 Expect(err).NotTo(HaveOccurred())
576 Expect(cmdAdd.CallCount).To(Equal(0))
577 Expect(cmdDel.CallCount).To(Equal(0))
578 })
579
580 DescribeTable("VERSION does not need the usual env vars", envVarChecker,
581 Entry("command", "CNI_COMMAND", true),
582 Entry("container id", "CNI_CONTAINERID", false),
583 Entry("net ns", "CNI_NETNS", false),
584 Entry("if name", "CNI_IFNAME", false),
585 Entry("args", "CNI_ARGS", false),
586 Entry("path", "CNI_PATH", false),
587 )
588
589 It("does not read from Stdin", func() {
590 r := &BadReader{}
591 dispatch.Stdin = r
592
593 err := dispatch.pluginMain(funcs, versionInfo, "")
594
595 Expect(err).NotTo(HaveOccurred())
596 Expect(r.ReadCount).To(Equal(0))
597 Expect(stdout).To(MatchJSON(fmt.Sprintf(`{
598 "cniVersion": "%s",
599 "supportedVersions": ["9.8.7", "10.0.0"]
600 }`, version.Current())))
601 })
602 })
603
604 Context("when the CNI_COMMAND is unrecognized", func() {
605 BeforeEach(func() {
606 environment["CNI_COMMAND"] = "NOPE"
607 })
608
609 It("does not call any cmd callback", func() {
610 err := dispatch.pluginMain(funcs, versionInfo, "")
611 Expect(err).To(HaveOccurred())
612 Expect(cmdAdd.CallCount).To(Equal(0))
613 Expect(cmdDel.CallCount).To(Equal(0))
614 })
615
616 It("returns an error", func() {
617 err := dispatch.pluginMain(funcs, versionInfo, "")
618
619 Expect(err).To(Equal(&types.Error{
620 Code: types.ErrInvalidEnvironmentVariables,
621 Msg: "unknown CNI_COMMAND: NOPE",
622 }))
623 })
624
625 It("prints the about string when the command is blank", func() {
626 environment["CNI_COMMAND"] = ""
627 err := dispatch.pluginMain(funcs, versionInfo, "test framework v42")
628 Expect(err).NotTo(HaveOccurred())
629 Expect(stderr.String()).To(ContainSubstring("test framework v42"))
630 })
631 })
632
633 Context("when the CNI_COMMAND is missing", func() {
634 It("prints the about string to stderr", func() {
635 environment = map[string]string{}
636 err := dispatch.pluginMain(funcs, versionInfo, "AWESOME PLUGIN")
637 Expect(err).NotTo(HaveOccurred())
638
639 Expect(cmdAdd.CallCount).To(Equal(0))
640 Expect(cmdDel.CallCount).To(Equal(0))
641 log := stderr.String()
642 Expect(log).To(Equal("AWESOME PLUGIN\nCNI protocol versions supported: 9.8.7, 10.0.0\n"))
643 })
644
645 It("fails if there is no about string", func() {
646 environment = map[string]string{}
647 err := dispatch.pluginMain(funcs, versionInfo, "")
648 Expect(err).To(HaveOccurred())
649
650 Expect(cmdAdd.CallCount).To(Equal(0))
651 Expect(cmdDel.CallCount).To(Equal(0))
652 Expect(err).To(Equal(&types.Error{
653 Code: types.ErrInvalidEnvironmentVariables,
654 Msg: "required env variables [CNI_COMMAND] missing",
655 }))
656 })
657 })
658
659 Context("when stdin cannot be read", func() {
660 BeforeEach(func() {
661 dispatch.Stdin = &BadReader{}
662 })
663
664 It("does not call any cmd callback", func() {
665 err := dispatch.pluginMain(funcs, versionInfo, "")
666 Expect(err).To(HaveOccurred())
667 Expect(cmdAdd.CallCount).To(Equal(0))
668 Expect(cmdDel.CallCount).To(Equal(0))
669 })
670
671 It("wraps and returns the error", func() {
672 err := dispatch.pluginMain(funcs, versionInfo, "")
673
674 Expect(err).To(Equal(&types.Error{
675 Code: types.ErrIOFailure,
676 Msg: "error reading from stdin: banana",
677 }))
678 })
679 })
680
681 Context("when the callback returns an error", func() {
682 Context("when it is a typed Error", func() {
683 BeforeEach(func() {
684 cmdAdd.Returns.Error = &types.Error{
685 Code: 1234,
686 Msg: "insufficient something",
687 }
688 })
689
690 It("returns the error as-is", func() {
691 err := dispatch.pluginMain(funcs, versionInfo, "")
692
693 Expect(err).To(Equal(&types.Error{
694 Code: 1234,
695 Msg: "insufficient something",
696 }))
697 })
698 })
699
700 Context("when it is an unknown error", func() {
701 BeforeEach(func() {
702 cmdAdd.Returns.Error = errors.New("potato")
703 })
704
705 It("wraps and returns the error", func() {
706 err := dispatch.pluginMain(funcs, versionInfo, "")
707
708 Expect(err).To(Equal(&types.Error{
709 Code: types.ErrInternal,
710 Msg: "potato",
711 }))
712 })
713 })
714 })
715 })
716
717 // BadReader is an io.Reader which always errors
718 type BadReader struct {
719 Error error
720 ReadCount int
721 }
722
723 func (r *BadReader) Read(buffer []byte) (int, error) {
724 r.ReadCount++
725 if r.Error != nil {
726 return 0, r.Error
727 }
728 return 0, errors.New("banana")
729 }
730
731 func (r *BadReader) Close() error {
732 return nil
733 }