docker_cli_ps_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. testRequires(c, DaemonIsLinux)
  18. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  19. firstID := strings.TrimSpace(out)
  20. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  21. secondID := strings.TrimSpace(out)
  22. // not long running
  23. out, _ = dockerCmd(c, "run", "-d", "busybox", "true")
  24. thirdID := strings.TrimSpace(out)
  25. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  26. fourthID := strings.TrimSpace(out)
  27. // make sure the second is running
  28. c.Assert(waitRun(secondID), checker.IsNil)
  29. // make sure third one is not running
  30. dockerCmd(c, "wait", thirdID)
  31. // make sure the forth is running
  32. c.Assert(waitRun(fourthID), checker.IsNil)
  33. // all
  34. out, _ = dockerCmd(c, "ps", "-a")
  35. 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))
  36. // running
  37. out, _ = dockerCmd(c, "ps")
  38. 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))
  39. // from here all flag '-a' is ignored
  40. // limit
  41. out, _ = dockerCmd(c, "ps", "-n=2", "-a")
  42. expected := []string{fourthID, thirdID}
  43. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("LIMIT & ALL: Container list is not in the correct order: \n%s", out))
  44. out, _ = dockerCmd(c, "ps", "-n=2")
  45. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("LIMIT: Container list is not in the correct order: \n%s", out))
  46. // filter since
  47. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-a")
  48. expected = []string{fourthID, thirdID, secondID}
  49. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE & ALL: Container list is not in the correct order: \n%s", out))
  50. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID)
  51. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE: Container list is not in the correct order: \n%s", out))
  52. // filter before
  53. out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID, "-a")
  54. expected = []string{secondID, firstID}
  55. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE & ALL: Container list is not in the correct order: \n%s", out))
  56. out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID)
  57. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE: Container list is not in the correct order: \n%s", out))
  58. // filter since & before
  59. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-a")
  60. expected = []string{thirdID, secondID}
  61. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE & ALL: Container list is not in the correct order: \n%s", out))
  62. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID)
  63. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE: Container list is not in the correct order: \n%s", out))
  64. // filter since & limit
  65. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2", "-a")
  66. expected = []string{fourthID, thirdID}
  67. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
  68. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2")
  69. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, LIMIT: Container list is not in the correct order: \n%s", out))
  70. // filter before & limit
  71. out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1", "-a")
  72. expected = []string{thirdID}
  73. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
  74. out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1")
  75. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE, LIMIT: Container list is not in the correct order: \n%s", out))
  76. // filter since & filter before & limit
  77. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1", "-a")
  78. expected = []string{thirdID}
  79. 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))
  80. out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1")
  81. c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE, LIMIT: Container list is not in the correct order: \n%s", out))
  82. }
  83. func assertContainerList(out string, expected []string) bool {
  84. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  85. if len(lines)-1 != len(expected) {
  86. return false
  87. }
  88. containerIDIndex := strings.Index(lines[0], "CONTAINER ID")
  89. for i := 0; i < len(expected); i++ {
  90. foundID := lines[i+1][containerIDIndex : containerIDIndex+12]
  91. if foundID != expected[i][:12] {
  92. return false
  93. }
  94. }
  95. return true
  96. }
  97. func (s *DockerSuite) TestPsListContainersSize(c *check.C) {
  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. testRequires(c, DaemonIsLinux)
  134. // start exited container
  135. out, _ := dockerCmd(c, "run", "-d", "busybox")
  136. firstID := strings.TrimSpace(out)
  137. // make sure the exited container is not running
  138. dockerCmd(c, "wait", firstID)
  139. // start running container
  140. out, _ = dockerCmd(c, "run", "-itd", "busybox")
  141. secondID := strings.TrimSpace(out)
  142. // filter containers by exited
  143. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter=status=exited")
  144. containerOut := strings.TrimSpace(out)
  145. c.Assert(containerOut, checker.Equals, firstID)
  146. out, _ = dockerCmd(c, "ps", "-a", "--no-trunc", "-q", "--filter=status=running")
  147. containerOut = strings.TrimSpace(out)
  148. c.Assert(containerOut, checker.Equals, secondID)
  149. out, _, _ = dockerCmdWithTimeout(time.Second*60, "ps", "-a", "-q", "--filter=status=rubbish")
  150. c.Assert(out, checker.Contains, "Unrecognised filter value for status", check.Commentf("Expected error response due to invalid status filter output: %q", out))
  151. // pause running container
  152. out, _ = dockerCmd(c, "run", "-itd", "busybox")
  153. pausedID := strings.TrimSpace(out)
  154. dockerCmd(c, "pause", pausedID)
  155. // make sure the container is unpaused to let the daemon stop it properly
  156. defer func() { dockerCmd(c, "unpause", pausedID) }()
  157. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter=status=paused")
  158. containerOut = strings.TrimSpace(out)
  159. c.Assert(containerOut, checker.Equals, pausedID)
  160. }
  161. func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) {
  162. testRequires(c, DaemonIsLinux)
  163. // start container
  164. out, _ := dockerCmd(c, "run", "-d", "busybox")
  165. firstID := strings.TrimSpace(out)
  166. // start another container
  167. dockerCmd(c, "run", "-d", "busybox", "top")
  168. // filter containers by id
  169. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=id="+firstID)
  170. containerOut := strings.TrimSpace(out)
  171. c.Assert(containerOut, checker.Equals, firstID[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out))
  172. }
  173. func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) {
  174. testRequires(c, DaemonIsLinux)
  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. dockerCmd(c, "run", "-d", "--name=b_name_to_match", "busybox", "top")
  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. testRequires(c, DaemonIsLinux)
  195. // Build images
  196. imageName1 := "images_ps_filter_test1"
  197. imageID1, err := buildImage(imageName1,
  198. `FROM busybox
  199. LABEL match me 1`, true)
  200. c.Assert(err, checker.IsNil)
  201. imageName1Tagged := "images_ps_filter_test1:tag"
  202. imageID1Tagged, err := buildImage(imageName1Tagged,
  203. `FROM busybox
  204. LABEL match me 1 tagged`, true)
  205. c.Assert(err, checker.IsNil)
  206. imageName2 := "images_ps_filter_test2"
  207. imageID2, err := buildImage(imageName2,
  208. fmt.Sprintf(`FROM %s
  209. LABEL match me 2`, imageName1), true)
  210. c.Assert(err, checker.IsNil)
  211. // start containers
  212. out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "hello")
  213. firstID := strings.TrimSpace(out)
  214. // start another container
  215. out, _ = dockerCmd(c, "run", "-d", "busybox", "echo", "hello")
  216. secondID := strings.TrimSpace(out)
  217. // start third container
  218. out, _ = dockerCmd(c, "run", "-d", imageName1, "echo", "hello")
  219. thirdID := strings.TrimSpace(out)
  220. // start fourth container
  221. out, _ = dockerCmd(c, "run", "-d", imageName1Tagged, "echo", "hello")
  222. fourthID := strings.TrimSpace(out)
  223. // start fifth container
  224. out, _ = dockerCmd(c, "run", "-d", imageName2, "echo", "hello")
  225. fifthID := strings.TrimSpace(out)
  226. var filterTestSuite = []struct {
  227. filterName string
  228. expectedIDs []string
  229. }{
  230. // non existent stuff
  231. {"nonexistent", []string{}},
  232. {"nonexistent:tag", []string{}},
  233. // image
  234. {"busybox", []string{firstID, secondID, thirdID, fourthID, fifthID}},
  235. {imageName1, []string{thirdID, fifthID}},
  236. {imageName2, []string{fifthID}},
  237. // image:tag
  238. {fmt.Sprintf("%s:latest", imageName1), []string{thirdID, fifthID}},
  239. {imageName1Tagged, []string{fourthID}},
  240. // short-id
  241. {stringid.TruncateID(imageID1), []string{thirdID, fifthID}},
  242. {stringid.TruncateID(imageID2), []string{fifthID}},
  243. // full-id
  244. {imageID1, []string{thirdID, fifthID}},
  245. {imageID1Tagged, []string{fourthID}},
  246. {imageID2, []string{fifthID}},
  247. }
  248. for _, filter := range filterTestSuite {
  249. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+filter.filterName)
  250. checkPsAncestorFilterOutput(c, out, filter.filterName, filter.expectedIDs)
  251. }
  252. // Multiple ancestor filter
  253. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+imageName2, "--filter=ancestor="+imageName1Tagged)
  254. checkPsAncestorFilterOutput(c, out, imageName2+","+imageName1Tagged, []string{fourthID, fifthID})
  255. }
  256. func checkPsAncestorFilterOutput(c *check.C, out string, filterName string, expectedIDs []string) {
  257. actualIDs := []string{}
  258. if out != "" {
  259. actualIDs = strings.Split(out[:len(out)-1], "\n")
  260. }
  261. sort.Strings(actualIDs)
  262. sort.Strings(expectedIDs)
  263. 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))
  264. if len(expectedIDs) > 0 {
  265. same := true
  266. for i := range expectedIDs {
  267. if actualIDs[i] != expectedIDs[i] {
  268. c.Logf("%s, %s", actualIDs[i], expectedIDs[i])
  269. same = false
  270. break
  271. }
  272. }
  273. c.Assert(same, checker.Equals, true, check.Commentf("Expected filtered container(s) for %s ancestor filter to be %v, got %v", filterName, expectedIDs, actualIDs))
  274. }
  275. }
  276. func (s *DockerSuite) TestPsListContainersFilterLabel(c *check.C) {
  277. testRequires(c, DaemonIsLinux)
  278. // start container
  279. out, _ := dockerCmd(c, "run", "-d", "-l", "match=me", "-l", "second=tag", "busybox")
  280. firstID := strings.TrimSpace(out)
  281. // start another container
  282. out, _ = dockerCmd(c, "run", "-d", "-l", "match=me too", "busybox")
  283. secondID := strings.TrimSpace(out)
  284. // start third container
  285. out, _ = dockerCmd(c, "run", "-d", "-l", "nomatch=me", "busybox")
  286. thirdID := strings.TrimSpace(out)
  287. // filter containers by exact match
  288. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me")
  289. containerOut := strings.TrimSpace(out)
  290. c.Assert(containerOut, checker.Equals, firstID, check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out))
  291. // filter containers by two labels
  292. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag")
  293. containerOut = strings.TrimSpace(out)
  294. c.Assert(containerOut, checker.Equals, firstID, check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out))
  295. // filter containers by two labels, but expect not found because of AND behavior
  296. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no")
  297. containerOut = strings.TrimSpace(out)
  298. c.Assert(containerOut, checker.Equals, "", check.Commentf("Expected nothing, got %s for exited filter, output: %q", containerOut, out))
  299. // filter containers by exact key
  300. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match")
  301. containerOut = strings.TrimSpace(out)
  302. c.Assert(containerOut, checker.Contains, firstID)
  303. c.Assert(containerOut, checker.Contains, secondID)
  304. c.Assert(containerOut, checker.Not(checker.Contains), thirdID)
  305. }
  306. func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) {
  307. testRequires(c, DaemonIsLinux)
  308. dockerCmd(c, "run", "-d", "--name", "top", "busybox", "top")
  309. dockerCmd(c, "run", "--name", "zero1", "busybox", "true")
  310. firstZero, err := getIDByName("zero1")
  311. c.Assert(err, checker.IsNil)
  312. dockerCmd(c, "run", "--name", "zero2", "busybox", "true")
  313. secondZero, err := getIDByName("zero2")
  314. c.Assert(err, checker.IsNil)
  315. out, _, err := dockerCmdWithError("run", "--name", "nonzero1", "busybox", "false")
  316. c.Assert(err, checker.NotNil, check.Commentf("Should fail.", out, err))
  317. firstNonZero, err := getIDByName("nonzero1")
  318. c.Assert(err, checker.IsNil)
  319. out, _, err = dockerCmdWithError("run", "--name", "nonzero2", "busybox", "false")
  320. c.Assert(err, checker.NotNil, check.Commentf("Should fail.", out, err))
  321. secondNonZero, err := getIDByName("nonzero2")
  322. c.Assert(err, checker.IsNil)
  323. // filter containers by exited=0
  324. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
  325. ids := strings.Split(strings.TrimSpace(out), "\n")
  326. c.Assert(ids, checker.HasLen, 2, check.Commentf("Should be 2 zero exited containers got %d: %s", len(ids), out))
  327. c.Assert(ids[0], checker.Equals, secondZero, check.Commentf("First in list should be %q, got %q", secondZero, ids[0]))
  328. c.Assert(ids[1], checker.Equals, firstZero, check.Commentf("Second in list should be %q, got %q", firstZero, ids[1]))
  329. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
  330. ids = strings.Split(strings.TrimSpace(out), "\n")
  331. c.Assert(ids, checker.HasLen, 2, check.Commentf("Should be 2 zero exited containers got %d", len(ids)))
  332. c.Assert(ids[0], checker.Equals, secondNonZero, check.Commentf("First in list should be %q, got %q", secondNonZero, ids[0]))
  333. c.Assert(ids[1], checker.Equals, firstNonZero, check.Commentf("Second in list should be %q, got %q", firstNonZero, ids[1]))
  334. }
  335. func (s *DockerSuite) TestPsRightTagName(c *check.C) {
  336. testRequires(c, DaemonIsLinux)
  337. tag := "asybox:shmatest"
  338. dockerCmd(c, "tag", "busybox", tag)
  339. var id1 string
  340. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  341. id1 = strings.TrimSpace(string(out))
  342. var id2 string
  343. out, _ = dockerCmd(c, "run", "-d", tag, "top")
  344. id2 = strings.TrimSpace(string(out))
  345. var imageID string
  346. out, _ = dockerCmd(c, "inspect", "-f", "{{.Id}}", "busybox")
  347. imageID = strings.TrimSpace(string(out))
  348. var id3 string
  349. out, _ = dockerCmd(c, "run", "-d", imageID, "top")
  350. id3 = strings.TrimSpace(string(out))
  351. out, _ = dockerCmd(c, "ps", "--no-trunc")
  352. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  353. // skip header
  354. lines = lines[1:]
  355. c.Assert(lines, checker.HasLen, 3, check.Commentf("There should be 3 running container, got %d", len(lines)))
  356. for _, line := range lines {
  357. f := strings.Fields(line)
  358. switch f[0] {
  359. case id1:
  360. c.Assert(f[1], checker.Equals, "busybox", check.Commentf("Expected %s tag for id %s, got %s", "busybox", id1, f[1]))
  361. case id2:
  362. c.Assert(f[1], checker.Equals, tag, check.Commentf("Expected %s tag for id %s, got %s", tag, id2, f[1]))
  363. case id3:
  364. c.Assert(f[1], checker.Equals, imageID, check.Commentf("Expected %s imageID for id %s, got %s", tag, id3, f[1]))
  365. default:
  366. c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3)
  367. }
  368. }
  369. }
  370. func (s *DockerSuite) TestPsLinkedWithNoTrunc(c *check.C) {
  371. testRequires(c, DaemonIsLinux)
  372. dockerCmd(c, "run", "--name=first", "-d", "busybox", "top")
  373. dockerCmd(c, "run", "--name=second", "--link=first:first", "-d", "busybox", "top")
  374. out, _ := dockerCmd(c, "ps", "--no-trunc")
  375. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  376. // strip header
  377. lines = lines[1:]
  378. expected := []string{"second", "first,second/first"}
  379. var names []string
  380. for _, l := range lines {
  381. fields := strings.Fields(l)
  382. names = append(names, fields[len(fields)-1])
  383. }
  384. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array: %v, got: %v", expected, names))
  385. }
  386. func (s *DockerSuite) TestPsGroupPortRange(c *check.C) {
  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. testRequires(c, DaemonIsLinux)
  395. dockerCmd(c, "run", "-d", "--name", "sizetest", "busybox", "top")
  396. out, _ := dockerCmd(c, "ps", "--size")
  397. c.Assert(out, checker.Contains, "virtual", check.Commentf("docker ps with --size should show virtual size of container"))
  398. }
  399. func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) {
  400. testRequires(c, DaemonIsLinux)
  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. testRequires(c, DaemonIsLinux)
  426. //create 2 containers and link them
  427. dockerCmd(c, "run", "--name=child", "-d", "busybox", "top")
  428. dockerCmd(c, "run", "--name=parent", "--link=child:linkedone", "-d", "busybox", "top")
  429. //use the new format capabilities to only list the names and --no-trunc to get all names
  430. out, _ := dockerCmd(c, "ps", "--format", "{{.Names}}", "--no-trunc")
  431. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  432. expected := []string{"parent", "child,parent/linkedone"}
  433. var names []string
  434. for _, l := range lines {
  435. names = append(names, l)
  436. }
  437. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with non-truncated names: %v, got: %v", expected, names))
  438. //now list without turning off truncation and make sure we only get the non-link names
  439. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}}")
  440. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  441. expected = []string{"parent", "child"}
  442. var truncNames []string
  443. for _, l := range lines {
  444. truncNames = append(truncNames, l)
  445. }
  446. c.Assert(expected, checker.DeepEquals, truncNames, check.Commentf("Expected array with truncated names: %v, got: %v", expected, truncNames))
  447. }
  448. func (s *DockerSuite) TestPsFormatHeaders(c *check.C) {
  449. testRequires(c, DaemonIsLinux)
  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. dockerCmd(c, "run", "--name=test", "-d", "busybox", "top")
  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. testRequires(c, DaemonIsLinux)
  460. config := `{
  461. "psFormat": "{{ .ID }} default"
  462. }`
  463. d, err := ioutil.TempDir("", "integration-cli-")
  464. c.Assert(err, checker.IsNil)
  465. defer os.RemoveAll(d)
  466. err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
  467. c.Assert(err, checker.IsNil)
  468. out, _ := dockerCmd(c, "run", "--name=test", "-d", "busybox", "top")
  469. id := strings.TrimSpace(out)
  470. out, _ = dockerCmd(c, "--config", d, "ps", "-q")
  471. c.Assert(id, checker.HasPrefix, strings.TrimSpace(out), check.Commentf("Expected to print only the container id, got %v\n", out))
  472. }
  473. // Test for GitHub issue #12595
  474. func (s *DockerSuite) TestPsImageIDAfterUpdate(c *check.C) {
  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, "run", "-d", originalImageName, "top")
  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. }