Debian Codegraph @

pkg/invoke/delegate_test.go

raw
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 invoke_test
16
17 import (
18 "context"
19 "encoding/json"
20 "net"
21 "os"
22 "path/filepath"
23
24 . "github.com/onsi/ginkgo/v2"
25 . "github.com/onsi/gomega"
26
27 "github.com/containernetworking/cni/pkg/invoke"
28 current "github.com/containernetworking/cni/pkg/types/100"
29 "github.com/containernetworking/cni/pkg/version"
30 "github.com/containernetworking/cni/plugins/test/noop/debug"
31 )
32
33 var _ = Describe("Delegate", func() {
34 var (
35 pluginName string
36 netConf []byte
37 debugFileName string
38 debugBehavior *debug.Debug
39 expectedResult *current.Result
40 ctx context.Context
41 )
42
43 BeforeEach(func() {
44 netConf, _ = json.Marshal(map[string]string{
45 "name": "delegate-test",
46 "cniVersion": version.Current(),
47 })
48
49 expectedResult = &current.Result{
50 CNIVersion: current.ImplementedSpecVersion,
51 IPs: []*current.IPConfig{
52 {
53 Address: net.IPNet{
54 IP: net.ParseIP("10.1.2.3"),
55 Mask: net.CIDRMask(24, 32),
56 },
57 },
58 },
59 }
60 expectedResultBytes, _ := json.Marshal(expectedResult)
61
62 debugFile, err := os.CreateTemp("", "cni_debug")
63 Expect(err).NotTo(HaveOccurred())
64 Expect(debugFile.Close()).To(Succeed())
65 debugFileName = debugFile.Name()
66 debugBehavior = &debug.Debug{
67 ReportResult: string(expectedResultBytes),
68 }
69 Expect(debugBehavior.WriteDebug(debugFileName)).To(Succeed())
70 pluginName = "noop"
71 ctx = context.TODO()
72 os.Setenv("CNI_ARGS", "DEBUG="+debugFileName)
73 os.Setenv("CNI_PATH", filepath.Dir(pathToPlugin))
74 os.Setenv("CNI_NETNS", "/tmp/some/netns/path")
75 os.Setenv("CNI_IFNAME", "eth7")
76 os.Setenv("CNI_CONTAINERID", "container")
77 })
78
79 AfterEach(func() {
80 os.RemoveAll(debugFileName)
81
82 for _, k := range []string{"CNI_COMMAND", "CNI_ARGS", "CNI_PATH", "CNI_NETNS", "CNI_IFNAME"} {
83 os.Unsetenv(k)
84 }
85 })
86
87 Describe("DelegateAdd", func() {
88 BeforeEach(func() {
89 os.Setenv("CNI_COMMAND", "ADD")
90 })
91
92 It("finds and execs the named plugin", func() {
93 result, err := invoke.DelegateAdd(ctx, pluginName, netConf, nil)
94 Expect(err).NotTo(HaveOccurred())
95 Expect(result).To(Equal(expectedResult))
96
97 pluginInvocation, err := debug.ReadDebug(debugFileName)
98 Expect(err).NotTo(HaveOccurred())
99 Expect(pluginInvocation.Command).To(Equal("ADD"))
100 Expect(pluginInvocation.CmdArgs.IfName).To(Equal("eth7"))
101 })
102
103 Context("if the ADD delegation runs on an existing non-ADD command, ", func() {
104 BeforeEach(func() {
105 os.Setenv("CNI_COMMAND", "NOPE")
106 })
107
108 It("aborts and returns a useful error", func() {
109 result, err := invoke.DelegateAdd(ctx, pluginName, netConf, nil)
110 Expect(err).NotTo(HaveOccurred())
111 Expect(result).To(Equal(expectedResult))
112
113 pluginInvocation, err := debug.ReadDebug(debugFileName)
114 Expect(err).NotTo(HaveOccurred())
115 Expect(pluginInvocation.Command).To(Equal("ADD"))
116 Expect(pluginInvocation.CmdArgs.IfName).To(Equal("eth7"))
117
118 // check the original env
119 Expect(os.Getenv("CNI_COMMAND")).To(Equal("NOPE"))
120 })
121 })
122
123 Context("when the plugin cannot be found", func() {
124 BeforeEach(func() {
125 pluginName = "non-existent-plugin"
126 })
127
128 It("returns a useful error", func() {
129 _, err := invoke.DelegateAdd(ctx, pluginName, netConf, nil)
130 Expect(err).To(MatchError(HavePrefix("failed to find plugin")))
131 })
132 })
133 })
134
135 Describe("DelegateCheck", func() {
136 BeforeEach(func() {
137 os.Setenv("CNI_COMMAND", "CHECK")
138 })
139
140 It("finds and execs the named plugin", func() {
141 err := invoke.DelegateCheck(ctx, pluginName, netConf, nil)
142 Expect(err).NotTo(HaveOccurred())
143
144 pluginInvocation, err := debug.ReadDebug(debugFileName)
145 Expect(err).NotTo(HaveOccurred())
146 Expect(pluginInvocation.Command).To(Equal("CHECK"))
147 Expect(pluginInvocation.CmdArgs.IfName).To(Equal("eth7"))
148 })
149
150 Context("if the CHECK delegation runs on an existing non-CHECK command", func() {
151 BeforeEach(func() {
152 os.Setenv("CNI_COMMAND", "NOPE")
153 })
154
155 It("aborts and returns a useful error", func() {
156 err := invoke.DelegateCheck(ctx, pluginName, netConf, nil)
157 Expect(err).NotTo(HaveOccurred())
158
159 pluginInvocation, err := debug.ReadDebug(debugFileName)
160 Expect(err).NotTo(HaveOccurred())
161 Expect(pluginInvocation.Command).To(Equal("CHECK"))
162 Expect(pluginInvocation.CmdArgs.IfName).To(Equal("eth7"))
163
164 // check the original env
165 Expect(os.Getenv("CNI_COMMAND")).To(Equal("NOPE"))
166 })
167 })
168
169 Context("when the plugin cannot be found", func() {
170 BeforeEach(func() {
171 pluginName = "non-existent-plugin"
172 })
173
174 It("returns a useful error", func() {
175 err := invoke.DelegateCheck(ctx, pluginName, netConf, nil)
176 Expect(err).To(MatchError(HavePrefix("failed to find plugin")))
177 })
178 })
179 })
180
181 Describe("DelegateDel", func() {
182 BeforeEach(func() {
183 os.Setenv("CNI_COMMAND", "DEL")
184 })
185
186 It("finds and execs the named plugin", func() {
187 err := invoke.DelegateDel(ctx, pluginName, netConf, nil)
188 Expect(err).NotTo(HaveOccurred())
189
190 pluginInvocation, err := debug.ReadDebug(debugFileName)
191 Expect(err).NotTo(HaveOccurred())
192 Expect(pluginInvocation.Command).To(Equal("DEL"))
193 Expect(pluginInvocation.CmdArgs.IfName).To(Equal("eth7"))
194 })
195
196 Context("if the DEL delegation runs on an existing non-DEL command", func() {
197 BeforeEach(func() {
198 os.Setenv("CNI_COMMAND", "NOPE")
199 })
200
201 It("aborts and returns a useful error", func() {
202 err := invoke.DelegateDel(ctx, pluginName, netConf, nil)
203 Expect(err).NotTo(HaveOccurred())
204
205 pluginInvocation, err := debug.ReadDebug(debugFileName)
206 Expect(err).NotTo(HaveOccurred())
207 Expect(pluginInvocation.Command).To(Equal("DEL"))
208 Expect(pluginInvocation.CmdArgs.IfName).To(Equal("eth7"))
209
210 // check the original env
211 Expect(os.Getenv("CNI_COMMAND")).To(Equal("NOPE"))
212 })
213 })
214
215 Context("when the plugin cannot be found", func() {
216 BeforeEach(func() {
217 pluginName = "non-existent-plugin"
218 })
219
220 It("returns a useful error", func() {
221 err := invoke.DelegateDel(ctx, pluginName, netConf, nil)
222 Expect(err).To(MatchError(HavePrefix("failed to find plugin")))
223 })
224 })
225 })
226
227 Describe("DelegateStatus", func() {
228 BeforeEach(func() {
229 os.Setenv("CNI_COMMAND", "STATUS")
230 })
231
232 It("finds and execs the named plugin", func() {
233 err := invoke.DelegateStatus(ctx, pluginName, netConf, nil)
234 Expect(err).NotTo(HaveOccurred())
235
236 pluginInvocation, err := debug.ReadDebug(debugFileName)
237 Expect(err).NotTo(HaveOccurred())
238 Expect(pluginInvocation.Command).To(Equal("STATUS"))
239 })
240
241 Context("if the STATUS delegation runs on an existing non-STATUS command", func() {
242 BeforeEach(func() {
243 os.Setenv("CNI_COMMAND", "NOPE")
244 })
245
246 It("aborts and returns a useful error", func() {
247 err := invoke.DelegateStatus(ctx, pluginName, netConf, nil)
248 Expect(err).NotTo(HaveOccurred())
249
250 pluginInvocation, err := debug.ReadDebug(debugFileName)
251 Expect(err).NotTo(HaveOccurred())
252 Expect(pluginInvocation.Command).To(Equal("STATUS"))
253
254 // check the original env
255 Expect(os.Getenv("CNI_COMMAND")).To(Equal("NOPE"))
256 })
257 })
258
259 Context("when the plugin cannot be found", func() {
260 BeforeEach(func() {
261 pluginName = "non-existent-plugin"
262 })
263
264 It("returns a useful error", func() {
265 err := invoke.DelegateStatus(ctx, pluginName, netConf, nil)
266 Expect(err).To(MatchError(HavePrefix("failed to find plugin")))
267 })
268 })
269 })
270 })