-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
- After creating new netns, switch back to main netns - Lock thread during test and test setup
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package main_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"runtime" | ||
|
||
"github.com/onsi/gomega/gexec" | ||
|
||
|
@@ -32,21 +34,38 @@ var _ = AfterSuite(func() { | |
|
||
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()) | ||
|
||
err = unix.Unshare(unix.CLONE_NEWNET) | ||
Expect(err).NotTo(HaveOccurred()) | ||
runtime.LockOSThread() | ||
defer runtime.UnlockOSThread() | ||
go (func() { | ||
defer GinkgoRecover() | ||
|
||
fd, err := os.Create(namespace) | ||
Expect(err).NotTo(HaveOccurred()) | ||
defer fd.Close() | ||
err = unix.Unshare(unix.CLONE_NEWNET) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
fd, err := os.Create(namespace) | ||
Expect(err).NotTo(HaveOccurred()) | ||
defer fd.Close() | ||
|
||
err = unix.Mount("/proc/self/ns/net", namespace, "none", unix.MS_BIND, "") | ||
Expect(err).NotTo(HaveOccurred()) | ||
})() | ||
|
||
err = unix.Mount("/proc/self/ns/net", namespace, "none", unix.MS_BIND, "") | ||
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()) | ||
|
||
Expect(namespace).To(BeAnExistingFile()) | ||
defer unix.Close(fd) | ||
|
||
_, _, e1 := unix.Syscall(unix.SYS_SETNS, uintptr(fd), uintptr(unix.CLONE_NEWNET), 0) | ||
Expect(e1).To(BeZero()) | ||
|
||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
bboreham
Contributor
|
||
return namespace | ||
} | ||
|
||
|
@zachgersh Do you have a reference you worked from for this code? I'm having trouble understanding it. Specifically, why are you opening the file on like 61, what is the syscall on line 66, and why do you expect
e1
to be zero on like 67?Context: I'm trying to extract this code to a
testhelpers
package, because I want to backfill more tests for other plugins. But my tests are behaving non-deterministically... so I'm digging into the namespace-switching stuff.Thanks!