cpuacct_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package cgroup
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. const cpuacctPath = "testdata/docker/sys/fs/cgroup/cpuacct/docker/b29faf21b7eff959f64b4192c34d5d67a707fe8561e9eaa608cb27693fba4242"
  8. func TestCPUAccountingStats(t *testing.T) {
  9. cpuacct := CPUAccountingSubsystem{}
  10. if err := cpuacctStat(cpuacctPath, &cpuacct); err != nil {
  11. t.Fatal(err)
  12. }
  13. assert.Equal(t, uint64(61950000000), cpuacct.Stats.UserNanos)
  14. assert.Equal(t, uint64(7730000000), cpuacct.Stats.SystemNanos)
  15. }
  16. func TestCpuacctUsage(t *testing.T) {
  17. cpuacct := CPUAccountingSubsystem{}
  18. if err := cpuacctUsage(cpuacctPath, &cpuacct); err != nil {
  19. t.Fatal(err)
  20. }
  21. assert.Equal(t, uint64(95996653175), cpuacct.TotalNanos)
  22. }
  23. func TestCpuacctUsagePerCPU(t *testing.T) {
  24. cpuacct := CPUAccountingSubsystem{}
  25. if err := cpuacctUsagePerCPU(cpuacctPath, &cpuacct); err != nil {
  26. t.Fatal(err)
  27. }
  28. assert.Equal(t, []uint64{26571825468, 23185259690, 24300973729, 21937433730}, cpuacct.UsagePerCPU)
  29. }
  30. func TestCPUAccountingSubsystem_Get(t *testing.T) {
  31. cpuacct := CPUAccountingSubsystem{}
  32. if err := cpuacct.get(cpuacctPath); err != nil {
  33. t.Fatal(err)
  34. }
  35. assert.Equal(t, uint64(61950000000), cpuacct.Stats.UserNanos)
  36. assert.Equal(t, uint64(95996653175), cpuacct.TotalNanos)
  37. assert.Len(t, cpuacct.UsagePerCPU, 4)
  38. }
  39. func TestCPUAccountingSubsystemJSON(t *testing.T) {
  40. cpuacct := CPUAccountingSubsystem{}
  41. if err := cpuacct.get(cpuacctPath); err != nil {
  42. t.Fatal(err)
  43. }
  44. json, err := json.MarshalIndent(cpuacct, "", " ")
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. t.Log(string(json))
  49. }