File: backwards_compatibility_test.go

package info (click to toggle)
golang-github-appc-cni 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,132 kB
  • sloc: sh: 154; makefile: 7
file content (102 lines) | stat: -rw-r--r-- 2,882 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// +build ignore

// 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 (
	"context"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"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() {
	var cacheDirPath string

	BeforeEach(func() {
		var err error
		cacheDirPath, err = ioutil.TempDir("", "cni_cachedir")
		Expect(err).NotTo(HaveOccurred())
	})

	AfterEach(func() {
		Expect(os.RemoveAll(cacheDirPath)).To(Succeed())
	})

	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.NewCNIConfigWithCacheDir([]string{filepath.Dir(pluginPath)}, cacheDirPath, nil)

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

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

		err = cniConfig.DelNetwork(context.TODO(), netConf, runtimeConf)
		Expect(err).NotTo(HaveOccurred())

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

	It("correctly handles the request from a runtime with an older libcni", func() {
		if runtime.GOOS == "windows" {
			Skip("cannot build old runtime on windows")
		}
		example := legacy_examples.V010_Runtime

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

		for _, configName := range example.NetConfs {
			conf, err := example.GenerateNetConf(configName)
			if err != nil {
				Fail("Failed to generate config name " + configName + ": " + err.Error())
			}
			defer conf.Cleanup()

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

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

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