docker_cli_ps_test.go 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "sort"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/docker/docker/integration-cli/checker"
  12. "github.com/docker/docker/pkg/stringid"
  13. icmd "github.com/docker/docker/pkg/testutil/cmd"
  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. out, _ = dockerCmd(c, "ps", "-f", "since="+thirdID)
  52. expected = []string{fourthID}
  53. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
  54. // filter before
  55. out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-a")
  56. expected = []string{thirdID, secondID, firstID}
  57. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter & ALL: Container list is not in the correct order: \n%s", out))
  58. out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID)
  59. expected = []string{secondID, firstID}
  60. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Container list is not in the correct order: \n%s", out))
  61. out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID)
  62. expected = []string{secondID, firstID}
  63. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
  64. // filter since & before
  65. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-a")
  66. expected = []string{thirdID, secondID}
  67. 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))
  68. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID)
  69. expected = []string{secondID}
  70. 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))
  71. // filter since & limit
  72. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2", "-a")
  73. expected = []string{fourthID, thirdID}
  74. 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))
  75. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2")
  76. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, LIMIT: Container list is not in the correct order: \n%s", out))
  77. // filter before & limit
  78. out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1", "-a")
  79. expected = []string{thirdID}
  80. 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))
  81. out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1")
  82. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter, LIMIT: Container list is not in the correct order: \n%s", out))
  83. // filter since & filter before & limit
  84. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1", "-a")
  85. expected = []string{thirdID}
  86. 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))
  87. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1")
  88. 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))
  89. }
  90. func assertContainerList(out string, expected []string) bool {
  91. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  92. if len(lines)-1 != len(expected) {
  93. return false
  94. }
  95. containerIDIndex := strings.Index(lines[0], "CONTAINER ID")
  96. for i := 0; i < len(expected); i++ {
  97. foundID := lines[i+1][containerIDIndex : containerIDIndex+12]
  98. if foundID != expected[i][:12] {
  99. return false
  100. }
  101. }
  102. return true
  103. }
  104. // FIXME(vdemeester) Move this into a unit test in daemon package
  105. func (s *DockerSuite) TestPsListContainersInvalidFilterName(c *check.C) {
  106. out, _, err := dockerCmdWithError("ps", "-f", "invalidFilter=test")
  107. c.Assert(err, checker.NotNil)
  108. c.Assert(out, checker.Contains, "Invalid filter")
  109. }
  110. func (s *DockerSuite) TestPsListContainersSize(c *check.C) {
  111. // Problematic on Windows as it doesn't report the size correctly @swernli
  112. testRequires(c, DaemonIsLinux)
  113. dockerCmd(c, "run", "-d", "busybox")
  114. baseOut, _ := dockerCmd(c, "ps", "-s", "-n=1")
  115. baseLines := strings.Split(strings.Trim(baseOut, "\n "), "\n")
  116. baseSizeIndex := strings.Index(baseLines[0], "SIZE")
  117. baseFoundsize := baseLines[1][baseSizeIndex:]
  118. baseBytes, err := strconv.Atoi(strings.Split(baseFoundsize, "B")[0])
  119. c.Assert(err, checker.IsNil)
  120. name := "test_size"
  121. dockerCmd(c, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test")
  122. id := getIDByName(c, name)
  123. var result *icmd.Result
  124. wait := make(chan struct{})
  125. go func() {
  126. result = icmd.RunCommand(dockerBinary, "ps", "-s", "-n=1")
  127. close(wait)
  128. }()
  129. select {
  130. case <-wait:
  131. case <-time.After(3 * time.Second):
  132. c.Fatalf("Calling \"docker ps -s\" timed out!")
  133. }
  134. result.Assert(c, icmd.Success)
  135. lines := strings.Split(strings.Trim(result.Combined(), "\n "), "\n")
  136. c.Assert(lines, checker.HasLen, 2, check.Commentf("Expected 2 lines for 'ps -s -n=1' output, got %d", len(lines)))
  137. sizeIndex := strings.Index(lines[0], "SIZE")
  138. idIndex := strings.Index(lines[0], "CONTAINER ID")
  139. foundID := lines[1][idIndex : idIndex+12]
  140. c.Assert(foundID, checker.Equals, id[:12], check.Commentf("Expected id %s, got %s", id[:12], foundID))
  141. expectedSize := fmt.Sprintf("%dB", (2 + baseBytes))
  142. foundSize := lines[1][sizeIndex:]
  143. c.Assert(foundSize, checker.Contains, expectedSize, check.Commentf("Expected size %q, got %q", expectedSize, foundSize))
  144. }
  145. func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) {
  146. // start exited container
  147. out, _ := dockerCmd(c, "run", "-d", "busybox")
  148. firstID := strings.TrimSpace(out)
  149. // make sure the exited container is not running
  150. dockerCmd(c, "wait", firstID)
  151. // start running container
  152. out, _ = dockerCmd(c, "run", "-itd", "busybox")
  153. secondID := strings.TrimSpace(out)
  154. // filter containers by exited
  155. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter=status=exited")
  156. containerOut := strings.TrimSpace(out)
  157. c.Assert(containerOut, checker.Equals, firstID)
  158. out, _ = dockerCmd(c, "ps", "-a", "--no-trunc", "-q", "--filter=status=running")
  159. containerOut = strings.TrimSpace(out)
  160. c.Assert(containerOut, checker.Equals, secondID)
  161. result := dockerCmdWithTimeout(time.Second*60, "ps", "-a", "-q", "--filter=status=rubbish")
  162. c.Assert(result, icmd.Matches, icmd.Expected{
  163. ExitCode: 1,
  164. Err: "Unrecognised filter value for status",
  165. })
  166. // Windows doesn't support pausing of containers
  167. if testEnv.DaemonPlatform() != "windows" {
  168. // pause running container
  169. out, _ = dockerCmd(c, "run", "-itd", "busybox")
  170. pausedID := strings.TrimSpace(out)
  171. dockerCmd(c, "pause", pausedID)
  172. // make sure the container is unpaused to let the daemon stop it properly
  173. defer func() { dockerCmd(c, "unpause", pausedID) }()
  174. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter=status=paused")
  175. containerOut = strings.TrimSpace(out)
  176. c.Assert(containerOut, checker.Equals, pausedID)
  177. }
  178. }
  179. func (s *DockerSuite) TestPsListContainersFilterHealth(c *check.C) {
  180. // Test legacy no health check
  181. out, _ := runSleepingContainer(c, "--name=none_legacy")
  182. containerID := strings.TrimSpace(out)
  183. waitForContainer(containerID)
  184. out, _ = dockerCmd(c, "ps", "-q", "-l", "--no-trunc", "--filter=health=none")
  185. containerOut := strings.TrimSpace(out)
  186. c.Assert(containerOut, checker.Equals, containerID, check.Commentf("Expected id %s, got %s for legacy none filter, output: %q", containerID, containerOut, out))
  187. // Test no health check specified explicitly
  188. out, _ = runSleepingContainer(c, "--name=none", "--no-healthcheck")
  189. containerID = strings.TrimSpace(out)
  190. waitForContainer(containerID)
  191. out, _ = dockerCmd(c, "ps", "-q", "-l", "--no-trunc", "--filter=health=none")
  192. containerOut = strings.TrimSpace(out)
  193. c.Assert(containerOut, checker.Equals, containerID, check.Commentf("Expected id %s, got %s for none filter, output: %q", containerID, containerOut, out))
  194. // Test failing health check
  195. out, _ = runSleepingContainer(c, "--name=failing_container", "--health-cmd=exit 1", "--health-interval=1s")
  196. containerID = strings.TrimSpace(out)
  197. waitForHealthStatus(c, "failing_container", "starting", "unhealthy")
  198. out, _ = dockerCmd(c, "ps", "-q", "--no-trunc", "--filter=health=unhealthy")
  199. containerOut = strings.TrimSpace(out)
  200. c.Assert(containerOut, checker.Equals, containerID, check.Commentf("Expected containerID %s, got %s for unhealthy filter, output: %q", containerID, containerOut, out))
  201. // Check passing healthcheck
  202. out, _ = runSleepingContainer(c, "--name=passing_container", "--health-cmd=exit 0", "--health-interval=1s")
  203. containerID = strings.TrimSpace(out)
  204. waitForHealthStatus(c, "passing_container", "starting", "healthy")
  205. out, _ = dockerCmd(c, "ps", "-q", "--no-trunc", "--filter=health=healthy")
  206. containerOut = strings.TrimSpace(out)
  207. c.Assert(containerOut, checker.Equals, containerID, check.Commentf("Expected containerID %s, got %s for healthy filter, output: %q", containerID, containerOut, out))
  208. }
  209. func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) {
  210. // start container
  211. out, _ := dockerCmd(c, "run", "-d", "busybox")
  212. firstID := strings.TrimSpace(out)
  213. // start another container
  214. runSleepingContainer(c)
  215. // filter containers by id
  216. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=id="+firstID)
  217. containerOut := strings.TrimSpace(out)
  218. c.Assert(containerOut, checker.Equals, firstID[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out))
  219. }
  220. func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) {
  221. // start container
  222. dockerCmd(c, "run", "--name=a_name_to_match", "busybox")
  223. id := getIDByName(c, "a_name_to_match")
  224. // start another container
  225. runSleepingContainer(c, "--name=b_name_to_match")
  226. // filter containers by name
  227. out, _ := dockerCmd(c, "ps", "-a", "-q", "--filter=name=a_name_to_match")
  228. containerOut := strings.TrimSpace(out)
  229. c.Assert(containerOut, checker.Equals, id[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", id[:12], containerOut, out))
  230. }
  231. // Test for the ancestor filter for ps.
  232. // There is also the same test but with image:tag@digest in docker_cli_by_digest_test.go
  233. //
  234. // What the test setups :
  235. // - Create 2 image based on busybox using the same repository but different tags
  236. // - Create an image based on the previous image (images_ps_filter_test2)
  237. // - Run containers for each of those image (busybox, images_ps_filter_test1, images_ps_filter_test2)
  238. // - Filter them out :P
  239. func (s *DockerSuite) TestPsListContainersFilterAncestorImage(c *check.C) {
  240. // Build images
  241. imageName1 := "images_ps_filter_test1"
  242. buildImageSuccessfully(c, imageName1, withDockerfile(`FROM busybox
  243. LABEL match me 1`))
  244. imageID1 := getIDByName(c, imageName1)
  245. imageName1Tagged := "images_ps_filter_test1:tag"
  246. buildImageSuccessfully(c, imageName1Tagged, withDockerfile(`FROM busybox
  247. LABEL match me 1 tagged`))
  248. imageID1Tagged := getIDByName(c, imageName1Tagged)
  249. imageName2 := "images_ps_filter_test2"
  250. buildImageSuccessfully(c, imageName2, withDockerfile(fmt.Sprintf(`FROM %s
  251. LABEL match me 2`, imageName1)))
  252. imageID2 := getIDByName(c, imageName2)
  253. // start containers
  254. dockerCmd(c, "run", "--name=first", "busybox", "echo", "hello")
  255. firstID := getIDByName(c, "first")
  256. // start another container
  257. dockerCmd(c, "run", "--name=second", "busybox", "echo", "hello")
  258. secondID := getIDByName(c, "second")
  259. // start third container
  260. dockerCmd(c, "run", "--name=third", imageName1, "echo", "hello")
  261. thirdID := getIDByName(c, "third")
  262. // start fourth container
  263. dockerCmd(c, "run", "--name=fourth", imageName1Tagged, "echo", "hello")
  264. fourthID := getIDByName(c, "fourth")
  265. // start fifth container
  266. dockerCmd(c, "run", "--name=fifth", imageName2, "echo", "hello")
  267. fifthID := getIDByName(c, "fifth")
  268. var filterTestSuite = []struct {
  269. filterName string
  270. expectedIDs []string
  271. }{
  272. // non existent stuff
  273. {"nonexistent", []string{}},
  274. {"nonexistent:tag", []string{}},
  275. // image
  276. {"busybox", []string{firstID, secondID, thirdID, fourthID, fifthID}},
  277. {imageName1, []string{thirdID, fifthID}},
  278. {imageName2, []string{fifthID}},
  279. // image:tag
  280. {fmt.Sprintf("%s:latest", imageName1), []string{thirdID, fifthID}},
  281. {imageName1Tagged, []string{fourthID}},
  282. // short-id
  283. {stringid.TruncateID(imageID1), []string{thirdID, fifthID}},
  284. {stringid.TruncateID(imageID2), []string{fifthID}},
  285. // full-id
  286. {imageID1, []string{thirdID, fifthID}},
  287. {imageID1Tagged, []string{fourthID}},
  288. {imageID2, []string{fifthID}},
  289. }
  290. var out string
  291. for _, filter := range filterTestSuite {
  292. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+filter.filterName)
  293. checkPsAncestorFilterOutput(c, out, filter.filterName, filter.expectedIDs)
  294. }
  295. // Multiple ancestor filter
  296. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+imageName2, "--filter=ancestor="+imageName1Tagged)
  297. checkPsAncestorFilterOutput(c, out, imageName2+","+imageName1Tagged, []string{fourthID, fifthID})
  298. }
  299. func checkPsAncestorFilterOutput(c *check.C, out string, filterName string, expectedIDs []string) {
  300. actualIDs := []string{}
  301. if out != "" {
  302. actualIDs = strings.Split(out[:len(out)-1], "\n")
  303. }
  304. sort.Strings(actualIDs)
  305. sort.Strings(expectedIDs)
  306. 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))
  307. if len(expectedIDs) > 0 {
  308. same := true
  309. for i := range expectedIDs {
  310. if actualIDs[i] != expectedIDs[i] {
  311. c.Logf("%s, %s", actualIDs[i], expectedIDs[i])
  312. same = false
  313. break
  314. }
  315. }
  316. c.Assert(same, checker.Equals, true, check.Commentf("Expected filtered container(s) for %s ancestor filter to be %v, got %v", filterName, expectedIDs, actualIDs))
  317. }
  318. }
  319. func (s *DockerSuite) TestPsListContainersFilterLabel(c *check.C) {
  320. // start container
  321. dockerCmd(c, "run", "--name=first", "-l", "match=me", "-l", "second=tag", "busybox")
  322. firstID := getIDByName(c, "first")
  323. // start another container
  324. dockerCmd(c, "run", "--name=second", "-l", "match=me too", "busybox")
  325. secondID := getIDByName(c, "second")
  326. // start third container
  327. dockerCmd(c, "run", "--name=third", "-l", "nomatch=me", "busybox")
  328. thirdID := getIDByName(c, "third")
  329. // filter containers by exact match
  330. out, _ := dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me")
  331. containerOut := strings.TrimSpace(out)
  332. c.Assert(containerOut, checker.Equals, firstID, check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out))
  333. // filter containers by two labels
  334. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag")
  335. containerOut = strings.TrimSpace(out)
  336. c.Assert(containerOut, checker.Equals, firstID, check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out))
  337. // filter containers by two labels, but expect not found because of AND behavior
  338. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no")
  339. containerOut = strings.TrimSpace(out)
  340. c.Assert(containerOut, checker.Equals, "", check.Commentf("Expected nothing, got %s for exited filter, output: %q", containerOut, out))
  341. // filter containers by exact key
  342. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match")
  343. containerOut = strings.TrimSpace(out)
  344. c.Assert(containerOut, checker.Contains, firstID)
  345. c.Assert(containerOut, checker.Contains, secondID)
  346. c.Assert(containerOut, checker.Not(checker.Contains), thirdID)
  347. }
  348. func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) {
  349. runSleepingContainer(c, "--name=sleep")
  350. dockerCmd(c, "run", "--name", "zero1", "busybox", "true")
  351. firstZero := getIDByName(c, "zero1")
  352. dockerCmd(c, "run", "--name", "zero2", "busybox", "true")
  353. secondZero := getIDByName(c, "zero2")
  354. out, _, err := dockerCmdWithError("run", "--name", "nonzero1", "busybox", "false")
  355. c.Assert(err, checker.NotNil, check.Commentf("Should fail.", out, err))
  356. firstNonZero := getIDByName(c, "nonzero1")
  357. out, _, err = dockerCmdWithError("run", "--name", "nonzero2", "busybox", "false")
  358. c.Assert(err, checker.NotNil, check.Commentf("Should fail.", out, err))
  359. secondNonZero := getIDByName(c, "nonzero2")
  360. // filter containers by exited=0
  361. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
  362. ids := strings.Split(strings.TrimSpace(out), "\n")
  363. c.Assert(ids, checker.HasLen, 2, check.Commentf("Should be 2 zero exited containers got %d: %s", len(ids), out))
  364. c.Assert(ids[0], checker.Equals, secondZero, check.Commentf("First in list should be %q, got %q", secondZero, ids[0]))
  365. c.Assert(ids[1], checker.Equals, firstZero, check.Commentf("Second in list should be %q, got %q", firstZero, ids[1]))
  366. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
  367. ids = strings.Split(strings.TrimSpace(out), "\n")
  368. c.Assert(ids, checker.HasLen, 2, check.Commentf("Should be 2 zero exited containers got %d", len(ids)))
  369. c.Assert(ids[0], checker.Equals, secondNonZero, check.Commentf("First in list should be %q, got %q", secondNonZero, ids[0]))
  370. c.Assert(ids[1], checker.Equals, firstNonZero, check.Commentf("Second in list should be %q, got %q", firstNonZero, ids[1]))
  371. }
  372. func (s *DockerSuite) TestPsRightTagName(c *check.C) {
  373. // TODO Investigate further why this fails on Windows to Windows CI
  374. testRequires(c, DaemonIsLinux)
  375. tag := "asybox:shmatest"
  376. dockerCmd(c, "tag", "busybox", tag)
  377. var id1 string
  378. out, _ := runSleepingContainer(c)
  379. id1 = strings.TrimSpace(string(out))
  380. var id2 string
  381. out, _ = runSleepingContainerInImage(c, tag)
  382. id2 = strings.TrimSpace(string(out))
  383. var imageID string
  384. out = inspectField(c, "busybox", "Id")
  385. imageID = strings.TrimSpace(string(out))
  386. var id3 string
  387. out, _ = runSleepingContainerInImage(c, imageID)
  388. id3 = strings.TrimSpace(string(out))
  389. out, _ = dockerCmd(c, "ps", "--no-trunc")
  390. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  391. // skip header
  392. lines = lines[1:]
  393. c.Assert(lines, checker.HasLen, 3, check.Commentf("There should be 3 running container, got %d", len(lines)))
  394. for _, line := range lines {
  395. f := strings.Fields(line)
  396. switch f[0] {
  397. case id1:
  398. c.Assert(f[1], checker.Equals, "busybox", check.Commentf("Expected %s tag for id %s, got %s", "busybox", id1, f[1]))
  399. case id2:
  400. c.Assert(f[1], checker.Equals, tag, check.Commentf("Expected %s tag for id %s, got %s", tag, id2, f[1]))
  401. case id3:
  402. c.Assert(f[1], checker.Equals, imageID, check.Commentf("Expected %s imageID for id %s, got %s", tag, id3, f[1]))
  403. default:
  404. c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3)
  405. }
  406. }
  407. }
  408. func (s *DockerSuite) TestPsLinkedWithNoTrunc(c *check.C) {
  409. // Problematic on Windows as it doesn't support links as of Jan 2016
  410. testRequires(c, DaemonIsLinux)
  411. runSleepingContainer(c, "--name=first")
  412. runSleepingContainer(c, "--name=second", "--link=first:first")
  413. out, _ := dockerCmd(c, "ps", "--no-trunc")
  414. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  415. // strip header
  416. lines = lines[1:]
  417. expected := []string{"second", "first,second/first"}
  418. var names []string
  419. for _, l := range lines {
  420. fields := strings.Fields(l)
  421. names = append(names, fields[len(fields)-1])
  422. }
  423. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array: %v, got: %v", expected, names))
  424. }
  425. func (s *DockerSuite) TestPsGroupPortRange(c *check.C) {
  426. // Problematic on Windows as it doesn't support port ranges as of Jan 2016
  427. testRequires(c, DaemonIsLinux)
  428. portRange := "3850-3900"
  429. dockerCmd(c, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top")
  430. out, _ := dockerCmd(c, "ps")
  431. c.Assert(string(out), checker.Contains, portRange, check.Commentf("docker ps output should have had the port range %q: %s", portRange, string(out)))
  432. }
  433. func (s *DockerSuite) TestPsWithSize(c *check.C) {
  434. // Problematic on Windows as it doesn't report the size correctly @swernli
  435. testRequires(c, DaemonIsLinux)
  436. dockerCmd(c, "run", "-d", "--name", "sizetest", "busybox", "top")
  437. out, _ := dockerCmd(c, "ps", "--size")
  438. c.Assert(out, checker.Contains, "virtual", check.Commentf("docker ps with --size should show virtual size of container"))
  439. }
  440. func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) {
  441. // create a container
  442. out, _ := dockerCmd(c, "create", "busybox")
  443. cID := strings.TrimSpace(out)
  444. shortCID := cID[:12]
  445. // Make sure it DOESN'T show up w/o a '-a' for normal 'ps'
  446. out, _ = dockerCmd(c, "ps", "-q")
  447. c.Assert(out, checker.Not(checker.Contains), shortCID, check.Commentf("Should have not seen '%s' in ps output:\n%s", shortCID, out))
  448. // Make sure it DOES show up as 'Created' for 'ps -a'
  449. out, _ = dockerCmd(c, "ps", "-a")
  450. hits := 0
  451. for _, line := range strings.Split(out, "\n") {
  452. if !strings.Contains(line, shortCID) {
  453. continue
  454. }
  455. hits++
  456. c.Assert(line, checker.Contains, "Created", check.Commentf("Missing 'Created' on '%s'", line))
  457. }
  458. c.Assert(hits, checker.Equals, 1, check.Commentf("Should have seen '%s' in ps -a output once:%d\n%s", shortCID, hits, out))
  459. // filter containers by 'create' - note, no -a needed
  460. out, _ = dockerCmd(c, "ps", "-q", "-f", "status=created")
  461. containerOut := strings.TrimSpace(out)
  462. c.Assert(cID, checker.HasPrefix, containerOut)
  463. }
  464. func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) {
  465. // Problematic on Windows as it doesn't support link as of Jan 2016
  466. testRequires(c, DaemonIsLinux)
  467. //create 2 containers and link them
  468. dockerCmd(c, "run", "--name=child", "-d", "busybox", "top")
  469. dockerCmd(c, "run", "--name=parent", "--link=child:linkedone", "-d", "busybox", "top")
  470. //use the new format capabilities to only list the names and --no-trunc to get all names
  471. out, _ := dockerCmd(c, "ps", "--format", "{{.Names}}", "--no-trunc")
  472. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  473. expected := []string{"parent", "child,parent/linkedone"}
  474. var names []string
  475. names = append(names, lines...)
  476. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with non-truncated names: %v, got: %v", expected, names))
  477. //now list without turning off truncation and make sure we only get the non-link names
  478. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}}")
  479. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  480. expected = []string{"parent", "child"}
  481. var truncNames []string
  482. truncNames = append(truncNames, lines...)
  483. c.Assert(expected, checker.DeepEquals, truncNames, check.Commentf("Expected array with truncated names: %v, got: %v", expected, truncNames))
  484. }
  485. // Test for GitHub issue #21772
  486. func (s *DockerSuite) TestPsNamesMultipleTime(c *check.C) {
  487. runSleepingContainer(c, "--name=test1")
  488. runSleepingContainer(c, "--name=test2")
  489. //use the new format capabilities to list the names twice
  490. out, _ := dockerCmd(c, "ps", "--format", "{{.Names}} {{.Names}}")
  491. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  492. expected := []string{"test2 test2", "test1 test1"}
  493. var names []string
  494. names = append(names, lines...)
  495. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with names displayed twice: %v, got: %v", expected, names))
  496. }
  497. func (s *DockerSuite) TestPsFormatHeaders(c *check.C) {
  498. // make sure no-container "docker ps" still prints the header row
  499. out, _ := dockerCmd(c, "ps", "--format", "table {{.ID}}")
  500. c.Assert(out, checker.Equals, "CONTAINER ID\n", check.Commentf(`Expected 'CONTAINER ID\n', got %v`, out))
  501. // verify that "docker ps" with a container still prints the header row also
  502. runSleepingContainer(c, "--name=test")
  503. out, _ = dockerCmd(c, "ps", "--format", "table {{.Names}}")
  504. c.Assert(out, checker.Equals, "NAMES\ntest\n", check.Commentf(`Expected 'NAMES\ntest\n', got %v`, out))
  505. }
  506. func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) {
  507. config := `{
  508. "psFormat": "default {{ .ID }}"
  509. }`
  510. d, err := ioutil.TempDir("", "integration-cli-")
  511. c.Assert(err, checker.IsNil)
  512. defer os.RemoveAll(d)
  513. err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
  514. c.Assert(err, checker.IsNil)
  515. out, _ := runSleepingContainer(c, "--name=test")
  516. id := strings.TrimSpace(out)
  517. out, _ = dockerCmd(c, "--config", d, "ps", "-q")
  518. c.Assert(id, checker.HasPrefix, strings.TrimSpace(out), check.Commentf("Expected to print only the container id, got %v\n", out))
  519. }
  520. // Test for GitHub issue #12595
  521. func (s *DockerSuite) TestPsImageIDAfterUpdate(c *check.C) {
  522. // TODO: Investigate why this fails on Windows to Windows CI further.
  523. testRequires(c, DaemonIsLinux)
  524. originalImageName := "busybox:TestPsImageIDAfterUpdate-original"
  525. updatedImageName := "busybox:TestPsImageIDAfterUpdate-updated"
  526. icmd.RunCommand(dockerBinary, "tag", "busybox:latest", originalImageName).Assert(c, icmd.Success)
  527. originalImageID := getIDByName(c, originalImageName)
  528. result := icmd.RunCommand(dockerBinary, append([]string{"run", "-d", originalImageName}, sleepCommandForDaemonPlatform()...)...)
  529. result.Assert(c, icmd.Success)
  530. containerID := strings.TrimSpace(result.Combined())
  531. result = icmd.RunCommand(dockerBinary, "ps", "--no-trunc")
  532. result.Assert(c, icmd.Success)
  533. lines := strings.Split(strings.TrimSpace(string(result.Combined())), "\n")
  534. // skip header
  535. lines = lines[1:]
  536. c.Assert(len(lines), checker.Equals, 1)
  537. for _, line := range lines {
  538. f := strings.Fields(line)
  539. c.Assert(f[1], checker.Equals, originalImageName)
  540. }
  541. icmd.RunCommand(dockerBinary, "commit", containerID, updatedImageName).Assert(c, icmd.Success)
  542. icmd.RunCommand(dockerBinary, "tag", updatedImageName, originalImageName).Assert(c, icmd.Success)
  543. result = icmd.RunCommand(dockerBinary, "ps", "--no-trunc")
  544. result.Assert(c, icmd.Success)
  545. lines = strings.Split(strings.TrimSpace(string(result.Combined())), "\n")
  546. // skip header
  547. lines = lines[1:]
  548. c.Assert(len(lines), checker.Equals, 1)
  549. for _, line := range lines {
  550. f := strings.Fields(line)
  551. c.Assert(f[1], checker.Equals, originalImageID)
  552. }
  553. }
  554. func (s *DockerSuite) TestPsNotShowPortsOfStoppedContainer(c *check.C) {
  555. testRequires(c, DaemonIsLinux)
  556. dockerCmd(c, "run", "--name=foo", "-d", "-p", "5000:5000", "busybox", "top")
  557. c.Assert(waitRun("foo"), checker.IsNil)
  558. out, _ := dockerCmd(c, "ps")
  559. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  560. expected := "0.0.0.0:5000->5000/tcp"
  561. fields := strings.Fields(lines[1])
  562. c.Assert(fields[len(fields)-2], checker.Equals, expected, check.Commentf("Expected: %v, got: %v", expected, fields[len(fields)-2]))
  563. dockerCmd(c, "kill", "foo")
  564. dockerCmd(c, "wait", "foo")
  565. out, _ = dockerCmd(c, "ps", "-l")
  566. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  567. fields = strings.Fields(lines[1])
  568. c.Assert(fields[len(fields)-2], checker.Not(checker.Equals), expected, check.Commentf("Should not got %v", expected))
  569. }
  570. func (s *DockerSuite) TestPsShowMounts(c *check.C) {
  571. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  572. mp := prefix + slash + "test"
  573. dockerCmd(c, "volume", "create", "ps-volume-test")
  574. // volume mount containers
  575. runSleepingContainer(c, "--name=volume-test-1", "--volume", "ps-volume-test:"+mp)
  576. c.Assert(waitRun("volume-test-1"), checker.IsNil)
  577. runSleepingContainer(c, "--name=volume-test-2", "--volume", mp)
  578. c.Assert(waitRun("volume-test-2"), checker.IsNil)
  579. // bind mount container
  580. var bindMountSource string
  581. var bindMountDestination string
  582. if DaemonIsWindows() {
  583. bindMountSource = "c:\\"
  584. bindMountDestination = "c:\\t"
  585. } else {
  586. bindMountSource = "/tmp"
  587. bindMountDestination = "/t"
  588. }
  589. runSleepingContainer(c, "--name=bind-mount-test", "-v", bindMountSource+":"+bindMountDestination)
  590. c.Assert(waitRun("bind-mount-test"), checker.IsNil)
  591. out, _ := dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}")
  592. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  593. c.Assert(lines, checker.HasLen, 3)
  594. fields := strings.Fields(lines[0])
  595. c.Assert(fields, checker.HasLen, 2)
  596. c.Assert(fields[0], checker.Equals, "bind-mount-test")
  597. c.Assert(fields[1], checker.Equals, bindMountSource)
  598. fields = strings.Fields(lines[1])
  599. c.Assert(fields, checker.HasLen, 2)
  600. annonymounsVolumeID := fields[1]
  601. fields = strings.Fields(lines[2])
  602. c.Assert(fields[1], checker.Equals, "ps-volume-test")
  603. // filter by volume name
  604. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume=ps-volume-test")
  605. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  606. c.Assert(lines, checker.HasLen, 1)
  607. fields = strings.Fields(lines[0])
  608. c.Assert(fields[1], checker.Equals, "ps-volume-test")
  609. // empty results filtering by unknown volume
  610. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume=this-volume-should-not-exist")
  611. c.Assert(strings.TrimSpace(string(out)), checker.HasLen, 0)
  612. // filter by mount destination
  613. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+mp)
  614. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  615. c.Assert(lines, checker.HasLen, 2)
  616. fields = strings.Fields(lines[0])
  617. c.Assert(fields[1], checker.Equals, annonymounsVolumeID)
  618. fields = strings.Fields(lines[1])
  619. c.Assert(fields[1], checker.Equals, "ps-volume-test")
  620. // filter by bind mount source
  621. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+bindMountSource)
  622. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  623. c.Assert(lines, checker.HasLen, 1)
  624. fields = strings.Fields(lines[0])
  625. c.Assert(fields, checker.HasLen, 2)
  626. c.Assert(fields[0], checker.Equals, "bind-mount-test")
  627. c.Assert(fields[1], checker.Equals, bindMountSource)
  628. // filter by bind mount destination
  629. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+bindMountDestination)
  630. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  631. c.Assert(lines, checker.HasLen, 1)
  632. fields = strings.Fields(lines[0])
  633. c.Assert(fields, checker.HasLen, 2)
  634. c.Assert(fields[0], checker.Equals, "bind-mount-test")
  635. c.Assert(fields[1], checker.Equals, bindMountSource)
  636. // empty results filtering by unknown mount point
  637. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+prefix+slash+"this-path-was-never-mounted")
  638. c.Assert(strings.TrimSpace(string(out)), checker.HasLen, 0)
  639. }
  640. func (s *DockerSuite) TestPsFormatSize(c *check.C) {
  641. testRequires(c, DaemonIsLinux)
  642. runSleepingContainer(c)
  643. out, _ := dockerCmd(c, "ps", "--format", "table {{.Size}}")
  644. lines := strings.Split(out, "\n")
  645. c.Assert(lines[1], checker.Not(checker.Equals), "0 B", check.Commentf("Should not display a size of 0 B"))
  646. out, _ = dockerCmd(c, "ps", "--size", "--format", "table {{.Size}}")
  647. lines = strings.Split(out, "\n")
  648. c.Assert(lines[0], checker.Equals, "SIZE", check.Commentf("Should only have one size column"))
  649. out, _ = dockerCmd(c, "ps", "--size", "--format", "raw")
  650. lines = strings.Split(out, "\n")
  651. c.Assert(lines[8], checker.HasPrefix, "size:", check.Commentf("Size should be appended on a newline"))
  652. }
  653. func (s *DockerSuite) TestPsListContainersFilterNetwork(c *check.C) {
  654. // TODO default network on Windows is not called "bridge", and creating a
  655. // custom network fails on Windows fails with "Error response from daemon: plugin not found")
  656. testRequires(c, DaemonIsLinux)
  657. // create some containers
  658. runSleepingContainer(c, "--net=bridge", "--name=onbridgenetwork")
  659. runSleepingContainer(c, "--net=none", "--name=onnonenetwork")
  660. // Filter docker ps on non existing network
  661. out, _ := dockerCmd(c, "ps", "--filter", "network=doesnotexist")
  662. containerOut := strings.TrimSpace(string(out))
  663. lines := strings.Split(containerOut, "\n")
  664. // skip header
  665. lines = lines[1:]
  666. // ps output should have no containers
  667. c.Assert(lines, checker.HasLen, 0)
  668. // Filter docker ps on network bridge
  669. out, _ = dockerCmd(c, "ps", "--filter", "network=bridge")
  670. containerOut = strings.TrimSpace(string(out))
  671. lines = strings.Split(containerOut, "\n")
  672. // skip header
  673. lines = lines[1:]
  674. // ps output should have only one container
  675. c.Assert(lines, checker.HasLen, 1)
  676. // Making sure onbridgenetwork is on the output
  677. c.Assert(containerOut, checker.Contains, "onbridgenetwork", check.Commentf("Missing the container on network\n"))
  678. // Filter docker ps on networks bridge and none
  679. out, _ = dockerCmd(c, "ps", "--filter", "network=bridge", "--filter", "network=none")
  680. containerOut = strings.TrimSpace(string(out))
  681. lines = strings.Split(containerOut, "\n")
  682. // skip header
  683. lines = lines[1:]
  684. //ps output should have both the containers
  685. c.Assert(lines, checker.HasLen, 2)
  686. // Making sure onbridgenetwork and onnonenetwork is on the output
  687. c.Assert(containerOut, checker.Contains, "onnonenetwork", check.Commentf("Missing the container on none network\n"))
  688. c.Assert(containerOut, checker.Contains, "onbridgenetwork", check.Commentf("Missing the container on bridge network\n"))
  689. nwID, _ := dockerCmd(c, "network", "inspect", "--format", "{{.ID}}", "bridge")
  690. // Filter by network ID
  691. out, _ = dockerCmd(c, "ps", "--filter", "network="+nwID)
  692. containerOut = strings.TrimSpace(string(out))
  693. c.Assert(containerOut, checker.Contains, "onbridgenetwork")
  694. }
  695. func (s *DockerSuite) TestPsByOrder(c *check.C) {
  696. name1 := "xyz-abc"
  697. out, err := runSleepingContainer(c, "--name", name1)
  698. c.Assert(err, checker.NotNil)
  699. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  700. container1 := strings.TrimSpace(out)
  701. name2 := "xyz-123"
  702. out, err = runSleepingContainer(c, "--name", name2)
  703. c.Assert(err, checker.NotNil)
  704. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  705. container2 := strings.TrimSpace(out)
  706. name3 := "789-abc"
  707. out, err = runSleepingContainer(c, "--name", name3)
  708. c.Assert(err, checker.NotNil)
  709. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  710. name4 := "789-123"
  711. out, err = runSleepingContainer(c, "--name", name4)
  712. c.Assert(err, checker.NotNil)
  713. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  714. // Run multiple time should have the same result
  715. out, err = dockerCmd(c, "ps", "--no-trunc", "-q", "-f", "name=xyz")
  716. c.Assert(err, checker.NotNil)
  717. c.Assert(strings.TrimSpace(out), checker.Equals, fmt.Sprintf("%s\n%s", container2, container1))
  718. // Run multiple time should have the same result
  719. out, err = dockerCmd(c, "ps", "--no-trunc", "-q", "-f", "name=xyz")
  720. c.Assert(err, checker.NotNil)
  721. c.Assert(strings.TrimSpace(out), checker.Equals, fmt.Sprintf("%s\n%s", container2, container1))
  722. }
  723. func (s *DockerSuite) TestPsFilterMissingArgErrorCode(c *check.C) {
  724. _, errCode, _ := dockerCmdWithError("ps", "--filter")
  725. c.Assert(errCode, checker.Equals, 125)
  726. }
  727. // Test case for 30291
  728. func (s *DockerSuite) TestPsFormatTemplateWithArg(c *check.C) {
  729. runSleepingContainer(c, "-d", "--name", "top", "--label", "some.label=label.foo-bar")
  730. out, _ := dockerCmd(c, "ps", "--format", `{{.Names}} {{.Label "some.label"}}`)
  731. c.Assert(strings.TrimSpace(out), checker.Equals, "top label.foo-bar")
  732. }
  733. func (s *DockerSuite) TestPsListContainersFilterPorts(c *check.C) {
  734. testRequires(c, DaemonIsLinux)
  735. out, _ := dockerCmd(c, "run", "-d", "--publish=80", "busybox", "top")
  736. id1 := strings.TrimSpace(out)
  737. out, _ = dockerCmd(c, "run", "-d", "--expose=8080", "busybox", "top")
  738. id2 := strings.TrimSpace(out)
  739. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q")
  740. c.Assert(strings.TrimSpace(out), checker.Contains, id1)
  741. c.Assert(strings.TrimSpace(out), checker.Contains, id2)
  742. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "publish=80-8080/udp")
  743. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), id1)
  744. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), id2)
  745. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "expose=8081")
  746. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), id1)
  747. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), id2)
  748. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "publish=80-81")
  749. c.Assert(strings.TrimSpace(out), checker.Equals, id1)
  750. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), id2)
  751. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "expose=80/tcp")
  752. c.Assert(strings.TrimSpace(out), checker.Equals, id1)
  753. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), id2)
  754. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "expose=8080/tcp")
  755. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), id1)
  756. c.Assert(strings.TrimSpace(out), checker.Equals, id2)
  757. }