| •1 |
// +build ignore |
| 2 |
|
| 3 |
// Copyright 2016 CNI authors |
| 4 |
// |
| 5 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
// you may not use this file except in compliance with the License. |
| 7 |
// You may obtain a copy of the License at |
| 8 |
// |
| 9 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
// |
| 11 |
// Unless required by applicable law or agreed to in writing, software |
| 12 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
// See the License for the specific language governing permissions and |
| 15 |
// limitations under the License. |
| 16 |
|
| 17 |
package libcni_test |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"os" |
| 23 |
"os/exec" |
| 24 |
"path/filepath" |
| 25 |
"runtime" |
| 26 |
"strings" |
| 27 |
|
| 28 |
. "github.com/onsi/ginkgo/v2" |
| 29 |
. "github.com/onsi/gomega" |
| 30 |
"github.com/onsi/gomega/gexec" |
| 31 |
|
| 32 |
"github.com/containernetworking/cni/libcni" |
| 33 |
"github.com/containernetworking/cni/pkg/version/legacy_examples" |
| 34 |
) |
| 35 |
|
| 36 |
var _ = Describe("Backwards compatibility", func() { |
| 37 |
var cacheDirPath string |
| 38 |
|
| 39 |
BeforeEach(func() { |
| 40 |
var err error |
| 41 |
cacheDirPath, err = os.MkdirTemp("", "cni_cachedir") |
| 42 |
Expect(err).NotTo(HaveOccurred()) |
| 43 |
}) |
| 44 |
|
| 45 |
AfterEach(func() { |
| 46 |
Expect(os.RemoveAll(cacheDirPath)).To(Succeed()) |
| 47 |
}) |
| 48 |
|
| 49 |
It("correctly handles the response from a legacy plugin", func() { |
| 50 |
example := legacy_examples.V010 |
| 51 |
pluginPath, err := example.Build() |
| 52 |
Expect(err).NotTo(HaveOccurred()) |
| 53 |
|
| 54 |
netConf, err := libcni.ConfFromBytes([]byte(fmt.Sprintf( |
| 55 |
`{ "name": "old-thing", "type": "%s" }`, example.Name))) |
| 56 |
Expect(err).NotTo(HaveOccurred()) |
| 57 |
|
| 58 |
runtimeConf := &libcni.RuntimeConf{ |
| 59 |
ContainerID: "some-container-id", |
| 60 |
NetNS: "/some/netns/path", |
| 61 |
IfName: "eth0", |
| 62 |
} |
| 63 |
|
| 64 |
cniConfig := libcni.NewCNIConfigWithCacheDir([]string{filepath.Dir(pluginPath)}, cacheDirPath, nil) |
| 65 |
|
| 66 |
result, err := cniConfig.AddNetwork(context.TODO(), netConf, runtimeConf) |
| 67 |
Expect(err).NotTo(HaveOccurred()) |
| 68 |
|
| 69 |
Expect(result).To(Equal(legacy_examples.ExpectedResult)) |
| 70 |
|
| 71 |
err = cniConfig.DelNetwork(context.TODO(), netConf, runtimeConf) |
| 72 |
Expect(err).NotTo(HaveOccurred()) |
| 73 |
|
| 74 |
Expect(os.RemoveAll(pluginPath)).To(Succeed()) |
| 75 |
}) |
| 76 |
|
| 77 |
It("correctly handles the request from a runtime with an older libcni", func() { |
| 78 |
if runtime.GOOS == "windows" { |
| 79 |
Skip("cannot build old runtime on windows") |
| 80 |
} |
| 81 |
example := legacy_examples.V010_Runtime |
| 82 |
|
| 83 |
binPath, err := example.Build() |
| 84 |
Expect(err).NotTo(HaveOccurred()) |
| 85 |
|
| 86 |
for _, configName := range example.NetConfs { |
| 87 |
conf, err := example.GenerateNetConf(configName) |
| 88 |
if err != nil { |
| 89 |
Fail("Failed to generate config name " + configName + ": " + err.Error()) |
| 90 |
} |
| 91 |
defer conf.Cleanup() |
| 92 |
|
| 93 |
cmd := exec.Command(binPath, pluginDirs...) |
| 94 |
cmd.Stdin = strings.NewReader(conf.Config) |
| 95 |
|
| 96 |
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) |
| 97 |
Expect(err).NotTo(HaveOccurred()) |
| 98 |
|
| 99 |
Eventually(session).Should(gexec.Exit(0)) |
| 100 |
} |
| 101 |
}) |
| 102 |
}) |