docker_cli_ps_test.go 26 KB

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