github.com/mccv1r0/cni@v0.7.0-alpha1/libcni/backwards_compatibility_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 libcni_test
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"os/exec"
    22  	"path/filepath"
    23  	"runtime"
    24  	"strings"
    25  
    26  	"github.com/containernetworking/cni/libcni"
    27  	"github.com/containernetworking/cni/pkg/version/legacy_examples"
    28  	. "github.com/onsi/ginkgo"
    29  	. "github.com/onsi/gomega"
    30  	"github.com/onsi/gomega/gexec"
    31  )
    32  
    33  var _ = Describe("Backwards compatibility", func() {
    34  	var cacheDirPath string
    35  
    36  	BeforeEach(func() {
    37  		var err error
    38  		cacheDirPath, err = ioutil.TempDir("", "cni_cachedir")
    39  		Expect(err).NotTo(HaveOccurred())
    40  	})
    41  
    42  	AfterEach(func() {
    43  		Expect(os.RemoveAll(cacheDirPath)).To(Succeed())
    44  	})
    45  
    46  	It("correctly handles the response from a legacy plugin", func() {
    47  		example := legacy_examples.V010
    48  		pluginPath, err := example.Build()
    49  		Expect(err).NotTo(HaveOccurred())
    50  
    51  		netConf, err := libcni.ConfFromBytes([]byte(fmt.Sprintf(
    52  			`{ "name": "old-thing", "type": "%s" }`, example.Name)))
    53  		Expect(err).NotTo(HaveOccurred())
    54  
    55  		runtimeConf := &libcni.RuntimeConf{
    56  			ContainerID: "some-container-id",
    57  			NetNS:       "/some/netns/path",
    58  			IfName:      "eth0",
    59  			CacheDir:    cacheDirPath,
    60  		}
    61  
    62  		cniConfig := libcni.NewCNIConfig([]string{filepath.Dir(pluginPath)}, nil)
    63  
    64  		result, err := cniConfig.AddNetwork(netConf, runtimeConf)
    65  		Expect(err).NotTo(HaveOccurred())
    66  
    67  		Expect(result).To(Equal(legacy_examples.ExpectedResult))
    68  
    69  		Expect(os.RemoveAll(pluginPath)).To(Succeed())
    70  	})
    71  
    72  	It("correctly handles the request from a runtime with an older libcni", func() {
    73  		if runtime.GOOS == "windows" {
    74  			Skip("cannot build old runtime on windows")
    75  		}
    76  		example := legacy_examples.V010_Runtime
    77  
    78  		binPath, err := example.Build()
    79  		Expect(err).NotTo(HaveOccurred())
    80  
    81  		for _, configName := range example.NetConfs {
    82  			conf, err := example.GenerateNetConf(configName)
    83  			if err != nil {
    84  				Fail("Failed to generate config name " + configName + ": " + err.Error())
    85  			}
    86  			defer conf.Cleanup()
    87  
    88  			cmd := exec.Command(binPath, pluginDirs...)
    89  			cmd.Stdin = strings.NewReader(conf.Config)
    90  
    91  			session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
    92  			Expect(err).NotTo(HaveOccurred())
    93  
    94  			Eventually(session).Should(gexec.Exit(0))
    95  		}
    96  	})
    97  })