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