docker_cli_ps_test.go 26 KB

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