. "github.com/onsi/gomega"
)
-const CurrentNetNS = "/proc/self/ns/net"
-
var _ = Describe("Linux namespace operations", func() {
Describe("WithNetNS", func() {
var (
- originalNetNS *os.File
-
targetNetNSName string
targetNetNSPath string
targetNetNS *os.File
BeforeEach(func() {
var err error
- originalNetNS, err = os.Open(CurrentNetNS)
- Expect(err).NotTo(HaveOccurred())
targetNetNSName = fmt.Sprintf("test-netns-%d", rand.Int())
err := exec.Command("ip", "netns", "del", targetNetNSName).Run()
Expect(err).NotTo(HaveOccurred())
-
- Expect(originalNetNS.Close()).To(Succeed())
})
It("executes the callback within the target network namespace", func() {
var actualInode uint64
var innerErr error
err = ns.WithNetNS(targetNetNS, false, func(*os.File) error {
- actualInode, innerErr = testhelpers.GetInode(CurrentNetNS)
+ actualInode, innerErr = testhelpers.GetInodeCurNetNS()
return nil
})
Expect(err).NotTo(HaveOccurred())
})
It("provides the original namespace as the argument to the callback", func() {
- hostNSInode, err := testhelpers.GetInode(CurrentNetNS)
+ hostNSInode, err := testhelpers.GetInodeCurNetNS()
Expect(err).NotTo(HaveOccurred())
var inputNSInode uint64
})
It("restores the calling thread to the original network namespace", func() {
- preTestInode, err := testhelpers.GetInode(CurrentNetNS)
+ preTestInode, err := testhelpers.GetInodeCurNetNS()
Expect(err).NotTo(HaveOccurred())
err = ns.WithNetNS(targetNetNS, false, func(*os.File) error {
})
Expect(err).NotTo(HaveOccurred())
- postTestInode, err := testhelpers.GetInode(CurrentNetNS)
+ postTestInode, err := testhelpers.GetInodeCurNetNS()
Expect(err).NotTo(HaveOccurred())
Expect(postTestInode).To(Equal(preTestInode))
Context("when the callback returns an error", func() {
It("restores the calling thread to the original namespace before returning", func() {
- preTestInode, err := testhelpers.GetInode(CurrentNetNS)
+ preTestInode, err := testhelpers.GetInodeCurNetNS()
Expect(err).NotTo(HaveOccurred())
_ = ns.WithNetNS(targetNetNS, false, func(*os.File) error {
return errors.New("potato")
})
- postTestInode, err := testhelpers.GetInode(CurrentNetNS)
+ postTestInode, err := testhelpers.GetInodeCurNetNS()
Expect(err).NotTo(HaveOccurred())
Expect(postTestInode).To(Equal(preTestInode))
Describe("validating inode mapping to namespaces", func() {
It("checks that different namespaces have different inodes", func() {
- hostNSInode, err := testhelpers.GetInode(CurrentNetNS)
+ hostNSInode, err := testhelpers.GetInodeCurNetNS()
Expect(err).NotTo(HaveOccurred())
testNsInode, err := testhelpers.GetInode(targetNetNSPath)
. "github.com/onsi/gomega"
)
+func getCurrentThreadNetNSPath() string {
+ pid := unix.Getpid()
+ tid := unix.Gettid()
+ return fmt.Sprintf("/proc/%d/task/%d/ns/net", pid, tid)
+}
+
+func GetInodeCurNetNS() (uint64, error) {
+ return GetInode(getCurrentThreadNetNSPath())
+}
+
func GetInode(path string) (uint64, error) {
file, err := os.Open(path)
if err != nil {
defer GinkgoRecover()
// capture current thread's original netns
- pid := unix.Getpid()
- tid := unix.Gettid()
- currentThreadNetNSPath := fmt.Sprintf("/proc/%d/task/%d/ns/net", pid, tid)
+ currentThreadNetNSPath := getCurrentThreadNetNSPath()
originalNetNS, err := unix.Open(currentThreadNetNSPath, unix.O_RDONLY, 0)
Expect(err).NotTo(HaveOccurred())
defer unix.Close(originalNetNS)