github.com/baraj55/containernetworking-cni@v0.7.2-0.20200219164625-56ace59a9e7f/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  	"context"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"os"
    22  	"os/exec"
    23  	"path/filepath"
    24  	"runtime"
    25  	"strings"
    26  
    27  	"github.com/containernetworking/cni/libcni"
    28  	"github.com/containernetworking/cni/pkg/version/legacy_examples"
    29  	. "github.com/onsi/ginkgo"
    30  	. "github.com/onsi/gomega"
    31  	"github.com/onsi/gomega/gexec"
    32  )
    33  
    34  var _ = Describe("Backwards compatibility", func() {
    35  	var cacheDirPath string
    36  
    37  	BeforeEach(func() {
    38  		var err error
    39  		cacheDirPath, err = ioutil.TempDir("", "cni_cachedir")
    40  		Expect(err).NotTo(HaveOccurred())
    41  	})
    42  
    43  	AfterEach(func() {
    44  		Expect(os.RemoveAll(cacheDirPath)).To(Succeed())
    45  	})
    46  
    47  	It("correctly handles the response from a legacy plugin", func() {
    48  		example := legacy_examples.V010
    49  		pluginPath, err := example.Build()
    50  		Expect(err).NotTo(HaveOccurred())
    51  
    52  		netConf, err := libcni.ConfFromBytes([]byte(fmt.Sprintf(
    53  			`{ "name": "old-thing", "type": "%s" }`, example.Name)))
    54  		Expect(err).NotTo(HaveOccurred())
    55  
    56  		runtimeConf := &libcni.RuntimeConf{
    57  			ContainerID: "some-container-id",
    58  			NetNS:       "/some/netns/path",
    59  			IfName:      "eth0",
    60  		}
    61  
    62  		cniConfig := libcni.NewCNIConfigWithCacheDir([]string{filepath.Dir(pluginPath)}, cacheDirPath, nil)
    63  
    64  		result, err := cniConfig.AddNetwork(context.TODO(), 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  })