docker_cli_ps_test.go 26 KB

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