docker_cli_ps_test.go 33 KB

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