2014-09-01 03:27:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
2017-05-05 09:02:53 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2014-09-01 03:27:34 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
2017-05-05 09:02:53 +00:00
|
|
|
"strings"
|
2014-09-01 03:27:34 +00:00
|
|
|
"testing"
|
2015-02-10 23:00:36 +00:00
|
|
|
|
|
|
|
"github.com/vishvananda/netns"
|
2014-09-01 03:27:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type tearDownNetlinkTest func()
|
|
|
|
|
2017-02-06 16:14:07 +00:00
|
|
|
func skipUnlessRoot(t *testing.T) {
|
2014-09-01 03:27:34 +00:00
|
|
|
if os.Getuid() != 0 {
|
|
|
|
msg := "Skipped test because it requires root privileges."
|
|
|
|
log.Printf(msg)
|
|
|
|
t.Skip(msg)
|
|
|
|
}
|
2017-02-06 16:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setUpNetlinkTest(t *testing.T) tearDownNetlinkTest {
|
|
|
|
skipUnlessRoot(t)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
}
|
2017-02-19 03:22:09 +00:00
|
|
|
|
|
|
|
func setUpMPLSNetlinkTest(t *testing.T) tearDownNetlinkTest {
|
|
|
|
if _, err := os.Stat("/proc/sys/net/mpls/platform_labels"); err != nil {
|
|
|
|
msg := "Skipped test because it requires MPLS support."
|
|
|
|
log.Printf(msg)
|
|
|
|
t.Skip(msg)
|
|
|
|
}
|
|
|
|
f := setUpNetlinkTest(t)
|
|
|
|
setUpF := func(path, value string) {
|
|
|
|
file, err := os.Create(path)
|
|
|
|
defer file.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to open %s: %s", path, err)
|
|
|
|
}
|
|
|
|
file.WriteString(value)
|
|
|
|
}
|
|
|
|
setUpF("/proc/sys/net/mpls/platform_labels", "1024")
|
|
|
|
setUpF("/proc/sys/net/mpls/conf/lo/input", "1")
|
|
|
|
return f
|
|
|
|
}
|
2017-05-05 09:02:53 +00:00
|
|
|
|
|
|
|
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 {
|
|
|
|
msg := fmt.Sprintf("Skipped test because it requres kmodule %s.", name)
|
|
|
|
log.Println(msg)
|
|
|
|
t.Skip(msg)
|
|
|
|
}
|
|
|
|
return setUpNetlinkTest(t)
|
|
|
|
}
|