Describe("MakeNetworkNS", func() { It("should return the filepath to a network namespace", func() { containerID := fmt.Sprintf("c-%x", rand.Int31()) nsPath := testhelpers.MakeNetworkNS(containerID) Expect(nsPath).To(BeAnExistingFile()) testhelpers.RemoveNetworkNS(containerID) }) It("should return a network namespace different from that of the caller", func() { containerID := fmt.Sprintf("c-%x", rand.Int31()) By("discovering the inode of the current netns") originalNetNSPath := currentNetNSPath() originalNetNSInode, err := testhelpers.GetInode(originalNetNSPath) Expect(err).NotTo(HaveOccurred()) By("creating a new netns") createdNetNSPath := testhelpers.MakeNetworkNS(containerID) defer testhelpers.RemoveNetworkNS(createdNetNSPath) By("discovering the inode of the created netns") createdNetNSInode, err := testhelpers.GetInode(createdNetNSPath) Expect(err).NotTo(HaveOccurred()) By("comparing the inodes") Expect(createdNetNSInode).NotTo(Equal(originalNetNSInode)) }) It("should not leak the new netns onto any threads in the process", func() {
Expect(err).NotTo(HaveOccurred()) targetNetNSPath = filepath.Join("/var/run/netns/", targetNetNSName) targetNetNS, err = os.Open(targetNetNSPath) Expect(err).NotTo(HaveOccurred()) }) AfterEach(func() { Expect(targetNetNS.Close()).To(Succeed()) err := exec.Command("ip", "netns", "del", targetNetNSName).Run() Expect(err).NotTo(HaveOccurred()) }) It("executes the callback within the target network namespace", func() { expectedInode, err := testhelpers.GetInode(targetNetNSPath) Expect(err).NotTo(HaveOccurred()) var actualInode uint64 var innerErr error err = ns.WithNetNS(targetNetNS, false, func(*os.File) error { actualInode, innerErr = testhelpers.GetInodeCurNetNS() return nil }) Expect(err).NotTo(HaveOccurred()) Expect(innerErr).NotTo(HaveOccurred()) Expect(actualInode).To(Equal(expectedInode)) }) It("provides the original namespace as the argument to the callback", func() {