docker_cli_ps_test.go 32 KB

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