File: backwards_compatibility_test.go

package info (click to toggle)
golang-github-appc-cni 0.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 804 kB
  • sloc: sh: 177; makefile: 6
file content (83 lines) | stat: -rw-r--r-- 2,413 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2016 CNI authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package libcni_test

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/containernetworking/cni/libcni"
	"github.com/containernetworking/cni/pkg/version/legacy_examples"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
)

var _ = Describe("Backwards compatibility", func() {
	It("correctly handles the response from a legacy plugin", func() {
		example := legacy_examples.V010
		pluginPath, err := example.Build()
		Expect(err).NotTo(HaveOccurred())

		netConf, err := libcni.ConfFromBytes([]byte(fmt.Sprintf(
			`{ "name": "old-thing", "type": "%s" }`, example.Name)))
		Expect(err).NotTo(HaveOccurred())

		runtimeConf := &libcni.RuntimeConf{
			ContainerID: "some-container-id",
			NetNS:       "/some/netns/path",
			IfName:      "eth0",
		}

		cniConfig := &libcni.CNIConfig{Path: []string{filepath.Dir(pluginPath)}}

		result, err := cniConfig.AddNetwork(netConf, runtimeConf)
		Expect(err).NotTo(HaveOccurred())

		Expect(result).To(Equal(legacy_examples.ExpectedResult))

		Expect(os.RemoveAll(pluginPath)).To(Succeed())
	})

	It("correctly handles the request from a runtime with an older libcni", func() {
		// We need to be root (or have CAP_SYS_ADMIN...)
		if os.Geteuid() != 0 {
			Fail("must be run as root")
		}

		example := legacy_examples.V010_Runtime

		binPath, err := example.Build()
		Expect(err).NotTo(HaveOccurred())

		for _, configName := range example.NetConfs {
			configStr, ok := legacy_examples.NetConfs[configName]
			if !ok {
				Fail("Invalid config name " + configName)
			}

			cmd := exec.Command(binPath, pluginDirs...)
			cmd.Stdin = strings.NewReader(configStr)

			session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
			Expect(err).NotTo(HaveOccurred())

			Eventually(session).Should(gexec.Exit(0))
		}
	})
})