-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespace_test.go
100 lines (81 loc) · 1.84 KB
/
namespace_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package namespace
import (
"os/exec"
"syscall"
"testing"
)
func newProcess(m Mask) (*exec.Cmd, error) {
c := exec.Command("sleep", "7200")
c.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: m.Uintptr(),
}
if err := c.Start(); err != nil {
return nil, err
}
return c, nil
}
func TestNewProc(t *testing.T) {
m := NewMask().SetAll()
c, err := newProcess(m)
if err != nil {
t.Fatal(err)
}
defer c.Wait()
ppid := c.Process.Pid
for _, nsType := range Types() {
_, err := FromPID(ppid, nsType)
if err != nil {
t.Fatalf("fail to get %s ns for pid %d", nsType, ppid)
}
}
if err = c.Process.Kill(); err != nil {
t.Fatal("fail to kill process", ppid)
}
}
func TestHierarchical(t *testing.T) {
m := NewMask().SetAll()
c, err := newProcess(m)
if err != nil {
t.Fatal(err)
}
defer c.Wait()
ppid := c.Process.Pid
mh := NewMask().Set(PID).Set(USER)
mnh := NewMask().SetAll().Remove(PID).Remove(USER)
for _, nsType := range Types() {
ns, err := FromPID(ppid, nsType)
if err != nil {
t.Fatalf("fail to get %s ns for pid %d: %v", nsType, ppid, err)
}
prnt := &Namespace{}
if nsType == USER {
prnt, err = ns.OwningUserNS()
} else {
prnt, err = ns.Parent()
}
if mnh.Has(ns.Type()) {
if err == nil {
t.Fatal("ns.Parent should have failed for", ns.Type().String())
}
if err != ErrNonHierarchicalNS {
t.Fatal("error should have been ErrNonHierarchicalNS instead of", err)
}
continue
}
if err != nil {
t.Fatal(nsType.StringLower(), err)
}
if mh.Has(ns.Type()) {
slf, err := Self(ns.Type())
if err != nil {
t.Fatal("could not get Self", ns.Type().StringLower())
}
if slf.Ino() != prnt.Ino() {
t.Fatal("self and parent Ino should have match", slf.Ino(), prnt.Ino())
}
}
}
if err = c.Process.Kill(); err != nil {
t.Fatal("fail to kill process", ppid)
}
}