func TestListOneUpContainer(t *testing.T) { fakeDataSource := func() []byte { response := `[ { "Command": "da command", "Created": 1398153384, "Id": "da id", "Image": "da image", "Names": [ "first name", "second name" ], "Ports": [ { "IP": "0.0.0.0", "PrivatePort": 27017, "PublicPort": 27017, "Type": "tcp" }, { "PublicPort": 28017, "Type": "tcp" } ], "Status": "Up 1 seconds" } ]` return []byte(response) } containers := containers.List(fakeDataSource) assert.NotEmpty(t, containers) assert.Equal(t, len(containers), 1) assert.Equal(t, containers["da id"], []string{"first name", "second name"}) }
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 TestListNoContainers(t *testing.T) { fakeDataSource := func() []byte { return []byte{} } containers := containers.List(fakeDataSource) assert.Empty(t, containers) }