disk_usage_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package formatter
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestDiskUsageContextFormatWrite(t *testing.T) {
  8. cases := []struct {
  9. context DiskUsageContext
  10. expected string
  11. }{
  12. // Check default output format (verbose and non-verbose mode) for table headers
  13. {
  14. DiskUsageContext{
  15. Context: Context{
  16. Format: NewDiskUsageFormat("table"),
  17. },
  18. Verbose: false},
  19. `TYPE TOTAL ACTIVE SIZE RECLAIMABLE
  20. Images 0 0 0B 0B
  21. Containers 0 0 0B 0B
  22. Local Volumes 0 0 0B 0B
  23. `,
  24. },
  25. {
  26. DiskUsageContext{Verbose: true},
  27. `Images space usage:
  28. REPOSITORY TAG IMAGE ID CREATED ago SIZE SHARED SIZE UNIQUE SiZE CONTAINERS
  29. Containers space usage:
  30. CONTAINER ID IMAGE COMMAND LOCAL VOLUMES SIZE CREATED ago STATUS NAMES
  31. Local Volumes space usage:
  32. VOLUME NAME LINKS SIZE
  33. `,
  34. },
  35. // Errors
  36. {
  37. DiskUsageContext{
  38. Context: Context{
  39. Format: "{{InvalidFunction}}",
  40. },
  41. },
  42. `Template parsing error: template: :1: function "InvalidFunction" not defined
  43. `,
  44. },
  45. {
  46. DiskUsageContext{
  47. Context: Context{
  48. Format: "{{nil}}",
  49. },
  50. },
  51. `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
  52. `,
  53. },
  54. // Table Format
  55. {
  56. DiskUsageContext{
  57. Context: Context{
  58. Format: NewDiskUsageFormat("table"),
  59. },
  60. },
  61. `TYPE TOTAL ACTIVE SIZE RECLAIMABLE
  62. Images 0 0 0B 0B
  63. Containers 0 0 0B 0B
  64. Local Volumes 0 0 0B 0B
  65. `,
  66. },
  67. {
  68. DiskUsageContext{
  69. Context: Context{
  70. Format: NewDiskUsageFormat("table {{.Type}}\t{{.Active}}"),
  71. },
  72. },
  73. `TYPE ACTIVE
  74. Images 0
  75. Containers 0
  76. Local Volumes 0
  77. `,
  78. },
  79. // Raw Format
  80. {
  81. DiskUsageContext{
  82. Context: Context{
  83. Format: NewDiskUsageFormat("raw"),
  84. },
  85. },
  86. `type: Images
  87. total: 0
  88. active: 0
  89. size: 0B
  90. reclaimable: 0B
  91. type: Containers
  92. total: 0
  93. active: 0
  94. size: 0B
  95. reclaimable: 0B
  96. type: Local Volumes
  97. total: 0
  98. active: 0
  99. size: 0B
  100. reclaimable: 0B
  101. `,
  102. },
  103. }
  104. for _, testcase := range cases {
  105. out := bytes.NewBufferString("")
  106. testcase.context.Output = out
  107. if err := testcase.context.Write(); err != nil {
  108. assert.Equal(t, testcase.expected, err.Error())
  109. } else {
  110. assert.Equal(t, testcase.expected, out.String())
  111. }
  112. }
  113. }