plugins/*: Don't error if the device doesn't exist
authorTom Denham <tom@tomdee.co.uk>
Mon, 20 Mar 2017 22:49:35 +0000 (15:49 -0700)
committerTom Denham <tom@tomdee.co.uk>
Wed, 22 Mar 2017 15:52:29 +0000 (08:52 -0700)
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
ip/link_test.go

index ff0bdb2..a984262 100644 (file)
@@ -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)
        }
 
index 85d8b94..23182a5 100644 (file)
@@ -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