docker_cli_ps_test.go 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. // Problematic on Windows as it doesn't support links as of Jan 2016
  417. testRequires(c, DaemonIsLinux)
  418. runSleepingContainer(c, "--name=first")
  419. runSleepingContainer(c, "--name=second", "--link=first:first")
  420. out, _ := dockerCmd(c, "ps", "--no-trunc")
  421. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  422. // strip header
  423. lines = lines[1:]
  424. expected := []string{"second", "first,second/first"}
  425. var names []string
  426. for _, l := range lines {
  427. fields := strings.Fields(l)
  428. names = append(names, fields[len(fields)-1])
  429. }
  430. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array: %v, got: %v", expected, names))
  431. }
  432. func (s *DockerSuite) TestPsGroupPortRange(c *check.C) {
  433. // Problematic on Windows as it doesn't support port ranges as of Jan 2016
  434. testRequires(c, DaemonIsLinux)
  435. portRange := "3850-3900"
  436. dockerCmd(c, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top")
  437. out, _ := dockerCmd(c, "ps")
  438. c.Assert(string(out), checker.Contains, portRange, check.Commentf("docker ps output should have had the port range %q: %s", portRange, string(out)))
  439. }
  440. func (s *DockerSuite) TestPsWithSize(c *check.C) {
  441. // Problematic on Windows as it doesn't report the size correctly @swernli
  442. testRequires(c, DaemonIsLinux)
  443. dockerCmd(c, "run", "-d", "--name", "sizetest", "busybox", "top")
  444. out, _ := dockerCmd(c, "ps", "--size")
  445. c.Assert(out, checker.Contains, "virtual", check.Commentf("docker ps with --size should show virtual size of container"))
  446. }
  447. func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) {
  448. // create a container
  449. out, _ := dockerCmd(c, "create", "busybox")
  450. cID := strings.TrimSpace(out)
  451. shortCID := cID[:12]
  452. // Make sure it DOESN'T show up w/o a '-a' for normal 'ps'
  453. out, _ = dockerCmd(c, "ps", "-q")
  454. c.Assert(out, checker.Not(checker.Contains), shortCID, check.Commentf("Should have not seen '%s' in ps output:\n%s", shortCID, out))
  455. // Make sure it DOES show up as 'Created' for 'ps -a'
  456. out, _ = dockerCmd(c, "ps", "-a")
  457. hits := 0
  458. for _, line := range strings.Split(out, "\n") {
  459. if !strings.Contains(line, shortCID) {
  460. continue
  461. }
  462. hits++
  463. c.Assert(line, checker.Contains, "Created", check.Commentf("Missing 'Created' on '%s'", line))
  464. }
  465. c.Assert(hits, checker.Equals, 1, check.Commentf("Should have seen '%s' in ps -a output once:%d\n%s", shortCID, hits, out))
  466. // filter containers by 'create' - note, no -a needed
  467. out, _ = dockerCmd(c, "ps", "-q", "-f", "status=created")
  468. containerOut := strings.TrimSpace(out)
  469. c.Assert(cID, checker.HasPrefix, containerOut)
  470. }
  471. func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) {
  472. // Problematic on Windows as it doesn't support link as of Jan 2016
  473. testRequires(c, DaemonIsLinux)
  474. existingContainers := ExistingContainerNames(c)
  475. //create 2 containers and link them
  476. dockerCmd(c, "run", "--name=child", "-d", "busybox", "top")
  477. dockerCmd(c, "run", "--name=parent", "--link=child:linkedone", "-d", "busybox", "top")
  478. //use the new format capabilities to only list the names and --no-trunc to get all names
  479. out, _ := dockerCmd(c, "ps", "--format", "{{.Names}}", "--no-trunc")
  480. out = RemoveOutputForExistingElements(out, existingContainers)
  481. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  482. expected := []string{"parent", "child,parent/linkedone"}
  483. var names []string
  484. names = append(names, lines...)
  485. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with non-truncated names: %v, got: %v", expected, names))
  486. //now list without turning off truncation and make sure we only get the non-link names
  487. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}}")
  488. out = RemoveOutputForExistingElements(out, existingContainers)
  489. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  490. expected = []string{"parent", "child"}
  491. var truncNames []string
  492. truncNames = append(truncNames, lines...)
  493. c.Assert(expected, checker.DeepEquals, truncNames, check.Commentf("Expected array with truncated names: %v, got: %v", expected, truncNames))
  494. }
  495. // Test for GitHub issue #21772
  496. func (s *DockerSuite) TestPsNamesMultipleTime(c *check.C) {
  497. existingContainers := ExistingContainerNames(c)
  498. runSleepingContainer(c, "--name=test1")
  499. runSleepingContainer(c, "--name=test2")
  500. //use the new format capabilities to list the names twice
  501. out, _ := dockerCmd(c, "ps", "--format", "{{.Names}} {{.Names}}")
  502. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  503. lines = RemoveLinesForExistingElements(lines, existingContainers)
  504. expected := []string{"test2 test2", "test1 test1"}
  505. var names []string
  506. names = append(names, lines...)
  507. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with names displayed twice: %v, got: %v", expected, names))
  508. }
  509. func (s *DockerSuite) TestPsFormatHeaders(c *check.C) {
  510. // make sure no-container "docker ps" still prints the header row
  511. out, _ := dockerCmd(c, "ps", "--format", "table {{.ID}}")
  512. c.Assert(out, checker.Equals, "CONTAINER ID\n", check.Commentf(`Expected 'CONTAINER ID\n', got %v`, out))
  513. // verify that "docker ps" with a container still prints the header row also
  514. runSleepingContainer(c, "--name=test")
  515. out, _ = dockerCmd(c, "ps", "--format", "table {{.Names}}")
  516. c.Assert(out, checker.Equals, "NAMES\ntest\n", check.Commentf(`Expected 'NAMES\ntest\n', got %v`, out))
  517. }
  518. func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) {
  519. existingContainers := ExistingContainerIDs(c)
  520. config := `{
  521. "psFormat": "default {{ .ID }}"
  522. }`
  523. d, err := ioutil.TempDir("", "integration-cli-")
  524. c.Assert(err, checker.IsNil)
  525. defer os.RemoveAll(d)
  526. err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
  527. c.Assert(err, checker.IsNil)
  528. out := runSleepingContainer(c, "--name=test")
  529. id := strings.TrimSpace(out)
  530. out, _ = dockerCmd(c, "--config", d, "ps", "-q")
  531. out = RemoveOutputForExistingElements(out, existingContainers)
  532. c.Assert(id, checker.HasPrefix, strings.TrimSpace(out), check.Commentf("Expected to print only the container id, got %v\n", out))
  533. }
  534. // Test for GitHub issue #12595
  535. func (s *DockerSuite) TestPsImageIDAfterUpdate(c *check.C) {
  536. // TODO: Investigate why this fails on Windows to Windows CI further.
  537. testRequires(c, DaemonIsLinux)
  538. originalImageName := "busybox:TestPsImageIDAfterUpdate-original"
  539. updatedImageName := "busybox:TestPsImageIDAfterUpdate-updated"
  540. existingContainers := ExistingContainerIDs(c)
  541. icmd.RunCommand(dockerBinary, "tag", "busybox:latest", originalImageName).Assert(c, icmd.Success)
  542. originalImageID := getIDByName(c, originalImageName)
  543. result := icmd.RunCommand(dockerBinary, append([]string{"run", "-d", originalImageName}, sleepCommandForDaemonPlatform()...)...)
  544. result.Assert(c, icmd.Success)
  545. containerID := strings.TrimSpace(result.Combined())
  546. result = icmd.RunCommand(dockerBinary, "ps", "--no-trunc")
  547. result.Assert(c, icmd.Success)
  548. lines := strings.Split(strings.TrimSpace(string(result.Combined())), "\n")
  549. lines = RemoveLinesForExistingElements(lines, existingContainers)
  550. // skip header
  551. lines = lines[1:]
  552. c.Assert(len(lines), checker.Equals, 1)
  553. for _, line := range lines {
  554. f := strings.Fields(line)
  555. c.Assert(f[1], checker.Equals, originalImageName)
  556. }
  557. icmd.RunCommand(dockerBinary, "commit", containerID, updatedImageName).Assert(c, icmd.Success)
  558. icmd.RunCommand(dockerBinary, "tag", updatedImageName, originalImageName).Assert(c, icmd.Success)
  559. result = icmd.RunCommand(dockerBinary, "ps", "--no-trunc")
  560. result.Assert(c, icmd.Success)
  561. lines = strings.Split(strings.TrimSpace(string(result.Combined())), "\n")
  562. lines = RemoveLinesForExistingElements(lines, existingContainers)
  563. // skip header
  564. lines = lines[1:]
  565. c.Assert(len(lines), checker.Equals, 1)
  566. for _, line := range lines {
  567. f := strings.Fields(line)
  568. c.Assert(f[1], checker.Equals, originalImageID)
  569. }
  570. }
  571. func (s *DockerSuite) TestPsNotShowPortsOfStoppedContainer(c *check.C) {
  572. testRequires(c, DaemonIsLinux)
  573. dockerCmd(c, "run", "--name=foo", "-d", "-p", "5000:5000", "busybox", "top")
  574. c.Assert(waitRun("foo"), checker.IsNil)
  575. out, _ := dockerCmd(c, "ps")
  576. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  577. expected := "0.0.0.0:5000->5000/tcp"
  578. fields := strings.Fields(lines[1])
  579. c.Assert(fields[len(fields)-2], checker.Equals, expected, check.Commentf("Expected: %v, got: %v", expected, fields[len(fields)-2]))
  580. dockerCmd(c, "kill", "foo")
  581. dockerCmd(c, "wait", "foo")
  582. out, _ = dockerCmd(c, "ps", "-l")
  583. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  584. fields = strings.Fields(lines[1])
  585. c.Assert(fields[len(fields)-2], checker.Not(checker.Equals), expected, check.Commentf("Should not got %v", expected))
  586. }
  587. func (s *DockerSuite) TestPsShowMounts(c *check.C) {
  588. existingContainers := ExistingContainerNames(c)
  589. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  590. mp := prefix + slash + "test"
  591. dockerCmd(c, "volume", "create", "ps-volume-test")
  592. // volume mount containers
  593. runSleepingContainer(c, "--name=volume-test-1", "--volume", "ps-volume-test:"+mp)
  594. c.Assert(waitRun("volume-test-1"), checker.IsNil)
  595. runSleepingContainer(c, "--name=volume-test-2", "--volume", mp)
  596. c.Assert(waitRun("volume-test-2"), checker.IsNil)
  597. // bind mount container
  598. var bindMountSource string
  599. var bindMountDestination string
  600. if DaemonIsWindows() {
  601. bindMountSource = "c:\\"
  602. bindMountDestination = "c:\\t"
  603. } else {
  604. bindMountSource = "/tmp"
  605. bindMountDestination = "/t"
  606. }
  607. runSleepingContainer(c, "--name=bind-mount-test", "-v", bindMountSource+":"+bindMountDestination)
  608. c.Assert(waitRun("bind-mount-test"), checker.IsNil)
  609. out, _ := dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}")
  610. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  611. lines = RemoveLinesForExistingElements(lines, existingContainers)
  612. c.Assert(lines, checker.HasLen, 3)
  613. fields := strings.Fields(lines[0])
  614. c.Assert(fields, checker.HasLen, 2)
  615. c.Assert(fields[0], checker.Equals, "bind-mount-test")
  616. c.Assert(fields[1], checker.Equals, bindMountSource)
  617. fields = strings.Fields(lines[1])
  618. c.Assert(fields, checker.HasLen, 2)
  619. anonymousVolumeID := fields[1]
  620. fields = strings.Fields(lines[2])
  621. c.Assert(fields[1], checker.Equals, "ps-volume-test")
  622. // filter by volume name
  623. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume=ps-volume-test")
  624. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  625. lines = RemoveLinesForExistingElements(lines, existingContainers)
  626. c.Assert(lines, checker.HasLen, 1)
  627. fields = strings.Fields(lines[0])
  628. c.Assert(fields[1], checker.Equals, "ps-volume-test")
  629. // empty results filtering by unknown volume
  630. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume=this-volume-should-not-exist")
  631. c.Assert(strings.TrimSpace(string(out)), checker.HasLen, 0)
  632. // filter by mount destination
  633. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+mp)
  634. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  635. lines = RemoveLinesForExistingElements(lines, existingContainers)
  636. c.Assert(lines, checker.HasLen, 2)
  637. fields = strings.Fields(lines[0])
  638. c.Assert(fields[1], checker.Equals, anonymousVolumeID)
  639. fields = strings.Fields(lines[1])
  640. c.Assert(fields[1], checker.Equals, "ps-volume-test")
  641. // filter by bind mount source
  642. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+bindMountSource)
  643. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  644. lines = RemoveLinesForExistingElements(lines, existingContainers)
  645. c.Assert(lines, checker.HasLen, 1)
  646. fields = strings.Fields(lines[0])
  647. c.Assert(fields, checker.HasLen, 2)
  648. c.Assert(fields[0], checker.Equals, "bind-mount-test")
  649. c.Assert(fields[1], checker.Equals, bindMountSource)
  650. // filter by bind mount destination
  651. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+bindMountDestination)
  652. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  653. lines = RemoveLinesForExistingElements(lines, existingContainers)
  654. c.Assert(lines, checker.HasLen, 1)
  655. fields = strings.Fields(lines[0])
  656. c.Assert(fields, checker.HasLen, 2)
  657. c.Assert(fields[0], checker.Equals, "bind-mount-test")
  658. c.Assert(fields[1], checker.Equals, bindMountSource)
  659. // empty results filtering by unknown mount point
  660. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+prefix+slash+"this-path-was-never-mounted")
  661. c.Assert(strings.TrimSpace(string(out)), checker.HasLen, 0)
  662. }
  663. func (s *DockerSuite) TestPsFormatSize(c *check.C) {
  664. testRequires(c, DaemonIsLinux)
  665. runSleepingContainer(c)
  666. out, _ := dockerCmd(c, "ps", "--format", "table {{.Size}}")
  667. lines := strings.Split(out, "\n")
  668. c.Assert(lines[1], checker.Not(checker.Equals), "0 B", check.Commentf("Should not display a size of 0 B"))
  669. out, _ = dockerCmd(c, "ps", "--size", "--format", "table {{.Size}}")
  670. lines = strings.Split(out, "\n")
  671. c.Assert(lines[0], checker.Equals, "SIZE", check.Commentf("Should only have one size column"))
  672. out, _ = dockerCmd(c, "ps", "--size", "--format", "raw")
  673. lines = strings.Split(out, "\n")
  674. c.Assert(lines[8], checker.HasPrefix, "size:", check.Commentf("Size should be appended on a newline"))
  675. }
  676. func (s *DockerSuite) TestPsListContainersFilterNetwork(c *check.C) {
  677. existing := ExistingContainerIDs(c)
  678. // TODO default network on Windows is not called "bridge", and creating a
  679. // custom network fails on Windows fails with "Error response from daemon: plugin not found")
  680. testRequires(c, DaemonIsLinux)
  681. // create some containers
  682. runSleepingContainer(c, "--net=bridge", "--name=onbridgenetwork")
  683. runSleepingContainer(c, "--net=none", "--name=onnonenetwork")
  684. // Filter docker ps on non existing network
  685. out, _ := dockerCmd(c, "ps", "--filter", "network=doesnotexist")
  686. containerOut := strings.TrimSpace(string(out))
  687. lines := strings.Split(containerOut, "\n")
  688. // skip header
  689. lines = lines[1:]
  690. // ps output should have no containers
  691. c.Assert(RemoveLinesForExistingElements(lines, existing), checker.HasLen, 0)
  692. // Filter docker ps on network bridge
  693. out, _ = dockerCmd(c, "ps", "--filter", "network=bridge")
  694. containerOut = strings.TrimSpace(string(out))
  695. lines = strings.Split(containerOut, "\n")
  696. // skip header
  697. lines = lines[1:]
  698. // ps output should have only one container
  699. c.Assert(RemoveLinesForExistingElements(lines, existing), checker.HasLen, 1)
  700. // Making sure onbridgenetwork is on the output
  701. c.Assert(containerOut, checker.Contains, "onbridgenetwork", check.Commentf("Missing the container on network\n"))
  702. // Filter docker ps on networks bridge and none
  703. out, _ = dockerCmd(c, "ps", "--filter", "network=bridge", "--filter", "network=none")
  704. containerOut = strings.TrimSpace(string(out))
  705. lines = strings.Split(containerOut, "\n")
  706. // skip header
  707. lines = lines[1:]
  708. //ps output should have both the containers
  709. c.Assert(RemoveLinesForExistingElements(lines, existing), checker.HasLen, 2)
  710. // Making sure onbridgenetwork and onnonenetwork is on the output
  711. c.Assert(containerOut, checker.Contains, "onnonenetwork", check.Commentf("Missing the container on none network\n"))
  712. c.Assert(containerOut, checker.Contains, "onbridgenetwork", check.Commentf("Missing the container on bridge network\n"))
  713. nwID, _ := dockerCmd(c, "network", "inspect", "--format", "{{.ID}}", "bridge")
  714. // Filter by network ID
  715. out, _ = dockerCmd(c, "ps", "--filter", "network="+nwID)
  716. containerOut = strings.TrimSpace(string(out))
  717. c.Assert(containerOut, checker.Contains, "onbridgenetwork")
  718. // Filter by partial network ID
  719. partialnwID := string(nwID[0:4])
  720. out, _ = dockerCmd(c, "ps", "--filter", "network="+partialnwID)
  721. containerOut = strings.TrimSpace(string(out))
  722. lines = strings.Split(containerOut, "\n")
  723. // skip header
  724. lines = lines[1:]
  725. // ps output should have only one container
  726. c.Assert(RemoveLinesForExistingElements(lines, existing), checker.HasLen, 1)
  727. // Making sure onbridgenetwork is on the output
  728. c.Assert(containerOut, checker.Contains, "onbridgenetwork", check.Commentf("Missing the container on network\n"))
  729. }
  730. func (s *DockerSuite) TestPsByOrder(c *check.C) {
  731. name1 := "xyz-abc"
  732. out := runSleepingContainer(c, "--name", name1)
  733. container1 := strings.TrimSpace(out)
  734. name2 := "xyz-123"
  735. out = runSleepingContainer(c, "--name", name2)
  736. container2 := strings.TrimSpace(out)
  737. name3 := "789-abc"
  738. out = runSleepingContainer(c, "--name", name3)
  739. name4 := "789-123"
  740. out = runSleepingContainer(c, "--name", name4)
  741. // Run multiple time should have the same result
  742. out = cli.DockerCmd(c, "ps", "--no-trunc", "-q", "-f", "name=xyz").Combined()
  743. c.Assert(strings.TrimSpace(out), checker.Equals, fmt.Sprintf("%s\n%s", container2, container1))
  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. }
  748. func (s *DockerSuite) TestPsFilterMissingArgErrorCode(c *check.C) {
  749. _, errCode, _ := dockerCmdWithError("ps", "--filter")
  750. c.Assert(errCode, checker.Equals, 125)
  751. }
  752. // Test case for 30291
  753. func (s *DockerSuite) TestPsFormatTemplateWithArg(c *check.C) {
  754. existingContainers := ExistingContainerNames(c)
  755. runSleepingContainer(c, "-d", "--name", "top", "--label", "some.label=label.foo-bar")
  756. out, _ := dockerCmd(c, "ps", "--format", `{{.Names}} {{.Label "some.label"}}`)
  757. out = RemoveOutputForExistingElements(out, existingContainers)
  758. c.Assert(strings.TrimSpace(out), checker.Equals, "top label.foo-bar")
  759. }
  760. func (s *DockerSuite) TestPsListContainersFilterPorts(c *check.C) {
  761. testRequires(c, DaemonIsLinux)
  762. out, _ := dockerCmd(c, "run", "-d", "--publish=80", "busybox", "top")
  763. id1 := strings.TrimSpace(out)
  764. out, _ = dockerCmd(c, "run", "-d", "--expose=8080", "busybox", "top")
  765. id2 := strings.TrimSpace(out)
  766. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q")
  767. c.Assert(strings.TrimSpace(out), checker.Contains, id1)
  768. c.Assert(strings.TrimSpace(out), checker.Contains, id2)
  769. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "publish=80-8080/udp")
  770. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), id1)
  771. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), id2)
  772. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "expose=8081")
  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", "publish=80-81")
  776. c.Assert(strings.TrimSpace(out), checker.Equals, id1)
  777. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), id2)
  778. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "expose=80/tcp")
  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=8080/tcp")
  782. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), id1)
  783. c.Assert(strings.TrimSpace(out), checker.Equals, id2)
  784. }
  785. func (s *DockerSuite) TestPsNotShowLinknamesOfDeletedContainer(c *check.C) {
  786. testRequires(c, DaemonIsLinux)
  787. existingContainers := ExistingContainerNames(c)
  788. dockerCmd(c, "create", "--name=aaa", "busybox", "top")
  789. dockerCmd(c, "create", "--name=bbb", "--link=aaa", "busybox", "top")
  790. out, _ := dockerCmd(c, "ps", "--no-trunc", "-a", "--format", "{{.Names}}")
  791. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  792. lines = RemoveLinesForExistingElements(lines, existingContainers)
  793. expected := []string{"bbb", "aaa,bbb/aaa"}
  794. var names []string
  795. names = append(names, lines...)
  796. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with non-truncated names: %v, got: %v", expected, names))
  797. dockerCmd(c, "rm", "bbb")
  798. out, _ = dockerCmd(c, "ps", "--no-trunc", "-a", "--format", "{{.Names}}")
  799. out = RemoveOutputForExistingElements(out, existingContainers)
  800. c.Assert(strings.TrimSpace(out), checker.Equals, "aaa")
  801. }