container_test.go 11 KB

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