forked from vishvananda/netlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netlink_test.go
216 lines (186 loc) · 4.83 KB
/
netlink_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// +build linux
package netlink
import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"runtime"
"strings"
"testing"
"github.com/vishvananda/netns"
"golang.org/x/sys/unix"
)
type tearDownNetlinkTest func()
func skipUnlessRoot(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("Test requires root privileges.")
}
}
func setUpNetlinkTest(t *testing.T) tearDownNetlinkTest {
skipUnlessRoot(t)
// new temporary namespace so we don't pollute the host
// lock thread since the namespace is thread local
runtime.LockOSThread()
var err error
ns, err := netns.New()
if err != nil {
t.Fatal("Failed to create newns", ns)
}
return func() {
ns.Close()
runtime.UnlockOSThread()
}
}
// setUpNamedNetlinkTest create a temporary named names space with a random name
func setUpNamedNetlinkTest(t *testing.T) (string, tearDownNetlinkTest) {
skipUnlessRoot(t)
origNS, err := netns.Get()
if err != nil {
t.Fatal("Failed saving orig namespace")
}
// create a random name
rnd := make([]byte, 4)
if _, err := rand.Read(rnd); err != nil {
t.Fatal("failed creating random ns name")
}
name := "netlinktest-" + hex.EncodeToString(rnd)
ns, err := netns.NewNamed(name)
if err != nil {
t.Fatal("Failed to create new ns", err)
}
runtime.LockOSThread()
cleanup := func() {
ns.Close()
netns.DeleteNamed(name)
netns.Set(origNS)
runtime.UnlockOSThread()
}
if err := netns.Set(ns); err != nil {
cleanup()
t.Fatal("Failed entering new namespace", err)
}
return name, cleanup
}
func setUpNetlinkTestWithLoopback(t *testing.T) tearDownNetlinkTest {
skipUnlessRoot(t)
runtime.LockOSThread()
ns, err := netns.New()
if err != nil {
t.Fatal("Failed to create new netns", ns)
}
link, err := LinkByName("lo")
if err != nil {
t.Fatalf("Failed to find \"lo\" in new netns: %v", err)
}
if err := LinkSetUp(link); err != nil {
t.Fatalf("Failed to bring up \"lo\" in new netns: %v", err)
}
return func() {
ns.Close()
runtime.UnlockOSThread()
}
}
func setUpF(t *testing.T, path, value string) {
file, err := os.Create(path)
if err != nil {
t.Fatalf("Failed to open %s: %s", path, err)
}
defer file.Close()
file.WriteString(value)
}
func setUpMPLSNetlinkTest(t *testing.T) tearDownNetlinkTest {
if _, err := os.Stat("/proc/sys/net/mpls/platform_labels"); err != nil {
t.Skip("Test requires MPLS support.")
}
f := setUpNetlinkTest(t)
setUpF(t, "/proc/sys/net/mpls/platform_labels", "1024")
setUpF(t, "/proc/sys/net/mpls/conf/lo/input", "1")
return f
}
func setUpSEG6NetlinkTest(t *testing.T) tearDownNetlinkTest {
// check if SEG6 options are enabled in Kernel Config
cmd := exec.Command("uname", "-r")
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
t.Fatal("Failed to run: uname -r")
}
s := []string{"/boot/config-", strings.TrimRight(out.String(), "\n")}
filename := strings.Join(s, "")
grepKey := func(key, fname string) (string, error) {
cmd := exec.Command("grep", key, filename)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run() // "err != nil" if no line matched with grep
return strings.TrimRight(out.String(), "\n"), err
}
key := string("CONFIG_IPV6_SEG6_LWTUNNEL=y")
if _, err := grepKey(key, filename); err != nil {
msg := "Skipped test because it requires SEG6_LWTUNNEL support."
log.Println(msg)
t.Skip(msg)
}
// Add CONFIG_IPV6_SEG6_HMAC to support seg6_hamc
// key := string("CONFIG_IPV6_SEG6_HMAC=y")
return setUpNetlinkTest(t)
}
func setUpNetlinkTestWithKModule(t *testing.T, name string) tearDownNetlinkTest {
file, err := ioutil.ReadFile("/proc/modules")
if err != nil {
t.Fatal("Failed to open /proc/modules", err)
}
found := false
for _, line := range strings.Split(string(file), "\n") {
n := strings.Split(line, " ")[0]
if n == name {
found = true
break
}
}
if !found {
t.Skipf("Test requires kmodule %q.", name)
}
return setUpNetlinkTest(t)
}
func remountSysfs() error {
if err := unix.Mount("", "/", "none", unix.MS_SLAVE|unix.MS_REC, ""); err != nil {
return err
}
if err := unix.Unmount("/sys", unix.MNT_DETACH); err != nil {
return err
}
return unix.Mount("", "/sys", "sysfs", 0, "")
}
func minKernelRequired(t *testing.T, kernel, major int) {
k, m, err := KernelVersion()
if err != nil {
t.Fatal(err)
}
if k < kernel || k == kernel && m < major {
t.Skipf("Host Kernel (%d.%d) does not meet test's minimum required version: (%d.%d)",
k, m, kernel, major)
}
}
func KernelVersion() (kernel, major int, err error) {
uts := unix.Utsname{}
if err = unix.Uname(&uts); err != nil {
return
}
ba := make([]byte, 0, len(uts.Release))
for _, b := range uts.Release {
if b == 0 {
break
}
ba = append(ba, byte(b))
}
var rest string
if n, _ := fmt.Sscanf(string(ba), "%d.%d%s", &kernel, &major, &rest); n < 2 {
err = fmt.Errorf("can't parse kernel version in %q", string(ba))
}
return
}