예제 #1
0
func TestRootInfo(t *testing.T) {
	fw := newServerTest()
	expectedInfo := &cadvisorApi.ContainerInfo{
		ContainerReference: cadvisorApi.ContainerReference{
			Name: "/",
		},
	}
	fw.fakeKubelet.rawInfoFunc = func(req *cadvisorApi.ContainerInfoRequest) (map[string]*cadvisorApi.ContainerInfo, error) {
		return map[string]*cadvisorApi.ContainerInfo{
			expectedInfo.Name: expectedInfo,
		}, nil
	}

	resp, err := http.Get(fw.testHTTPServer.URL + "/stats")
	if err != nil {
		t.Fatalf("Got error GETing: %v", err)
	}
	defer resp.Body.Close()
	var receivedInfo cadvisorApi.ContainerInfo
	err = json.NewDecoder(resp.Body).Decode(&receivedInfo)
	if err != nil {
		t.Fatalf("received invalid json data: %v", err)
	}
	if !receivedInfo.Eq(expectedInfo) {
		t.Errorf("received wrong data: %#v, expected %#v", receivedInfo, expectedInfo)
	}
}
예제 #2
0
func TestContainerInfoWithUidNamespace(t *testing.T) {
	fw := newServerTest()
	expectedInfo := &cadvisorApi.ContainerInfo{}
	podID := "somepod"
	expectedNamespace := "custom"
	expectedPodID := getPodName(podID, expectedNamespace)
	expectedContainerName := "goodcontainer"
	expectedUid := "9b01b80f-8fb4-11e4-95ab-4200af06647"
	fw.fakeKubelet.containerInfoFunc = func(podID string, uid types.UID, containerName string, req *cadvisorApi.ContainerInfoRequest) (*cadvisorApi.ContainerInfo, error) {
		if podID != expectedPodID || string(uid) != expectedUid || containerName != expectedContainerName {
			return nil, fmt.Errorf("bad podID or uid or containerName: podID=%v; uid=%v; containerName=%v", podID, uid, containerName)
		}
		return expectedInfo, nil
	}

	resp, err := http.Get(fw.testHTTPServer.URL + fmt.Sprintf("/stats/%v/%v/%v/%v", expectedNamespace, podID, expectedUid, expectedContainerName))
	if err != nil {
		t.Fatalf("Got error GETing: %v", err)
	}
	defer resp.Body.Close()
	var receivedInfo cadvisorApi.ContainerInfo
	err = json.NewDecoder(resp.Body).Decode(&receivedInfo)
	if err != nil {
		t.Fatalf("received invalid json data: %v", err)
	}
	if !receivedInfo.Eq(expectedInfo) {
		t.Errorf("received wrong data: %#v", receivedInfo)
	}
}
예제 #3
0
func (self *KubeletClient) parseStat(containerInfo *cadvisor.ContainerInfo) *cadvisor.ContainerInfo {
	containerInfo.Stats = sampleContainerStats(containerInfo.Stats)
	if len(containerInfo.Aliases) > 0 {
		containerInfo.Name = containerInfo.Aliases[0]
	}
	return containerInfo
}
예제 #4
0
func testHTTPContainerInfoGetter(
	req *cadvisorApi.ContainerInfoRequest,
	cinfo *cadvisorApi.ContainerInfo,
	podID string,
	containerID string,
	status int,
	t *testing.T,
) {
	expectedPath := "/stats"
	if len(podID) > 0 && len(containerID) > 0 {
		expectedPath = path.Join(expectedPath, podID, containerID)
	}
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if status != 0 {
			w.WriteHeader(status)
		}
		if strings.TrimRight(r.URL.Path, "/") != strings.TrimRight(expectedPath, "/") {
			t.Fatalf("Received request to an invalid path. Should be %v. got %v",
				expectedPath, r.URL.Path)
		}

		var receivedReq cadvisorApi.ContainerInfoRequest
		err := json.NewDecoder(r.Body).Decode(&receivedReq)
		if err != nil {
			t.Fatal(err)
		}
		// Note: This will not make a deep copy of req.
		// So changing req after Get*Info would be a race.
		expectedReq := req
		// Fill any empty fields with default value
		if !expectedReq.Equals(receivedReq) {
			t.Errorf("received wrong request")
		}
		err = json.NewEncoder(w).Encode(cinfo)
		if err != nil {
			t.Fatal(err)
		}
	}))
	defer ts.Close()
	hostURL, err := url.Parse(ts.URL)
	if err != nil {
		t.Fatal(err)
	}
	parts := strings.Split(hostURL.Host, ":")

	port, err := strconv.Atoi(parts[1])
	if err != nil {
		t.Fatal(err)
	}

	containerInfoGetter := &HTTPContainerInfoGetter{
		Client: http.DefaultClient,
		Port:   port,
	}

	var receivedContainerInfo *cadvisorApi.ContainerInfo
	if len(podID) > 0 && len(containerID) > 0 {
		receivedContainerInfo, err = containerInfoGetter.GetContainerInfo(parts[0], podID, containerID, req)
	} else {
		receivedContainerInfo, err = containerInfoGetter.GetRootInfo(parts[0], req)
	}
	if status == 0 || status == http.StatusOK {
		if err != nil {
			t.Errorf("received unexpected error: %v", err)
		}

		if !receivedContainerInfo.Eq(cinfo) {
			t.Error("received unexpected container info")
		}
	} else {
		if err == nil {
			t.Error("did not receive expected error.")
		}
	}
}