docker_cli_ps_test.go 42 KB

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