stats_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package formatter
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/docker/docker/pkg/stringid"
  6. "github.com/docker/docker/pkg/testutil/assert"
  7. )
  8. func TestContainerStatsContext(t *testing.T) {
  9. containerID := stringid.GenerateRandomID()
  10. var ctx containerStatsContext
  11. tt := []struct {
  12. stats StatsEntry
  13. expValue string
  14. expHeader string
  15. call func() string
  16. }{
  17. {StatsEntry{Container: containerID}, containerID, containerHeader, ctx.Container},
  18. {StatsEntry{CPUPercentage: 5.5}, "5.50%", cpuPercHeader, ctx.CPUPerc},
  19. {StatsEntry{CPUPercentage: 5.5, IsInvalid: true}, "--", cpuPercHeader, ctx.CPUPerc},
  20. {StatsEntry{NetworkRx: 0.31, NetworkTx: 12.3}, "0.31 B / 12.3 B", netIOHeader, ctx.NetIO},
  21. {StatsEntry{NetworkRx: 0.31, NetworkTx: 12.3, IsInvalid: true}, "--", netIOHeader, ctx.NetIO},
  22. {StatsEntry{BlockRead: 0.1, BlockWrite: 2.3}, "0.1 B / 2.3 B", blockIOHeader, ctx.BlockIO},
  23. {StatsEntry{BlockRead: 0.1, BlockWrite: 2.3, IsInvalid: true}, "--", blockIOHeader, ctx.BlockIO},
  24. {StatsEntry{MemoryPercentage: 10.2}, "10.20%", memPercHeader, ctx.MemPerc},
  25. {StatsEntry{MemoryPercentage: 10.2, IsInvalid: true}, "--", memPercHeader, ctx.MemPerc},
  26. {StatsEntry{MemoryPercentage: 10.2, OSType: "windows"}, "--", memPercHeader, ctx.MemPerc},
  27. {StatsEntry{Memory: 24, MemoryLimit: 30}, "24 B / 30 B", memUseHeader, ctx.MemUsage},
  28. {StatsEntry{Memory: 24, MemoryLimit: 30, IsInvalid: true}, "-- / --", memUseHeader, ctx.MemUsage},
  29. {StatsEntry{Memory: 24, MemoryLimit: 30, OSType: "windows"}, "24 B", winMemUseHeader, ctx.MemUsage},
  30. {StatsEntry{PidsCurrent: 10}, "10", pidsHeader, ctx.PIDs},
  31. {StatsEntry{PidsCurrent: 10, IsInvalid: true}, "--", pidsHeader, ctx.PIDs},
  32. {StatsEntry{PidsCurrent: 10, OSType: "windows"}, "--", pidsHeader, ctx.PIDs},
  33. }
  34. for _, te := range tt {
  35. ctx = containerStatsContext{s: te.stats}
  36. if v := te.call(); v != te.expValue {
  37. t.Fatalf("Expected %q, got %q", te.expValue, v)
  38. }
  39. h := ctx.FullHeader()
  40. if h != te.expHeader {
  41. t.Fatalf("Expected %q, got %q", te.expHeader, h)
  42. }
  43. }
  44. }
  45. func TestContainerStatsContextWrite(t *testing.T) {
  46. tt := []struct {
  47. context Context
  48. expected string
  49. }{
  50. {
  51. Context{Format: "{{InvalidFunction}}"},
  52. `Template parsing error: template: :1: function "InvalidFunction" not defined
  53. `,
  54. },
  55. {
  56. Context{Format: "{{nil}}"},
  57. `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
  58. `,
  59. },
  60. {
  61. Context{Format: "table {{.MemUsage}}"},
  62. `MEM USAGE / LIMIT
  63. 20 B / 20 B
  64. -- / --
  65. `,
  66. },
  67. {
  68. Context{Format: "{{.Container}} {{.CPUPerc}}"},
  69. `container1 20.00%
  70. container2 --
  71. `,
  72. },
  73. }
  74. for _, te := range tt {
  75. stats := []StatsEntry{
  76. {
  77. Container: "container1",
  78. CPUPercentage: 20,
  79. Memory: 20,
  80. MemoryLimit: 20,
  81. MemoryPercentage: 20,
  82. NetworkRx: 20,
  83. NetworkTx: 20,
  84. BlockRead: 20,
  85. BlockWrite: 20,
  86. PidsCurrent: 2,
  87. IsInvalid: false,
  88. OSType: "linux",
  89. },
  90. {
  91. Container: "container2",
  92. CPUPercentage: 30,
  93. Memory: 30,
  94. MemoryLimit: 30,
  95. MemoryPercentage: 30,
  96. NetworkRx: 30,
  97. NetworkTx: 30,
  98. BlockRead: 30,
  99. BlockWrite: 30,
  100. PidsCurrent: 3,
  101. IsInvalid: true,
  102. OSType: "linux",
  103. },
  104. }
  105. var out bytes.Buffer
  106. te.context.Output = &out
  107. err := ContainerStatsWrite(te.context, stats)
  108. if err != nil {
  109. assert.Error(t, err, te.expected)
  110. } else {
  111. assert.Equal(t, out.String(), te.expected)
  112. }
  113. }
  114. }
  115. func TestContainerStatsContextWriteWindows(t *testing.T) {
  116. tt := []struct {
  117. context Context
  118. expected string
  119. }{
  120. {
  121. Context{Format: "table {{.MemUsage}}"},
  122. `PRIV WORKING SET
  123. 20 B
  124. -- / --
  125. `,
  126. },
  127. {
  128. Context{Format: "{{.Container}} {{.CPUPerc}}"},
  129. `container1 20.00%
  130. container2 --
  131. `,
  132. },
  133. {
  134. Context{Format: "{{.Container}} {{.MemPerc}} {{.PIDs}}"},
  135. `container1 -- --
  136. container2 -- --
  137. `,
  138. },
  139. }
  140. for _, te := range tt {
  141. stats := []StatsEntry{
  142. {
  143. Container: "container1",
  144. CPUPercentage: 20,
  145. Memory: 20,
  146. MemoryLimit: 20,
  147. MemoryPercentage: 20,
  148. NetworkRx: 20,
  149. NetworkTx: 20,
  150. BlockRead: 20,
  151. BlockWrite: 20,
  152. PidsCurrent: 2,
  153. IsInvalid: false,
  154. OSType: "windows",
  155. },
  156. {
  157. Container: "container2",
  158. CPUPercentage: 30,
  159. Memory: 30,
  160. MemoryLimit: 30,
  161. MemoryPercentage: 30,
  162. NetworkRx: 30,
  163. NetworkTx: 30,
  164. BlockRead: 30,
  165. BlockWrite: 30,
  166. PidsCurrent: 3,
  167. IsInvalid: true,
  168. OSType: "windows",
  169. },
  170. }
  171. var out bytes.Buffer
  172. te.context.Output = &out
  173. err := ContainerStatsWrite(te.context, stats)
  174. if err != nil {
  175. assert.Error(t, err, te.expected)
  176. } else {
  177. assert.Equal(t, out.String(), te.expected)
  178. }
  179. }
  180. }
  181. func TestContainerStatsContextWriteWithNoStats(t *testing.T) {
  182. var out bytes.Buffer
  183. contexts := []struct {
  184. context Context
  185. expected string
  186. }{
  187. {
  188. Context{
  189. Format: "{{.Container}}",
  190. Output: &out,
  191. },
  192. "",
  193. },
  194. {
  195. Context{
  196. Format: "table {{.Container}}",
  197. Output: &out,
  198. },
  199. "CONTAINER\n",
  200. },
  201. {
  202. Context{
  203. Format: "table {{.Container}}\t{{.CPUPerc}}",
  204. Output: &out,
  205. },
  206. "CONTAINER CPU %\n",
  207. },
  208. }
  209. for _, context := range contexts {
  210. ContainerStatsWrite(context.context, []StatsEntry{})
  211. assert.Equal(t, context.expected, out.String())
  212. // Clean buffer
  213. out.Reset()
  214. }
  215. }