File: noop_test.go

package info (click to toggle)
golang-github-appc-cni 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,132 kB
  • sloc: sh: 154; makefile: 7
file content (306 lines) | stat: -rw-r--r-- 10,142 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2016 CNI authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main_test

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"

	"github.com/containernetworking/cni/pkg/skel"
	"github.com/containernetworking/cni/pkg/types"
	"github.com/containernetworking/cni/pkg/version"
	noop_debug "github.com/containernetworking/cni/plugins/test/noop/debug"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
)

var _ = Describe("No-op plugin", func() {
	var (
		cmd             *exec.Cmd
		debugFileName   string
		debug           *noop_debug.Debug
		expectedCmdArgs skel.CmdArgs
	)

	const reportResult = `{ "ips": [{ "version": "4", "address": "10.1.2.3/24" }], "dns": {} }`

	BeforeEach(func() {
		debug = &noop_debug.Debug{
			ReportResult:         reportResult,
			ReportVersionSupport: []string{"0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0"},
		}

		debugFile, err := ioutil.TempFile("", "cni_debug")
		Expect(err).NotTo(HaveOccurred())
		Expect(debugFile.Close()).To(Succeed())
		debugFileName = debugFile.Name()

		Expect(debug.WriteDebug(debugFileName)).To(Succeed())

		cmd = exec.Command(pathToPlugin)

		args := fmt.Sprintf("DEBUG=%s;FOO=BAR", debugFileName)
		cmd.Env = []string{
			"CNI_COMMAND=ADD",
			"CNI_CONTAINERID=some-container-id",
			"CNI_NETNS=/some/netns/path",
			"CNI_IFNAME=some-eth0",
			"CNI_PATH=/some/bin/path",
			// Keep this last
			"CNI_ARGS=" + args,
		}
		stdinData := `{"name": "noop-test", "some":"stdin-json", "cniVersion": "0.3.1"}`
		cmd.Stdin = strings.NewReader(stdinData)
		expectedCmdArgs = skel.CmdArgs{
			ContainerID: "some-container-id",
			Netns:       "/some/netns/path",
			IfName:      "some-eth0",
			Args:        args,
			Path:        "/some/bin/path",
			StdinData:   []byte(stdinData),
		}
	})

	AfterEach(func() {
		os.Remove(debugFileName)
	})

	It("responds to ADD using the ReportResult debug field", func() {
		session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
		Expect(err).NotTo(HaveOccurred())
		Eventually(session).Should(gexec.Exit(0))
		Expect(session.Out.Contents()).To(MatchJSON(reportResult))
	})

	It("panics when no debug file is given", func() {
		// Remove the DEBUG option from CNI_ARGS and regular args
		cmd.Env[len(cmd.Env)-1] = "CNI_ARGS=FOO=BAR"
		expectedCmdArgs.Args = "FOO=BAR"

		session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
		Expect(err).NotTo(HaveOccurred())
		Eventually(session).Should(gexec.Exit(2))
	})

	It("pass previous result 0.3.1 through when ReportResult is PASSTHROUGH", func() {
		debug = &noop_debug.Debug{ReportResult: "PASSTHROUGH"}
		Expect(debug.WriteDebug(debugFileName)).To(Succeed())

		cmd.Stdin = strings.NewReader(`{
	"name":"noop-test",
	"some":"stdin-json",
	"cniVersion": "0.3.1",
	"prevResult": {
		"cniVersion": "0.3.1",
		"ips": [{"version": "4", "address": "10.1.2.15/24"}]
	}
}`)
		session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
		Expect(err).NotTo(HaveOccurred())
		Eventually(session).Should(gexec.Exit(0))
		Expect(session.Out.Contents()).To(MatchJSON(`{"cniVersion": "0.3.1", "ips": [{"version": "4", "address": "10.1.2.15/24"}], "dns": {}}`))
	})

	It("pass previous result 0.4.0 through when ReportResult is PASSTHROUGH", func() {
		debug = &noop_debug.Debug{ReportResult: "PASSTHROUGH"}
		Expect(debug.WriteDebug(debugFileName)).To(Succeed())

		cmd.Stdin = strings.NewReader(`{
	"name":"noop-test",
	"some":"stdin-json",
	"cniVersion": "0.4.0",
	"prevResult": {
		"cniVersion": "0.4.0",
		"ips": [{"version": "4", "address": "10.1.2.15/24"}]
	}
}`)
		session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
		Expect(err).NotTo(HaveOccurred())
		Eventually(session).Should(gexec.Exit(0))
		Expect(session.Out.Contents()).To(MatchJSON(`{"cniVersion": "0.4.0", "ips": [{"version": "4", "address": "10.1.2.15/24"}], "dns": {}}`))
	})

	It("injects DNS into previous result when ReportResult is INJECT-DNS", func() {
		debug = &noop_debug.Debug{ReportResult: "INJECT-DNS"}
		Expect(debug.WriteDebug(debugFileName)).To(Succeed())

		cmd.Stdin = strings.NewReader(`{
	"name":"noop-test",
	"some":"stdin-json",
	"cniVersion": "0.4.0",
	"prevResult": {
		"cniVersion": "0.4.0",
		"ips": [{"version": "4", "address": "10.1.2.3/24"}],
		"dns": {}
	}
}`)

		session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
		Expect(err).NotTo(HaveOccurred())
		Eventually(session).Should(gexec.Exit(0))
		Expect(session.Out.Contents()).To(MatchJSON(`{
	"cniVersion": "0.4.0",
	"ips": [{"version": "4", "address": "10.1.2.3/24"}],
	"dns": {"nameservers": ["1.2.3.4"]}
}`))
	})

	It("allows passing debug file in config JSON", func() {
		// Remove the DEBUG option from CNI_ARGS and regular args
		newArgs := "FOO=BAR"
		cmd.Env[len(cmd.Env)-1] = "CNI_ARGS=" + newArgs
		newStdin := fmt.Sprintf(`{"name":"noop-test", "some": "stdin-json", "cniVersion": "0.4.0", "debugFile": %q}`, debugFileName)
		cmd.Stdin = strings.NewReader(newStdin)
		expectedCmdArgs.Args = newArgs
		expectedCmdArgs.StdinData = []byte(newStdin)

		session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
		Expect(err).NotTo(HaveOccurred())
		Eventually(session).Should(gexec.Exit(0))
		Expect(session.Out.Contents()).To(MatchJSON(reportResult))

		debug, err := noop_debug.ReadDebug(debugFileName)
		Expect(err).NotTo(HaveOccurred())
		Expect(debug.Command).To(Equal("ADD"))
		Expect(debug.CmdArgs).To(Equal(expectedCmdArgs))
	})

	It("records all the args provided by skel.PluginMain", func() {
		session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
		Expect(err).NotTo(HaveOccurred())
		Eventually(session).Should(gexec.Exit(0))

		debug, err := noop_debug.ReadDebug(debugFileName)
		Expect(err).NotTo(HaveOccurred())
		Expect(debug.Command).To(Equal("ADD"))
		Expect(debug.CmdArgs).To(Equal(expectedCmdArgs))
	})

	Context("when the ReportResult debug field is empty", func() {
		BeforeEach(func() {
			debug.ReportResult = ""
			Expect(debug.WriteDebug(debugFileName)).To(Succeed())
		})

		It("returns no result", func() {
			session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
			Expect(err).NotTo(HaveOccurred())
			Eventually(session).Should(gexec.Exit(0))
			Expect(session.Out.Contents()).To(Equal([]byte{}))

			debug, err := noop_debug.ReadDebug(debugFileName)
			Expect(err).NotTo(HaveOccurred())
			Expect(debug.ReportResult).To(Equal(""))
		})
	})

	Context("when the ExitWithCode debug field is set", func() {
		BeforeEach(func() {
			debug.ReportResult = ""
			debug.ExitWithCode = 3
			Expect(debug.WriteDebug(debugFileName)).To(Succeed())
		})

		It("returns no result and exits with the expected code", func() {
			session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
			Expect(err).NotTo(HaveOccurred())
			Eventually(session).Should(gexec.Exit(3))
			Expect(session.Out.Contents()).To(Equal([]byte{}))

			debug, err := noop_debug.ReadDebug(debugFileName)
			Expect(err).NotTo(HaveOccurred())
			Expect(debug.ReportResult).To(Equal(""))
		})
	})

	Context("when the ReportResult debug field is set", func() {
		var expectedResultString = fmt.Sprintf(` { "result": %q }`, noop_debug.EmptyReportResultMessage)

		BeforeEach(func() {
			debug.ReportResult = expectedResultString
			Expect(debug.WriteDebug(debugFileName)).To(Succeed())
		})

		It("substitutes a helpful message for the test author", func() {
			session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
			Expect(err).NotTo(HaveOccurred())
			Eventually(session).Should(gexec.Exit(0))
			Expect(session.Out.Contents()).To(MatchJSON(expectedResultString))

			debug, err := noop_debug.ReadDebug(debugFileName)
			Expect(err).NotTo(HaveOccurred())
			Expect(debug.ReportResult).To(MatchJSON(expectedResultString))
		})
	})

	Context("when the ReportError debug field is set", func() {
		BeforeEach(func() {
			debug.ReportError = "banana"
			Expect(debug.WriteDebug(debugFileName)).To(Succeed())
		})

		It("returns an error to skel.PluginMain, causing the process to exit code 1", func() {
			session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
			Expect(err).NotTo(HaveOccurred())
			Eventually(session).Should(gexec.Exit(1))
			Expect(session.Out.Contents()).To(MatchJSON(fmt.Sprintf(`{ "code": %d, "msg": "banana" }`, types.ErrInternal)))
		})
	})

	Context("when the CNI_COMMAND is DEL", func() {
		BeforeEach(func() {
			cmd.Env[0] = "CNI_COMMAND=DEL"
			debug.ReportResult = `{ "some": "delete-data" }`
			Expect(debug.WriteDebug(debugFileName)).To(Succeed())
		})

		It("still does all the debug behavior", func() {
			session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
			Expect(err).NotTo(HaveOccurred())
			Eventually(session).Should(gexec.Exit(0))
			Expect(session.Out.Contents()).To(MatchJSON(`{
				"some": "delete-data"
      }`))
			debug, err := noop_debug.ReadDebug(debugFileName)
			Expect(err).NotTo(HaveOccurred())
			Expect(debug.Command).To(Equal("DEL"))
			Expect(debug.CmdArgs).To(Equal(expectedCmdArgs))
		})
	})

	Context("when the CNI_COMMAND is VERSION", func() {
		BeforeEach(func() {
			cmd.Env[0] = "CNI_COMMAND=VERSION"
			debug.ReportVersionSupport = []string{"0.123.0", "0.2.0"}

			Expect(debug.WriteDebug(debugFileName)).To(Succeed())
		})

		It("claims to support the specified versions", func() {
			session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
			Expect(err).NotTo(HaveOccurred())
			Eventually(session).Should(gexec.Exit(0))
			decoder := &version.PluginDecoder{}
			pluginInfo, err := decoder.Decode(session.Out.Contents())
			Expect(err).NotTo(HaveOccurred())
			Expect(pluginInfo.SupportedVersions()).To(ConsistOf(
				"0.123.0", "0.2.0"))
		})
	})
})