From 58b78e72fd390a2cafcd3b6b71a0aed4e75f897d Mon Sep 17 00:00:00 2001 From: Tom Denham Date: Mon, 20 Mar 2017 15:49:35 -0700 Subject: [PATCH] plugins/*: Don't error if the device doesn't exist I wasn't able to test or update the dhcp plugin but from a code read it should be fine. All the other plugins are tested and fixed --- ip/link.go | 8 ++++++++ ip/link_test.go | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/ip/link.go b/ip/link.go index ff0bdb29..a9842627 100644 --- a/ip/link.go +++ b/ip/link.go @@ -16,6 +16,7 @@ package ip import ( "crypto/rand" + "errors" "fmt" "net" "os" @@ -25,6 +26,10 @@ import ( "github.com/vishvananda/netlink" ) +var ( + ErrLinkNotFound = errors.New("link not found") +) + func makeVethPair(name, peer string, mtu int) (netlink.Link, error) { veth := &netlink.Veth{ LinkAttrs: netlink.LinkAttrs{ @@ -168,6 +173,9 @@ func DelLinkByName(ifName string) error { func DelLinkByNameAddr(ifName string, family int) (*net.IPNet, error) { iface, err := netlink.LinkByName(ifName) if err != nil { + if err != nil && err.Error() == "Link not found" { + return nil, ErrLinkNotFound + } return nil, fmt.Errorf("failed to lookup %q: %v", ifName, err) } diff --git a/ip/link_test.go b/ip/link_test.go index 85d8b944..23182a54 100644 --- a/ip/link_test.go +++ b/ip/link_test.go @@ -127,6 +127,20 @@ var _ = Describe("Link", func() { }) }) + Context("deleting an non-existent device", func() { + It("returns known error", func() { + _ = containerNetNS.Do(func(ns.NetNS) error { + defer GinkgoRecover() + + // This string should match the expected error codes in the cmdDel functions of some of the plugins + _, err := ip.DelLinkByNameAddr("THIS_DONT_EXIST", netlink.FAMILY_V4) + Expect(err).To(Equal(ip.ErrLinkNotFound)) + + return nil + }) + }) + }) + Context("when there is no name available for the host-side", func() { BeforeEach(func() { //adding different interface to container ns