container_test.go 9.5 KB

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