2014-05-15 06:21:44 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2014-05-28 08:01:08 +08:00
|
|
|
"fmt"
|
2014-05-15 06:21:44 +08:00
|
|
|
"testing"
|
2014-05-28 08:01:08 +08:00
|
|
|
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/cgroups"
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCpuStats(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("cpu", t)
|
|
|
|
defer helper.cleanup()
|
2014-05-28 08:01:08 +08:00
|
|
|
|
|
|
|
const (
|
|
|
|
kNrPeriods = 2000
|
|
|
|
kNrThrottled = 200
|
|
|
|
kThrottledTime = uint64(18446744073709551615)
|
|
|
|
)
|
|
|
|
|
|
|
|
cpuStatContent := fmt.Sprintf("nr_periods %d\n nr_throttled %d\n throttled_time %d\n",
|
|
|
|
kNrPeriods, kNrThrottled, kThrottledTime)
|
2014-05-15 06:21:44 +08:00
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
"cpu.stat": cpuStatContent,
|
|
|
|
})
|
|
|
|
|
|
|
|
cpu := &cpuGroup{}
|
2014-05-28 08:01:08 +08:00
|
|
|
err := cpu.GetStats(helper.CgroupData, &actualStats)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-05-28 08:01:08 +08:00
|
|
|
expectedStats := cgroups.ThrottlingData{
|
|
|
|
Periods: kNrPeriods,
|
|
|
|
ThrottledPeriods: kNrThrottled,
|
|
|
|
ThrottledTime: kThrottledTime}
|
|
|
|
|
|
|
|
expectThrottlingDataEquals(t, expectedStats, actualStats.CpuStats.ThrottlingData)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoCpuStatFile(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("cpu", t)
|
|
|
|
defer helper.cleanup()
|
|
|
|
|
|
|
|
cpu := &cpuGroup{}
|
2014-05-28 08:01:08 +08:00
|
|
|
err := cpu.GetStats(helper.CgroupData, &actualStats)
|
2014-06-03 02:54:23 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Expected not to fail, but did")
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidCpuStat(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("cpu", t)
|
|
|
|
defer helper.cleanup()
|
|
|
|
cpuStatContent := `nr_periods 2000
|
|
|
|
nr_throttled 200
|
|
|
|
throttled_time fortytwo`
|
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
"cpu.stat": cpuStatContent,
|
|
|
|
})
|
|
|
|
|
|
|
|
cpu := &cpuGroup{}
|
2014-05-28 08:01:08 +08:00
|
|
|
err := cpu.GetStats(helper.CgroupData, &actualStats)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected failed stat parsing.")
|
|
|
|
}
|
|
|
|
}
|