blkio_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package cgroup
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. const blkioPath = "testdata/docker/sys/fs/cgroup/blkio/docker/b29faf21b7eff959f64b4192c34d5d67a707fe8561e9eaa608cb27693fba4242"
  8. func TestParseBlkioValueWithOp(t *testing.T) {
  9. line := `253:1 Async 1638912`
  10. opValue, err := parseBlkioValue(line)
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. assert.Equal(t, uint64(253), opValue.Major)
  15. assert.Equal(t, uint64(1), opValue.Minor)
  16. assert.Equal(t, "async", opValue.Operation)
  17. assert.Equal(t, uint64(1638912), opValue.Value)
  18. }
  19. func TestParseBlkioValueWithoutOp(t *testing.T) {
  20. line := `1:2 10088`
  21. opValue, err := parseBlkioValue(line)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. assert.Equal(t, uint64(1), opValue.Major)
  26. assert.Equal(t, uint64(2), opValue.Minor)
  27. assert.Equal(t, "", opValue.Operation)
  28. assert.Equal(t, uint64(10088), opValue.Value)
  29. }
  30. func TestBlkioThrottle(t *testing.T) {
  31. blkio := BlockIOSubsystem{}
  32. err := blkioThrottle(blkioPath, &blkio)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. assert.Equal(t, uint64(46), blkio.Throttle.TotalIOs)
  37. assert.Equal(t, uint64(1648128), blkio.Throttle.TotalBytes)
  38. assert.Len(t, blkio.Throttle.Devices, 3)
  39. for _, device := range blkio.Throttle.Devices {
  40. if device.DeviceID.Major == 7 && device.DeviceID.Minor == 0 {
  41. assert.Equal(t, uint64(1000), device.ReadLimitBPS)
  42. assert.Equal(t, uint64(2000), device.ReadLimitIOPS)
  43. assert.Equal(t, uint64(3000), device.WriteLimitBPS)
  44. assert.Equal(t, uint64(4000), device.WriteLimitIOPS)
  45. assert.Equal(t, uint64(4608), device.Bytes.Read)
  46. assert.Equal(t, uint64(0), device.Bytes.Write)
  47. assert.Equal(t, uint64(4608), device.Bytes.Async)
  48. assert.Equal(t, uint64(0), device.Bytes.Sync)
  49. assert.Equal(t, uint64(2), device.IOs.Read)
  50. assert.Equal(t, uint64(0), device.IOs.Write)
  51. assert.Equal(t, uint64(2), device.IOs.Async)
  52. assert.Equal(t, uint64(0), device.IOs.Sync)
  53. }
  54. }
  55. }
  56. func TestBlockIOSubsystemGet(t *testing.T) {
  57. blkio := BlockIOSubsystem{}
  58. if err := blkio.get(blkioPath); err != nil {
  59. t.Fatal(err)
  60. }
  61. assert.True(t, len(blkio.Throttle.Devices) > 0)
  62. }
  63. func TestBlockIOSubsystemJSON(t *testing.T) {
  64. blkio := BlockIOSubsystem{}
  65. if err := blkio.get(blkioPath); err != nil {
  66. t.Fatal(err)
  67. }
  68. json, err := json.MarshalIndent(blkio, "", " ")
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. t.Log(string(json))
  73. }