Example #1
0
func TestMemoryService_GetMemoryStats(t *testing.T) {

	//Container  + dockerstats
	containerID := "containerID"
	labels := map[string]string{
		"label1": "val1",
		"label2": "val2",
	}
	container := dc.APIContainers{
		ID:         containerID,
		Image:      "image",
		Command:    "command",
		Created:    123789,
		Status:     "Up",
		Ports:      []dc.APIPort{{PrivatePort: 1234, PublicPort: 4567, Type: "portType", IP: "123.456.879.1"}},
		SizeRw:     123,
		SizeRootFs: 456,
		Names:      []string{"/name1", "name1/fake"},
		Labels:     labels,
		Networks:   dc.NetworkList{},
	}
	memoryService := &MemoryService{}
	memorystats := getMemoryStats(time.Now(), 1)

	memoryRawStats := docker.DockerStat{}
	memoryRawStats.Container = container
	memoryRawStats.Stats = memorystats

	expectedEvent := common.MapStr{
		"_module": common.MapStr{
			"container": common.MapStr{
				"id":     containerID,
				"name":   "name1",
				"socket": docker.GetSocket(),
				"labels": docker.BuildLabelArray(labels),
			},
		},
		"fail": common.MapStr{
			"count": memorystats.MemoryStats.Failcnt,
		},
		"limit": memorystats.MemoryStats.Limit,
		"rss": common.MapStr{
			"total": memorystats.MemoryStats.Stats.TotalRss,
			"pct":   float64(memorystats.MemoryStats.Stats.TotalRss) / float64(memorystats.MemoryStats.Limit),
		},
		"usage": common.MapStr{
			"total": memorystats.MemoryStats.Usage,
			"pct":   float64(memorystats.MemoryStats.Usage) / float64(memorystats.MemoryStats.Limit),
			"max":   memorystats.MemoryStats.MaxUsage,
		},
	}
	//WHEN
	rawStats := memoryService.GetMemoryStats(memoryRawStats)
	event := eventMapping(&rawStats)
	//THEN
	assert.True(t, equalEvent(expectedEvent, event))
	t.Logf(" expected : %v", expectedEvent)
	t.Logf(" returned : %v", event)
}
Example #2
0
func TestCPUService_GetCpuStats(t *testing.T) {
	// GIVEN
	containerID := "containerID"
	labels := map[string]string{
		"label1": "val1",
		"label2": "val2",
	}
	container := dc.APIContainers{
		ID:         containerID,
		Image:      "image",
		Command:    "command",
		Created:    123789,
		Status:     "Up",
		Ports:      []dc.APIPort{{PrivatePort: 1234, PublicPort: 4567, Type: "portType", IP: "123.456.879.1"}},
		SizeRw:     123,
		SizeRootFs: 456,
		Names:      []string{"/name1", "name1/fake"},
		Labels:     labels,
		Networks:   dc.NetworkList{},
	}

	preCPUStats := getCPUStats([]uint64{1, 9, 9, 5}, []uint64{0, 50, 0})
	CPUStats := getCPUStats([]uint64{100000001, 900000009, 900000009, 500000005}, []uint64{500000000, 500000050, 500000000})

	//CPU stats
	stats := dc.Stats{}
	stats.Read = time.Now()
	stats.CPUStats = CPUStats
	stats.PreCPUStats = preCPUStats

	cpuStatsStruct := docker.DockerStat{}
	cpuStatsStruct.Container = container
	cpuStatsStruct.Stats = stats

	mockedCPUCalculator := getMockedCPUCalcul(1.0)
	// expected events : The generated event should be equal to the expected event
	expectedEvent := common.MapStr{
		"_module": common.MapStr{
			"container": common.MapStr{
				"id":     containerID,
				"name":   "name1",
				"socket": docker.GetSocket(),
				"labels": docker.BuildLabelArray(labels),
			},
		},
		"usage": common.MapStr{
			"per_cpu":     mockedCPUCalculator.PerCpuUsage(&stats),
			"total":       mockedCPUCalculator.TotalUsage(&stats),
			"kernel_mode": mockedCPUCalculator.UsageInKernelmode(&stats),
			"user_mode":   mockedCPUCalculator.UsageInUsermode(&stats),
		},
	}

	cpuData := cpuService.getCpuStats(&cpuStatsStruct)
	event := eventMapping(&cpuData)
	//THEN
	assert.True(t, equalEvent(expectedEvent, event))
}