func TestHTTPContainerInfoGetterGetRootInfoWithError(t *testing.T) {
	req := &info.ContainerInfoRequest{
		NumStats: 10,
	}
	cinfo := itest.GenerateRandomContainerInfo(
		"dockerIDWhichWillNotBeChecked", // docker ID
		2, // Number of cores
		req,
		1*time.Second,
	)
	testHTTPContainerInfoGetter(req, cinfo, "", "", http.StatusNotFound, t)
}
func TestHTTPContainerInfoGetterGetContainerInfoSuccessfully(t *testing.T) {
	req := &info.ContainerInfoRequest{
		NumStats: 10,
	}
	cinfo := itest.GenerateRandomContainerInfo(
		"dockerIDWhichWillNotBeChecked", // docker ID
		2, // Number of cores
		req,
		1*time.Second,
	)
	testHTTPContainerInfoGetter(req, cinfo, "somePodID", "containerNameInK8S", 0, t)
}
func TestHTTPContainerInfoGetterGetContainerInfoWithError(t *testing.T) {
	req := &info.ContainerInfoRequest{
		NumStats:   10,
		NumSamples: 10,
	}
	req = req.FillDefaults()
	cinfo := itest.GenerateRandomContainerInfo(
		"dockerIDWhichWillNotBeChecked", // docker ID
		2, // Number of cores
		req,
		1*time.Second,
	)
	testHTTPContainerInfoGetter(req, cinfo, "somePodID", "containerNameInK8S", http.StatusNotFound, t)
}
func TestHTTPContainerInfoGetterGetRootInfoSuccessfully(t *testing.T) {
	req := &info.ContainerInfoRequest{
		NumStats:   10,
		NumSamples: 10,
	}
	req = req.FillDefaults()
	cinfo := itest.GenerateRandomContainerInfo(
		"dockerIDWhichWillNotBeChecked", // docker ID
		2, // Number of cores
		req,
		1*time.Second,
	)
	testHTTPContainerInfoGetter(req, cinfo, "", "", 0, t)
}
func TestHTTPContainerInfoGetterGetRootInfoWithError(t *testing.T) {
	req := &info.ContainerInfoRequest{
		NumStats:               10,
		NumSamples:             10,
		CpuUsagePercentiles:    []int{20, 30},
		MemoryUsagePercentiles: []int{40, 50},
	}
	cinfo := itest.GenerateRandomContainerInfo(
		"dockerIDWhichWillNotBeChecked", // docker ID
		2, // Number of cores
		req,
		1*time.Second,
	)
	testHTTPContainerInfoGetter(req, cinfo, "", "", http.StatusNotFound, t)
}
func TestHTTPContainerInfoGetterGetContainerInfoSuccessfully(t *testing.T) {
	req := &info.ContainerInfoRequest{
		NumStats:               10,
		NumSamples:             10,
		CpuUsagePercentiles:    []int{20, 30},
		MemoryUsagePercentiles: []int{40, 50},
	}
	cinfo := itest.GenerateRandomContainerInfo(
		"dockerIDWhichWillNotBeChecked", // docker ID
		2, // Number of cores
		req,
		1*time.Second,
	)
	testHTTPContainerInfoGetter(req, cinfo, "somePodID", "containerNameInK8S", 0, t)
}
Esempio n. 7
0
func TestGetSubcontainersInfo(t *testing.T) {
	query := &info.ContainerInfoRequest{
		NumStats: 3,
	}
	containerName := "/some/container"
	cinfo := itest.GenerateRandomContainerInfo(containerName, 4, query, 1*time.Second)
	cinfo1 := itest.GenerateRandomContainerInfo(path.Join(containerName, "sub1"), 4, query, 1*time.Second)
	cinfo2 := itest.GenerateRandomContainerInfo(path.Join(containerName, "sub2"), 4, query, 1*time.Second)
	response := []info.ContainerInfo{
		*cinfo,
		*cinfo1,
		*cinfo2,
	}
	client, server, err := cadvisorTestClient(fmt.Sprintf("/api/v1.1/subcontainers%v", containerName), query, &info.ContainerInfoRequest{}, response, t)
	if err != nil {
		t.Fatalf("unable to get a client %v", err)
	}
	defer server.Close()
	returned, err := client.SubcontainersInfo(containerName, query)
	if err != nil {
		t.Fatal(err)
	}

	if len(returned) != 3 {
		t.Errorf("unexpected number of results: got %d, expected 3", len(returned))
	}
	if !returned[0].Eq(cinfo) {
		t.Error("received unexpected ContainerInfo")
	}
	if !returned[1].Eq(cinfo1) {
		t.Error("received unexpected ContainerInfo")
	}
	if !returned[2].Eq(cinfo2) {
		t.Error("received unexpected ContainerInfo")
	}
}
Esempio n. 8
0
// Expect a manager with the specified containers and query. Returns the manager, map of ContainerInfo objects,
// and map of MockContainerHandler objects.}
func expectManagerWithContainers(containers []string, query *info.ContainerInfoRequest, t *testing.T) (*manager, map[string]*info.ContainerInfo, map[string]*container.MockContainerHandler) {
	infosMap := make(map[string]*info.ContainerInfo, len(containers))
	handlerMap := make(map[string]*container.MockContainerHandler, len(containers))

	for _, container := range containers {
		infosMap[container] = itest.GenerateRandomContainerInfo(container, 4, query, 1*time.Second)
	}

	driver := &stest.MockStorageDriver{}
	sysfs := &fakesysfs.FakeSysFs{}
	m := createManagerAndAddContainers(
		driver,
		sysfs,
		containers,
		func(h *container.MockContainerHandler) {
			cinfo := infosMap[h.Name]
			stats := cinfo.Stats
			spec := cinfo.Spec
			driver.On(
				"RecentStats",
				h.Name,
				query.NumStats,
			).Return(
				stats,
				nil,
			)

			h.On("ListContainers", container.ListSelf).Return(
				[]info.ContainerReference(nil),
				nil,
			)
			h.On("GetSpec").Return(
				spec,
				nil,
			)
			handlerMap[h.Name] = h
		},
		t,
	)

	return m, infosMap, handlerMap
}
Esempio n. 9
0
func TestGetContainerInfo(t *testing.T) {
	query := &info.ContainerInfoRequest{
		NumStats: 3,
	}
	containerName := "/some/container"
	cinfo := itest.GenerateRandomContainerInfo(containerName, 4, query, 1*time.Second)
	client, server, err := cadvisorTestClient(fmt.Sprintf("/api/v1.0/containers%v", containerName), query, &info.ContainerInfoRequest{}, cinfo, t)
	if err != nil {
		t.Fatalf("unable to get a client %v", err)
	}
	defer server.Close()
	returned, err := client.ContainerInfo(containerName, query)
	if err != nil {
		t.Fatal(err)
	}

	if !returned.Eq(cinfo) {
		t.Error("received unexpected ContainerInfo")
	}
}
Esempio n. 10
0
func TestGetContainerInfo(t *testing.T) {
	containers := []string{
		"/c1",
		"/c2",
	}

	query := &info.ContainerInfoRequest{
		NumStats:               256,
		NumSamples:             128,
		CpuUsagePercentiles:    []int{10, 50, 90},
		MemoryUsagePercentages: []int{10, 80, 90},
	}

	infosMap := make(map[string]*info.ContainerInfo, len(containers))
	handlerMap := make(map[string]*ctest.MockContainerHandler, len(containers))

	for _, container := range containers {
		infosMap[container] = itest.GenerateRandomContainerInfo(container, 4, query, 1*time.Second)
	}

	driver := &stest.MockStorageDriver{}
	m := createManagerAndAddContainers(
		driver,
		containers,
		func(h *ctest.MockContainerHandler) {
			cinfo := infosMap[h.Name]
			stats := cinfo.Stats
			samples := cinfo.Samples
			percentiles := cinfo.StatsPercentiles
			spec := cinfo.Spec
			driver.On(
				"Percentiles",
				h.Name,
				query.CpuUsagePercentiles,
				query.MemoryUsagePercentages,
			).Return(
				percentiles,
				nil,
			)

			driver.On(
				"Samples",
				h.Name,
				query.NumSamples,
			).Return(
				samples,
				nil,
			)

			driver.On(
				"RecentStats",
				h.Name,
				query.NumStats,
			).Return(
				stats,
				nil,
			)

			h.On("ListContainers", container.LIST_SELF).Return(
				[]info.ContainerReference(nil),
				nil,
			)
			h.On("GetSpec").Return(
				spec,
				nil,
			)
			handlerMap[h.Name] = h
		},
		t,
	)

	returnedInfos := make(map[string]*info.ContainerInfo, len(containers))

	for _, container := range containers {
		cinfo, err := m.GetContainerInfo(container, query)
		if err != nil {
			t.Fatalf("Unable to get info for container %v: %v", container, err)
		}
		returnedInfos[container] = cinfo
	}

	for container, handler := range handlerMap {
		handler.AssertExpectations(t)
		returned := returnedInfos[container]
		expected := infosMap[container]
		if !reflect.DeepEqual(returned, expected) {
			t.Errorf("returned unexpected info for container %v; returned %+v; expected %+v", container, returned, expected)
		}
	}

}