github.com/mccv1r0/cni@v0.7.0-alpha1/pkg/invoke/delegate_test.go (about) 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 "encoding/json" 19 "io/ioutil" 20 "net" 21 "os" 22 "path/filepath" 23 24 "github.com/containernetworking/cni/pkg/invoke" 25 "github.com/containernetworking/cni/pkg/types/current" 26 "github.com/containernetworking/cni/plugins/test/noop/debug" 27 28 . "github.com/onsi/ginkgo" 29 . "github.com/onsi/gomega" 30 ) 31 32 var _ = Describe("Delegate", func() { 33 var ( 34 pluginName string 35 netConf []byte 36 debugFileName string 37 debugBehavior *debug.Debug 38 expectedResult *current.Result 39 ) 40 41 BeforeEach(func() { 42 netConf, _ = json.Marshal(map[string]string{ 43 "name": "delegate-test", 44 "cniVersion": "0.4.0", 45 }) 46 47 expectedResult = ¤t.Result{ 48 CNIVersion: "0.4.0", 49 IPs: []*current.IPConfig{ 50 { 51 Version: "4", 52 Address: net.IPNet{ 53 IP: net.ParseIP("10.1.2.3"), 54 Mask: net.CIDRMask(24, 32), 55 }, 56 }, 57 }, 58 } 59 expectedResultBytes, _ := json.Marshal(expectedResult) 60 61 debugFile, err := ioutil.TempFile("", "cni_debug") 62 Expect(err).NotTo(HaveOccurred()) 63 Expect(debugFile.Close()).To(Succeed()) 64 debugFileName = debugFile.Name() 65 debugBehavior = &debug.Debug{ 66 ReportResult: string(expectedResultBytes), 67 } 68 Expect(debugBehavior.WriteDebug(debugFileName)).To(Succeed()) 69 pluginName = "noop" 70 71 os.Setenv("CNI_ARGS", "DEBUG="+debugFileName) 72 os.Setenv("CNI_PATH", filepath.Dir(pathToPlugin)) 73 os.Setenv("CNI_NETNS", "/tmp/some/netns/path") 74 os.Setenv("CNI_IFNAME", "eth7") 75 os.Setenv("CNI_CONTAINERID", "container") 76 }) 77 78 AfterEach(func() { 79 os.RemoveAll(debugFileName) 80 81 for _, k := range []string{"CNI_COMMAND", "CNI_ARGS", "CNI_PATH", "CNI_NETNS", "CNI_IFNAME"} { 82 os.Unsetenv(k) 83 } 84 }) 85 86 Describe("DelegateAdd", func() { 87 BeforeEach(func() { 88 os.Setenv("CNI_COMMAND", "ADD") 89 }) 90 91 It("finds and execs the named plugin", func() { 92 result, err := invoke.DelegateAdd(pluginName, netConf, nil) 93 Expect(err).NotTo(HaveOccurred()) 94 Expect(result).To(Equal(expectedResult)) 95 96 pluginInvocation, err := debug.ReadDebug(debugFileName) 97 Expect(err).NotTo(HaveOccurred()) 98 Expect(pluginInvocation.Command).To(Equal("ADD")) 99 Expect(pluginInvocation.CmdArgs.IfName).To(Equal("eth7")) 100 }) 101 102 Context("if the delegation isn't part of an existing ADD command", func() { 103 BeforeEach(func() { 104 os.Setenv("CNI_COMMAND", "NOPE") 105 }) 106 107 It("aborts and returns a useful error", func() { 108 _, err := invoke.DelegateAdd(pluginName, netConf, nil) 109 Expect(err).To(MatchError("CNI_COMMAND is not ADD")) 110 }) 111 }) 112 113 Context("when the plugin cannot be found", func() { 114 BeforeEach(func() { 115 pluginName = "non-existent-plugin" 116 }) 117 118 It("returns a useful error", func() { 119 _, err := invoke.DelegateAdd(pluginName, netConf, nil) 120 Expect(err).To(MatchError(HavePrefix("failed to find plugin"))) 121 }) 122 }) 123 }) 124 125 Describe("DelegateGet", func() { 126 BeforeEach(func() { 127 os.Setenv("CNI_COMMAND", "GET") 128 }) 129 130 It("finds and execs the named plugin", func() { 131 result, err := invoke.DelegateGet(pluginName, netConf, nil) 132 Expect(err).NotTo(HaveOccurred()) 133 Expect(result).To(Equal(expectedResult)) 134 135 pluginInvocation, err := debug.ReadDebug(debugFileName) 136 Expect(err).NotTo(HaveOccurred()) 137 Expect(pluginInvocation.Command).To(Equal("GET")) 138 Expect(pluginInvocation.CmdArgs.IfName).To(Equal("eth7")) 139 }) 140 141 Context("if the delegation isn't part of an existing GET command", func() { 142 BeforeEach(func() { 143 os.Setenv("CNI_COMMAND", "NOPE") 144 }) 145 146 It("aborts and returns a useful error", func() { 147 _, err := invoke.DelegateGet(pluginName, netConf, nil) 148 Expect(err).To(MatchError("CNI_COMMAND is not GET")) 149 }) 150 }) 151 152 Context("when the plugin cannot be found", func() { 153 BeforeEach(func() { 154 pluginName = "non-existent-plugin" 155 }) 156 157 It("returns a useful error", func() { 158 _, err := invoke.DelegateGet(pluginName, netConf, nil) 159 Expect(err).To(MatchError(HavePrefix("failed to find plugin"))) 160 }) 161 }) 162 }) 163 164 Describe("DelegateDel", func() { 165 BeforeEach(func() { 166 os.Setenv("CNI_COMMAND", "DEL") 167 }) 168 169 It("finds and execs the named plugin", func() { 170 err := invoke.DelegateDel(pluginName, netConf, nil) 171 Expect(err).NotTo(HaveOccurred()) 172 173 pluginInvocation, err := debug.ReadDebug(debugFileName) 174 Expect(err).NotTo(HaveOccurred()) 175 Expect(pluginInvocation.Command).To(Equal("DEL")) 176 Expect(pluginInvocation.CmdArgs.IfName).To(Equal("eth7")) 177 }) 178 179 Context("if the delegation isn't part of an existing DEL command", func() { 180 BeforeEach(func() { 181 os.Setenv("CNI_COMMAND", "NOPE") 182 }) 183 184 It("aborts and returns a useful error", func() { 185 err := invoke.DelegateDel(pluginName, netConf, nil) 186 Expect(err).To(MatchError("CNI_COMMAND is not DEL")) 187 }) 188 }) 189 190 Context("when the plugin cannot be found", func() { 191 BeforeEach(func() { 192 pluginName = "non-existent-plugin" 193 }) 194 195 It("returns a useful error", func() { 196 err := invoke.DelegateDel(pluginName, netConf, nil) 197 Expect(err).To(MatchError(HavePrefix("failed to find plugin"))) 198 }) 199 }) 200 }) 201 })