Skip to content

Commit

Permalink
Merge pull request #74 from rosenhouse/host-device-fixes
Browse files Browse the repository at this point in the history
Host-device fixes
  • Loading branch information
Casey Callendrello authored Sep 13, 2017
2 parents 6be2e8a + c238c93 commit e256564
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
9 changes: 5 additions & 4 deletions plugins/host-device/host-device.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import (
"runtime"
"strings"

"github.com/containernetworking/cni/pkg/ns"
"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"
)

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -115,9 +118,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 {
Expand Down
24 changes: 16 additions & 8 deletions plugins/host-device/host-device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@
package main

import (
"github.com/containernetworking/cni/pkg/ns"
"fmt"
"math/rand"

"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"
)

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() {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e256564

Please sign in to comment.