Beispiel #1
0
// Each test that calls setUp runs in it's own netns
func setUp(t *testing.T) tearDown {
	// 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)
	}

	netlink.LinkAdd(&netlink.Dummy{netlink.LinkAttrs{Name: testIface}})

	return func() {
		ns.Close()
		runtime.UnlockOSThread()
	}
}
Beispiel #2
0
func setUpNetlinkTest(t *testing.T) tearDownNetlinkTest {
	if os.Getuid() != 0 {
		msg := "Skipped test because it requires root privileges."
		log.Printf(msg)
		t.Skip(msg)
	}

	// 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()
	}
}
Beispiel #3
0
func TestSetInterfaceInNamespaceFd(t *testing.T) {
	teardown := setUp(t)
	defer teardown()

	newns, err := netns.New()
	if err != nil {
		t.Fatal("Failed to create newns")
	}
	defer newns.Close()

	netlink.LinkAdd(&netlink.Dummy{netlink.LinkAttrs{Name: "foo2"}})

	err = SetInterfaceInNamespaceFd("foo2", uintptr(newns))
	if err != nil {
		t.Fatal(err)
	}

	_, err = netlink.LinkByName("bar")
	if err == nil {
		t.Fatal("Link foo2 is still in newns")
	}
}
Beispiel #4
0
func TestLinkSetNs(t *testing.T) {
	tearDown := setUpNetlinkTest(t)
	defer tearDown()

	basens, err := netns.Get()
	if err != nil {
		t.Fatal("Failed to get basens")
	}
	defer basens.Close()

	newns, err := netns.New()
	if err != nil {
		t.Fatal("Failed to create newns")
	}
	defer newns.Close()

	link := &Veth{LinkAttrs{Name: "foo"}, "bar"}
	if err := LinkAdd(link); err != nil {
		t.Fatal(err)
	}

	peer, err := LinkByName("bar")
	if err != nil {
		t.Fatal(err)
	}

	LinkSetNsFd(peer, int(basens))
	if err != nil {
		t.Fatal("Failed to set newns for link")
	}

	_, err = LinkByName("bar")
	if err == nil {
		t.Fatal("Link bar is still in newns")
	}

	err = netns.Set(basens)
	if err != nil {
		t.Fatal("Failed to set basens")
	}

	peer, err = LinkByName("bar")
	if err != nil {
		t.Fatal("Link is not in basens")
	}

	if err := LinkDel(peer); err != nil {
		t.Fatal(err)
	}

	err = netns.Set(newns)
	if err != nil {
		t.Fatal("Failed to set newns")
	}

	_, err = LinkByName("foo")
	if err == nil {
		t.Fatal("Other half of veth pair not deleted")
	}

}