示例#1
0
	"golang.org/x/sys/unix"

	"github.com/containernetworking/cni/pkg/testhelpers"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Test helper functions", func() {
	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")
示例#2
0
		command = exec.Command(pathToLoPlugin)
		containerID = "some-container-id"
		networkNS = testhelpers.MakeNetworkNS(containerID)

		environ = []string{
			fmt.Sprintf("CNI_CONTAINERID=%s", containerID),
			fmt.Sprintf("CNI_NETNS=%s", networkNS),
			fmt.Sprintf("CNI_IFNAME=%s", "this is ignored"),
			fmt.Sprintf("CNI_ARGS=%s", "none"),
			fmt.Sprintf("CNI_PATH=%s", "/some/test/path"),
		}
		command.Stdin = strings.NewReader("this doesn't matter")
	})

	AfterEach(func() {
		Expect(testhelpers.RemoveNetworkNS(networkNS)).To(Succeed())
	})

	Context("when given a network namespace", func() {
		It("sets the lo device to UP", func() {
			command.Env = append(environ, fmt.Sprintf("CNI_COMMAND=%s", "ADD"))

			session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
			Expect(err).NotTo(HaveOccurred())

			Eventually(session).Should(gbytes.Say(`{.*}`))
			Eventually(session).Should(gexec.Exit(0))

			var lo *net.Interface
			err = ns.WithNetNSPath(networkNS, true, func(hostNS *os.File) error {
				var err error