formatter_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package ps
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/docker/docker/api/types"
  6. )
  7. func TestFormat(t *testing.T) {
  8. contexts := []struct {
  9. context Context
  10. expected string
  11. }{
  12. // Errors
  13. {
  14. Context{
  15. Format: "{{InvalidFunction}}",
  16. },
  17. `Template parsing error: template: :1: function "InvalidFunction" not defined
  18. `,
  19. },
  20. {
  21. Context{
  22. Format: "{{nil}}",
  23. },
  24. `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
  25. `,
  26. },
  27. // Table Format
  28. {
  29. Context{
  30. Format: "table",
  31. },
  32. `CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  33. containerID1 ubuntu "" 45 years ago foobar_baz
  34. containerID2 ubuntu "" 45 years ago foobar_bar
  35. `,
  36. },
  37. {
  38. Context{
  39. Format: "table {{.Image}}",
  40. },
  41. "IMAGE\nubuntu\nubuntu\n",
  42. },
  43. {
  44. Context{
  45. Format: "table {{.Image}}",
  46. Size: true,
  47. },
  48. "IMAGE SIZE\nubuntu 0 B\nubuntu 0 B\n",
  49. },
  50. {
  51. Context{
  52. Format: "table {{.Image}}",
  53. Quiet: true,
  54. },
  55. "IMAGE\nubuntu\nubuntu\n",
  56. },
  57. {
  58. Context{
  59. Format: "table",
  60. Quiet: true,
  61. },
  62. "containerID1\ncontainerID2\n",
  63. },
  64. // Raw Format
  65. {
  66. Context{
  67. Format: "raw",
  68. },
  69. `container_id: containerID1
  70. image: ubuntu
  71. command: ""
  72. created_at: 1970-01-01 00:00:00 +0000 UTC
  73. status:
  74. names: foobar_baz
  75. labels:
  76. ports:
  77. container_id: containerID2
  78. image: ubuntu
  79. command: ""
  80. created_at: 1970-01-01 00:00:00 +0000 UTC
  81. status:
  82. names: foobar_bar
  83. labels:
  84. ports:
  85. `,
  86. },
  87. {
  88. Context{
  89. Format: "raw",
  90. Size: true,
  91. },
  92. `container_id: containerID1
  93. image: ubuntu
  94. command: ""
  95. created_at: 1970-01-01 00:00:00 +0000 UTC
  96. status:
  97. names: foobar_baz
  98. labels:
  99. ports:
  100. size: 0 B
  101. container_id: containerID2
  102. image: ubuntu
  103. command: ""
  104. created_at: 1970-01-01 00:00:00 +0000 UTC
  105. status:
  106. names: foobar_bar
  107. labels:
  108. ports:
  109. size: 0 B
  110. `,
  111. },
  112. {
  113. Context{
  114. Format: "raw",
  115. Quiet: true,
  116. },
  117. "container_id: containerID1\ncontainer_id: containerID2\n",
  118. },
  119. // Custom Format
  120. {
  121. Context{
  122. Format: "{{.Image}}",
  123. },
  124. "ubuntu\nubuntu\n",
  125. },
  126. {
  127. Context{
  128. Format: "{{.Image}}",
  129. Size: true,
  130. },
  131. "ubuntu\nubuntu\n",
  132. },
  133. }
  134. for _, context := range contexts {
  135. containers := []types.Container{
  136. {ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu"},
  137. {ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu"},
  138. }
  139. out := bytes.NewBufferString("")
  140. context.context.Output = out
  141. Format(context.context, containers)
  142. actual := out.String()
  143. if actual != context.expected {
  144. t.Fatalf("Expected \n%s, got \n%s", context.expected, actual)
  145. }
  146. // Clean buffer
  147. out.Reset()
  148. }
  149. }
  150. func TestCustomFormatNoContainers(t *testing.T) {
  151. out := bytes.NewBufferString("")
  152. containers := []types.Container{}
  153. contexts := []struct {
  154. context Context
  155. expected string
  156. }{
  157. {
  158. Context{
  159. Format: "{{.Image}}",
  160. Output: out,
  161. },
  162. "",
  163. },
  164. {
  165. Context{
  166. Format: "table {{.Image}}",
  167. Output: out,
  168. },
  169. "IMAGE\n",
  170. },
  171. {
  172. Context{
  173. Format: "{{.Image}}",
  174. Output: out,
  175. Size: true,
  176. },
  177. "",
  178. },
  179. {
  180. Context{
  181. Format: "table {{.Image}}",
  182. Output: out,
  183. Size: true,
  184. },
  185. "IMAGE SIZE\n",
  186. },
  187. }
  188. for _, context := range contexts {
  189. customFormat(context.context, containers)
  190. actual := out.String()
  191. if actual != context.expected {
  192. t.Fatalf("Expected \n%s, got \n%s", context.expected, actual)
  193. }
  194. // Clean buffer
  195. out.Reset()
  196. }
  197. }