Merge pull request #36 from jwilder/jw-panic

Fix panics with docker stats
This commit is contained in:
shirou 2015-02-11 18:22:04 +09:00
commit 3907a842a7
2 changed files with 22 additions and 2 deletions

View File

@ -71,7 +71,10 @@ func CgroupCPU(containerid string, base string) (*cpu.CPUTimesStat, error) {
}
path := path.Join(base, containerid, "cpuacct.stat")
lines, _ := common.ReadLines(path)
lines, err := common.ReadLines(path)
if err != nil {
return nil, err
}
// empty containerid means all cgroup
if len(containerid) == 0 {
containerid = "all"
@ -109,7 +112,10 @@ func CgroupMem(containerid string, base string) (*CgroupMemStat, error) {
if len(containerid) == 0 {
containerid = "all"
}
lines, _ := common.ReadLines(path)
lines, err := common.ReadLines(path)
if err != nil {
return nil, err
}
ret := &CgroupMemStat{ContainerID: containerid}
for _, line := range lines {
fields := strings.Split(line, " ")

View File

@ -31,6 +31,13 @@ func TestCgroupCPU(t *testing.T) {
}
}
func TestCgroupCPUInvalidId(t *testing.T) {
_, err := CgroupCPUDocker("bad id")
if err == nil {
t.Error("Expected path does not exist error")
}
}
func TestCgroupMem(t *testing.T) {
v, _ := GetDockerIDList()
for _, id := range v {
@ -44,3 +51,10 @@ func TestCgroupMem(t *testing.T) {
}
}
}
func TestCgroupMemInvalidId(t *testing.T) {
_, err := CgroupMemDocker("bad id")
if err == nil {
t.Error("Expected path does not exist error")
}
}