Extract testhelpers from loopback test suite
authorGabe Rosenhouse <grosenhouse@pivotal.io>
Mon, 18 Apr 2016 01:28:10 +0000 (18:28 -0700)
committerStefan Junker <mail@stefanjunker.de>
Thu, 19 May 2016 10:06:16 +0000 (12:06 +0200)
pkg/testhelpers/testhelpers.go [new file with mode: 0644]
plugins/main/loopback/loopback_suite_test.go
plugins/main/loopback/loopback_test.go

diff --git a/pkg/testhelpers/testhelpers.go b/pkg/testhelpers/testhelpers.go
new file mode 100644 (file)
index 0000000..f1ccb24
--- /dev/null
@@ -0,0 +1,69 @@
+// Copyright 2016 CNI authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package testhelpers
+
+import (
+       "fmt"
+       "os"
+       "runtime"
+
+       "golang.org/x/sys/unix"
+
+       . "github.com/onsi/ginkgo"
+       . "github.com/onsi/gomega"
+)
+
+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()
+       go (func() {
+               defer GinkgoRecover()
+
+               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())
+       })()
+
+       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)
+
+       _, _, e1 := unix.Syscall(unix.SYS_SETNS, uintptr(fd), uintptr(unix.CLONE_NEWNET), 0)
+       Expect(e1).To(BeZero())
+
+       return namespace
+}
+
+func RemoveNetworkNS(networkNS string) error {
+       err := unix.Unmount(networkNS, unix.MNT_DETACH)
+
+       err = os.RemoveAll(networkNS)
+       return err
+}
index 36d646a..be179aa 100644 (file)
 package main_test
 
 import (
-       "fmt"
-       "os"
-       "runtime"
-
        "github.com/onsi/gomega/gexec"
 
        . "github.com/onsi/ginkgo"
        . "github.com/onsi/gomega"
 
        "testing"
-
-       "golang.org/x/sys/unix"
 )
 
 var pathToLoPlugin string
@@ -45,47 +39,3 @@ var _ = BeforeSuite(func() {
 var _ = AfterSuite(func() {
        gexec.CleanupBuildArtifacts()
 })
-
-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()
-       go (func() {
-               defer GinkgoRecover()
-
-               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())
-       })()
-
-       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)
-
-       _, _, e1 := unix.Syscall(unix.SYS_SETNS, uintptr(fd), uintptr(unix.CLONE_NEWNET), 0)
-       Expect(e1).To(BeZero())
-
-       return namespace
-}
-
-func removeNetworkNS(networkNS string) error {
-       err := unix.Unmount(networkNS, unix.MNT_DETACH)
-
-       err = os.RemoveAll(networkNS)
-       return err
-}
index fbacc70..037fbfe 100644 (file)
@@ -22,6 +22,7 @@ import (
        "strings"
 
        "github.com/appc/cni/pkg/ns"
+       "github.com/appc/cni/pkg/testhelpers"
        . "github.com/onsi/ginkgo"
        . "github.com/onsi/gomega"
        "github.com/onsi/gomega/gbytes"
@@ -39,7 +40,7 @@ var _ = Describe("Loopback", func() {
        BeforeEach(func() {
                command = exec.Command(pathToLoPlugin)
                containerID = "some-container-id"
-               networkNS = makeNetworkNS(containerID)
+               networkNS = testhelpers.MakeNetworkNS(containerID)
 
                environ = []string{
                        fmt.Sprintf("CNI_CONTAINERID=%s", containerID),
@@ -52,7 +53,7 @@ var _ = Describe("Loopback", func() {
        })
 
        AfterEach(func() {
-               Expect(removeNetworkNS(networkNS)).To(Succeed())
+               Expect(testhelpers.RemoveNetworkNS(networkNS)).To(Succeed())
        })
 
        Context("when given a network namespace", func() {