2014-04-29 00:18:18 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2014-05-28 00:01:08 +00:00
|
|
|
"fmt"
|
2014-04-29 00:18:18 +00:00
|
|
|
"testing"
|
2014-05-28 00:01:08 +00:00
|
|
|
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/cgroups"
|
2014-04-29 00:18:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCpuStats(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("cpu", t)
|
|
|
|
defer helper.cleanup()
|
2014-05-28 00:01:08 +00: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-04-29 00:18:18 +00:00
|
|
|
helper.writeFileContents(map[string]string{
|
|
|
|
"cpu.stat": cpuStatContent,
|
|
|
|
})
|
|
|
|
|
|
|
|
cpu := &cpuGroup{}
|
2014-05-28 00:01:08 +00:00
|
|
|
err := cpu.GetStats(helper.CgroupData, &actualStats)
|
2014-04-29 00:18:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-05-28 00:01:08 +00:00
|
|
|
expectedStats := cgroups.ThrottlingData{
|
|
|
|
Periods: kNrPeriods,
|
|
|
|
ThrottledPeriods: kNrThrottled,
|
|
|
|
ThrottledTime: kThrottledTime}
|
|
|
|
|
|
|
|
expectThrottlingDataEquals(t, expectedStats, actualStats.CpuStats.ThrottlingData)
|
2014-04-29 00:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoCpuStatFile(t *testing.T) {
|
|
|
|
helper := NewCgroupTestUtil("cpu", t)
|
|
|
|
defer helper.cleanup()
|
|
|
|
|
|
|
|
cpu := &cpuGroup{}
|
2014-05-28 00:01:08 +00:00
|
|
|
err := cpu.GetStats(helper.CgroupData, &actualStats)
|
2014-06-02 18:54:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Expected not to fail, but did")
|
2014-04-29 00:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-29 00:32:05 +00: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 00:01:08 +00:00
|
|
|
err := cpu.GetStats(helper.CgroupData, &actualStats)
|
2014-04-29 00:32:05 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected failed stat parsing.")
|
|
|
|
}
|
|
|
|
}
|