Skip to content

Commit

Permalink
daemon: Fix netns usage in kpr privileged unit tests
Browse files Browse the repository at this point in the history
Previously, the SetUpSuite() routine called netns.New(). It expected
that the latter only creates a new netns without setting it.  However,
according to the docs it's not the case:

    package netns // import "github.com/vishvananda/netns"

    func New() (ns NsHandle, err error)
        New creates a new network namespace, sets it as current and returns
        a handle to it.

This meant that we changed the netns before locking the OS thread which
could result in other Go runtime threads running in the test netns.

Fixes: b059c31 ("daemon: Add unit tests for device detection")
Signed-off-by: Martynas Pumputis <m@lambda.lt>
  • Loading branch information
brb authored and aanm committed Nov 27, 2020
1 parent 1eec075 commit 885a319
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions daemon/cmd/kube_proxy_replacement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (

type KubeProxySuite struct {
currentNetNS netns.NsHandle
testNetNS netns.NsHandle
prevConfigDevices []string
prevConfigDirectRoutingDevice string
prevConfigEnableIPv4 bool
Expand All @@ -53,8 +52,6 @@ func (s *KubeProxySuite) SetUpSuite(c *C) {

s.currentNetNS, err = netns.Get()
c.Assert(err, IsNil)
s.testNetNS, err = netns.New()
c.Assert(err, IsNil)
}

func (s *KubeProxySuite) TearDownTest(c *C) {
Expand All @@ -63,15 +60,15 @@ func (s *KubeProxySuite) TearDownTest(c *C) {
option.Config.EnableIPv4 = s.prevConfigEnableIPv4
option.Config.EnableIPv6 = s.prevConfigEnableIPv6
node.SetK8sNodeIP(s.prevK8sNodeIP)

c.Assert(s.testNetNS.Close(), IsNil)
}

func (s *KubeProxySuite) TestDetectDevices(c *C) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

c.Assert(netns.Set(s.testNetNS), IsNil)
testNetNS, err := netns.New() // creates netns, and sets it to current
c.Assert(err, IsNil)
defer func() { c.Assert(testNetNS.Close(), IsNil) }()
defer func() { c.Assert(netns.Set(s.currentNetNS), IsNil) }()

// 1. No devices = impossible to detect
Expand Down

0 comments on commit 885a319

Please sign in to comment.