container_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. package formatter
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/pkg/stringid"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestContainerPsContext(t *testing.T) {
  14. containerID := stringid.GenerateRandomID()
  15. unix := time.Now().Add(-65 * time.Second).Unix()
  16. var ctx containerContext
  17. cases := []struct {
  18. container types.Container
  19. trunc bool
  20. expValue string
  21. call func() string
  22. }{
  23. {types.Container{ID: containerID}, true, stringid.TruncateID(containerID), ctx.ID},
  24. {types.Container{ID: containerID}, false, containerID, ctx.ID},
  25. {types.Container{Names: []string{"/foobar_baz"}}, true, "foobar_baz", ctx.Names},
  26. {types.Container{Image: "ubuntu"}, true, "ubuntu", ctx.Image},
  27. {types.Container{Image: "verylongimagename"}, true, "verylongimagename", ctx.Image},
  28. {types.Container{Image: "verylongimagename"}, false, "verylongimagename", ctx.Image},
  29. {types.Container{
  30. Image: "a5a665ff33eced1e0803148700880edab4",
  31. ImageID: "a5a665ff33eced1e0803148700880edab4269067ed77e27737a708d0d293fbf5",
  32. },
  33. true,
  34. "a5a665ff33ec",
  35. ctx.Image,
  36. },
  37. {types.Container{
  38. Image: "a5a665ff33eced1e0803148700880edab4",
  39. ImageID: "a5a665ff33eced1e0803148700880edab4269067ed77e27737a708d0d293fbf5",
  40. },
  41. false,
  42. "a5a665ff33eced1e0803148700880edab4",
  43. ctx.Image,
  44. },
  45. {types.Container{Image: ""}, true, "<no image>", ctx.Image},
  46. {types.Container{Command: "sh -c 'ls -la'"}, true, `"sh -c 'ls -la'"`, ctx.Command},
  47. {types.Container{Created: unix}, true, time.Unix(unix, 0).String(), ctx.CreatedAt},
  48. {types.Container{Ports: []types.Port{{PrivatePort: 8080, PublicPort: 8080, Type: "tcp"}}}, true, "8080/tcp", ctx.Ports},
  49. {types.Container{Status: "RUNNING"}, true, "RUNNING", ctx.Status},
  50. {types.Container{SizeRw: 10}, true, "10B", ctx.Size},
  51. {types.Container{SizeRw: 10, SizeRootFs: 20}, true, "10B (virtual 20B)", ctx.Size},
  52. {types.Container{}, true, "", ctx.Labels},
  53. {types.Container{Labels: map[string]string{"cpu": "6", "storage": "ssd"}}, true, "cpu=6,storage=ssd", ctx.Labels},
  54. {types.Container{Created: unix}, true, "About a minute ago", ctx.RunningFor},
  55. {types.Container{
  56. Mounts: []types.MountPoint{
  57. {
  58. Name: "this-is-a-long-volume-name-and-will-be-truncated-if-trunc-is-set",
  59. Driver: "local",
  60. Source: "/a/path",
  61. },
  62. },
  63. }, true, "this-is-a-lo...", ctx.Mounts},
  64. {types.Container{
  65. Mounts: []types.MountPoint{
  66. {
  67. Driver: "local",
  68. Source: "/a/path",
  69. },
  70. },
  71. }, false, "/a/path", ctx.Mounts},
  72. {types.Container{
  73. Mounts: []types.MountPoint{
  74. {
  75. Name: "733908409c91817de8e92b0096373245f329f19a88e2c849f02460e9b3d1c203",
  76. Driver: "local",
  77. Source: "/a/path",
  78. },
  79. },
  80. }, false, "733908409c91817de8e92b0096373245f329f19a88e2c849f02460e9b3d1c203", ctx.Mounts},
  81. }
  82. for _, c := range cases {
  83. ctx = containerContext{c: c.container, trunc: c.trunc}
  84. v := c.call()
  85. if strings.Contains(v, ",") {
  86. compareMultipleValues(t, v, c.expValue)
  87. } else if v != c.expValue {
  88. t.Fatalf("Expected %s, was %s\n", c.expValue, v)
  89. }
  90. }
  91. c1 := types.Container{Labels: map[string]string{"com.docker.swarm.swarm-id": "33", "com.docker.swarm.node_name": "ubuntu"}}
  92. ctx = containerContext{c: c1, trunc: true}
  93. sid := ctx.Label("com.docker.swarm.swarm-id")
  94. node := ctx.Label("com.docker.swarm.node_name")
  95. if sid != "33" {
  96. t.Fatalf("Expected 33, was %s\n", sid)
  97. }
  98. if node != "ubuntu" {
  99. t.Fatalf("Expected ubuntu, was %s\n", node)
  100. }
  101. c2 := types.Container{}
  102. ctx = containerContext{c: c2, trunc: true}
  103. label := ctx.Label("anything.really")
  104. if label != "" {
  105. t.Fatalf("Expected an empty string, was %s", label)
  106. }
  107. }
  108. func TestContainerContextWrite(t *testing.T) {
  109. unixTime := time.Now().AddDate(0, 0, -1).Unix()
  110. expectedTime := time.Unix(unixTime, 0).String()
  111. cases := []struct {
  112. context Context
  113. expected string
  114. }{
  115. // Errors
  116. {
  117. Context{Format: "{{InvalidFunction}}"},
  118. `Template parsing error: template: :1: function "InvalidFunction" not defined
  119. `,
  120. },
  121. {
  122. Context{Format: "{{nil}}"},
  123. `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
  124. `,
  125. },
  126. // Table Format
  127. {
  128. Context{Format: NewContainerFormat("table", false, true)},
  129. `CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
  130. containerID1 ubuntu "" 24 hours ago foobar_baz 0B
  131. containerID2 ubuntu "" 24 hours ago foobar_bar 0B
  132. `,
  133. },
  134. {
  135. Context{Format: NewContainerFormat("table", false, false)},
  136. `CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  137. containerID1 ubuntu "" 24 hours ago foobar_baz
  138. containerID2 ubuntu "" 24 hours ago foobar_bar
  139. `,
  140. },
  141. {
  142. Context{Format: NewContainerFormat("table {{.Image}}", false, false)},
  143. "IMAGE\nubuntu\nubuntu\n",
  144. },
  145. {
  146. Context{Format: NewContainerFormat("table {{.Image}}", false, true)},
  147. "IMAGE\nubuntu\nubuntu\n",
  148. },
  149. {
  150. Context{Format: NewContainerFormat("table {{.Image}}", true, false)},
  151. "IMAGE\nubuntu\nubuntu\n",
  152. },
  153. {
  154. Context{Format: NewContainerFormat("table", true, false)},
  155. "containerID1\ncontainerID2\n",
  156. },
  157. // Raw Format
  158. {
  159. Context{Format: NewContainerFormat("raw", false, false)},
  160. fmt.Sprintf(`container_id: containerID1
  161. image: ubuntu
  162. command: ""
  163. created_at: %s
  164. status:
  165. names: foobar_baz
  166. labels:
  167. ports:
  168. container_id: containerID2
  169. image: ubuntu
  170. command: ""
  171. created_at: %s
  172. status:
  173. names: foobar_bar
  174. labels:
  175. ports:
  176. `, expectedTime, expectedTime),
  177. },
  178. {
  179. Context{Format: NewContainerFormat("raw", false, true)},
  180. fmt.Sprintf(`container_id: containerID1
  181. image: ubuntu
  182. command: ""
  183. created_at: %s
  184. status:
  185. names: foobar_baz
  186. labels:
  187. ports:
  188. size: 0B
  189. container_id: containerID2
  190. image: ubuntu
  191. command: ""
  192. created_at: %s
  193. status:
  194. names: foobar_bar
  195. labels:
  196. ports:
  197. size: 0B
  198. `, expectedTime, expectedTime),
  199. },
  200. {
  201. Context{Format: NewContainerFormat("raw", true, false)},
  202. "container_id: containerID1\ncontainer_id: containerID2\n",
  203. },
  204. // Custom Format
  205. {
  206. Context{Format: "{{.Image}}"},
  207. "ubuntu\nubuntu\n",
  208. },
  209. {
  210. Context{Format: NewContainerFormat("{{.Image}}", false, true)},
  211. "ubuntu\nubuntu\n",
  212. },
  213. // Special headers for customerized table format
  214. {
  215. Context{Format: NewContainerFormat(`table {{truncate .ID 5}}\t{{json .Image}} {{.RunningFor}}/{{title .Status}}/{{pad .Ports 2 2}}.{{upper .Names}} {{lower .Status}}`, false, true)},
  216. `CONTAINER ID IMAGE CREATED/STATUS/ PORTS .NAMES STATUS
  217. conta "ubuntu" 24 hours ago//.FOOBAR_BAZ
  218. conta "ubuntu" 24 hours ago//.FOOBAR_BAR
  219. `,
  220. },
  221. }
  222. for _, testcase := range cases {
  223. containers := []types.Container{
  224. {ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unixTime},
  225. {ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unixTime},
  226. }
  227. out := bytes.NewBufferString("")
  228. testcase.context.Output = out
  229. err := ContainerWrite(testcase.context, containers)
  230. if err != nil {
  231. assert.EqualError(t, err, testcase.expected)
  232. } else {
  233. assert.Equal(t, testcase.expected, out.String())
  234. }
  235. }
  236. }
  237. func TestContainerContextWriteWithNoContainers(t *testing.T) {
  238. out := bytes.NewBufferString("")
  239. containers := []types.Container{}
  240. contexts := []struct {
  241. context Context
  242. expected string
  243. }{
  244. {
  245. Context{
  246. Format: "{{.Image}}",
  247. Output: out,
  248. },
  249. "",
  250. },
  251. {
  252. Context{
  253. Format: "table {{.Image}}",
  254. Output: out,
  255. },
  256. "IMAGE\n",
  257. },
  258. {
  259. Context{
  260. Format: NewContainerFormat("{{.Image}}", false, true),
  261. Output: out,
  262. },
  263. "",
  264. },
  265. {
  266. Context{
  267. Format: NewContainerFormat("table {{.Image}}", false, true),
  268. Output: out,
  269. },
  270. "IMAGE\n",
  271. },
  272. {
  273. Context{
  274. Format: "table {{.Image}}\t{{.Size}}",
  275. Output: out,
  276. },
  277. "IMAGE SIZE\n",
  278. },
  279. {
  280. Context{
  281. Format: NewContainerFormat("table {{.Image}}\t{{.Size}}", false, true),
  282. Output: out,
  283. },
  284. "IMAGE SIZE\n",
  285. },
  286. }
  287. for _, context := range contexts {
  288. ContainerWrite(context.context, containers)
  289. assert.Equal(t, context.expected, out.String())
  290. // Clean buffer
  291. out.Reset()
  292. }
  293. }
  294. func TestContainerContextWriteJSON(t *testing.T) {
  295. unix := time.Now().Add(-65 * time.Second).Unix()
  296. containers := []types.Container{
  297. {ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unix},
  298. {ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unix},
  299. }
  300. expectedCreated := time.Unix(unix, 0).String()
  301. expectedJSONs := []map[string]interface{}{
  302. {"Command": "\"\"", "CreatedAt": expectedCreated, "ID": "containerID1", "Image": "ubuntu", "Labels": "", "LocalVolumes": "0", "Mounts": "", "Names": "foobar_baz", "Networks": "", "Ports": "", "RunningFor": "About a minute ago", "Size": "0B", "Status": ""},
  303. {"Command": "\"\"", "CreatedAt": expectedCreated, "ID": "containerID2", "Image": "ubuntu", "Labels": "", "LocalVolumes": "0", "Mounts": "", "Names": "foobar_bar", "Networks": "", "Ports": "", "RunningFor": "About a minute ago", "Size": "0B", "Status": ""},
  304. }
  305. out := bytes.NewBufferString("")
  306. err := ContainerWrite(Context{Format: "{{json .}}", Output: out}, containers)
  307. if err != nil {
  308. t.Fatal(err)
  309. }
  310. for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
  311. t.Logf("Output: line %d: %s", i, line)
  312. var m map[string]interface{}
  313. if err := json.Unmarshal([]byte(line), &m); err != nil {
  314. t.Fatal(err)
  315. }
  316. assert.Equal(t, expectedJSONs[i], m)
  317. }
  318. }
  319. func TestContainerContextWriteJSONField(t *testing.T) {
  320. containers := []types.Container{
  321. {ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu"},
  322. {ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu"},
  323. }
  324. out := bytes.NewBufferString("")
  325. err := ContainerWrite(Context{Format: "{{json .ID}}", Output: out}, containers)
  326. if err != nil {
  327. t.Fatal(err)
  328. }
  329. for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
  330. t.Logf("Output: line %d: %s", i, line)
  331. var s string
  332. if err := json.Unmarshal([]byte(line), &s); err != nil {
  333. t.Fatal(err)
  334. }
  335. assert.Equal(t, containers[i].ID, s)
  336. }
  337. }
  338. func TestContainerBackCompat(t *testing.T) {
  339. containers := []types.Container{{ID: "brewhaha"}}
  340. cases := []string{
  341. "ID",
  342. "Names",
  343. "Image",
  344. "Command",
  345. "CreatedAt",
  346. "RunningFor",
  347. "Ports",
  348. "Status",
  349. "Size",
  350. "Labels",
  351. "Mounts",
  352. }
  353. buf := bytes.NewBuffer(nil)
  354. for _, c := range cases {
  355. ctx := Context{Format: Format(fmt.Sprintf("{{ .%s }}", c)), Output: buf}
  356. if err := ContainerWrite(ctx, containers); err != nil {
  357. t.Logf("could not render template for field '%s': %v", c, err)
  358. t.Fail()
  359. }
  360. buf.Reset()
  361. }
  362. }