Fix issues with MakeNetworkNS test helper
authorGabe Rosenhouse <grosenhouse@pivotal.io>
Mon, 18 Apr 2016 02:44:00 +0000 (19:44 -0700)
committerStefan Junker <mail@stefanjunker.de>
Thu, 19 May 2016 10:06:16 +0000 (12:06 +0200)
testhelpers/testhelpers.go

index 4eb42bd..0963121 100644 (file)
@@ -19,6 +19,7 @@ import (
        "fmt"
        "os"
        "runtime"
+       "sync"
 
        "golang.org/x/sys/unix"
 
@@ -43,37 +44,51 @@ func GetInodeF(file *os.File) (uint64, error) {
 
 func MakeNetworkNS(containerID string) string {
        namespace := "/var/run/netns/" + containerID
-       pid := unix.Getpid()
-       tid := unix.Gettid()
 
        err := os.MkdirAll("/var/run/netns", 0600)
        Expect(err).NotTo(HaveOccurred())
 
-       runtime.LockOSThread()
-       defer runtime.UnlockOSThread()
+       // create an empty file at the mount point
+       mountPointFd, err := os.Create(namespace)
+       Expect(err).NotTo(HaveOccurred())
+       mountPointFd.Close()
+
+       var wg sync.WaitGroup
+       wg.Add(1)
+
+       // do namespace work in a dedicated goroutine, so that we can safely
+       // Lock/Unlock OSThread without upsetting the lock/unlock state of
+       // the caller of this function
        go (func() {
+               defer wg.Done()
+
+               runtime.LockOSThread()
+               defer runtime.UnlockOSThread()
+
                defer GinkgoRecover()
 
-               err = unix.Unshare(unix.CLONE_NEWNET)
+               // capture current thread's original netns
+               pid := unix.Getpid()
+               tid := unix.Gettid()
+               currentThreadNetNSPath := fmt.Sprintf("/proc/%d/task/%d/ns/net", pid, tid)
+               originalNetNS, err := unix.Open(currentThreadNetNSPath, unix.O_RDONLY, 0)
                Expect(err).NotTo(HaveOccurred())
+               defer unix.Close(originalNetNS)
 
-               fd, err := os.Create(namespace)
+               // create a new netns on the current thread
+               err = unix.Unshare(unix.CLONE_NEWNET)
                Expect(err).NotTo(HaveOccurred())
-               defer fd.Close()
 
-               err = unix.Mount("/proc/self/ns/net", namespace, "none", unix.MS_BIND, "")
+               // bind mount the new netns from the current thread onto the mount point
+               err = unix.Mount(currentThreadNetNSPath, namespace, "none", unix.MS_BIND, "")
                Expect(err).NotTo(HaveOccurred())
-       })()
-
-       Eventually(namespace).Should(BeAnExistingFile())
 
-       fd, err := unix.Open(fmt.Sprintf("/proc/%d/task/%d/ns/net", pid, tid), unix.O_RDONLY, 0)
-       Expect(err).NotTo(HaveOccurred())
-
-       defer unix.Close(fd)
+               // reset current thread's netns to the original
+               _, _, e1 := unix.Syscall(unix.SYS_SETNS, uintptr(originalNetNS), uintptr(unix.CLONE_NEWNET), 0)
+               Expect(e1).To(BeZero())
+       })()
 
-       _, _, e1 := unix.Syscall(unix.SYS_SETNS, uintptr(fd), uintptr(unix.CLONE_NEWNET), 0)
-       Expect(e1).To(BeZero())
+       wg.Wait()
 
        return namespace
 }