From 6e7bb56ca47cfb10667c44949ff6fe2af3d91c66 Mon Sep 17 00:00:00 2001 From: Alessandro Boch Date: Mon, 27 Nov 2017 16:10:03 -0800 Subject: [PATCH] Run TestSocketGet in dedicated netns Signed-off-by: Alessandro Boch --- netlink_test.go | 23 +++++++++++++++++++++++ socket_test.go | 6 ++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/netlink_test.go b/netlink_test.go index 7e861e6..915e18b 100644 --- a/netlink_test.go +++ b/netlink_test.go @@ -43,6 +43,29 @@ func setUpNetlinkTest(t *testing.T) tearDownNetlinkTest { } } +func setUpNetlinkTestWithLoopback(t *testing.T) tearDownNetlinkTest { + skipUnlessRoot(t) + + runtime.LockOSThread() + ns, err := netns.New() + if err != nil { + t.Fatal("Failed to create new netns", ns) + } + + link, err := LinkByName("lo") + if err != nil { + t.Fatalf("Failed to find \"lo\" in new netns: %v", err) + } + if err := LinkSetUp(link); err != nil { + t.Fatalf("Failed to bring up \"lo\" in new netns: %v", err) + } + + return func() { + ns.Close() + runtime.UnlockOSThread() + } +} + func setUpMPLSNetlinkTest(t *testing.T) tearDownNetlinkTest { if _, err := os.Stat("/proc/sys/net/mpls/platform_labels"); err != nil { t.Skip("Test requires MPLS support.") diff --git a/socket_test.go b/socket_test.go index 4a24b89..1698d6e 100644 --- a/socket_test.go +++ b/socket_test.go @@ -5,16 +5,14 @@ package netlink import ( "log" "net" - "os" "os/user" "strconv" "testing" ) func TestSocketGet(t *testing.T) { - if os.Getenv("TRAVIS_BUILD_DIR") != "" { - t.Skipf("Goroutines + network namespaces == inconsistent results") - } + defer setUpNetlinkTestWithLoopback(t)() + addr, err := net.ResolveTCPAddr("tcp", "localhost:0") if err != nil { log.Fatal(err)