cpu_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package fs
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/dotcloud/docker/pkg/libcontainer/cgroups"
  6. )
  7. func TestCpuStats(t *testing.T) {
  8. helper := NewCgroupTestUtil("cpu", t)
  9. defer helper.cleanup()
  10. const (
  11. kNrPeriods = 2000
  12. kNrThrottled = 200
  13. kThrottledTime = uint64(18446744073709551615)
  14. )
  15. cpuStatContent := fmt.Sprintf("nr_periods %d\n nr_throttled %d\n throttled_time %d\n",
  16. kNrPeriods, kNrThrottled, kThrottledTime)
  17. helper.writeFileContents(map[string]string{
  18. "cpu.stat": cpuStatContent,
  19. })
  20. cpu := &cpuGroup{}
  21. err := cpu.GetStats(helper.CgroupData, &actualStats)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. expectedStats := cgroups.ThrottlingData{
  26. Periods: kNrPeriods,
  27. ThrottledPeriods: kNrThrottled,
  28. ThrottledTime: kThrottledTime}
  29. expectThrottlingDataEquals(t, expectedStats, actualStats.CpuStats.ThrottlingData)
  30. }
  31. func TestNoCpuStatFile(t *testing.T) {
  32. helper := NewCgroupTestUtil("cpu", t)
  33. defer helper.cleanup()
  34. cpu := &cpuGroup{}
  35. err := cpu.GetStats(helper.CgroupData, &actualStats)
  36. if err != nil {
  37. t.Fatal("Expected not to fail, but did")
  38. }
  39. }
  40. func TestInvalidCpuStat(t *testing.T) {
  41. helper := NewCgroupTestUtil("cpu", t)
  42. defer helper.cleanup()
  43. cpuStatContent := `nr_periods 2000
  44. nr_throttled 200
  45. throttled_time fortytwo`
  46. helper.writeFileContents(map[string]string{
  47. "cpu.stat": cpuStatContent,
  48. })
  49. cpu := &cpuGroup{}
  50. err := cpu.GetStats(helper.CgroupData, &actualStats)
  51. if err == nil {
  52. t.Fatal("Expected failed stat parsing.")
  53. }
  54. }