Debian Codegraph @

libcni/backwards_compatibility_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 "os"
21 "os/exec"
22 "path/filepath"
23 "runtime"
24 "strings"
25
26 . "github.com/onsi/ginkgo/v2"
27 . "github.com/onsi/gomega"
28 "github.com/onsi/gomega/gexec"
29
30 "github.com/containernetworking/cni/libcni"
31 "github.com/containernetworking/cni/pkg/version/legacy_examples"
32 )
33
34 var _ = Describe("Backwards compatibility", func() {
35 var cacheDirPath string
36
37 BeforeEach(func() {
38 var err error
39 cacheDirPath, err = os.MkdirTemp("", "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 err = cniConfig.DelNetwork(context.TODO(), netConf, runtimeConf)
70 Expect(err).NotTo(HaveOccurred())
71
72 Expect(os.RemoveAll(pluginPath)).To(Succeed())
73 })
74
75 It("correctly handles the request from a runtime with an older libcni", func() {
76 if runtime.GOOS == "windows" {
77 Skip("cannot build old runtime on windows")
78 }
79 example := legacy_examples.V010_Runtime
80
81 binPath, err := example.Build()
82 Expect(err).NotTo(HaveOccurred())
83
84 for _, configName := range example.NetConfs {
85 conf, err := example.GenerateNetConf(configName)
86 if err != nil {
87 Fail("Failed to generate config name " + configName + ": " + err.Error())
88 }
89 defer conf.Cleanup()
90
91 cmd := exec.Command(binPath, pluginDirs...)
92 cmd.Stdin = strings.NewReader(conf.Config)
93
94 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
95 Expect(err).NotTo(HaveOccurred())
96
97 Eventually(session).Should(gexec.Exit(0))
98 }
99 })
100 })