Skip to content

Commit

Permalink
testing: test invocation of newer plugins with an older libcni
Browse files Browse the repository at this point in the history
  • Loading branch information
squeed committed Oct 21, 2016
1 parent bff05e7 commit 663303d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
2 changes: 1 addition & 1 deletion libcni/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var _ = Describe("Invoking the plugin", func() {
}
Expect(debug.WriteDebug(debugFilePath)).To(Succeed())

cniBinPath = filepath.Dir(pathToPlugin)
cniBinPath = filepath.Dir(pluginPaths["noop"])
pluginConfig = `{ "type": "noop", "some-key": "some-value", "cniVersion": "0.2.0" }`
cniConfig = libcni.CNIConfig{Path: []string{cniBinPath}}
netConfig = &libcni.NetworkConfig{
Expand Down
36 changes: 36 additions & 0 deletions libcni/backwards_compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ package libcni_test
import (
"fmt"
"os"
"os/exec"
"path/filepath"

"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() {
Expand Down Expand Up @@ -50,4 +52,38 @@ var _ = Describe("Backwards compatibility", func() {

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_Client

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...)
procIn, err := cmd.StdinPipe()
Expect(err).NotTo(HaveOccurred())

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

// Write in the network config and close
_, err = procIn.Write([]byte(configStr))
Expect(err).NotTo(HaveOccurred())
err = procIn.Close()
Expect(err).NotTo(HaveOccurred())

Eventually(session).Should(gexec.Exit(0))
}
})
})
36 changes: 29 additions & 7 deletions libcni/libcni_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
package libcni_test

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

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
Expand All @@ -27,17 +31,35 @@ func TestLibcni(t *testing.T) {
RunSpecs(t, "Libcni Suite")
}

const packagePath = "github.com/containernetworking/cni/plugins/test/noop"
var plugins = map[string]string{
"noop": "github.com/containernetworking/cni/plugins/test/noop",
"ptp": "github.com/containernetworking/cni/plugins/main/ptp",
"host-local": "github.com/containernetworking/cni/plugins/ipam/host-local",
}

var pathToPlugin string
var pluginPaths map[string]string
var pluginDirs []string // array of plugin dirs

var _ = SynchronizedBeforeSuite(func() []byte {
var err error
pathToPlugin, err = gexec.Build(packagePath)
Expect(err).NotTo(HaveOccurred())
return []byte(pathToPlugin)
dirs := make([]string, 0, len(plugins))

for name, packagePath := range plugins {
execPath, err := gexec.Build(packagePath)
Expect(err).NotTo(HaveOccurred())
dirs = append(dirs, fmt.Sprintf("%s=%s", name, execPath))
}

return []byte(strings.Join(dirs, ":"))
}, func(crossNodeData []byte) {
pathToPlugin = string(crossNodeData)
pluginPaths = make(map[string]string)
for _, str := range strings.Split(string(crossNodeData), ":") {
kvs := strings.SplitN(str, "=", 2)
if len(kvs) != 2 {
Fail("Invalid inter-node data...")
}
pluginPaths[kvs[0]] = kvs[1]
pluginDirs = append(pluginDirs, filepath.Dir(kvs[1]))
}
})

var _ = SynchronizedAfterSuite(func() {}, func() {
Expand Down

0 comments on commit 663303d

Please sign in to comment.