docker_cli_ps_test.go 42 KB

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