docker_cli_ps_test.go 32 KB

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