docker_cli_ps_test.go 34 KB

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