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 14, 2016
1 parent d872391 commit 099bbd8
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
15 changes: 15 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,17 @@ var _ = Describe("Backwards compatibility", func() {

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

It("correctly handles the request from a legacy libcni", func() {
example := legacy_examples.V010_Client

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

cmd := exec.Command(binPath)

session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
})
})
134 changes: 134 additions & 0 deletions pkg/version/legacy_examples/example_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// 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 legacy_examples

// An ExampleInvoker is a git reference to the CNI repo and a Golang CNI
// plugin invoker that builds against that version of the repo
// In other words, it tests forward-compatability
//
// The invoker source should basically be a simplistic cnitool. It will execute
// a noop plugin provided as argv[1]
type ExampleClient struct {
Example
}

// A lot of this is borrowed from the ptp test. Consider refactoring
// This tests a non-versioned network config
var V010_Client = ExampleClient{
Example{
Name: "example_invoker_v010",
CNIRepoGitRef: "c0d34c69", //version with ns.Do
PluginSource: `package main
import (
"fmt"
"os"
"strings"
"net"
"github.com/containernetworking/cni/pkg/ns"
"github.com/containernetworking/cni/libcni"
)
func main(){
code := exec()
os.Exit(code)
}
func exec() int {
confStr := "{ \"name\": \"default\", \"type\":\"ptp\", \"ipam\":{ \"type\": \"host-local\", \"subnet\": \"10.1.2.0/24\"}}"
netConf, err := libcni.ConfFromBytes([]byte(confStr))
if err != nil {
fmt.Printf("could not parse netconfig: %+v", err)
return 1
}
targetNs, err := ns.NewNS()
if err != nil {
fmt.Printf("Could not create ns: %+v", err)
return 1
}
defer targetNs.Close()
ifName := "eth0"
runtimeConf := &libcni.RuntimeConf{
ContainerID: "some-container-id",
NetNS: targetNs.Path(),
IfName: ifName,
}
cniConfig := &libcni.CNIConfig{Path: strings.Split(os.Getenv("PATH"), ":")}
result, err := cniConfig.AddNetwork(netConf, runtimeConf)
fmt.Printf("Result: %+v", result)
if err != nil {
fmt.Printf("noop add failed: %+v", err)
return 2
}
expectedIP := result.IP4.IP
err = targetNs.Do(func(ns.NetNS) error {
netif, err := net.InterfaceByName(ifName)
if err != nil {
return fmt.Errorf("could not retrieve interface: %v", err)
}
addrs, err := netif.Addrs()
if err != nil {
return fmt.Errorf("could not retrieve addresses, %+v", err)
}
found := false
for _, addr := range addrs {
if addr.String() == expectedIP.String() {
found = true
break
}
}
if !found {
return fmt.Errorf("Far-side link did not have expected address %s", expectedIP)
}
return nil
})
if err != nil {
fmt.Println(err)
return 4
}
err = cniConfig.DelNetwork(netConf, runtimeConf)
if err != nil {
fmt.Printf("error deleting network: %v", err)
return 5
}
err = targetNs.Do(func(ns.NetNS) error {
_, err := net.InterfaceByName(ifName)
if err == nil {
return fmt.Errorf("interface was not deleted")
}
return nil
})
if err != nil {
fmt.Println(err)
return 6
}
return 0
}
`,
}}

0 comments on commit 099bbd8

Please sign in to comment.