Esempio n. 1
0
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 TestNetwordNoData(t *testing.T) {
	fakeDataSource := func(containerId string) map[string]uint64 {
		fakeResult := make(map[string]uint64, 0)
		return fakeResult
	}

	result := networkMetrics.Grab(fakeDataSource, "FOO")

	assert.Equal(t, result.RxOk, uint64(0))
	assert.Equal(t, result.RxErr, uint64(0))
	assert.Equal(t, result.TxOk, uint64(0))
	assert.Equal(t, result.TxErr, uint64(0))

}
func TestNetworkDataParsing(t *testing.T) {
	fakeDataSource := func(containerId string) map[string]uint64 {
		fakeResult := make(map[string]uint64, 0)
		fakeResult["rx-ok"] = uint64(123)
		fakeResult["rx-err"] = uint64(34)
		fakeResult["tx-ok"] = uint64(4567)
		fakeResult["tx-err"] = uint64(78)
		return fakeResult
	}

	result := networkMetrics.Grab(fakeDataSource, "FOO")

	assert.Equal(t, result.RxOk, uint64(123))
	assert.Equal(t, result.RxErr, uint64(34))
	assert.Equal(t, result.TxOk, uint64(4567))
	assert.Equal(t, result.TxErr, uint64(78))
}