Debian Codegraph @

pkg/invoke/raw_exec_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 invoke_test
16
17 import (
18 "bytes"
19 "context"
20 "os"
21
22 . "github.com/onsi/ginkgo/v2"
23 . "github.com/onsi/gomega"
24
25 "github.com/containernetworking/cni/pkg/invoke"
26 noop_debug "github.com/containernetworking/cni/plugins/test/noop/debug"
27 )
28
29 var _ = Describe("RawExec", func() {
30 var (
31 debugFileName string
32 debug *noop_debug.Debug
33 environ []string
34 stdin []byte
35 execer *invoke.RawExec
36 ctx context.Context
37 )
38
39 const reportResult = `{ "some": "result" }`
40
41 BeforeEach(func() {
42 debugFile, err := os.CreateTemp("", "cni_debug")
43 Expect(err).NotTo(HaveOccurred())
44 Expect(debugFile.Close()).To(Succeed())
45 debugFileName = debugFile.Name()
46
47 debug = &noop_debug.Debug{
48 ReportResult: reportResult,
49 ReportStderr: "some stderr message",
50 }
51 Expect(debug.WriteDebug(debugFileName)).To(Succeed())
52
53 environ = []string{
54 "CNI_COMMAND=ADD",
55 "CNI_CONTAINERID=some-container-id",
56 "CNI_ARGS=DEBUG=" + debugFileName,
57 "CNI_NETNS=/some/netns/path",
58 "CNI_PATH=/some/bin/path",
59 "CNI_IFNAME=some-eth0",
60 }
61 stdin = []byte(`{"name": "raw-exec-test", "some":"stdin-json", "cniVersion": "0.3.1"}`)
62 execer = &invoke.RawExec{}
63 ctx = context.TODO()
64 })
65
66 AfterEach(func() {
67 Expect(os.Remove(debugFileName)).To(Succeed())
68 })
69
70 It("runs the plugin with the given stdin and environment", func() {
71 _, err := execer.ExecPlugin(ctx, pathToPlugin, stdin, environ)
72 Expect(err).NotTo(HaveOccurred())
73
74 debug, err := noop_debug.ReadDebug(debugFileName)
75 Expect(err).NotTo(HaveOccurred())
76 Expect(debug.Command).To(Equal("ADD"))
77 Expect(debug.CmdArgs.StdinData).To(Equal(stdin))
78 Expect(debug.CmdArgs.Netns).To(Equal("/some/netns/path"))
79 })
80
81 It("returns the resulting stdout as bytes", func() {
82 resultBytes, err := execer.ExecPlugin(ctx, pathToPlugin, stdin, environ)
83 Expect(err).NotTo(HaveOccurred())
84
85 Expect(resultBytes).To(BeEquivalentTo(reportResult))
86 })
87
88 Context("when the Stderr writer is set", func() {
89 var stderrBuffer *bytes.Buffer
90
91 BeforeEach(func() {
92 stderrBuffer = &bytes.Buffer{}
93 execer.Stderr = stderrBuffer
94 })
95
96 It("forwards any stderr bytes to the Stderr writer", func() {
97 _, err := execer.ExecPlugin(ctx, pathToPlugin, stdin, environ)
98 Expect(err).NotTo(HaveOccurred())
99
100 Expect(stderrBuffer.String()).To(Equal("some stderr message"))
101 })
102 })
103
104 Context("when the plugin errors", func() {
105 BeforeEach(func() {
106 debug.ReportResult = ""
107 })
108
109 Context("and writes valid error JSON to stdout", func() {
110 It("wraps and returns the error", func() {
111 debug.ReportError = "banana"
112 Expect(debug.WriteDebug(debugFileName)).To(Succeed())
113 _, err := execer.ExecPlugin(ctx, pathToPlugin, stdin, environ)
114 Expect(err).To(HaveOccurred())
115 Expect(err).To(MatchError("banana"))
116 })
117 })
118
119 Context("and writes to stderr", func() {
120 It("returns an error message with stderr output", func() {
121 debug.ExitWithCode = 1
122 Expect(debug.WriteDebug(debugFileName)).To(Succeed())
123 _, err := execer.ExecPlugin(ctx, pathToPlugin, stdin, environ)
124 Expect(err).To(HaveOccurred())
125 Expect(err).To(MatchError(`netplugin failed: "some stderr message"`))
126 })
127 })
128 })
129
130 Context("when the plugin errors with no output on stdout or stderr", func() {
131 It("returns the exec error message", func() {
132 debug.ExitWithCode = 1
133 debug.ReportResult = ""
134 debug.ReportStderr = ""
135 Expect(debug.WriteDebug(debugFileName)).To(Succeed())
136 _, err := execer.ExecPlugin(ctx, pathToPlugin, stdin, environ)
137 Expect(err).To(HaveOccurred())
138 Expect(err).To(MatchError("netplugin failed with no error message: exit status 1"))
139 })
140 })
141
142 Context("when the system is unable to execute the plugin", func() {
143 It("returns the error", func() {
144 _, err := execer.ExecPlugin(ctx, "/tmp/some/invalid/plugin/path", stdin, environ)
145 Expect(err).To(HaveOccurred())
146 Expect(err).To(MatchError(ContainSubstring("/tmp/some/invalid/plugin/path")))
147 })
148 })
149 })