예제 #1
0
func TestBasicPolicy(t *testing.T) {
	hoverServer := hover.NewServer()
	defer hoverServer.Close()
	hover := httptest.NewServer(hoverServer.Handler())
	defer hover.Close()
	upstream := httptest.NewServer(newMockUpstream())
	defer upstream.Close()
	g, err := NewServer(upstream.URL, hover.URL)
	if err != nil {
		t.Fatal(err)
	}
	srv := httptest.NewServer(g.Handler())
	defer srv.Close()
	testValues := []testCase{
		{
			url:  srv.URL + "/policies/",
			body: strings.NewReader(`{"resolved-policy-uri": "` + basicResolvedPolicyUri + `"}`),
			code: http.StatusOK,
		},
	}
	for _, test := range testValues {
		testOne(t, test, nil)
	}
}
예제 #2
0
func TestInterfaces(t *testing.T) {
	hoverServer := hover.NewServer()
	defer hoverServer.Close()
	hover := httptest.NewServer(hoverServer.Handler())
	defer hover.Close()
	upstream := httptest.NewServer(newMockUpstream())
	defer upstream.Close()
	g, err := NewServer(upstream.URL, hover.URL)
	if err != nil {
		t.Fatal(err)
	}
	srv := httptest.NewServer(g.Handler())
	defer srv.Close()

	// launch one policy dataplane
	testOne(t, testCase{
		url:  srv.URL + "/policies/",
		body: strings.NewReader(`{"resolved-policy-uri": "` + basicResolvedPolicyUri + `"}`),
		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:  fmt.Sprintf("%s/modules/host/interfaces/%s/policies/", hover.URL, link1.Attrs().Name),
		body: strings.NewReader(fmt.Sprintf(`{"module": "%s"}`, g.dataplane.Id())),
		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:  fmt.Sprintf("%s/modules/host/interfaces/%s/policies/", hover.URL, link2.Attrs().Name),
		body: strings.NewReader(fmt.Sprintf(`{"module": "%s"}`, g.dataplane.Id())),
		code: http.StatusOK,
	}, nil)

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

	Debug.Println("Endpoints:")
	for endpoint := range g.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())
}