Esempio n. 1
0
func TestBasicPolicy(t *testing.T) {
	hive := httptest.NewServer(hive.NewServer())
	defer hive.Close()
	if err := dataplane.Init(hive.URL); err != nil {
		t.Fatal(err)
	}
	upstream := httptest.NewServer(newMockUpstream())
	defer upstream.Close()
	srv := httptest.NewServer(NewServer(upstream.URL))
	defer srv.Close()
	testValues := []testCase{
		{
			url:  srv.URL + "/policies/",
			body: strings.NewReader(testPolicy),
			code: http.StatusOK,
		},
	}
	for _, test := range testValues {
		testOne(t, test, nil)
	}
}
Esempio n. 2
0
func TestInterfaces(t *testing.T) {
	hive := httptest.NewServer(hive.NewServer())
	defer hive.Close()
	if err := dataplane.Init(hive.URL); err != nil {
		t.Fatal(err)
	}
	upstream := httptest.NewServer(newMockUpstream())
	defer upstream.Close()
	srv := httptest.NewServer(NewServer(upstream.URL))
	defer srv.Close()

	// launch one policy dataplane
	testOne(t, testCase{
		url:  srv.URL + "/policies/",
		body: strings.NewReader(testPolicy),
		code: http.StatusOK,
	}, nil)

	// set up docker prereqs
	runTestCommand(t, "bash -ex", strings.NewReader(dockerSetup))

	// monitor for netlink updates to find docker veth
	ch, done := make(chan netlink.LinkUpdate), make(chan struct{})
	defer close(done)
	if err := netlink.LinkSubscribe(ch, done); err != nil {
		t.Error(err)
	}

	id1, id2 := "TestInterfaces-1", "TestInterfaces-2"
	// spawn one test server process
	runTestCommand(t, "docker run --name="+id1+" -d moutten/iperf", nil)
	defer runCommand("docker rm -f "+id1, nil)

	link1 := gatherOneLink(t, ch)
	testOne(t, testCase{
		url: hive.URL + "/links/",
		body: strings.NewReader(fmt.Sprintf(
			`{"modules": ["host", "%s"], "interfaces": ["%s", ""]}`,
			dataplane.Id(), link1.Attrs().Name)),
		code: http.StatusOK,
	}, nil)

	// find the ip of the test server
	ip1 := runTestCommand(t, "docker inspect -f {{.NetworkSettings.IPAddress}} "+id1, nil)

	// start a test client
	clientCmd, clientStdin, clientOutput := startCommand(t, "docker run -i --name="+id2+" --entrypoint /bin/sh moutten/iperf -ex")
	defer runCommand("docker rm -f "+id2, nil)

	link2 := gatherOneLink(t, ch)

	ip2 := runTestCommand(t, "docker inspect -f {{.NetworkSettings.IPAddress}} "+id2, nil)
	Debug.Printf("ip2=%s\n", ip2)

	testOne(t, testCase{
		url: hive.URL + "/links/",
		body: strings.NewReader(fmt.Sprintf(
			`{"modules": ["host", "%s"], "interfaces": ["%s", ""]}`,
			dataplane.Id(), link2.Attrs().Name)),
		code: http.StatusOK,
	}, nil)

	if err := dataplane.AddEndpoint(ip1, "pepsi", "web"); err != nil {
		t.Fatal(err)
	}
	if err := dataplane.AddEndpoint(ip2, "pepsi", "client"); err != nil {
		t.Fatal(err)
	}

	Debug.Println("Endpoints:")
	for endpoint := range dataplane.Endpoints() {
		Debug.Printf("%v\n", *endpoint)
	}

	clientStdin.Write([]byte("iperf -t 2 -c " + ip1 + "\n"))
	clientStdin.Close()
	if err := clientCmd.Wait(); err != nil {
		Error.Print(clientOutput.String())
		t.Fatal(err)
	}
	Debug.Print(clientOutput.String())
}