Example #1
0
func testNetnsPair(t *testing.T, prefix string) ([]*netlink.Veth, []netns.NsHandle, func()) {
	testns1 := hover.NewNs()
	testns2 := hover.NewNs()

	cleanup := func() {
		testns2.Close()
		testns1.Close()
	}

	l1, err := hover.NewVeth(testns1, prefix+"1", "eth0", "10.10.1.1/24", nil)
	if err != nil {
		cleanup()
		t.Fatal(err)
	}
	l2, err := hover.NewVeth(testns2, prefix+"2", "eth0", "10.10.1.2/24", nil)
	if err != nil {
		cleanup()
		t.Fatal(err)
	}
	return []*netlink.Veth{l1, l2}, []netns.NsHandle{testns1, testns2}, cleanup
}
Example #2
0
func TestModulePolicy(t *testing.T) {
	srv, cleanup := testSetup(t)
	defer cleanup()

	testns1 := hover.NewNs()
	defer testns1.Close()
	testns2 := hover.NewNs()
	defer testns2.Close()

	l1, err := hover.NewVeth(testns1, "ns1", "eth0", "10.10.1.1/24", nil)
	if err != nil {
		t.Error(err)
	}
	defer netlink.LinkDel(l1)
	l2, err := hover.NewVeth(testns2, "ns2", "eth0", "10.10.1.2/24", nil)
	if err != nil {
		t.Error(err)
	}
	defer netlink.LinkDel(l2)

	var t1, t2 api.Module

	// create a redirect bpf/forward module
	testOne(t, testCase{
		url:  srv.URL + "/modules/",
		body: wrapCode(t, redirectC, []string{}),
	}, &t2)
	Info.Printf("Forward module id=%s\n", t2.Id)

	// create a allow and count bpf/policy module
	testOne(t, testCase{
		url:  srv.URL + "/modules/",
		body: wrapCodePolicy(t, policyC, []string{t2.Id}),
	}, &t1)
	Info.Printf("Policy module id=%s\n", t1.Id)

	// populate entries in the redirect bpf table
	testSetTableEntry(t, srv, t2.Id, "redirect", 1, 2)
	testSetTableEntry(t, srv, t2.Id, "redirect", 2, 1)

	// create ns1 <-> t2 <-> ns2
	testLinkModules(t, srv, "i:"+l1.Name, t2.Id)
	testLinkModules(t, srv, t2.Id, "i:"+l2.Name)

	var wg sync.WaitGroup
	wg.Add(1)
	go hover.RunInNs(testns1, func() error {
		defer wg.Done()
		out, err := exec.Command("ping", "-c", "1", "10.10.1.2").Output()
		if err != nil {
			t.Error(string(out), err)
		}
		return nil
	})
	wg.Wait()

	var c1, c2 api.ModuleTableEntry
	testOne(t, testCase{
		url:    srv.URL + "/modules/" + t1.Id + "/tables/counters/entries/0x0",
		method: "GET",
	}, &c1)
	if c1.Key != "0x0" || c1.Value == "0x0" {
		t.Fatalf("Expected counter 1 != 0, got %s", c1.Value)
	}
	testOne(t, testCase{
		url:    srv.URL + "/modules/" + t1.Id + "/tables/counters/entries/0x1",
		method: "GET",
	}, &c2)
	if c2.Key != "0x1" || c2.Value == "0x0" {
		t.Fatalf("Expected counter 1 != 0, got %s", c2.Value)
	}

	// remove policy
	testOne(t, testCase{
		url:    srv.URL + "/modules/" + t1.Id,
		body:   wrapCodePolicy(t, policyC, []string{}),
		method: "PUT",
	}, &t1)
}