Don't ignore err when getting CgroupMemDocker stats

Fixes panic: runtime error: index out of range

goroutine 10 [running]:
testing.func·006()
	/usr/local/go/src/testing/testing.go:441 +0x181
github.com/shirou/gopsutil/docker.CgroupMem(0x586b30, 0x6, 0x5a87d0, 0x1c, 0x0, 0x0, 0x0)
	/home/jwilder/go/src/github.com/shirou/gopsutil/docker/docker_linux.go:119 +0xf48
github.com/shirou/gopsutil/docker.CgroupMemDocker(0x586b30, 0x6, 0x0, 0x0, 0x0)
	/home/jwilder/go/src/github.com/shirou/gopsutil/docker/docker_linux.go:184 +0x57

If the ID passed to the CGroupMemDocker does not exist, you can get
a panic at runtime.  This can happen when a container exits before
calling the func.
pull/36/head
Jason Wilder 10 years ago
parent d6802457a3
commit 812b04d363

@ -109,7 +109,10 @@ func CgroupMem(containerid string, base string) (*CgroupMemStat, error) {
if len(containerid) == 0 { if len(containerid) == 0 {
containerid = "all" containerid = "all"
} }
lines, _ := common.ReadLines(path) lines, err := common.ReadLines(path)
if err != nil {
return nil, err
}
ret := &CgroupMemStat{ContainerID: containerid} ret := &CgroupMemStat{ContainerID: containerid}
for _, line := range lines { for _, line := range lines {
fields := strings.Split(line, " ") fields := strings.Split(line, " ")

@ -44,3 +44,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")
}
}

Loading…
Cancel
Save