docker_cli_ps_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "sort"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/docker/docker/pkg/integration/checker"
  13. "github.com/docker/docker/pkg/stringid"
  14. "github.com/go-check/check"
  15. )
  16. func (s *DockerSuite) TestPsListContainersBase(c *check.C) {
  17. // TODO Windows CI (TP5 timeframe). This test has a very high likelyhood
  18. // of hitting a known bug in TP4. Hence disabling for now until TP5
  19. // is available.
  20. testRequires(c, DaemonIsLinux)
  21. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  22. firstID := strings.TrimSpace(out)
  23. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  24. secondID := strings.TrimSpace(out)
  25. // not long running
  26. out, _ = dockerCmd(c, "run", "-d", "busybox", "true")
  27. thirdID := strings.TrimSpace(out)
  28. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  29. fourthID := strings.TrimSpace(out)
  30. // make sure the second is running
  31. c.Assert(waitRun(secondID), checker.IsNil)
  32. // make sure third one is not running
  33. dockerCmd(c, "wait", thirdID)
  34. // make sure the forth is running
  35. c.Assert(waitRun(fourthID), checker.IsNil)
  36. // all
  37. out, _ = dockerCmd(c, "ps", "-a")
  38. c.Assert(assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}), checker.Equals, true, check.Commentf("ALL: Container list is not in the correct order: \n%s", out))
  39. // running
  40. out, _ = dockerCmd(c, "ps")
  41. c.Assert(assertContainerList(out, []string{fourthID, secondID, firstID}), checker.Equals, true, check.Commentf("RUNNING: Container list is not in the correct order: \n%s", out))
  42. // from here all flag '-a' is ignored
  43. // limit
  44. out, _ = dockerCmd(c, "ps", "-n=2", "-a")
  45. expected := []string{fourthID, thirdID}
  46. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("LIMIT & ALL: Container list is not in the correct order: \n%s", out))
  47. out, _ = dockerCmd(c, "ps", "-n=2")
  48. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("LIMIT: Container list is not in the correct order: \n%s", out))
  49. // filter since
  50. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-a")
  51. expected = []string{fourthID, thirdID, secondID}
  52. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE & ALL: Container list is not in the correct order: \n%s", out))
  53. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID)
  54. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE: Container list is not in the correct order: \n%s", out))
  55. // filter before
  56. out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID, "-a")
  57. expected = []string{secondID, firstID}
  58. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE & ALL: Container list is not in the correct order: \n%s", out))
  59. out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID)
  60. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE: Container list is not in the correct order: \n%s", out))
  61. // filter since & before
  62. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-a")
  63. expected = []string{thirdID, secondID}
  64. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE & ALL: Container list is not in the correct order: \n%s", out))
  65. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID)
  66. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE: Container list is not in the correct order: \n%s", out))
  67. // filter since & limit
  68. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2", "-a")
  69. expected = []string{fourthID, thirdID}
  70. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
  71. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2")
  72. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, LIMIT: Container list is not in the correct order: \n%s", out))
  73. // filter before & limit
  74. out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1", "-a")
  75. expected = []string{thirdID}
  76. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
  77. out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1")
  78. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE, LIMIT: Container list is not in the correct order: \n%s", out))
  79. // filter since & filter before & limit
  80. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1", "-a")
  81. expected = []string{thirdID}
  82. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
  83. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1")
  84. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE, LIMIT: Container list is not in the correct order: \n%s", out))
  85. }
  86. func assertContainerList(out string, expected []string) bool {
  87. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  88. if len(lines)-1 != len(expected) {
  89. return false
  90. }
  91. containerIDIndex := strings.Index(lines[0], "CONTAINER ID")
  92. for i := 0; i < len(expected); i++ {
  93. foundID := lines[i+1][containerIDIndex : containerIDIndex+12]
  94. if foundID != expected[i][:12] {
  95. return false
  96. }
  97. }
  98. return true
  99. }
  100. func (s *DockerSuite) TestPsListContainersSize(c *check.C) {
  101. // Problematic on Windows as it doesn't report the size correctly @swernli
  102. testRequires(c, DaemonIsLinux)
  103. dockerCmd(c, "run", "-d", "busybox", "echo", "hello")
  104. baseOut, _ := dockerCmd(c, "ps", "-s", "-n=1")
  105. baseLines := strings.Split(strings.Trim(baseOut, "\n "), "\n")
  106. baseSizeIndex := strings.Index(baseLines[0], "SIZE")
  107. baseFoundsize := baseLines[1][baseSizeIndex:]
  108. baseBytes, err := strconv.Atoi(strings.Split(baseFoundsize, " ")[0])
  109. c.Assert(err, checker.IsNil)
  110. name := "test_size"
  111. out, _ := dockerCmd(c, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test")
  112. id, err := getIDByName(name)
  113. c.Assert(err, checker.IsNil)
  114. runCmd := exec.Command(dockerBinary, "ps", "-s", "-n=1")
  115. wait := make(chan struct{})
  116. go func() {
  117. out, _, err = runCommandWithOutput(runCmd)
  118. close(wait)
  119. }()
  120. select {
  121. case <-wait:
  122. case <-time.After(3 * time.Second):
  123. c.Fatalf("Calling \"docker ps -s\" timed out!")
  124. }
  125. c.Assert(err, checker.IsNil)
  126. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  127. c.Assert(lines, checker.HasLen, 2, check.Commentf("Expected 2 lines for 'ps -s -n=1' output, got %d", len(lines)))
  128. sizeIndex := strings.Index(lines[0], "SIZE")
  129. idIndex := strings.Index(lines[0], "CONTAINER ID")
  130. foundID := lines[1][idIndex : idIndex+12]
  131. c.Assert(foundID, checker.Equals, id[:12], check.Commentf("Expected id %s, got %s", id[:12], foundID))
  132. expectedSize := fmt.Sprintf("%d B", (2 + baseBytes))
  133. foundSize := lines[1][sizeIndex:]
  134. c.Assert(foundSize, checker.Contains, expectedSize, check.Commentf("Expected size %q, got %q", expectedSize, foundSize))
  135. }
  136. func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) {
  137. // TODO Windows CI (TP5 timeframe). This test has a very high likelyhood
  138. // of hitting a known bug in TP4. Hence disabling for now until TP5
  139. // is available.
  140. testRequires(c, DaemonIsLinux)
  141. // start exited container
  142. out, _ := dockerCmd(c, "run", "-d", "busybox")
  143. firstID := strings.TrimSpace(out)
  144. // make sure the exited container is not running
  145. dockerCmd(c, "wait", firstID)
  146. // start running container
  147. out, _ = dockerCmd(c, "run", "-itd", "busybox")
  148. secondID := strings.TrimSpace(out)
  149. // filter containers by exited
  150. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter=status=exited")
  151. containerOut := strings.TrimSpace(out)
  152. c.Assert(containerOut, checker.Equals, firstID)
  153. out, _ = dockerCmd(c, "ps", "-a", "--no-trunc", "-q", "--filter=status=running")
  154. containerOut = strings.TrimSpace(out)
  155. c.Assert(containerOut, checker.Equals, secondID)
  156. out, _, _ = dockerCmdWithTimeout(time.Second*60, "ps", "-a", "-q", "--filter=status=rubbish")
  157. c.Assert(out, checker.Contains, "Unrecognised filter value for status", check.Commentf("Expected error response due to invalid status filter output: %q", out))
  158. // Windows doesn't support pausing of containers
  159. if daemonPlatform != "windows" {
  160. // pause running container
  161. out, _ = dockerCmd(c, "run", "-itd", "busybox")
  162. pausedID := strings.TrimSpace(out)
  163. dockerCmd(c, "pause", pausedID)
  164. // make sure the container is unpaused to let the daemon stop it properly
  165. defer func() { dockerCmd(c, "unpause", pausedID) }()
  166. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter=status=paused")
  167. containerOut = strings.TrimSpace(out)
  168. c.Assert(containerOut, checker.Equals, pausedID)
  169. }
  170. }
  171. func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) {
  172. // TODO Windows CI (TP5 timeframe). This test has a very high likelyhood
  173. // of hitting a known bug in TP4. Hence disabling for now until TP5
  174. // is available.
  175. testRequires(c, DaemonIsLinux)
  176. // start container
  177. out, _ := dockerCmd(c, "run", "-d", "busybox")
  178. firstID := strings.TrimSpace(out)
  179. // start another container
  180. runSleepingContainer(c)
  181. // filter containers by id
  182. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=id="+firstID)
  183. containerOut := strings.TrimSpace(out)
  184. c.Assert(containerOut, checker.Equals, firstID[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out))
  185. }
  186. func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) {
  187. // start container
  188. out, _ := dockerCmd(c, "run", "-d", "--name=a_name_to_match", "busybox")
  189. firstID := strings.TrimSpace(out)
  190. // start another container
  191. runSleepingContainer(c, "--name=b_name_to_match")
  192. // filter containers by name
  193. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=name=a_name_to_match")
  194. containerOut := strings.TrimSpace(out)
  195. c.Assert(containerOut, checker.Equals, firstID[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out))
  196. }
  197. // Test for the ancestor filter for ps.
  198. // There is also the same test but with image:tag@digest in docker_cli_by_digest_test.go
  199. //
  200. // What the test setups :
  201. // - Create 2 image based on busybox using the same repository but different tags
  202. // - Create an image based on the previous image (images_ps_filter_test2)
  203. // - Run containers for each of those image (busybox, images_ps_filter_test1, images_ps_filter_test2)
  204. // - Filter them out :P
  205. func (s *DockerSuite) TestPsListContainersFilterAncestorImage(c *check.C) {
  206. // TODO Windows CI (TP5 timeframe). This test has a very high likelyhood
  207. // of hitting a known bug in TP4. Hence disabling for now until TP5
  208. // is available.
  209. testRequires(c, DaemonIsLinux)
  210. // Build images
  211. imageName1 := "images_ps_filter_test1"
  212. imageID1, err := buildImage(imageName1,
  213. `FROM busybox
  214. LABEL match me 1`, true)
  215. c.Assert(err, checker.IsNil)
  216. imageName1Tagged := "images_ps_filter_test1:tag"
  217. imageID1Tagged, err := buildImage(imageName1Tagged,
  218. `FROM busybox
  219. LABEL match me 1 tagged`, true)
  220. c.Assert(err, checker.IsNil)
  221. imageName2 := "images_ps_filter_test2"
  222. imageID2, err := buildImage(imageName2,
  223. fmt.Sprintf(`FROM %s
  224. LABEL match me 2`, imageName1), true)
  225. c.Assert(err, checker.IsNil)
  226. // start containers
  227. out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "hello")
  228. firstID := strings.TrimSpace(out)
  229. // start another container
  230. out, _ = dockerCmd(c, "run", "-d", "busybox", "echo", "hello")
  231. secondID := strings.TrimSpace(out)
  232. // start third container
  233. out, _ = dockerCmd(c, "run", "-d", imageName1, "echo", "hello")
  234. thirdID := strings.TrimSpace(out)
  235. // start fourth container
  236. out, _ = dockerCmd(c, "run", "-d", imageName1Tagged, "echo", "hello")
  237. fourthID := strings.TrimSpace(out)
  238. // start fifth container
  239. out, _ = dockerCmd(c, "run", "-d", imageName2, "echo", "hello")
  240. fifthID := strings.TrimSpace(out)
  241. var filterTestSuite = []struct {
  242. filterName string
  243. expectedIDs []string
  244. }{
  245. // non existent stuff
  246. {"nonexistent", []string{}},
  247. {"nonexistent:tag", []string{}},
  248. // image
  249. {"busybox", []string{firstID, secondID, thirdID, fourthID, fifthID}},
  250. {imageName1, []string{thirdID, fifthID}},
  251. {imageName2, []string{fifthID}},
  252. // image:tag
  253. {fmt.Sprintf("%s:latest", imageName1), []string{thirdID, fifthID}},
  254. {imageName1Tagged, []string{fourthID}},
  255. // short-id
  256. {stringid.TruncateID(imageID1), []string{thirdID, fifthID}},
  257. {stringid.TruncateID(imageID2), []string{fifthID}},
  258. // full-id
  259. {imageID1, []string{thirdID, fifthID}},
  260. {imageID1Tagged, []string{fourthID}},
  261. {imageID2, []string{fifthID}},
  262. }
  263. for _, filter := range filterTestSuite {
  264. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+filter.filterName)
  265. checkPsAncestorFilterOutput(c, out, filter.filterName, filter.expectedIDs)
  266. }
  267. // Multiple ancestor filter
  268. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+imageName2, "--filter=ancestor="+imageName1Tagged)
  269. checkPsAncestorFilterOutput(c, out, imageName2+","+imageName1Tagged, []string{fourthID, fifthID})
  270. }
  271. func checkPsAncestorFilterOutput(c *check.C, out string, filterName string, expectedIDs []string) {
  272. actualIDs := []string{}
  273. if out != "" {
  274. actualIDs = strings.Split(out[:len(out)-1], "\n")
  275. }
  276. sort.Strings(actualIDs)
  277. sort.Strings(expectedIDs)
  278. c.Assert(actualIDs, checker.HasLen, len(expectedIDs), check.Commentf("Expected filtered container(s) for %s ancestor filter to be %v:%v, got %v:%v", filterName, len(expectedIDs), expectedIDs, len(actualIDs), actualIDs))
  279. if len(expectedIDs) > 0 {
  280. same := true
  281. for i := range expectedIDs {
  282. if actualIDs[i] != expectedIDs[i] {
  283. c.Logf("%s, %s", actualIDs[i], expectedIDs[i])
  284. same = false
  285. break
  286. }
  287. }
  288. c.Assert(same, checker.Equals, true, check.Commentf("Expected filtered container(s) for %s ancestor filter to be %v, got %v", filterName, expectedIDs, actualIDs))
  289. }
  290. }
  291. func (s *DockerSuite) TestPsListContainersFilterLabel(c *check.C) {
  292. // TODO Windows CI (TP5 timeframe). This test has a very high likelyhood
  293. // of hitting a known bug in TP4. Hence disabling for now until TP5
  294. // is available.
  295. testRequires(c, DaemonIsLinux)
  296. // start container
  297. out, _ := dockerCmd(c, "run", "-d", "-l", "match=me", "-l", "second=tag", "busybox")
  298. firstID := strings.TrimSpace(out)
  299. // start another container
  300. out, _ = dockerCmd(c, "run", "-d", "-l", "match=me too", "busybox")
  301. secondID := strings.TrimSpace(out)
  302. // start third container
  303. out, _ = dockerCmd(c, "run", "-d", "-l", "nomatch=me", "busybox")
  304. thirdID := strings.TrimSpace(out)
  305. // filter containers by exact match
  306. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me")
  307. containerOut := strings.TrimSpace(out)
  308. c.Assert(containerOut, checker.Equals, firstID, check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out))
  309. // filter containers by two labels
  310. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag")
  311. containerOut = strings.TrimSpace(out)
  312. c.Assert(containerOut, checker.Equals, firstID, check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out))
  313. // filter containers by two labels, but expect not found because of AND behavior
  314. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no")
  315. containerOut = strings.TrimSpace(out)
  316. c.Assert(containerOut, checker.Equals, "", check.Commentf("Expected nothing, got %s for exited filter, output: %q", containerOut, out))
  317. // filter containers by exact key
  318. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match")
  319. containerOut = strings.TrimSpace(out)
  320. c.Assert(containerOut, checker.Contains, firstID)
  321. c.Assert(containerOut, checker.Contains, secondID)
  322. c.Assert(containerOut, checker.Not(checker.Contains), thirdID)
  323. }
  324. func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) {
  325. // TODO Windows CI (TP5 timeframe). This test has a very high likelyhood
  326. // of hitting a known bug in TP4. Hence disabling for now until TP5
  327. // is available.
  328. testRequires(c, DaemonIsLinux)
  329. testRequires(c, DaemonIsLinux)
  330. runSleepingContainer(c, "--name=sleep")
  331. dockerCmd(c, "run", "--name", "zero1", "busybox", "true")
  332. firstZero, err := getIDByName("zero1")
  333. c.Assert(err, checker.IsNil)
  334. dockerCmd(c, "run", "--name", "zero2", "busybox", "true")
  335. secondZero, err := getIDByName("zero2")
  336. c.Assert(err, checker.IsNil)
  337. out, _, err := dockerCmdWithError("run", "--name", "nonzero1", "busybox", "false")
  338. c.Assert(err, checker.NotNil, check.Commentf("Should fail.", out, err))
  339. firstNonZero, err := getIDByName("nonzero1")
  340. c.Assert(err, checker.IsNil)
  341. out, _, err = dockerCmdWithError("run", "--name", "nonzero2", "busybox", "false")
  342. c.Assert(err, checker.NotNil, check.Commentf("Should fail.", out, err))
  343. secondNonZero, err := getIDByName("nonzero2")
  344. c.Assert(err, checker.IsNil)
  345. // filter containers by exited=0
  346. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
  347. ids := strings.Split(strings.TrimSpace(out), "\n")
  348. c.Assert(ids, checker.HasLen, 2, check.Commentf("Should be 2 zero exited containers got %d: %s", len(ids), out))
  349. c.Assert(ids[0], checker.Equals, secondZero, check.Commentf("First in list should be %q, got %q", secondZero, ids[0]))
  350. c.Assert(ids[1], checker.Equals, firstZero, check.Commentf("Second in list should be %q, got %q", firstZero, ids[1]))
  351. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
  352. ids = strings.Split(strings.TrimSpace(out), "\n")
  353. c.Assert(ids, checker.HasLen, 2, check.Commentf("Should be 2 zero exited containers got %d", len(ids)))
  354. c.Assert(ids[0], checker.Equals, secondNonZero, check.Commentf("First in list should be %q, got %q", secondNonZero, ids[0]))
  355. c.Assert(ids[1], checker.Equals, firstNonZero, check.Commentf("Second in list should be %q, got %q", firstNonZero, ids[1]))
  356. }
  357. func (s *DockerSuite) TestPsRightTagName(c *check.C) {
  358. // TODO Investigate further why this fails on Windows to Windows CI
  359. testRequires(c, DaemonIsLinux)
  360. tag := "asybox:shmatest"
  361. dockerCmd(c, "tag", "busybox", tag)
  362. var id1 string
  363. out, _ := runSleepingContainer(c)
  364. id1 = strings.TrimSpace(string(out))
  365. var id2 string
  366. out, _ = runSleepingContainerInImage(c, tag)
  367. id2 = strings.TrimSpace(string(out))
  368. var imageID string
  369. out = inspectField(c, "busybox", "Id")
  370. imageID = strings.TrimSpace(string(out))
  371. var id3 string
  372. out, _ = runSleepingContainerInImage(c, imageID)
  373. id3 = strings.TrimSpace(string(out))
  374. out, _ = dockerCmd(c, "ps", "--no-trunc")
  375. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  376. // skip header
  377. lines = lines[1:]
  378. c.Assert(lines, checker.HasLen, 3, check.Commentf("There should be 3 running container, got %d", len(lines)))
  379. for _, line := range lines {
  380. f := strings.Fields(line)
  381. switch f[0] {
  382. case id1:
  383. c.Assert(f[1], checker.Equals, "busybox", check.Commentf("Expected %s tag for id %s, got %s", "busybox", id1, f[1]))
  384. case id2:
  385. c.Assert(f[1], checker.Equals, tag, check.Commentf("Expected %s tag for id %s, got %s", tag, id2, f[1]))
  386. case id3:
  387. c.Assert(f[1], checker.Equals, imageID, check.Commentf("Expected %s imageID for id %s, got %s", tag, id3, f[1]))
  388. default:
  389. c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3)
  390. }
  391. }
  392. }
  393. func (s *DockerSuite) TestPsLinkedWithNoTrunc(c *check.C) {
  394. // Problematic on Windows as it doesn't support links as of Jan 2016
  395. testRequires(c, DaemonIsLinux)
  396. runSleepingContainer(c, "--name=first")
  397. runSleepingContainer(c, "--name=second", "--link=first:first")
  398. out, _ := dockerCmd(c, "ps", "--no-trunc")
  399. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  400. // strip header
  401. lines = lines[1:]
  402. expected := []string{"second", "first,second/first"}
  403. var names []string
  404. for _, l := range lines {
  405. fields := strings.Fields(l)
  406. names = append(names, fields[len(fields)-1])
  407. }
  408. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array: %v, got: %v", expected, names))
  409. }
  410. func (s *DockerSuite) TestPsGroupPortRange(c *check.C) {
  411. // Problematic on Windows as it doesn't support port ranges as of Jan 2016
  412. testRequires(c, DaemonIsLinux)
  413. portRange := "3800-3900"
  414. dockerCmd(c, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top")
  415. out, _ := dockerCmd(c, "ps")
  416. c.Assert(string(out), checker.Contains, portRange, check.Commentf("docker ps output should have had the port range %q: %s", portRange, string(out)))
  417. }
  418. func (s *DockerSuite) TestPsWithSize(c *check.C) {
  419. // Problematic on Windows as it doesn't report the size correctly @swernli
  420. testRequires(c, DaemonIsLinux)
  421. dockerCmd(c, "run", "-d", "--name", "sizetest", "busybox", "top")
  422. out, _ := dockerCmd(c, "ps", "--size")
  423. c.Assert(out, checker.Contains, "virtual", check.Commentf("docker ps with --size should show virtual size of container"))
  424. }
  425. func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) {
  426. // create a container
  427. out, _ := dockerCmd(c, "create", "busybox")
  428. cID := strings.TrimSpace(out)
  429. shortCID := cID[:12]
  430. // Make sure it DOESN'T show up w/o a '-a' for normal 'ps'
  431. out, _ = dockerCmd(c, "ps", "-q")
  432. c.Assert(out, checker.Not(checker.Contains), shortCID, check.Commentf("Should have not seen '%s' in ps output:\n%s", shortCID, out))
  433. // Make sure it DOES show up as 'Created' for 'ps -a'
  434. out, _ = dockerCmd(c, "ps", "-a")
  435. hits := 0
  436. for _, line := range strings.Split(out, "\n") {
  437. if !strings.Contains(line, shortCID) {
  438. continue
  439. }
  440. hits++
  441. c.Assert(line, checker.Contains, "Created", check.Commentf("Missing 'Created' on '%s'", line))
  442. }
  443. c.Assert(hits, checker.Equals, 1, check.Commentf("Should have seen '%s' in ps -a output once:%d\n%s", shortCID, hits, out))
  444. // filter containers by 'create' - note, no -a needed
  445. out, _ = dockerCmd(c, "ps", "-q", "-f", "status=created")
  446. containerOut := strings.TrimSpace(out)
  447. c.Assert(cID, checker.HasPrefix, containerOut)
  448. }
  449. func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) {
  450. // Problematic on Windows as it doesn't support link as of Jan 2016
  451. testRequires(c, DaemonIsLinux)
  452. //create 2 containers and link them
  453. dockerCmd(c, "run", "--name=child", "-d", "busybox", "top")
  454. dockerCmd(c, "run", "--name=parent", "--link=child:linkedone", "-d", "busybox", "top")
  455. //use the new format capabilities to only list the names and --no-trunc to get all names
  456. out, _ := dockerCmd(c, "ps", "--format", "{{.Names}}", "--no-trunc")
  457. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  458. expected := []string{"parent", "child,parent/linkedone"}
  459. var names []string
  460. for _, l := range lines {
  461. names = append(names, l)
  462. }
  463. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with non-truncated names: %v, got: %v", expected, names))
  464. //now list without turning off truncation and make sure we only get the non-link names
  465. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}}")
  466. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  467. expected = []string{"parent", "child"}
  468. var truncNames []string
  469. for _, l := range lines {
  470. truncNames = append(truncNames, l)
  471. }
  472. c.Assert(expected, checker.DeepEquals, truncNames, check.Commentf("Expected array with truncated names: %v, got: %v", expected, truncNames))
  473. }
  474. func (s *DockerSuite) TestPsFormatHeaders(c *check.C) {
  475. // make sure no-container "docker ps" still prints the header row
  476. out, _ := dockerCmd(c, "ps", "--format", "table {{.ID}}")
  477. c.Assert(out, checker.Equals, "CONTAINER ID\n", check.Commentf(`Expected 'CONTAINER ID\n', got %v`, out))
  478. // verify that "docker ps" with a container still prints the header row also
  479. runSleepingContainer(c, "--name=test")
  480. out, _ = dockerCmd(c, "ps", "--format", "table {{.Names}}")
  481. c.Assert(out, checker.Equals, "NAMES\ntest\n", check.Commentf(`Expected 'NAMES\ntest\n', got %v`, out))
  482. }
  483. func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) {
  484. config := `{
  485. "psFormat": "default {{ .ID }}"
  486. }`
  487. d, err := ioutil.TempDir("", "integration-cli-")
  488. c.Assert(err, checker.IsNil)
  489. defer os.RemoveAll(d)
  490. err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
  491. c.Assert(err, checker.IsNil)
  492. out, _ := runSleepingContainer(c, "--name=test")
  493. id := strings.TrimSpace(out)
  494. out, _ = dockerCmd(c, "--config", d, "ps", "-q")
  495. c.Assert(id, checker.HasPrefix, strings.TrimSpace(out), check.Commentf("Expected to print only the container id, got %v\n", out))
  496. }
  497. // Test for GitHub issue #12595
  498. func (s *DockerSuite) TestPsImageIDAfterUpdate(c *check.C) {
  499. // TODO: Investigate why this fails on Windows to Windows CI further.
  500. testRequires(c, DaemonIsLinux)
  501. originalImageName := "busybox:TestPsImageIDAfterUpdate-original"
  502. updatedImageName := "busybox:TestPsImageIDAfterUpdate-updated"
  503. runCmd := exec.Command(dockerBinary, "tag", "busybox:latest", originalImageName)
  504. out, _, err := runCommandWithOutput(runCmd)
  505. c.Assert(err, checker.IsNil)
  506. originalImageID, err := getIDByName(originalImageName)
  507. c.Assert(err, checker.IsNil)
  508. runCmd = exec.Command(dockerBinary, append([]string{"run", "-d", originalImageName}, defaultSleepCommand...)...)
  509. out, _, err = runCommandWithOutput(runCmd)
  510. c.Assert(err, checker.IsNil)
  511. containerID := strings.TrimSpace(out)
  512. linesOut, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  513. c.Assert(err, checker.IsNil)
  514. lines := strings.Split(strings.TrimSpace(string(linesOut)), "\n")
  515. // skip header
  516. lines = lines[1:]
  517. c.Assert(len(lines), checker.Equals, 1)
  518. for _, line := range lines {
  519. f := strings.Fields(line)
  520. c.Assert(f[1], checker.Equals, originalImageName)
  521. }
  522. runCmd = exec.Command(dockerBinary, "commit", containerID, updatedImageName)
  523. out, _, err = runCommandWithOutput(runCmd)
  524. c.Assert(err, checker.IsNil)
  525. runCmd = exec.Command(dockerBinary, "tag", "-f", updatedImageName, originalImageName)
  526. out, _, err = runCommandWithOutput(runCmd)
  527. c.Assert(err, checker.IsNil)
  528. linesOut, err = exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  529. c.Assert(err, checker.IsNil)
  530. lines = strings.Split(strings.TrimSpace(string(linesOut)), "\n")
  531. // skip header
  532. lines = lines[1:]
  533. c.Assert(len(lines), checker.Equals, 1)
  534. for _, line := range lines {
  535. f := strings.Fields(line)
  536. c.Assert(f[1], checker.Equals, originalImageID)
  537. }
  538. }
  539. func (s *DockerSuite) TestPsNotShowPortsOfStoppedContainer(c *check.C) {
  540. testRequires(c, DaemonIsLinux)
  541. dockerCmd(c, "run", "--name=foo", "-d", "-p", "5000:5000", "busybox", "top")
  542. c.Assert(waitRun("foo"), checker.IsNil)
  543. out, _ := dockerCmd(c, "ps")
  544. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  545. expected := "0.0.0.0:5000->5000/tcp"
  546. fields := strings.Fields(lines[1])
  547. c.Assert(fields[len(fields)-2], checker.Equals, expected, check.Commentf("Expected: %v, got: %v", expected, fields[len(fields)-2]))
  548. dockerCmd(c, "kill", "foo")
  549. dockerCmd(c, "wait", "foo")
  550. out, _ = dockerCmd(c, "ps", "-l")
  551. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  552. fields = strings.Fields(lines[1])
  553. c.Assert(fields[len(fields)-2], checker.Not(checker.Equals), expected, check.Commentf("Should not got %v", expected))
  554. }