forked from vishvananda/netlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netns_test.go
79 lines (66 loc) · 2.04 KB
/
netns_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
// +build linux
package netlink
import (
"os"
"runtime"
"syscall"
"testing"
"github.com/vishvananda/netns"
)
// TestNetNsIdByFd tests setting and getting the network namespace ID
// by file descriptor. It opens a namespace fd, sets it to a random id,
// then retrieves the ID.
// This does not do any namespace switching.
func TestNetNsIdByFd(t *testing.T) {
skipUnlessRoot(t)
// create a network namespace
ns, err := netns.New()
CheckErrorFail(t, err)
// set its ID
// In an attempt to avoid namespace id collisions, set this to something
// insanely high. When the kernel assigns IDs, it does so starting from 0
// So, just use our pid shifted up 16 bits
wantID := os.Getpid() << 16
h, err := NewHandle()
CheckErrorFail(t, err)
err = h.SetNetNsIdByFd(int(ns), wantID)
CheckErrorFail(t, err)
// Get the ID back, make sure it matches
haveID, _ := h.GetNetNsIdByFd(int(ns))
if haveID != wantID {
t.Errorf("GetNetNsIdByFd returned %d, want %d", haveID, wantID)
}
ns.Close()
}
// TestNetNsIdByPid tests manipulating namespace IDs by pid (really, task / thread id)
// Does the same as TestNetNsIdByFd, but we need to change namespaces so we
// actually have a pid in that namespace
func TestNetNsIdByPid(t *testing.T) {
skipUnlessRoot(t)
runtime.LockOSThread() // we need a constant OS thread
origNs, _ := netns.Get()
// create and enter a new netns
ns, err := netns.New()
CheckErrorFail(t, err)
err = netns.Set(ns)
CheckErrorFail(t, err)
// make sure we go back to the original namespace when done
defer func() {
err := netns.Set(origNs)
if err != nil {
panic("failed to restore network ns, bailing")
}
runtime.UnlockOSThread()
}()
// As above, we'll pick a crazy large netnsid to avoid collisions
wantID := syscall.Gettid() << 16
h, err := NewHandle()
CheckErrorFail(t, err)
err = h.SetNetNsIdByPid(syscall.Gettid(), wantID)
CheckErrorFail(t, err)
//Get the ID and see if it worked
haveID, _ := h.GetNetNsIdByPid(syscall.Gettid())
if haveID != wantID {
t.Errorf("GetNetNsIdByPid returned %d, want %d", haveID, wantID)
}
}