Ejemplo n.º 1
0
func TestGetContainerInfoWithDefaultValue(t *testing.T) {
	containers := []string{
		"/c1",
		"/c2",
	}

	var query *info.ContainerInfoRequest
	query = query.FillDefaults()

	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 {
		// nil should give us default values
		cinfo, err := m.GetContainerInfo(container, nil)
		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)
		}
	}

}
Ejemplo n.º 2
0
// Get a container by name.
func (m *manager) GetContainerInfo(containerName string, query *info.ContainerInfoRequest) (*info.ContainerInfo, error) {
	log.Printf("Get(%s); %+v", containerName, query)
	var cont *containerData
	var ok bool
	func() {
		m.containersLock.RLock()
		defer m.containersLock.RUnlock()

		// Ensure we have the container.
		cont, ok = m.containers[containerName]
	}()
	if !ok {
		return nil, fmt.Errorf("unknown container \"%s\"", containerName)
	}

	// Get the info from the container.
	cinfo, err := cont.GetInfo()
	if err != nil {
		return nil, err
	}

	var percentiles *info.ContainerStatsPercentiles
	var samples []*info.ContainerStatsSample
	var stats []*info.ContainerStats
	query = query.FillDefaults()
	percentiles, err = m.storageDriver.Percentiles(
		cinfo.Name,
		query.CpuUsagePercentiles,
		query.MemoryUsagePercentages,
	)
	if err != nil {
		return nil, err
	}
	samples, err = m.storageDriver.Samples(cinfo.Name, query.NumSamples)
	if err != nil {
		return nil, err
	}

	stats, err = m.storageDriver.RecentStats(cinfo.Name, query.NumStats)
	if err != nil {
		return nil, err
	}

	// Make a copy of the info for the user.
	ret := &info.ContainerInfo{
		ContainerReference: info.ContainerReference{
			Name:    cinfo.Name,
			Aliases: cinfo.Aliases,
		},
		Subcontainers:    cinfo.Subcontainers,
		Spec:             cinfo.Spec,
		StatsPercentiles: percentiles,
		Samples:          samples,
		Stats:            stats,
	}

	// Set default value to an actual value
	if ret.Spec.Memory != nil {
		// Memory.Limit is 0 means there's no limit
		if ret.Spec.Memory.Limit == 0 {
			ret.Spec.Memory.Limit = uint64(m.machineInfo.MemoryCapacity)
		}
	}
	return ret, nil
}