From dbc72376c86301bb873c332830857e73c9f701e3 Mon Sep 17 00:00:00 2001 From: Sebastien Boving Date: Mon, 6 Feb 2017 08:14:07 -0800 Subject: [PATCH] Properly skip tests which require root. All these tests currently fail with 'Operation not permitted' when run as non-root. --- handle_test.go | 3 +++ link_test.go | 2 ++ netlink_test.go | 6 +++++- route_test.go | 2 ++ rule_test.go | 2 ++ 5 files changed, 14 insertions(+), 1 deletion(-) diff --git a/handle_test.go b/handle_test.go index d15a139..dbf6cc2 100644 --- a/handle_test.go +++ b/handle_test.go @@ -39,6 +39,8 @@ func TestHandleCreateDelete(t *testing.T) { } func TestHandleCreateNetns(t *testing.T) { + skipUnlessRoot(t) + id := make([]byte, 4) if _, err := io.ReadFull(rand.Reader, id); err != nil { t.Fatal(err) @@ -236,6 +238,7 @@ func parallelDone() { // Do few route and xfrm operation on the two handles in parallel func runParallelTests(t *testing.T, thread int) { + skipUnlessRoot(t) defer parallelDone() t.Parallel() diff --git a/link_test.go b/link_test.go index 4a4fa24..fb548c8 100644 --- a/link_test.go +++ b/link_test.go @@ -862,6 +862,8 @@ func TestLinkSubscribe(t *testing.T) { } func TestLinkSubscribeAt(t *testing.T) { + skipUnlessRoot(t) + // Create an handle on a custom netns newNs, err := netns.New() if err != nil { diff --git a/netlink_test.go b/netlink_test.go index 3292b75..90aa74d 100644 --- a/netlink_test.go +++ b/netlink_test.go @@ -11,12 +11,16 @@ import ( type tearDownNetlinkTest func() -func setUpNetlinkTest(t *testing.T) tearDownNetlinkTest { +func skipUnlessRoot(t *testing.T) { if os.Getuid() != 0 { msg := "Skipped test because it requires root privileges." log.Printf(msg) t.Skip(msg) } +} + +func setUpNetlinkTest(t *testing.T) tearDownNetlinkTest { + skipUnlessRoot(t) // new temporary namespace so we don't pollute the host // lock thread since the namespace is thread local diff --git a/route_test.go b/route_test.go index bb64c7f..01906f3 100644 --- a/route_test.go +++ b/route_test.go @@ -208,6 +208,8 @@ func TestRouteSubscribe(t *testing.T) { } func TestRouteSubscribeAt(t *testing.T) { + skipUnlessRoot(t) + // Create an handle on a custom netns newNs, err := netns.New() if err != nil { diff --git a/rule_test.go b/rule_test.go index cc5fc9b..e4db0ce 100644 --- a/rule_test.go +++ b/rule_test.go @@ -7,6 +7,8 @@ import ( ) func TestRuleAddDel(t *testing.T) { + skipUnlessRoot(t) + srcNet := &net.IPNet{IP: net.IPv4(172, 16, 0, 1), Mask: net.CIDRMask(16, 32)} dstNet := &net.IPNet{IP: net.IPv4(172, 16, 1, 1), Mask: net.CIDRMask(24, 32)}