2014-05-15 06:21:44 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2014-05-28 08:01:08 +08:00
|
|
|
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer/cgroups"
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
memoryStatContents = `cache 512
|
|
|
|
rss 1024`
|
|
|
|
memoryUsageContents = "2048\n"
|
|
|
|
memoryMaxUsageContents = "4096\n"
|
2014-06-04 14:41:03 +08:00
|
|
|
memoryFailcnt = "100\n"
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMemoryStats(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("memory", t)
|
|
|
|
defer helper.cleanup()
|
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
"memory.stat": memoryStatContents,
|
|
|
|
"memory.usage_in_bytes": memoryUsageContents,
|
|
|
|
"memory.max_usage_in_bytes": memoryMaxUsageContents,
|
2014-06-04 14:41:03 +08:00
|
|
|
"memory.failcnt": memoryFailcnt,
|
2014-05-15 06:21:44 +08:00
|
|
|
})
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
memory := &MemoryGroup{}
|
2014-08-27 19:42:38 +08:00
|
|
|
actualStats := *cgroups.NewStats()
|
2014-06-20 21:13:56 +08:00
|
|
|
err := memory.GetStats(helper.CgroupPath, &actualStats)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-06-04 14:41:03 +08:00
|
|
|
expectedStats := cgroups.MemoryStats{Usage: 2048, MaxUsage: 4096, Failcnt: 100, Stats: map[string]uint64{"cache": 512, "rss": 1024}}
|
2014-05-28 08:01:08 +08:00
|
|
|
expectMemoryStatEquals(t, expectedStats, actualStats.MemoryStats)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryStatsNoStatFile(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("memory", t)
|
|
|
|
defer helper.cleanup()
|
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
"memory.usage_in_bytes": memoryUsageContents,
|
|
|
|
"memory.max_usage_in_bytes": memoryMaxUsageContents,
|
|
|
|
})
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
memory := &MemoryGroup{}
|
2014-08-27 19:42:38 +08:00
|
|
|
actualStats := *cgroups.NewStats()
|
2014-06-20 21:13:56 +08:00
|
|
|
err := memory.GetStats(helper.CgroupPath, &actualStats)
|
2014-07-09 05:25:55 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryStatsNoUsageFile(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("memory", t)
|
|
|
|
defer helper.cleanup()
|
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
"memory.stat": memoryStatContents,
|
|
|
|
"memory.max_usage_in_bytes": memoryMaxUsageContents,
|
|
|
|
})
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
memory := &MemoryGroup{}
|
2014-08-27 19:42:38 +08:00
|
|
|
actualStats := *cgroups.NewStats()
|
2014-06-20 21:13:56 +08:00
|
|
|
err := memory.GetStats(helper.CgroupPath, &actualStats)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryStatsNoMaxUsageFile(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("memory", t)
|
|
|
|
defer helper.cleanup()
|
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
"memory.stat": memoryStatContents,
|
|
|
|
"memory.usage_in_bytes": memoryUsageContents,
|
|
|
|
})
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
memory := &MemoryGroup{}
|
2014-08-27 19:42:38 +08:00
|
|
|
actualStats := *cgroups.NewStats()
|
2014-06-20 21:13:56 +08:00
|
|
|
err := memory.GetStats(helper.CgroupPath, &actualStats)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryStatsBadStatFile(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("memory", t)
|
|
|
|
defer helper.cleanup()
|
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
"memory.stat": "rss rss",
|
|
|
|
"memory.usage_in_bytes": memoryUsageContents,
|
|
|
|
"memory.max_usage_in_bytes": memoryMaxUsageContents,
|
|
|
|
})
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
memory := &MemoryGroup{}
|
2014-08-27 19:42:38 +08:00
|
|
|
actualStats := *cgroups.NewStats()
|
2014-06-20 21:13:56 +08:00
|
|
|
err := memory.GetStats(helper.CgroupPath, &actualStats)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryStatsBadUsageFile(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("memory", t)
|
|
|
|
defer helper.cleanup()
|
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
"memory.stat": memoryStatContents,
|
|
|
|
"memory.usage_in_bytes": "bad",
|
|
|
|
"memory.max_usage_in_bytes": memoryMaxUsageContents,
|
|
|
|
})
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
memory := &MemoryGroup{}
|
2014-08-27 19:42:38 +08:00
|
|
|
actualStats := *cgroups.NewStats()
|
2014-06-20 21:13:56 +08:00
|
|
|
err := memory.GetStats(helper.CgroupPath, &actualStats)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryStatsBadMaxUsageFile(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("memory", t)
|
|
|
|
defer helper.cleanup()
|
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
"memory.stat": memoryStatContents,
|
|
|
|
"memory.usage_in_bytes": memoryUsageContents,
|
|
|
|
"memory.max_usage_in_bytes": "bad",
|
|
|
|
})
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
memory := &MemoryGroup{}
|
2014-08-27 19:42:38 +08:00
|
|
|
actualStats := *cgroups.NewStats()
|
2014-06-20 21:13:56 +08:00
|
|
|
err := memory.GetStats(helper.CgroupPath, &actualStats)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected failure")
|
|
|
|
}
|
|
|
|
}
|