func getJsonData(conf agentConf) map[string]*dataStruct { dockerService := &services.DockerService{Url: conf.DockerUrl} cgroupService := &services.CgroupService{DockerPrefix: conf.CgroupDockerPrefix} containersMap := containers.List(dockerService.List) name2Id := make(map[string]string, 0) data := make(map[string]*dataStruct) for id, names := range containersMap { data[id] = &dataStruct{} data[id].Info = containers.Grab(dockerService.Inspect, id) name2Id[data[id].Info.Name] = id for i := 0; i < len(names); i++ { name := names[i] if strings.LastIndex(name, "/") > 0 { parent := strings.Split(name, "/") data[id].Info.Parents = append(data[id].Info.Parents, parent[1]) } } if data[id].Info.Running { data[id].Memory = memoryMetrics.Grab(cgroupService.GetMemLines, id) data[id].Cpu = cpuMetrics.Grab(cgroupService.GetCpuLines, id) data[id].Network = networkMetrics.Grab(cgroupService.GetNetworkLines, id) } } for _, value := range data { if value.Info.Parents != nil { for i := 0; i < len(value.Info.Parents); i++ { parentId := name2Id[value.Info.Parents[i]] data[parentId].Info.Children = append(data[parentId].Info.Children, value.Info.Name) } } } return data }
func TestMemoryDataParsing(t *testing.T) { fakeDataSource := func(containerId string) []string { return []string{"cache 123", "rss 2345", "swap 45678", "hierarchical_memory_limit 9876"} } result := memoryMetrics.Grab(fakeDataSource, "FOO") assert.NotNil(t, result) assert.Equal(t, result.Cache, 123) assert.Equal(t, result.Rss, 2345) assert.Equal(t, result.Swap, 45678) assert.Equal(t, result.Total, 9876) }
func TestMemoryNoData(t *testing.T) { fakeDataSource := func(containerId string) []string { return []string{} } result := memoryMetrics.Grab(fakeDataSource, "FOO") assert.NotNil(t, result) assert.Equal(t, result.Cache, -1) assert.Equal(t, result.Rss, -1) assert.Equal(t, result.Swap, -1) assert.Equal(t, result.Total, -1) }