Debian Codegraph @

plugins/sample/sample_linux_test.go

raw symbols SCIP
1 // Copyright 2017 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
20 . "github.com/onsi/ginkgo/v2"
21 . "github.com/onsi/gomega"
22
23 "github.com/containernetworking/cni/pkg/skel"
24 "github.com/containernetworking/plugins/pkg/ns"
25 "github.com/containernetworking/plugins/pkg/testutils"
26 )
27
28 var _ = Describe("sample test", func() {
29 var targetNs ns.NetNS
30
31 BeforeEach(func() {
32 var err error
33 targetNs, err = testutils.NewNS()
34 Expect(err).NotTo(HaveOccurred())
35 })
36
37 AfterEach(func() {
38 targetNs.Close()
39 })
40
41 It("Works with a 0.3.0 config", func() {
42 ifname := "eth0"
43 conf := `{
44 "cniVersion": "0.3.0",
45 "name": "cni-plugin-sample-test",
46 "type": "sample",
47 "anotherAwesomeArg": "awesome",
48 "prevResult": {
49 "cniVersion": "0.3.0",
50 "interfaces": [
51 {
52 "name": "%s",
53 "sandbox": "%s"
54 }
55 ],
56 "ips": [
57 {
58 "version": "4",
59 "address": "10.0.0.2/24",
60 "gateway": "10.0.0.1",
61 "interface": 0
62 }
63 ],
64 "routes": []
65 }
66 }`
67 conf = fmt.Sprintf(conf, ifname, targetNs.Path())
68 args := &skel.CmdArgs{
69 ContainerID: "dummy",
70 Netns: targetNs.Path(),
71 IfName: ifname,
72 StdinData: []byte(conf),
73 }
74 _, _, err := testutils.CmdAddWithArgs(args, func() error { return cmdAdd(args) })
75 Expect(err).NotTo(HaveOccurred())
76 })
77
78 It("fails an invalid config", func() {
79 conf := `{
80 "cniVersion": "0.3.0",
81 "name": "cni-plugin-sample-test",
82 "type": "sample",
83 "prevResult": {
84 "interfaces": [
85 {
86 "name": "eth0",
87 "sandbox": "/var/run/netns/test"
88 }
89 ],
90 "ips": [
91 {
92 "version": "4",
93 "address": "10.0.0.2/24",
94 "gateway": "10.0.0.1",
95 "interface": 0
96 }
97 ],
98 "routes": []
99 }
100 }`
101
102 args := &skel.CmdArgs{
103 ContainerID: "dummy",
104 Netns: targetNs.Path(),
105 IfName: "eth0",
106 StdinData: []byte(conf),
107 }
108 _, _, err := testutils.CmdAddWithArgs(args, func() error { return cmdAdd(args) })
109 Expect(err).To(MatchError("anotherAwesomeArg must be specified"))
110 })
111
112 It("fails with CNI spec versions that don't support plugin chaining", func() {
113 conf := `{
114 "cniVersion": "0.2.0",
115 "name": "cni-plugin-sample-test",
116 "type": "sample",
117 "anotherAwesomeArg": "foo"
118 }`
119
120 args := &skel.CmdArgs{
121 ContainerID: "dummy",
122 Netns: targetNs.Path(),
123 IfName: "eth0",
124 StdinData: []byte(conf),
125 }
126 _, _, err := testutils.CmdAddWithArgs(args, func() error { return cmdAdd(args) })
127 Expect(err).To(MatchError("must be called as chained plugin"))
128 })
129 })