Beispiel #1
0
func TestHostStats(t *testing.T) {
	dialer := &websocket.Dialer{}
	headers := http.Header{}

	payload := map[string]interface{}{
		"hostUuid":   "1",
		"resourceId": "1h1",
	}

	token := wsp_utils.CreateTokenWithPayload(payload, privateKey)
	url := "ws://localhost:1111/v1/hoststats?token=" + token
	ws, _, err := dialer.Dial(url, headers)
	if err != nil {
		t.Fatal(err)
	}
	defer ws.Close()

	for count := 0; count < 4; count++ {
		_, msg, err := ws.ReadMessage()
		if err != nil {
			t.Fatal(err)
		}
		stats := string(msg)
		if !strings.Contains(stats, "1h1") {
			t.Fatalf("Stats are not working. Output: [%s]", stats)
		}
	}
}
Beispiel #2
0
func (s *LogsTestSuite) doLogTest(tty bool, prefix string, c *check.C) {
	dialer := &websocket.Dialer{}
	headers := http.Header{}

	createContainerOptions := docker.CreateContainerOptions{
		Name: "logstest",
		Config: &docker.Config{
			Image:     "hello-world",
			OpenStdin: true,
			Tty:       tty,
		},
	}

	newCtr, err := s.client.CreateContainer(createContainerOptions)
	if err != nil {
		c.Fatalf("Error creating container, err : [%v]", err)
	}
	err = s.client.StartContainer(newCtr.ID, nil)
	if err != nil {
		c.Fatalf("Error starting container, err : [%v]", err)
	}
	defer func() {
		s.client.StopContainer(newCtr.ID, 1)
		s.client.RemoveContainer(docker.RemoveContainerOptions{
			ID:            newCtr.ID,
			RemoveVolumes: true,
			Force:         true,
		})
	}()

	payload := map[string]interface{}{
		"hostUuid": "1",
		"logs": map[string]interface{}{
			"Container": newCtr.ID,
			"Follow":    true,
		},
	}

	token := wsp_utils.CreateTokenWithPayload(payload, privateKey)
	url := "ws://localhost:3333/v1/logs/?token=" + token
	ws, _, err := dialer.Dial(url, headers)
	if err != nil {
		c.Fatal(err)
	}
	defer ws.Close()

	for count := 0; count < 20; count++ {
		_, msg, err := ws.ReadMessage()
		if err != nil {
			if err == io.EOF {
				return
			}
			c.Fatal(err)
		}
		msgStr := string(msg)
		if !strings.HasPrefix(msgStr, prefix) {
			c.Fatalf("Message didn't have prefix %s: [%s]", prefix, msgStr)
		}
	}
}
Beispiel #3
0
func (s *ProxyTestSuite) connect(c *check.C) *websocket.Conn {
	dialer := &websocket.Dialer{}
	headers := http.Header{}
	payload := map[string]interface{}{
		"hostUuid": "1",
	}
	token := wsp_utils.CreateTokenWithPayload(payload, s.privateKey)
	url := "ws://localhost:4444/v1/dockersocket/?token=" + token
	ws, _, err := dialer.Dial(url, headers)
	if err != nil {
		c.Fatal(err)
	}
	return ws
}
Beispiel #4
0
// This test wont work in dind. Disabling it for now, until I figure out a solution
func unTestContainerStatSingleContainer(t *testing.T) {
	dialer := &websocket.Dialer{}
	headers := http.Header{}

	c, err := client.NewClient("unix:///var/run/docker.sock")
	if err != nil {
		t.Fatalf("Could not connect to docker, err: [%v]", err)
	}

	ctrs, err := c.ListContainers(client.ListContainersOptions{
		Filters: map[string][]string{
			"image": []string{"google/cadvisor"},
		},
	})
	if err != nil || len(ctrs) == 0 {
		t.Fatalf("Error listing all images, err : [%v]", err)
	}
	payload := map[string]interface{}{
		"hostUuid": "1",
		"containerIds": map[string]string{
			ctrs[0].ID: "1i1",
		},
	}

	log.Info(ctrs[0].ID)

	token := wsp_utils.CreateTokenWithPayload(payload, privateKey)
	url := "ws://localhost:1111/v1/containerstats/" + ctrs[0].ID + "?token=" + token
	ws, _, err := dialer.Dial(url, headers)
	if err != nil {
		t.Fatal(err)
	}
	defer ws.Close()

	for count := 0; count < 4; count++ {
		_, msg, err := ws.ReadMessage()
		if err != nil {
			t.Fatal(err)
		}
		stats := string(msg)
		if !strings.Contains(stats, "1i1") {
			t.Fatalf("Stats are not working. Output: [%s]", stats)
		}
	}
}
Beispiel #5
0
func TestMultiHostStats(t *testing.T) {
	payload := map[string]interface{}{
		"project": []map[string]string{
			{
				"url":   "ws://localhost:1111/v1/hostStats/project",
				"token": test_utils.CreateToken("1", privateKey),
			},
			{
				"url":   "ws://localhost:1111/v1/hostStats/project",
				"token": test_utils.CreateToken("2", privateKey),
			},
		},
	}
	signedToken := test_utils.CreateTokenWithPayload(payload, privateKey)
	ws := getClientConnection("ws://localhost:1111/v1/hostStats/project?token="+signedToken, t)
	one := false
	two := false
	for i := 0; i < 100; i++ {
		err := ws.WriteMessage(1, []byte("x"))
		if err != nil {
			t.Fatal("Error talking to host")
		}
		_, msgBytes, err := ws.ReadMessage()
		if err != nil {
			t.Fatal("Error reading response from various containers")
		}
		if string(msgBytes) == "1" {
			one = true
		}
		if string(msgBytes) == "2" {
			two = true
		}
		if one && two {
			return
		}
	}
	t.Fatal("Did not get container stats from two hosts")
}
Beispiel #6
0
func TestContainerStats(t *testing.T) {
	dialer := &websocket.Dialer{}
	headers := http.Header{}
	c, err := client.NewClient("unix:///var/run/docker.sock")
	if err != nil {
		t.Fatalf("Could not connect to docker, err: [%v]", err)
	}

	createContainerOptions := client.CreateContainerOptions{
		Name: "cadvisortest",
		Config: &client.Config{
			Image: "google/cadvisor",
		},
	}

	newCtr, err := c.CreateContainer(createContainerOptions)
	if err != nil {
		t.Fatalf("Error creating container, err : [%v]", err)
	}
	err = c.StartContainer(newCtr.ID, nil)
	if err != nil {
		t.Fatalf("Error starting container, err : [%v]", err)
	}
	defer func() {
		c.StopContainer(newCtr.ID, 1)
		c.RemoveContainer(client.RemoveContainerOptions{
			ID:            newCtr.ID,
			RemoveVolumes: true,
			Force:         true,
		})
	}()
	newCtr, err = c.InspectContainer(newCtr.ID)
	if err != nil {
		t.Fatalf("Error inspecting container, err : [%v]", err)
	}
	log.Infof("%+v", newCtr)
	allCtrs, err := c.ListContainers(client.ListContainersOptions{})
	if err != nil {
		t.Fatalf("Error listing all images, err : [%v]", err)
	}
	ctrs := []client.APIContainers{}
	for _, ctr := range allCtrs {
		if strings.HasPrefix(ctr.Image, "google/cadvisor") {
			ctrs = append(ctrs, ctr)
		}
	}
	if len(ctrs) != 2 {
		t.Fatalf("Expected 2 containers, but got %v: [%v]", len(ctrs), ctrs)
	}

	cIds := map[string]string{}
	payload := map[string]interface{}{
		"hostUuid":     "1",
		"containerIds": cIds,
	}

	for i, ctr := range ctrs {
		cIds[ctr.ID] = "1i" + strconv.Itoa(i+1)
	}

	log.Infof("%+v", cIds)

	token := wsp_utils.CreateTokenWithPayload(payload, privateKey)
	url := "ws://localhost:1111/v1/containerstats?token=" + token
	ws, _, err := dialer.Dial(url, headers)
	if err != nil {
		t.Fatal(err)
	}
	defer ws.Close()

	for count := 0; count < 4; count++ {
		_, msg, err := ws.ReadMessage()
		if err != nil {
			t.Fatal(err)
		}
		stats := string(msg)
		if !strings.Contains(stats, "1i1") || !strings.Contains(stats, "1i2") {
			t.Fatalf("Stats are not working. Output: [%s]", stats)
		}
	}
}