host-device plugin: result is valid JSON
authorGabriel Rosenhouse <grosenhouse@pivotal.io>
Wed, 13 Sep 2017 04:01:58 +0000 (21:01 -0700)
committerGabriel Rosenhouse <grosenhouse@pivotal.io>
Wed, 13 Sep 2017 04:01:58 +0000 (21:01 -0700)
test:
- feed valid config JSON to plugin
- execute plugin inside the namespace with the test device

plugins/host-device/host-device.go
plugins/host-device/host-device_test.go

index 80c0d0b..954b777 100644 (file)
@@ -25,6 +25,7 @@ import (
        "strings"
 
        "github.com/containernetworking/cni/pkg/skel"
+       "github.com/containernetworking/cni/pkg/types/current"
        "github.com/containernetworking/cni/pkg/version"
        "github.com/containernetworking/plugins/pkg/ns"
        "github.com/vishvananda/netlink"
@@ -64,6 +65,7 @@ func cmdAdd(args *skel.CmdArgs) error {
                return fmt.Errorf("failed to open netns %q: %v", args.Netns, err)
        }
        defer containerNs.Close()
+       defer (&current.Result{}).Print()
        return addLink(cfg.Device, cfg.HWAddr, cfg.KernelPath, containerNs)
 }
 
@@ -77,6 +79,7 @@ func cmdDel(args *skel.CmdArgs) error {
                return fmt.Errorf("failed to open netns %q: %v", args.Netns, err)
        }
        defer containerNs.Close()
+       defer fmt.Println(`{}`)
        return removeLink(cfg.Device, cfg.HWAddr, cfg.KernelPath, containerNs)
 }
 
index e50cc0f..128ad73 100644 (file)
@@ -15,6 +15,9 @@
 package main
 
 import (
+       "fmt"
+       "math/rand"
+
        "github.com/containernetworking/cni/pkg/skel"
        "github.com/containernetworking/plugins/pkg/ns"
        "github.com/containernetworking/plugins/pkg/testutils"
@@ -23,15 +26,16 @@ import (
        "github.com/vishvananda/netlink"
 )
 
-var ifname = "dummy0"
-
 var _ = Describe("base functionality", func() {
        var originalNS ns.NetNS
+       var ifname string
 
        BeforeEach(func() {
                var err error
                originalNS, err = ns.NewNS()
                Expect(err).NotTo(HaveOccurred())
+
+               ifname = fmt.Sprintf("dummy-%x", rand.Int31())
        })
 
        AfterEach(func() {
@@ -61,19 +65,23 @@ var _ = Describe("base functionality", func() {
                targetNS, err := ns.NewNS()
                Expect(err).NotTo(HaveOccurred())
 
-               conf := `{
+               conf := fmt.Sprintf(`{
                        "cniVersion": "0.3.0",
                        "name": "cni-plugin-host-device-test",
                        "type": "host-device",
-                       "device": ifname
-               }`
+                       "device": %q
+               }`, ifname)
                args := &skel.CmdArgs{
                        ContainerID: "dummy",
                        Netns:       targetNS.Path(),
                        IfName:      ifname,
                        StdinData:   []byte(conf),
                }
-               _, _, err = testutils.CmdAddWithResult(targetNS.Path(), ifname, []byte(conf), func() error { return cmdAdd(args) })
+               err = originalNS.Do(func(ns.NetNS) error {
+                       defer GinkgoRecover()
+                       _, _, err := testutils.CmdAddWithResult(targetNS.Path(), ifname, []byte(conf), func() error { return cmdAdd(args) })
+                       return err
+               })
                Expect(err).NotTo(HaveOccurred())
 
                // assert that dummy0 is now in the target namespace