memory_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package cgroup
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. const memoryPath = "testdata/docker/sys/fs/cgroup/memory/docker/b29faf21b7eff959f64b4192c34d5d67a707fe8561e9eaa608cb27693fba4242"
  8. func TestMemoryStat(t *testing.T) {
  9. mem := MemorySubsystem{}
  10. if err := memoryStats(memoryPath, &mem); err != nil {
  11. t.Fatal(err)
  12. }
  13. assert.Equal(t, uint64(65101824), mem.Stats.Cache)
  14. assert.Equal(t, uint64(230662144), mem.Stats.RSS)
  15. assert.Equal(t, uint64(174063616), mem.Stats.RSSHuge)
  16. assert.Equal(t, uint64(17633280), mem.Stats.MappedFile)
  17. assert.Equal(t, uint64(0), mem.Stats.Swap)
  18. assert.Equal(t, uint64(103258), mem.Stats.PagesIn)
  19. assert.Equal(t, uint64(77551), mem.Stats.PagesOut)
  20. assert.Equal(t, uint64(91651), mem.Stats.PageFaults)
  21. assert.Equal(t, uint64(166), mem.Stats.MajorPageFaults)
  22. assert.Equal(t, uint64(28672), mem.Stats.InactiveAnon)
  23. assert.Equal(t, uint64(230780928), mem.Stats.ActiveAnon)
  24. assert.Equal(t, uint64(40108032), mem.Stats.InactiveFile)
  25. assert.Equal(t, uint64(24813568), mem.Stats.ActiveFile)
  26. assert.Equal(t, uint64(0), mem.Stats.Unevictable)
  27. assert.Equal(t, uint64(9223372036854771712), mem.Stats.HierarchicalMemoryLimit)
  28. assert.Equal(t, uint64(9223372036854771712), mem.Stats.HierarchicalMemswLimit)
  29. }
  30. func TestMemoryData(t *testing.T) {
  31. usage := MemoryData{}
  32. if err := memoryData(memoryPath, "memory", &usage); err != nil {
  33. t.Fatal(err)
  34. }
  35. assert.Equal(t, uint64(295997440), usage.Usage)
  36. assert.Equal(t, uint64(298532864), usage.MaxUsage)
  37. assert.Equal(t, uint64(9223372036854771712), usage.Limit)
  38. assert.Equal(t, uint64(0), usage.FailCount)
  39. }
  40. func TestMemoryDataSwap(t *testing.T) {
  41. usage := MemoryData{}
  42. if err := memoryData(memoryPath, "memory.memsw", &usage); err != nil {
  43. t.Fatal(err)
  44. }
  45. assert.Equal(t, uint64(295997440), usage.Usage)
  46. assert.Equal(t, uint64(298532864), usage.MaxUsage)
  47. assert.Equal(t, uint64(9223372036854771712), usage.Limit)
  48. assert.Equal(t, uint64(0), usage.FailCount)
  49. }
  50. func TestMemoryDataKernel(t *testing.T) {
  51. usage := MemoryData{}
  52. if err := memoryData(memoryPath, "memory.kmem", &usage); err != nil {
  53. t.Fatal(err)
  54. }
  55. assert.Equal(t, uint64(40), usage.Usage)
  56. assert.Equal(t, uint64(50), usage.MaxUsage)
  57. assert.Equal(t, uint64(9223372036854771712), usage.Limit)
  58. assert.Equal(t, uint64(0), usage.FailCount)
  59. }
  60. func TestMemoryDataKernelTCP(t *testing.T) {
  61. usage := MemoryData{}
  62. if err := memoryData(memoryPath, "memory.kmem.tcp", &usage); err != nil {
  63. t.Fatal(err)
  64. }
  65. assert.Equal(t, uint64(10), usage.Usage)
  66. assert.Equal(t, uint64(70), usage.MaxUsage)
  67. assert.Equal(t, uint64(9223372036854771712), usage.Limit)
  68. assert.Equal(t, uint64(0), usage.FailCount)
  69. }
  70. func TestMemorySubsystemGet(t *testing.T) {
  71. mem := MemorySubsystem{}
  72. if err := mem.get(memoryPath); err != nil {
  73. t.Fatal(err)
  74. }
  75. assert.Equal(t, uint64(65101824), mem.Stats.Cache)
  76. assert.Equal(t, uint64(295997440), mem.Mem.Usage)
  77. assert.Equal(t, uint64(295997440), mem.MemSwap.Usage)
  78. assert.Equal(t, uint64(40), mem.Kernel.Usage)
  79. assert.Equal(t, uint64(10), mem.KernelTCP.Usage)
  80. }
  81. func TestMemorySubsystemJSON(t *testing.T) {
  82. mem := MemorySubsystem{}
  83. if err := mem.get(memoryPath); err != nil {
  84. t.Fatal(err)
  85. }
  86. json, err := json.MarshalIndent(mem, "", " ")
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. t.Log(string(json))
  91. }