github.com/jk-he/cni@v0.8.1/pkg/invoke/args_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  	"os"
    19  
    20  	"github.com/containernetworking/cni/pkg/invoke"
    21  
    22  	. "github.com/onsi/ginkgo"
    23  	. "github.com/onsi/gomega"
    24  )
    25  
    26  var _ = Describe("CNIArgs AsEnv", func() {
    27  	Describe("Args AsEnv", func() {
    28  		BeforeEach(func() {
    29  			os.Setenv("CNI_COMMAND", "DEL")
    30  			os.Setenv("CNI_IFNAME", "eth0")
    31  			os.Setenv("CNI_CONTAINERID", "id")
    32  			os.Setenv("CNI_ARGS", "args")
    33  			os.Setenv("CNI_NETNS", "testns")
    34  			os.Setenv("CNI_PATH", "testpath")
    35  		})
    36  
    37  		It("places the CNI environment variables in the end to be prepended", func() {
    38  			args := invoke.Args{
    39  				Command:     "ADD",
    40  				ContainerID: "some-container-id",
    41  				NetNS:       "/some/netns/path",
    42  				PluginArgs: [][2]string{
    43  					{"KEY1", "VALUE1"},
    44  					{"KEY2", "VALUE2"},
    45  				},
    46  				IfName: "eth7",
    47  				Path:   "/some/cni/path",
    48  			}
    49  
    50  			latentEnvs := os.Environ()
    51  			numLatentEnvs := len(latentEnvs)
    52  
    53  			cniEnvs := args.AsEnv()
    54  			Expect(len(cniEnvs)).To(Equal(numLatentEnvs))
    55  
    56  			Expect(inStringSlice("CNI_COMMAND=ADD", cniEnvs)).To(Equal(true))
    57  			Expect(inStringSlice("CNI_IFNAME=eth7", cniEnvs)).To(Equal(true))
    58  			Expect(inStringSlice("CNI_CONTAINERID=some-container-id", cniEnvs)).To(Equal(true))
    59  			Expect(inStringSlice("CNI_NETNS=/some/netns/path", cniEnvs)).To(Equal(true))
    60  			Expect(inStringSlice("CNI_ARGS=KEY1=VALUE1;KEY2=VALUE2", cniEnvs)).To(Equal(true))
    61  			Expect(inStringSlice("CNI_PATH=/some/cni/path", cniEnvs)).To(Equal(true))
    62  
    63  			Expect(inStringSlice("CNI_COMMAND=DEL", cniEnvs)).To(Equal(false))
    64  			Expect(inStringSlice("CNI_IFNAME=eth0", cniEnvs)).To(Equal(false))
    65  			Expect(inStringSlice("CNI_CONTAINERID=id", cniEnvs)).To(Equal(false))
    66  			Expect(inStringSlice("CNI_NETNS=testns", cniEnvs)).To(Equal(false))
    67  			Expect(inStringSlice("CNI_ARGS=args", cniEnvs)).To(Equal(false))
    68  			Expect(inStringSlice("CNI_PATH=testpath", cniEnvs)).To(Equal(false))
    69  		})
    70  
    71  		AfterEach(func() {
    72  			os.Unsetenv("CNI_COMMAND")
    73  			os.Unsetenv("CNI_IFNAME")
    74  			os.Unsetenv("CNI_CONTAINERID")
    75  			os.Unsetenv("CNI_ARGS")
    76  			os.Unsetenv("CNI_NETNS")
    77  			os.Unsetenv("CNI_PATH")
    78  		})
    79  	})
    80  
    81  	Describe("DelegateArgs AsEnv", func() {
    82  		BeforeEach(func() {
    83  			os.Unsetenv("CNI_COMMAND")
    84  		})
    85  
    86  		It("override CNI_COMMAND if it already exists in environment variables", func() {
    87  			os.Setenv("CNI_COMMAND", "DEL")
    88  
    89  			delegateArgs := invoke.DelegateArgs{
    90  				Command: "ADD",
    91  			}
    92  
    93  			latentEnvs := os.Environ()
    94  			numLatentEnvs := len(latentEnvs)
    95  
    96  			cniEnvs := delegateArgs.AsEnv()
    97  			Expect(len(cniEnvs)).To(Equal(numLatentEnvs))
    98  
    99  			Expect(inStringSlice("CNI_COMMAND=ADD", cniEnvs)).To(Equal(true))
   100  			Expect(inStringSlice("CNI_COMMAND=DEL", cniEnvs)).To(Equal(false))
   101  		})
   102  
   103  		It("append CNI_COMMAND if it does not exist in environment variables", func() {
   104  			delegateArgs := invoke.DelegateArgs{
   105  				Command: "ADD",
   106  			}
   107  
   108  			latentEnvs := os.Environ()
   109  			numLatentEnvs := len(latentEnvs)
   110  
   111  			cniEnvs := delegateArgs.AsEnv()
   112  			Expect(len(cniEnvs)).To(Equal(numLatentEnvs + 1))
   113  
   114  			Expect(inStringSlice("CNI_COMMAND=ADD", cniEnvs)).To(Equal(true))
   115  		})
   116  
   117  		AfterEach(func() {
   118  			os.Unsetenv("CNI_COMMAND")
   119  		})
   120  	})
   121  
   122  	Describe("inherited AsEnv", func() {
   123  		It("return nil string slice if we call AsEnv of inherited", func() {
   124  			inheritedArgs := invoke.ArgsFromEnv()
   125  
   126  			var nilSlice []string = nil
   127  			Expect(inheritedArgs.AsEnv()).To(Equal(nilSlice))
   128  		})
   129  	})
   130  })
   131  
   132  func inStringSlice(in string, slice []string) bool {
   133  	for _, s := range slice {
   134  		if in == s {
   135  			return true
   136  		}
   137  	}
   138  	return false
   139  }