From 820712cae66227bf4e551c93319f8729f60c5a05 Mon Sep 17 00:00:00 2001 From: junxu Date: Tue, 19 May 2015 03:02:57 +0000 Subject: [PATCH] Fix RemoveInterface in sandbox The networkNamespace will record all interfaces joined into this sandbox. While RremoveInterface func does't remove the leaved interfaces. Signed-off-by: junxu --- libnetwork/sandbox/namespace_linux.go | 11 +++++ libnetwork/sandbox/sandbox_linux_test.go | 5 ++- libnetwork/sandbox/sandbox_test.go | 52 ++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/libnetwork/sandbox/namespace_linux.go b/libnetwork/sandbox/namespace_linux.go index 17881f1404..01bc7f3fef 100644 --- a/libnetwork/sandbox/namespace_linux.go +++ b/libnetwork/sandbox/namespace_linux.go @@ -165,6 +165,15 @@ func (n *networkNamespace) RemoveInterface(i *Interface) error { return err } + n.Lock() + for index, intf := range n.sinfo.Interfaces { + if intf == i { + n.sinfo.Interfaces = append(n.sinfo.Interfaces[:index], n.sinfo.Interfaces[index+1:]...) + break + } + } + n.Unlock() + return nil } @@ -255,6 +264,8 @@ func (n *networkNamespace) SetGatewayIPv6(gw net.IP) error { } func (n *networkNamespace) Interfaces() []*Interface { + n.Lock() + defer n.Unlock() return n.sinfo.Interfaces } diff --git a/libnetwork/sandbox/sandbox_linux_test.go b/libnetwork/sandbox/sandbox_linux_test.go index d4af061f91..5c4448a3e9 100644 --- a/libnetwork/sandbox/sandbox_linux_test.go +++ b/libnetwork/sandbox/sandbox_linux_test.go @@ -84,6 +84,7 @@ func newInfo(t *testing.T) (*Info, error) { // ip6, addrv6, err := net.ParseCIDR("2001:DB8::ABCD/48") ip6, addrv6, err = net.ParseCIDR("fe80::3/64") + if err != nil { return nil, err } @@ -127,13 +128,13 @@ func verifySandbox(t *testing.T, s Sandbox) { _, err = netlink.LinkByName(sboxIfaceName + "0") if err != nil { - t.Fatalf("Could not find the interface %s inside the sandbox: %v", sboxIfaceName, + t.Fatalf("Could not find the interface %s inside the sandbox: %v", sboxIfaceName+"0", err) } _, err = netlink.LinkByName(sboxIfaceName + "1") if err != nil { - t.Fatalf("Could not find the interface %s inside the sandbox: %v", sboxIfaceName, + t.Fatalf("Could not find the interface %s inside the sandbox: %v", sboxIfaceName+"1", err) } } diff --git a/libnetwork/sandbox/sandbox_test.go b/libnetwork/sandbox/sandbox_test.go index 811af6d916..c0dd6c8e75 100644 --- a/libnetwork/sandbox/sandbox_test.go +++ b/libnetwork/sandbox/sandbox_test.go @@ -66,6 +66,58 @@ func TestSandboxCreateTwice(t *testing.T) { s.Destroy() } +func TestAddRemoveInterface(t *testing.T) { + key, err := newKey(t) + if err != nil { + t.Fatalf("Failed to obtain a key: %v", err) + } + + s, err := NewSandbox(key, true) + if err != nil { + t.Fatalf("Failed to create a new sandbox: %v", err) + } + + if s.Key() != key { + t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key) + } + + info, err := newInfo(t) + if err != nil { + t.Fatalf("Failed to generate new sandbox info: %v", err) + } + + for _, i := range info.Interfaces { + err = s.AddInterface(i) + if err != nil { + t.Fatalf("Failed to add interfaces to sandbox: %v", err) + } + } + + interfaces := s.Interfaces() + if !(interfaces[0].Equal(info.Interfaces[0]) && interfaces[1].Equal(info.Interfaces[1])) { + t.Fatalf("Failed to update Sandbox.sinfo.Interfaces in AddInterfaces") + } + + if err := s.RemoveInterface(info.Interfaces[0]); err != nil { + t.Fatalf("Failed to remove interfaces from sandbox: %v", err) + } + + if !s.Interfaces()[0].Equal(info.Interfaces[1]) { + t.Fatalf("Failed to update the sanbox.sinfo.Interfaces in RemoveInterferce") + } + + if err := s.AddInterface(info.Interfaces[0]); err != nil { + t.Fatalf("Failed to add interfaces to sandbox: %v", err) + } + + interfaces = s.Interfaces() + if !(interfaces[0].Equal(info.Interfaces[1]) && interfaces[1].Equal(info.Interfaces[0])) { + t.Fatalf("Failed to update Sandbox.sinfo.Interfaces in AddInterfaces") + } + + s.Destroy() +} + func TestInterfaceEqual(t *testing.T) { list := getInterfaceList()