Debian Codegraph @

libcni/conf_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 libcni_test
16
17 import (
18 "fmt"
19 "os"
20 "path/filepath"
21 "strings"
22
23 . "github.com/onsi/ginkgo/v2"
24 . "github.com/onsi/gomega"
25
26 "github.com/containernetworking/cni/libcni"
27 "github.com/containernetworking/cni/pkg/types"
28 )
29
30 var _ = Describe("Loading configuration from disk", func() {
31 Describe("LoadConf", func() {
32 var (
33 configDir string
34 pluginConfig []byte
35 )
36
37 BeforeEach(func() {
38 var err error
39 configDir, err = os.MkdirTemp("", "plugin-conf")
40 Expect(err).NotTo(HaveOccurred())
41
42 pluginConfig = []byte(`{ "name": "some-plugin", "type": "foobar", "some-key": "some-value" }`)
43 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conf"), pluginConfig, 0o600)).To(Succeed())
44 })
45
46 AfterEach(func() {
47 Expect(os.RemoveAll(configDir)).To(Succeed())
48 })
49
50 It("finds the network config file for the plugin of the given type", func() {
51 netConfig, err := libcni.LoadConf(configDir, "some-plugin")
52 Expect(err).NotTo(HaveOccurred())
53 Expect(netConfig).To(Equal(&libcni.PluginConfig{
54 Network: &types.PluginConf{
55 Name: "some-plugin",
56 Type: "foobar",
57 },
58 Bytes: pluginConfig,
59 }))
60 })
61
62 Context("when the config directory does not exist", func() {
63 BeforeEach(func() {
64 Expect(os.RemoveAll(configDir)).To(Succeed())
65 })
66
67 It("returns a useful error", func() {
68 _, err := libcni.LoadConf(configDir, "some-plugin")
69 Expect(err).To(MatchError(libcni.NoConfigsFoundError{Dir: configDir}))
70 })
71 })
72
73 Context("when the config file is .json extension instead of .conf", func() {
74 BeforeEach(func() {
75 Expect(os.Remove(configDir + "/50-whatever.conf")).To(Succeed())
76 pluginConfig = []byte(`{ "name": "some-plugin", "some-key": "some-value", "type": "foobar" }`)
77 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.json"), pluginConfig, 0o600)).To(Succeed())
78 })
79 It("finds the network config file for the plugin of the given type", func() {
80 netConfig, err := libcni.LoadConf(configDir, "some-plugin")
81 Expect(err).NotTo(HaveOccurred())
82 Expect(netConfig).To(Equal(&libcni.PluginConfig{
83 Network: &types.PluginConf{
84 Name: "some-plugin",
85 Type: "foobar",
86 },
87 Bytes: pluginConfig,
88 }))
89 })
90 })
91
92 Context("when there is no config for the desired plugin", func() {
93 It("returns a useful error", func() {
94 _, err := libcni.LoadConf(configDir, "some-other-plugin")
95 Expect(err).To(MatchError(ContainSubstring(`no net configuration with name "some-other-plugin" in`)))
96 })
97 })
98
99 Context("when a config file is malformed", func() {
100 BeforeEach(func() {
101 Expect(os.WriteFile(filepath.Join(configDir, "00-bad.conf"), []byte(`{`), 0o600)).To(Succeed())
102 })
103
104 It("returns a useful error", func() {
105 _, err := libcni.LoadConf(configDir, "some-plugin")
106 Expect(err).To(MatchError(`error parsing configuration: unexpected end of JSON input`))
107 })
108 })
109
110 Context("when the config is in a nested subdir", func() {
111 BeforeEach(func() {
112 subdir := filepath.Join(configDir, "subdir1", "subdir2")
113 Expect(os.MkdirAll(subdir, 0o700)).To(Succeed())
114
115 pluginConfig = []byte(`{ "name": "deep", "some-key": "some-value" }`)
116 Expect(os.WriteFile(filepath.Join(subdir, "90-deep.conf"), pluginConfig, 0o600)).To(Succeed())
117 })
118
119 It("will not find the config", func() {
120 _, err := libcni.LoadConf(configDir, "deep")
121 Expect(err).To(MatchError(HavePrefix("no net configuration with name")))
122 })
123 })
124 })
125
126 Describe("Capabilities", func() {
127 var configDir string
128
129 BeforeEach(func() {
130 var err error
131 configDir, err = os.MkdirTemp("", "plugin-conf")
132 Expect(err).NotTo(HaveOccurred())
133
134 pluginConfig := []byte(`{ "name": "some-plugin", "type": "noop", "cniVersion": "0.3.1", "capabilities": { "portMappings": true, "somethingElse": true, "noCapability": false } }`)
135 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conf"), pluginConfig, 0o600)).To(Succeed())
136 })
137
138 AfterEach(func() {
139 Expect(os.RemoveAll(configDir)).To(Succeed())
140 })
141
142 It("reads plugin capabilities from network config", func() {
143 netConfig, err := libcni.LoadConf(configDir, "some-plugin")
144 Expect(err).NotTo(HaveOccurred())
145 Expect(netConfig.Network.Capabilities).To(Equal(map[string]bool{
146 "portMappings": true,
147 "somethingElse": true,
148 "noCapability": false,
149 }))
150 })
151 })
152
153 Describe("ConfFromFile", func() {
154 Context("when the file cannot be opened", func() {
155 It("returns a useful error", func() {
156 _, err := libcni.ConfFromFile("/tmp/nope/not-here")
157 Expect(err).To(MatchError(HavePrefix(`error reading /tmp/nope/not-here: open /tmp/nope/not-here`)))
158 })
159 })
160
161 Context("when the file is missing 'type'", func() {
162 var fileName, configDir string
163 BeforeEach(func() {
164 var err error
165 configDir, err = os.MkdirTemp("", "plugin-conf")
166 Expect(err).NotTo(HaveOccurred())
167
168 fileName = filepath.Join(configDir, "50-whatever.conf")
169 pluginConfig := []byte(`{ "name": "some-plugin", "some-key": "some-value" }`)
170 Expect(os.WriteFile(fileName, pluginConfig, 0o600)).To(Succeed())
171 })
172
173 AfterEach(func() {
174 Expect(os.RemoveAll(configDir)).To(Succeed())
175 })
176
177 It("returns a useful error", func() {
178 _, err := libcni.ConfFromFile(fileName)
179 Expect(err).To(MatchError(`error parsing configuration: missing 'type'`))
180 })
181 })
182 })
183
184 Describe("NetworkPluginConfFromBytes", func() {
185 Context("when the config is missing 'type'", func() {
186 It("returns a useful error", func() {
187 _, err := libcni.ConfFromBytes([]byte(`{ "name": "some-plugin", "some-key": "some-value" }`))
188 Expect(err).To(MatchError(`error parsing configuration: missing 'type'`))
189 })
190 })
191 })
192
193 Describe("LoadNetworkConf", func() {
194 var (
195 configDir string
196 configList []byte
197 )
198
199 BeforeEach(func() {
200 var err error
201 configDir, err = os.MkdirTemp("", "plugin-conf")
202 Expect(err).NotTo(HaveOccurred())
203
204 configList = []byte(`{
205 "name": "some-network",
206 "cniVersion": "0.2.0",
207 "disableCheck": true,
208 "plugins": [
209 {
210 "type": "host-local",
211 "subnet": "10.0.0.1/24"
212 },
213 {
214 "type": "bridge",
215 "mtu": 1400
216 },
217 {
218 "type": "port-forwarding",
219 "ports": {"20.0.0.1:8080": "80"}
220 }
221 ]
222 }`)
223 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
224 })
225
226 AfterEach(func() {
227 Expect(os.RemoveAll(configDir)).To(Succeed())
228 })
229
230 It("finds the network config file for the plugin of the given type", func() {
231 netConfigList, err := libcni.LoadNetworkConf(configDir, "some-network")
232 Expect(err).NotTo(HaveOccurred())
233 Expect(netConfigList).To(Equal(&libcni.NetworkConfigList{
234 Name: "some-network",
235 CNIVersion: "0.2.0",
236 DisableCheck: true,
237 Plugins: []*libcni.PluginConfig{
238 {
239 Network: &types.PluginConf{Type: "host-local"},
240 Bytes: []byte(`{"subnet":"10.0.0.1/24","type":"host-local"}`),
241 },
242 {
243 Network: &types.PluginConf{Type: "bridge"},
244 Bytes: []byte(`{"mtu":1400,"type":"bridge"}`),
245 },
246 {
247 Network: &types.PluginConf{Type: "port-forwarding"},
248 Bytes: []byte(`{"ports":{"20.0.0.1:8080":"80"},"type":"port-forwarding"}`),
249 },
250 },
251 Bytes: configList,
252 }))
253 })
254
255 Context("when there is a config file with the same name as the list", func() {
256 BeforeEach(func() {
257 configFile := []byte(`{
258 "name": "some-network",
259 "cniVersion": "0.2.0",
260 "type": "bridge"
261 }`)
262 Expect(os.WriteFile(filepath.Join(configDir, "49-whatever.conf"), configFile, 0o600)).To(Succeed())
263 })
264
265 It("Loads the config list first", func() {
266 netConfigList, err := libcni.LoadNetworkConf(configDir, "some-network")
267 Expect(err).NotTo(HaveOccurred())
268 Expect(netConfigList.Plugins).To(HaveLen(3))
269 })
270
271 It("falls back to the config file", func() {
272 Expect(os.Remove(filepath.Join(configDir, "50-whatever.conflist"))).To(Succeed())
273
274 netConfigList, err := libcni.LoadNetworkConf(configDir, "some-network")
275 Expect(err).NotTo(HaveOccurred())
276 Expect(netConfigList.Plugins).To(HaveLen(1))
277 Expect(netConfigList.Plugins[0].Network.Type).To(Equal("bridge"))
278 })
279 })
280
281 Context("when the config directory does not exist", func() {
282 BeforeEach(func() {
283 Expect(os.RemoveAll(configDir)).To(Succeed())
284 })
285
286 It("returns a useful error", func() {
287 _, err := libcni.LoadNetworkConf(configDir, "some-network")
288 Expect(err).To(MatchError(libcni.NoConfigsFoundError{Dir: configDir}))
289 })
290 })
291
292 Context("when there is no config for the desired network name", func() {
293 It("returns a useful error", func() {
294 _, err := libcni.LoadNetworkConf(configDir, "some-other-network")
295 Expect(err).To(MatchError(libcni.NotFoundError{Dir: configDir, Name: "some-other-network"}))
296 })
297 })
298
299 Context("when a config file is malformed", func() {
300 BeforeEach(func() {
301 Expect(os.WriteFile(filepath.Join(configDir, "00-bad.conflist"), []byte(`{`), 0o600)).To(Succeed())
302 })
303
304 It("returns a useful error", func() {
305 _, err := libcni.LoadNetworkConf(configDir, "some-plugin")
306 Expect(err).To(MatchError(`error parsing configuration list: unexpected end of JSON input`))
307 })
308 })
309
310 Context("when the config is in a nested subdir", func() {
311 BeforeEach(func() {
312 subdir := filepath.Join(configDir, "subdir1", "subdir2")
313 Expect(os.MkdirAll(subdir, 0o700)).To(Succeed())
314
315 configList = []byte(`{
316 "name": "deep",
317 "cniVersion": "0.2.0",
318 "plugins": [
319 {
320 "type": "host-local",
321 "subnet": "10.0.0.1/24"
322 },
323 ]
324 }`)
325 Expect(os.WriteFile(filepath.Join(subdir, "90-deep.conflist"), configList, 0o600)).To(Succeed())
326 })
327
328 It("will not find the config", func() {
329 _, err := libcni.LoadNetworkConf(configDir, "deep")
330 Expect(err).To(MatchError(HavePrefix("no net configuration with name")))
331 })
332 })
333
334 Context("when disableCheck is a string not a boolean", func() {
335 It("will read a 'true' value and convert to boolean", func() {
336 configList = []byte(`{
337 "name": "some-network",
338 "cniVersion": "0.4.0",
339 "disableCheck": "true",
340 "plugins": [
341 {
342 "type": "host-local",
343 "subnet": "10.0.0.1/24"
344 }
345 ]
346 }`)
347 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
348
349 netConfigList, err := libcni.LoadNetworkConf(configDir, "some-network")
350 Expect(err).NotTo(HaveOccurred())
351 Expect(netConfigList.DisableCheck).To(BeTrue())
352 })
353
354 It("will read a 'false' value and convert to boolean", func() {
355 configList = []byte(`{
356 "name": "some-network",
357 "cniVersion": "0.4.0",
358 "disableCheck": "false",
359 "plugins": [
360 {
361 "type": "host-local",
362 "subnet": "10.0.0.1/24"
363 }
364 ]
365 }`)
366 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
367
368 netConfigList, err := libcni.LoadNetworkConf(configDir, "some-network")
369 Expect(err).NotTo(HaveOccurred())
370 Expect(netConfigList.DisableCheck).To(BeFalse())
371 })
372
373 It("will return an error on an unrecognized value", func() {
374 const badValue string = "adsfasdfasf"
375 configList = []byte(fmt.Sprintf(`{
376 "name": "some-network",
377 "cniVersion": "0.4.0",
378 "disableCheck": "%s",
379 "plugins": [
380 {
381 "type": "host-local",
382 "subnet": "10.0.0.1/24"
383 }
384 ]
385 }`, badValue))
386 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
387
388 _, err := libcni.LoadNetworkConf(configDir, "some-network")
389 Expect(err).To(MatchError(fmt.Sprintf("error parsing configuration list: invalid value \"%s\" for disableCheck", badValue)))
390 })
391 })
392
393 Context("for loadOnlyInlinedPlugins", func() {
394 It("the value will be parsed", func() {
395 configList = []byte(`{
396 "name": "some-network",
397 "cniVersion": "0.4.0",
398 "loadOnlyInlinedPlugins": true,
399 "plugins": [
400 {
401 "type": "host-local",
402 "subnet": "10.0.0.1/24"
403 }
404 ]
405 }`)
406 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
407
408 dirPluginConf := []byte(`{
409 "type": "bro-check-out-my-plugin",
410 "subnet": "10.0.0.1/24"
411 }`)
412
413 subDir := filepath.Join(configDir, "some-network")
414 Expect(os.MkdirAll(subDir, 0o700)).To(Succeed())
415 Expect(os.WriteFile(filepath.Join(subDir, "funky-second-plugin.conf"), dirPluginConf, 0o600)).To(Succeed())
416
417 netConfigList, err := libcni.LoadNetworkConf(configDir, "some-network")
418 Expect(err).NotTo(HaveOccurred())
419 Expect(netConfigList.LoadOnlyInlinedPlugins).To(BeTrue())
420 })
421
422 It("the value will be false if not in config", func() {
423 configList = []byte(`{
424 "name": "some-network",
425 "cniVersion": "0.4.0",
426 "plugins": [
427 {
428 "type": "host-local",
429 "subnet": "10.0.0.1/24"
430 }
431 ]
432 }`)
433 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
434
435 netConfigList, err := libcni.LoadNetworkConf(configDir, "some-network")
436 Expect(err).NotTo(HaveOccurred())
437 Expect(netConfigList.LoadOnlyInlinedPlugins).To(BeFalse())
438 })
439
440 It("will return an error on an unrecognized value", func() {
441 const badValue string = "sphagnum"
442 configList = []byte(fmt.Sprintf(`{
443 "name": "some-network",
444 "cniVersion": "0.4.0",
445 "loadOnlyInlinedPlugins": "%s",
446 "plugins": [
447 {
448 "type": "host-local",
449 "subnet": "10.0.0.1/24"
450 }
451 ]
452 }`, badValue))
453 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
454
455 _, err := libcni.LoadNetworkConf(configDir, "some-network")
456 Expect(err).To(MatchError(fmt.Sprintf(`error parsing configuration list: invalid value "%s" for loadOnlyInlinedPlugins`, badValue)))
457 })
458
459 It("will return an error if `plugins` is missing and `loadOnlyInlinedPlugins` is `true`", func() {
460 configList = []byte(`{
461 "name": "some-network",
462 "cniVersion": "0.4.0",
463 "loadOnlyInlinedPlugins": true
464 }`)
465 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
466
467 _, err := libcni.LoadNetworkConf(configDir, "some-network")
468 Expect(err).To(MatchError("error parsing configuration list: `loadOnlyInlinedPlugins` is true, and no 'plugins' key"))
469 })
470
471 It("will return no error if `plugins` is missing and `loadOnlyInlinedPlugins` is false", func() {
472 configList = []byte(`{
473 "name": "some-network",
474 "cniVersion": "0.4.0",
475 "loadOnlyInlinedPlugins": false
476 }`)
477 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
478
479 dirPluginConf := []byte(`{
480 "type": "bro-check-out-my-plugin",
481 "subnet": "10.0.0.1/24"
482 }`)
483
484 subDir := filepath.Join(configDir, "some-network")
485 Expect(os.MkdirAll(subDir, 0o700)).To(Succeed())
486 Expect(os.WriteFile(filepath.Join(subDir, "funky-second-plugin.conf"), dirPluginConf, 0o600)).To(Succeed())
487
488 netConfigList, err := libcni.LoadNetworkConf(configDir, "some-network")
489 Expect(err).NotTo(HaveOccurred())
490 Expect(netConfigList.LoadOnlyInlinedPlugins).To(BeFalse())
491 Expect(netConfigList.Plugins).To(HaveLen(1))
492 })
493
494 It("will return error if `loadOnlyInlinedPlugins` is implicitly false + no conf plugin is defined, but no plugins subfolder with network name exists", func() {
495 configList = []byte(`{
496 "name": "some-network",
497 "cniVersion": "0.4.0"
498 }`)
499 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
500
501 _, err := libcni.LoadNetworkConf(configDir, "some-network")
502 Expect(err).To(MatchError("no plugin configs found"))
503 })
504
505 It("will return NO error if `loadOnlyInlinedPlugins` is implicitly false + at least 1 conf plugin is defined, but no plugins subfolder with network name exists", func() {
506 configList = []byte(`{
507 "name": "some-network",
508 "cniVersion": "0.4.0",
509 "plugins": [
510 {
511 "type": "host-local",
512 "subnet": "10.0.0.1/24"
513 }
514 ]
515 }`)
516 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
517
518 _, err := libcni.LoadNetworkConf(configDir, "some-network")
519 Expect(err).NotTo(HaveOccurred())
520 })
521
522 It("will return NO error if `loadOnlyInlinedPlugins` is implicitly false + at least 1 conf plugin is defined and network name subfolder exists, but is empty/unreadable", func() {
523 configList = []byte(`{
524 "name": "some-network",
525 "cniVersion": "0.4.0",
526 "plugins": [
527 {
528 "type": "host-local",
529 "subnet": "10.0.0.1/24"
530 }
531 ]
532 }`)
533 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
534
535 subDir := filepath.Join(configDir, "some-network")
536 Expect(os.MkdirAll(subDir, 0o700)).To(Succeed())
537
538 _, err := libcni.LoadNetworkConf(configDir, "some-network")
539 Expect(err).NotTo(HaveOccurred())
540 })
541
542 It("will merge loaded and inlined plugin lists if both `plugins` is set and `loadOnlyInlinedPlugins` is false", func() {
543 configList = []byte(`{
544 "name": "some-network",
545 "cniVersion": "0.4.0",
546 "plugins": [
547 {
548 "type": "host-local",
549 "subnet": "10.0.0.1/24"
550 }
551 ]
552 }`)
553
554 dirPluginConf := []byte(`{
555 "type": "bro-check-out-my-plugin",
556 "subnet": "10.0.0.1/24"
557 }`)
558
559 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
560
561 subDir := filepath.Join(configDir, "some-network")
562 Expect(os.MkdirAll(subDir, 0o700)).To(Succeed())
563 Expect(os.WriteFile(filepath.Join(subDir, "funky-second-plugin.conf"), dirPluginConf, 0o600)).To(Succeed())
564
565 netConfigList, err := libcni.LoadNetworkConf(configDir, "some-network")
566 Expect(err).NotTo(HaveOccurred())
567 Expect(netConfigList.LoadOnlyInlinedPlugins).To(BeFalse())
568 Expect(netConfigList.Plugins).To(HaveLen(2))
569 })
570
571 It("will ignore loaded plugins if `plugins` is set and `loadOnlyInlinedPlugins` is true", func() {
572 configList = []byte(`{
573 "name": "some-network",
574 "cniVersion": "0.4.0",
575 "loadOnlyInlinedPlugins": true,
576 "plugins": [
577 {
578 "type": "host-local",
579 "subnet": "10.0.0.1/24"
580 }
581 ]
582 }`)
583
584 dirPluginConf := []byte(`{
585 "type": "bro-check-out-my-plugin",
586 "subnet": "10.0.0.1/24"
587 }`)
588
589 Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
590 subDir := filepath.Join(configDir, "some-network")
591 Expect(os.MkdirAll(subDir, 0o700)).To(Succeed())
592 Expect(os.WriteFile(filepath.Join(subDir, "funky-second-plugin.conf"), dirPluginConf, 0o600)).To(Succeed())
593
594 netConfigList, err := libcni.LoadNetworkConf(configDir, "some-network")
595 Expect(err).NotTo(HaveOccurred())
596 Expect(netConfigList.LoadOnlyInlinedPlugins).To(BeTrue())
597 Expect(netConfigList.Plugins).To(HaveLen(1))
598 Expect(netConfigList.Plugins[0].Network.Type).To(Equal("host-local"))
599 })
600 })
601 })
602
603 Describe("NetworkConfFromFile", func() {
604 Context("when the file cannot be opened", func() {
605 It("returns a useful error", func() {
606 _, err := libcni.NetworkConfFromFile("/tmp/nope/not-here")
607 Expect(err).To(MatchError(HavePrefix(`error reading /tmp/nope/not-here: open /tmp/nope/not-here`)))
608 })
609 })
610 })
611
612 Describe("InjectConf", func() {
613 var testNetConfig *libcni.PluginConfig
614
615 BeforeEach(func() {
616 testNetConfig = &libcni.PluginConfig{
617 Network: &types.PluginConf{Name: "some-plugin", Type: "foobar"},
618 Bytes: []byte(`{ "name": "some-plugin", "type": "foobar" }`),
619 }
620 })
621
622 Context("when function parameters are incorrect", func() {
623 It("returns unmarshal error", func() {
624 conf := &libcni.PluginConfig{
625 Network: &types.PluginConf{Name: "some-plugin"},
626 Bytes: []byte(`{ cc cc cc}`),
627 }
628
629 _, err := libcni.InjectConf(conf, map[string]interface{}{"": nil})
630 Expect(err).To(MatchError(HavePrefix(`unmarshal existing network bytes`)))
631 })
632
633 It("returns key error", func() {
634 _, err := libcni.InjectConf(testNetConfig, map[string]interface{}{"": nil})
635 Expect(err).To(MatchError(HavePrefix(`keys cannot be empty`)))
636 })
637
638 It("returns newValue error", func() {
639 _, err := libcni.InjectConf(testNetConfig, map[string]interface{}{"test": nil})
640 Expect(err).To(MatchError(HavePrefix(`key 'test' value must not be nil`)))
641 })
642 })
643
644 Context("when new string value added", func() {
645 It("adds the new key & value to the config", func() {
646 newPluginConfig := []byte(`{"name":"some-plugin","test":"test","type":"foobar"}`)
647
648 resultConfig, err := libcni.InjectConf(testNetConfig, map[string]interface{}{"test": "test"})
649 Expect(err).NotTo(HaveOccurred())
650 Expect(resultConfig).To(Equal(&libcni.PluginConfig{
651 Network: &types.PluginConf{
652 Name: "some-plugin",
653 Type: "foobar",
654 },
655 Bytes: newPluginConfig,
656 }))
657 })
658
659 It("adds the new value for exiting key", func() {
660 newPluginConfig := []byte(`{"name":"some-plugin","test":"changedValue","type":"foobar"}`)
661
662 resultConfig, err := libcni.InjectConf(testNetConfig, map[string]interface{}{"test": "test"})
663 Expect(err).NotTo(HaveOccurred())
664
665 resultConfig, err = libcni.InjectConf(resultConfig, map[string]interface{}{"test": "changedValue"})
666 Expect(err).NotTo(HaveOccurred())
667
668 Expect(resultConfig).To(Equal(&libcni.PluginConfig{
669 Network: &types.PluginConf{
670 Name: "some-plugin",
671 Type: "foobar",
672 },
673 Bytes: newPluginConfig,
674 }))
675 })
676
677 It("adds existing key & value", func() {
678 newPluginConfig := []byte(`{"name":"some-plugin","test":"test","type":"foobar"}`)
679
680 resultConfig, err := libcni.InjectConf(testNetConfig, map[string]interface{}{"test": "test"})
681 Expect(err).NotTo(HaveOccurred())
682
683 resultConfig, err = libcni.InjectConf(resultConfig, map[string]interface{}{"test": "test"})
684 Expect(err).NotTo(HaveOccurred())
685
686 Expect(resultConfig).To(Equal(&libcni.PluginConfig{
687 Network: &types.PluginConf{
688 Name: "some-plugin",
689 Type: "foobar",
690 },
691 Bytes: newPluginConfig,
692 }))
693 })
694
695 It("adds sub-fields of NetworkConfig.Network to the config", func() {
696 expectedPluginConfig := []byte(`{"dns":{"domain":"local","nameservers":["server1","server2"]},"name":"some-plugin","type":"bridge"}`)
697 servers := []string{"server1", "server2"}
698 newDNS := &types.DNS{Nameservers: servers, Domain: "local"}
699
700 // inject DNS
701 resultConfig, err := libcni.InjectConf(testNetConfig, map[string]interface{}{"dns": newDNS})
702 Expect(err).NotTo(HaveOccurred())
703
704 // inject type
705 resultConfig, err = libcni.InjectConf(resultConfig, map[string]interface{}{"type": "bridge"})
706 Expect(err).NotTo(HaveOccurred())
707
708 Expect(resultConfig).To(Equal(&libcni.PluginConfig{
709 Network: &types.PluginConf{Name: "some-plugin", Type: "bridge", DNS: types.DNS{Nameservers: servers, Domain: "local"}},
710 Bytes: expectedPluginConfig,
711 }))
712 })
713 })
714 })
715 })
716
717 var _ = Describe("NetworkConfFromBytes", func() {
718 Describe("Version selection", func() {
719 makeConfig := func(versions ...string) []byte {
720 // ugly fake json encoding, but whatever
721 vs := []string{}
722 for _, v := range versions {
723 vs = append(vs, fmt.Sprintf(`"%s"`, v))
724 }
725 return []byte(fmt.Sprintf(`{"name": "test", "cniVersions": [%s], "plugins": [{"type": "foo"}]}`, strings.Join(vs, ",")))
726 }
727 It("correctly selects the maximum version", func() {
728 conf, err := libcni.NetworkConfFromBytes(makeConfig("1.1.0", "0.4.0", "1.0.0"))
729 Expect(err).NotTo(HaveOccurred())
730 Expect(conf.CNIVersion).To(Equal("1.1.0"))
731 })
732
733 It("selects the highest version supported by libcni", func() {
734 conf, err := libcni.NetworkConfFromBytes(makeConfig("99.0.0", "1.1.0", "0.4.0", "1.0.0"))
735 Expect(err).NotTo(HaveOccurred())
736 Expect(conf.CNIVersion).To(Equal("1.1.0"))
737 })
738
739 It("fails when invalid versions are specified", func() {
740 _, err := libcni.NetworkConfFromBytes(makeConfig("1.1.0", "0.4.0", "1.0.f"))
741 Expect(err).To(HaveOccurred())
742 })
743
744 It("falls back to cniVersion", func() {
745 conf, err := libcni.NetworkConfFromBytes([]byte(`{"name": "test", "cniVersion": "1.2.3", "plugins": [{"type": "foo"}]}`))
746 Expect(err).NotTo(HaveOccurred())
747 Expect(conf.CNIVersion).To(Equal("1.2.3"))
748 })
749
750 It("merges cniVersions and cniVersion", func() {
751 conf, err := libcni.NetworkConfFromBytes([]byte(`{"name": "test", "cniVersion": "1.0.0", "cniVersions": ["0.1.0", "0.4.0"], "plugins": [{"type": "foo"}]}`))
752 Expect(err).NotTo(HaveOccurred())
753 Expect(conf.CNIVersion).To(Equal("1.0.0"))
754 })
755
756 It("handles an empty cniVersions array", func() {
757 conf, err := libcni.NetworkConfFromBytes([]byte(`{"name": "test", "cniVersions": [], "plugins": [{"type": "foo"}]}`))
758 Expect(err).NotTo(HaveOccurred())
759 Expect(conf.CNIVersion).To(Equal(""))
760 })
761 })
762 })
763
764 var _ = Describe("ConfListFromConf", func() {
765 var testNetConfig *libcni.PluginConfig
766
767 BeforeEach(func() {
768 pb := []byte(`{"name":"some-plugin","cniVersion":"0.3.1", "type":"foobar"}`)
769 tc, err := libcni.ConfFromBytes(pb)
770 Expect(err).NotTo(HaveOccurred())
771 testNetConfig = tc
772 })
773
774 It("correctly upconverts a NetworkConfig to a NetworkConfigList", func() {
775 ncl, err := libcni.ConfListFromConf(testNetConfig)
776 Expect(err).NotTo(HaveOccurred())
777 bytes := ncl.Bytes
778
779 // null out the json - we don't care about the exact marshalling
780 ncl.Bytes = nil
781 ncl.Plugins[0].Bytes = nil
782 testNetConfig.Bytes = nil
783
784 Expect(ncl).To(Equal(&libcni.NetworkConfigList{
785 Name: "some-plugin",
786 CNIVersion: "0.3.1",
787 Plugins: []*libcni.PluginConfig{testNetConfig},
788 }))
789
790 // Test that the json unmarshals to the same data
791 ncl2, err := libcni.NetworkConfFromBytes(bytes)
792 Expect(err).NotTo(HaveOccurred())
793 ncl2.Bytes = nil
794 ncl2.Plugins[0].Bytes = nil
795
796 Expect(ncl2).To(Equal(ncl))
797 })
798 })