github.com/mccv1r0/cni@v0.7.0-alpha1/pkg/invoke/raw_exec_test.go (about) 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 "io/ioutil" 20 "os" 21 22 "github.com/containernetworking/cni/pkg/invoke" 23 24 noop_debug "github.com/containernetworking/cni/plugins/test/noop/debug" 25 26 . "github.com/onsi/ginkgo" 27 . "github.com/onsi/gomega" 28 ) 29 30 var _ = Describe("RawExec", func() { 31 var ( 32 debugFileName string 33 debug *noop_debug.Debug 34 environ []string 35 stdin []byte 36 execer *invoke.RawExec 37 ) 38 39 const reportResult = `{ "some": "result" }` 40 41 BeforeEach(func() { 42 debugFile, err := ioutil.TempFile("", "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 }) 64 65 AfterEach(func() { 66 Expect(os.Remove(debugFileName)).To(Succeed()) 67 }) 68 69 It("runs the plugin with the given stdin and environment", func() { 70 _, err := execer.ExecPlugin(pathToPlugin, stdin, environ) 71 Expect(err).NotTo(HaveOccurred()) 72 73 debug, err := noop_debug.ReadDebug(debugFileName) 74 Expect(err).NotTo(HaveOccurred()) 75 Expect(debug.Command).To(Equal("ADD")) 76 Expect(debug.CmdArgs.StdinData).To(Equal(stdin)) 77 Expect(debug.CmdArgs.Netns).To(Equal("/some/netns/path")) 78 }) 79 80 It("returns the resulting stdout as bytes", func() { 81 resultBytes, err := execer.ExecPlugin(pathToPlugin, stdin, environ) 82 Expect(err).NotTo(HaveOccurred()) 83 84 Expect(resultBytes).To(BeEquivalentTo(reportResult)) 85 }) 86 87 Context("when the Stderr writer is set", func() { 88 var stderrBuffer *bytes.Buffer 89 90 BeforeEach(func() { 91 stderrBuffer = &bytes.Buffer{} 92 execer.Stderr = stderrBuffer 93 }) 94 95 It("forwards any stderr bytes to the Stderr writer", func() { 96 _, err := execer.ExecPlugin(pathToPlugin, stdin, environ) 97 Expect(err).NotTo(HaveOccurred()) 98 99 Expect(stderrBuffer.String()).To(Equal("some stderr message")) 100 }) 101 }) 102 103 Context("when the plugin errors", func() { 104 BeforeEach(func() { 105 debug.ReportError = "banana" 106 Expect(debug.WriteDebug(debugFileName)).To(Succeed()) 107 }) 108 109 It("wraps and returns the error", func() { 110 _, err := execer.ExecPlugin(pathToPlugin, stdin, environ) 111 Expect(err).To(HaveOccurred()) 112 Expect(err).To(MatchError("banana")) 113 }) 114 }) 115 116 Context("when the system is unable to execute the plugin", func() { 117 It("returns the error", func() { 118 _, err := execer.ExecPlugin("/tmp/some/invalid/plugin/path", stdin, environ) 119 Expect(err).To(HaveOccurred()) 120 Expect(err).To(MatchError(ContainSubstring("/tmp/some/invalid/plugin/path"))) 121 }) 122 }) 123 })