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 TestCpuDataParsing(t *testing.T) { fakeDataSource := func(containerId string) []string { return []string{"user 24", "system 35"} } result := cpuMetrics.Grab(fakeDataSource, "FOO") assert.NotNil(t, result) assert.Equal(t, result.User, 24) assert.Equal(t, result.System, 35) }
func TestCpuNoData(t *testing.T) { fakeDataSource := func(containerId string) []string { return []string{} } result := cpuMetrics.Grab(fakeDataSource, "FOO") assert.NotNil(t, result) assert.Equal(t, result.User, -1) assert.Equal(t, result.System, -1) }