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