From 5e46a66c89d6e9380374797db895d9ed6e311c0a Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Tue, 12 Sep 2017 05:08:38 +0000 Subject: [PATCH 1/3] Fix `go get github.com/containernetworking/plugins`. Signed-off-by: Lantao Liu --- plugins/host-device/host-device.go | 2 +- plugins/host-device/host-device_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/host-device/host-device.go b/plugins/host-device/host-device.go index 853cab9e..573dc1ce 100644 --- a/plugins/host-device/host-device.go +++ b/plugins/host-device/host-device.go @@ -24,9 +24,9 @@ import ( "runtime" "strings" - "github.com/containernetworking/cni/pkg/ns" "github.com/containernetworking/cni/pkg/skel" "github.com/containernetworking/cni/pkg/version" + "github.com/containernetworking/plugins/pkg/ns" "github.com/vishvananda/netlink" ) diff --git a/plugins/host-device/host-device_test.go b/plugins/host-device/host-device_test.go index 68cfe4be..e50cc0f5 100644 --- a/plugins/host-device/host-device_test.go +++ b/plugins/host-device/host-device_test.go @@ -15,9 +15,9 @@ package main import ( - "github.com/containernetworking/cni/pkg/ns" "github.com/containernetworking/cni/pkg/skel" - "github.com/containernetworking/cni/pkg/testutils" + "github.com/containernetworking/plugins/pkg/ns" + "github.com/containernetworking/plugins/pkg/testutils" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/vishvananda/netlink" From 25ca6ccb521d2e38e4c97fb824af3dd7cc921ae7 Mon Sep 17 00:00:00 2001 From: Gabriel Rosenhouse Date: Tue, 12 Sep 2017 20:26:37 -0700 Subject: [PATCH 2/3] host-device: do not swallow netlink errors --- plugins/host-device/host-device.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugins/host-device/host-device.go b/plugins/host-device/host-device.go index 573dc1ce..80c0d0be 100644 --- a/plugins/host-device/host-device.go +++ b/plugins/host-device/host-device.go @@ -115,9 +115,7 @@ func getLink(devname, hwaddr, kernelpath string) (netlink.Link, error) { } if len(devname) > 0 { - if m, err := netlink.LinkByName(devname); err == nil { - return m, nil - } + return netlink.LinkByName(devname) } else if len(hwaddr) > 0 { hwAddr, err := net.ParseMAC(hwaddr) if err != nil { From c238c93b5e7c681f1935ff813b30e82f96f6c367 Mon Sep 17 00:00:00 2001 From: Gabriel Rosenhouse Date: Tue, 12 Sep 2017 21:01:58 -0700 Subject: [PATCH 3/3] host-device plugin: result is valid JSON test: - feed valid config JSON to plugin - execute plugin inside the namespace with the test device --- plugins/host-device/host-device.go | 3 +++ plugins/host-device/host-device_test.go | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/plugins/host-device/host-device.go b/plugins/host-device/host-device.go index 80c0d0be..954b7778 100644 --- a/plugins/host-device/host-device.go +++ b/plugins/host-device/host-device.go @@ -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 (¤t.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) } diff --git a/plugins/host-device/host-device_test.go b/plugins/host-device/host-device_test.go index e50cc0f5..128ad73c 100644 --- a/plugins/host-device/host-device_test.go +++ b/plugins/host-device/host-device_test.go @@ -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