-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathintegration_suite_test.go
84 lines (65 loc) · 1.91 KB
/
integration_suite_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
"github.com/onsi/gomega/gexec"
)
var (
pathToNsProcessCLI string
rootfsFilepath string
)
func TestNsProcess(t *testing.T) {
BeforeSuite(func() {
var err error
// build the ns-process binary
pathToNsProcessCLI, err = gexec.Build("github.com/teddyking/ns-process")
Expect(err).NotTo(HaveOccurred())
// setup a test rootfs dir
busyboxTarFilepath := filepath.Join("assets", "busybox.tar")
rootfsFilepath = filepath.Join("/tmp", "ns-process", "test", "rootfs")
Expect(os.RemoveAll(rootfsFilepath)).To(Succeed())
Expect(os.MkdirAll(rootfsFilepath, 0755)).To(Succeed())
untar(busyboxTarFilepath, rootfsFilepath)
// check for netsetgo
checkNetsetgo()
})
AfterEach(func() {
// allow time for various network operations to complete between tests
// super gross hack I know, but the tests here are very simplified and
// really serve only to support a basic TDD workflow
time.Sleep(time.Second / 2)
})
AfterSuite(func() {
gexec.CleanupBuildArtifacts()
})
SetDefaultEventuallyTimeout(time.Second * 5)
RegisterFailHandler(Fail)
RunSpecs(t, "ns-process")
}
func inode(pid, namespaceType string) string {
namespace, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/%s", pid, namespaceType))
Expect(err).NotTo(HaveOccurred())
requiredFormat := regexp.MustCompile(`^\w+:\[\d+\]$`)
Expect(requiredFormat.MatchString(namespace)).To(BeTrue())
namespace = strings.Split(namespace, ":")[1]
namespace = namespace[1:]
namespace = namespace[:len(namespace)-1]
return namespace
}
func untar(src, dst string) {
// use tar command for sake of simplicity
untarCmd := exec.Command("tar", "-C", dst, "-xf", src)
Expect(untarCmd.Run()).To(Succeed())
}
func checkNetsetgo() {
_, err := exec.LookPath("netsetgo")
Expect(err).NotTo(HaveOccurred())
}