Debian Codegraph @

plugins/test/noop/noop_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 main_test
16
17 import (
18 "fmt"
19 "os"
20 "os/exec"
21 "strings"
22
23 . "github.com/onsi/ginkgo/v2"
24 . "github.com/onsi/gomega"
25 "github.com/onsi/gomega/gexec"
26
27 "github.com/containernetworking/cni/pkg/skel"
28 "github.com/containernetworking/cni/pkg/types"
29 "github.com/containernetworking/cni/pkg/version"
30 noop_debug "github.com/containernetworking/cni/plugins/test/noop/debug"
31 )
32
33 var _ = Describe("No-op plugin", func() {
34 var (
35 cmd *exec.Cmd
36 debugFileName string
37 debug *noop_debug.Debug
38 expectedCmdArgs skel.CmdArgs
39 )
40
41 const reportResult = `{ "ips": [{ "version": "4", "address": "10.1.2.3/24" }], "dns": {} }`
42
43 BeforeEach(func() {
44 debug = &noop_debug.Debug{
45 ReportResult: reportResult,
46 ReportVersionSupport: []string{"0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0"},
47 }
48
49 debugFile, err := os.CreateTemp("", "cni_debug")
50 Expect(err).NotTo(HaveOccurred())
51 Expect(debugFile.Close()).To(Succeed())
52 debugFileName = debugFile.Name()
53
54 Expect(debug.WriteDebug(debugFileName)).To(Succeed())
55
56 cmd = exec.Command(pathToPlugin)
57
58 args := fmt.Sprintf("DEBUG=%s;FOO=BAR", debugFileName)
59 cmd.Env = []string{
60 "CNI_COMMAND=ADD",
61 "CNI_CONTAINERID=some-container-id",
62 "CNI_NETNS=/some/netns/path",
63 "CNI_IFNAME=some-eth0",
64 "CNI_PATH=/some/bin/path",
65 // Keep this last
66 "CNI_ARGS=" + args,
67 }
68 stdinData := `{"name": "noop-test", "some":"stdin-json", "cniVersion": "0.3.1"}`
69 cmd.Stdin = strings.NewReader(stdinData)
70 expectedCmdArgs = skel.CmdArgs{
71 ContainerID: "some-container-id",
72 Netns: "/some/netns/path",
73 IfName: "some-eth0",
74 Args: args,
75 Path: "/some/bin/path",
76 StdinData: []byte(stdinData),
77 }
78 })
79
80 AfterEach(func() {
81 os.Remove(debugFileName)
82 })
83
84 It("responds to ADD using the ReportResult debug field", func() {
85 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
86 Expect(err).NotTo(HaveOccurred())
87 Eventually(session).Should(gexec.Exit(0))
88 Expect(session.Out.Contents()).To(MatchJSON(reportResult))
89 })
90
91 It("panics when no debug file is given", func() {
92 // Remove the DEBUG option from CNI_ARGS and regular args
93 cmd.Env[len(cmd.Env)-1] = "CNI_ARGS=FOO=BAR"
94 expectedCmdArgs.Args = "FOO=BAR"
95
96 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
97 Expect(err).NotTo(HaveOccurred())
98 Eventually(session).Should(gexec.Exit(2))
99 })
100
101 It("pass previous result 0.3.1 through when ReportResult is PASSTHROUGH", func() {
102 debug = &noop_debug.Debug{ReportResult: "PASSTHROUGH"}
103 Expect(debug.WriteDebug(debugFileName)).To(Succeed())
104
105 cmd.Stdin = strings.NewReader(`{
106 "name":"noop-test",
107 "some":"stdin-json",
108 "cniVersion": "0.3.1",
109 "prevResult": {
110 "cniVersion": "0.3.1",
111 "ips": [{"version": "4", "address": "10.1.2.15/24"}]
112 }
113 }`)
114 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
115 Expect(err).NotTo(HaveOccurred())
116 Eventually(session).Should(gexec.Exit(0))
117 Expect(session.Out.Contents()).To(MatchJSON(`{"cniVersion": "0.3.1", "ips": [{"version": "4", "address": "10.1.2.15/24"}], "dns": {}}`))
118 })
119
120 It("pass previous result 0.4.0 through when ReportResult is PASSTHROUGH", func() {
121 debug = &noop_debug.Debug{ReportResult: "PASSTHROUGH"}
122 Expect(debug.WriteDebug(debugFileName)).To(Succeed())
123
124 cmd.Stdin = strings.NewReader(`{
125 "name":"noop-test",
126 "some":"stdin-json",
127 "cniVersion": "0.4.0",
128 "prevResult": {
129 "cniVersion": "0.4.0",
130 "ips": [{"version": "4", "address": "10.1.2.15/24"}]
131 }
132 }`)
133 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
134 Expect(err).NotTo(HaveOccurred())
135 Eventually(session).Should(gexec.Exit(0))
136 Expect(session.Out.Contents()).To(MatchJSON(`{"cniVersion": "0.4.0", "ips": [{"version": "4", "address": "10.1.2.15/24"}], "dns": {}}`))
137 })
138
139 It("injects DNS into previous result when ReportResult is INJECT-DNS", func() {
140 debug = &noop_debug.Debug{ReportResult: "INJECT-DNS"}
141 Expect(debug.WriteDebug(debugFileName)).To(Succeed())
142
143 cmd.Stdin = strings.NewReader(`{
144 "name":"noop-test",
145 "some":"stdin-json",
146 "cniVersion": "0.4.0",
147 "prevResult": {
148 "cniVersion": "0.4.0",
149 "ips": [{"version": "4", "address": "10.1.2.3/24"}],
150 "dns": {}
151 }
152 }`)
153
154 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
155 Expect(err).NotTo(HaveOccurred())
156 Eventually(session).Should(gexec.Exit(0))
157 Expect(session.Out.Contents()).To(MatchJSON(`{
158 "cniVersion": "0.4.0",
159 "ips": [{"version": "4", "address": "10.1.2.3/24"}],
160 "dns": {"nameservers": ["1.2.3.4"]}
161 }`))
162 })
163
164 It("allows passing debug file in config JSON", func() {
165 // Remove the DEBUG option from CNI_ARGS and regular args
166 newArgs := "FOO=BAR"
167 cmd.Env[len(cmd.Env)-1] = "CNI_ARGS=" + newArgs
168 newStdin := fmt.Sprintf(`{"name":"noop-test", "some": "stdin-json", "cniVersion": "0.4.0", "debugFile": %q}`, debugFileName)
169 cmd.Stdin = strings.NewReader(newStdin)
170 expectedCmdArgs.Args = newArgs
171 expectedCmdArgs.StdinData = []byte(newStdin)
172
173 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
174 Expect(err).NotTo(HaveOccurred())
175 Eventually(session).Should(gexec.Exit(0))
176 Expect(session.Out.Contents()).To(MatchJSON(reportResult))
177
178 debug, err := noop_debug.ReadDebug(debugFileName)
179 Expect(err).NotTo(HaveOccurred())
180 Expect(debug.Command).To(Equal("ADD"))
181 Expect(debug.CmdArgs).To(Equal(expectedCmdArgs))
182 })
183
184 It("records all the args provided by skel.PluginMain", func() {
185 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
186 Expect(err).NotTo(HaveOccurred())
187 Eventually(session).Should(gexec.Exit(0))
188
189 debug, err := noop_debug.ReadDebug(debugFileName)
190 Expect(err).NotTo(HaveOccurred())
191 Expect(debug.Command).To(Equal("ADD"))
192 Expect(debug.CmdArgs).To(Equal(expectedCmdArgs))
193 })
194
195 Context("when the ReportResult debug field is empty", func() {
196 BeforeEach(func() {
197 debug.ReportResult = ""
198 Expect(debug.WriteDebug(debugFileName)).To(Succeed())
199 })
200
201 It("returns no result", func() {
202 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
203 Expect(err).NotTo(HaveOccurred())
204 Eventually(session).Should(gexec.Exit(0))
205 Expect(session.Out.Contents()).To(Equal([]byte{}))
206
207 debug, err := noop_debug.ReadDebug(debugFileName)
208 Expect(err).NotTo(HaveOccurred())
209 Expect(debug.ReportResult).To(Equal(""))
210 })
211 })
212
213 Context("when the ExitWithCode debug field is set", func() {
214 BeforeEach(func() {
215 debug.ReportResult = ""
216 debug.ExitWithCode = 3
217 Expect(debug.WriteDebug(debugFileName)).To(Succeed())
218 })
219
220 It("returns no result and exits with the expected code", func() {
221 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
222 Expect(err).NotTo(HaveOccurred())
223 Eventually(session).Should(gexec.Exit(3))
224 Expect(session.Out.Contents()).To(Equal([]byte{}))
225
226 debug, err := noop_debug.ReadDebug(debugFileName)
227 Expect(err).NotTo(HaveOccurred())
228 Expect(debug.ReportResult).To(Equal(""))
229 })
230 })
231
232 Context("when the ReportResult debug field is set", func() {
233 expectedResultString := fmt.Sprintf(` { "result": %q }`, noop_debug.EmptyReportResultMessage)
234
235 BeforeEach(func() {
236 debug.ReportResult = expectedResultString
237 Expect(debug.WriteDebug(debugFileName)).To(Succeed())
238 })
239
240 It("substitutes a helpful message for the test author", func() {
241 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
242 Expect(err).NotTo(HaveOccurred())
243 Eventually(session).Should(gexec.Exit(0))
244 Expect(session.Out.Contents()).To(MatchJSON(expectedResultString))
245
246 debug, err := noop_debug.ReadDebug(debugFileName)
247 Expect(err).NotTo(HaveOccurred())
248 Expect(debug.ReportResult).To(MatchJSON(expectedResultString))
249 })
250 })
251
252 Context("when the ReportError debug field is set", func() {
253 BeforeEach(func() {
254 debug.ReportError = "banana"
255 Expect(debug.WriteDebug(debugFileName)).To(Succeed())
256 })
257
258 It("returns an error to skel.PluginMain, causing the process to exit code 1", func() {
259 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
260 Expect(err).NotTo(HaveOccurred())
261 Eventually(session).Should(gexec.Exit(1))
262 Expect(session.Out.Contents()).To(MatchJSON(fmt.Sprintf(`{ "code": %d, "msg": "banana" }`, types.ErrInternal)))
263 })
264 })
265
266 Context("when the CNI_COMMAND is DEL", func() {
267 BeforeEach(func() {
268 cmd.Env[0] = "CNI_COMMAND=DEL"
269 debug.ReportResult = `{ "some": "delete-data" }`
270 Expect(debug.WriteDebug(debugFileName)).To(Succeed())
271 })
272
273 It("still does all the debug behavior", func() {
274 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
275 Expect(err).NotTo(HaveOccurred())
276 Eventually(session).Should(gexec.Exit(0))
277 Expect(session.Out.Contents()).To(MatchJSON(`{
278 "some": "delete-data"
279 }`))
280 debug, err := noop_debug.ReadDebug(debugFileName)
281 Expect(err).NotTo(HaveOccurred())
282 Expect(debug.Command).To(Equal("DEL"))
283 Expect(debug.CmdArgs).To(Equal(expectedCmdArgs))
284 })
285 })
286
287 Context("when the CNI_COMMAND is VERSION", func() {
288 BeforeEach(func() {
289 cmd.Env[0] = "CNI_COMMAND=VERSION"
290 debug.ReportVersionSupport = []string{"0.123.0", "0.2.0"}
291
292 Expect(debug.WriteDebug(debugFileName)).To(Succeed())
293 })
294
295 It("claims to support the specified versions", func() {
296 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
297 Expect(err).NotTo(HaveOccurred())
298 Eventually(session).Should(gexec.Exit(0))
299 decoder := &version.PluginDecoder{}
300 pluginInfo, err := decoder.Decode(session.Out.Contents())
301 Expect(err).NotTo(HaveOccurred())
302 Expect(pluginInfo.SupportedVersions()).To(ConsistOf(
303 "0.123.0", "0.2.0"))
304 })
305 })
306 })