docker_cli_ps_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. // since
  47. out, _ = dockerCmd(c, "ps", "--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", "--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. // before
  53. out, _ = dockerCmd(c, "ps", "--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", "--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. // since & before
  59. out, _ = dockerCmd(c, "ps", "--since", firstID, "--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", "--since", firstID, "--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. // since & limit
  65. out, _ = dockerCmd(c, "ps", "--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", "--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. // before & limit
  71. out, _ = dockerCmd(c, "ps", "--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", "--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. out, _ = dockerCmd(c, "ps", "--since", firstID, "--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", "--since", firstID, "--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. testRequires(c, DaemonIsLinux)
  98. dockerCmd(c, "run", "-d", "busybox", "echo", "hello")
  99. baseOut, _ := dockerCmd(c, "ps", "-s", "-n=1")
  100. baseLines := strings.Split(strings.Trim(baseOut, "\n "), "\n")
  101. baseSizeIndex := strings.Index(baseLines[0], "SIZE")
  102. baseFoundsize := baseLines[1][baseSizeIndex:]
  103. baseBytes, err := strconv.Atoi(strings.Split(baseFoundsize, " ")[0])
  104. c.Assert(err, checker.IsNil)
  105. name := "test_size"
  106. out, _ := dockerCmd(c, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test")
  107. id, err := getIDByName(name)
  108. c.Assert(err, checker.IsNil)
  109. runCmd := exec.Command(dockerBinary, "ps", "-s", "-n=1")
  110. wait := make(chan struct{})
  111. go func() {
  112. out, _, err = runCommandWithOutput(runCmd)
  113. close(wait)
  114. }()
  115. select {
  116. case <-wait:
  117. case <-time.After(3 * time.Second):
  118. c.Fatalf("Calling \"docker ps -s\" timed out!")
  119. }
  120. c.Assert(err, checker.IsNil)
  121. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  122. c.Assert(lines, checker.HasLen, 2, check.Commentf("Expected 2 lines for 'ps -s -n=1' output, got %d", len(lines)))
  123. sizeIndex := strings.Index(lines[0], "SIZE")
  124. idIndex := strings.Index(lines[0], "CONTAINER ID")
  125. foundID := lines[1][idIndex : idIndex+12]
  126. c.Assert(foundID, checker.Equals, id[:12], check.Commentf("Expected id %s, got %s", id[:12], foundID))
  127. expectedSize := fmt.Sprintf("%d B", (2 + baseBytes))
  128. foundSize := lines[1][sizeIndex:]
  129. c.Assert(foundSize, checker.Contains, expectedSize, check.Commentf("Expected size %q, got %q", expectedSize, foundSize))
  130. }
  131. func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) {
  132. testRequires(c, DaemonIsLinux)
  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. // pause running container
  151. out, _ = dockerCmd(c, "run", "-itd", "busybox")
  152. pausedID := strings.TrimSpace(out)
  153. dockerCmd(c, "pause", pausedID)
  154. // make sure the container is unpaused to let the daemon stop it properly
  155. defer func() { dockerCmd(c, "unpause", pausedID) }()
  156. out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter=status=paused")
  157. containerOut = strings.TrimSpace(out)
  158. c.Assert(containerOut, checker.Equals, pausedID)
  159. }
  160. func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) {
  161. testRequires(c, DaemonIsLinux)
  162. // start container
  163. out, _ := dockerCmd(c, "run", "-d", "busybox")
  164. firstID := strings.TrimSpace(out)
  165. // start another container
  166. dockerCmd(c, "run", "-d", "busybox", "top")
  167. // filter containers by id
  168. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=id="+firstID)
  169. containerOut := strings.TrimSpace(out)
  170. c.Assert(containerOut, checker.Equals, firstID[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out))
  171. }
  172. func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) {
  173. testRequires(c, DaemonIsLinux)
  174. // start container
  175. out, _ := dockerCmd(c, "run", "-d", "--name=a_name_to_match", "busybox")
  176. firstID := strings.TrimSpace(out)
  177. // start another container
  178. dockerCmd(c, "run", "-d", "--name=b_name_to_match", "busybox", "top")
  179. // filter containers by name
  180. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=name=a_name_to_match")
  181. containerOut := strings.TrimSpace(out)
  182. c.Assert(containerOut, checker.Equals, firstID[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out))
  183. }
  184. // Test for the ancestor filter for ps.
  185. // There is also the same test but with image:tag@digest in docker_cli_by_digest_test.go
  186. //
  187. // What the test setups :
  188. // - Create 2 image based on busybox using the same repository but different tags
  189. // - Create an image based on the previous image (images_ps_filter_test2)
  190. // - Run containers for each of those image (busybox, images_ps_filter_test1, images_ps_filter_test2)
  191. // - Filter them out :P
  192. func (s *DockerSuite) TestPsListContainersFilterAncestorImage(c *check.C) {
  193. testRequires(c, DaemonIsLinux)
  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. testRequires(c, DaemonIsLinux)
  277. // start container
  278. out, _ := dockerCmd(c, "run", "-d", "-l", "match=me", "-l", "second=tag", "busybox")
  279. firstID := strings.TrimSpace(out)
  280. // start another container
  281. out, _ = dockerCmd(c, "run", "-d", "-l", "match=me too", "busybox")
  282. secondID := strings.TrimSpace(out)
  283. // start third container
  284. out, _ = dockerCmd(c, "run", "-d", "-l", "nomatch=me", "busybox")
  285. thirdID := strings.TrimSpace(out)
  286. // filter containers by exact match
  287. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me")
  288. containerOut := strings.TrimSpace(out)
  289. c.Assert(containerOut, checker.Equals, firstID, check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out))
  290. // filter containers by two labels
  291. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag")
  292. containerOut = strings.TrimSpace(out)
  293. c.Assert(containerOut, checker.Equals, firstID, check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out))
  294. // filter containers by two labels, but expect not found because of AND behavior
  295. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no")
  296. containerOut = strings.TrimSpace(out)
  297. c.Assert(containerOut, checker.Equals, "", check.Commentf("Expected nothing, got %s for exited filter, output: %q", containerOut, out))
  298. // filter containers by exact key
  299. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match")
  300. containerOut = strings.TrimSpace(out)
  301. c.Assert(containerOut, checker.Contains, firstID)
  302. c.Assert(containerOut, checker.Contains, secondID)
  303. c.Assert(containerOut, checker.Not(checker.Contains), thirdID)
  304. }
  305. func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) {
  306. testRequires(c, DaemonIsLinux)
  307. dockerCmd(c, "run", "-d", "--name", "top", "busybox", "top")
  308. dockerCmd(c, "run", "--name", "zero1", "busybox", "true")
  309. firstZero, err := getIDByName("zero1")
  310. c.Assert(err, checker.IsNil)
  311. dockerCmd(c, "run", "--name", "zero2", "busybox", "true")
  312. secondZero, err := getIDByName("zero2")
  313. c.Assert(err, checker.IsNil)
  314. out, _, err := dockerCmdWithError("run", "--name", "nonzero1", "busybox", "false")
  315. c.Assert(err, checker.NotNil, check.Commentf("Should fail.", out, err))
  316. firstNonZero, err := getIDByName("nonzero1")
  317. c.Assert(err, checker.IsNil)
  318. out, _, err = dockerCmdWithError("run", "--name", "nonzero2", "busybox", "false")
  319. c.Assert(err, checker.NotNil, check.Commentf("Should fail.", out, err))
  320. secondNonZero, err := getIDByName("nonzero2")
  321. c.Assert(err, checker.IsNil)
  322. // filter containers by exited=0
  323. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
  324. ids := strings.Split(strings.TrimSpace(out), "\n")
  325. c.Assert(ids, checker.HasLen, 2, check.Commentf("Should be 2 zero exited containers got %d: %s", len(ids), out))
  326. c.Assert(ids[0], checker.Equals, secondZero, check.Commentf("First in list should be %q, got %q", secondZero, ids[0]))
  327. c.Assert(ids[1], checker.Equals, firstZero, check.Commentf("Second in list should be %q, got %q", firstZero, ids[1]))
  328. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
  329. ids = strings.Split(strings.TrimSpace(out), "\n")
  330. c.Assert(ids, checker.HasLen, 2, check.Commentf("Should be 2 zero exited containers got %d", len(ids)))
  331. c.Assert(ids[0], checker.Equals, secondNonZero, check.Commentf("First in list should be %q, got %q", secondNonZero, ids[0]))
  332. c.Assert(ids[1], checker.Equals, firstNonZero, check.Commentf("Second in list should be %q, got %q", firstNonZero, ids[1]))
  333. }
  334. func (s *DockerSuite) TestPsRightTagName(c *check.C) {
  335. testRequires(c, DaemonIsLinux)
  336. tag := "asybox:shmatest"
  337. dockerCmd(c, "tag", "busybox", tag)
  338. var id1 string
  339. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  340. id1 = strings.TrimSpace(string(out))
  341. var id2 string
  342. out, _ = dockerCmd(c, "run", "-d", tag, "top")
  343. id2 = strings.TrimSpace(string(out))
  344. var imageID string
  345. out, _ = dockerCmd(c, "inspect", "-f", "{{.Id}}", "busybox")
  346. imageID = strings.TrimSpace(string(out))
  347. var id3 string
  348. out, _ = dockerCmd(c, "run", "-d", imageID, "top")
  349. id3 = strings.TrimSpace(string(out))
  350. out, _ = dockerCmd(c, "ps", "--no-trunc")
  351. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  352. // skip header
  353. lines = lines[1:]
  354. c.Assert(lines, checker.HasLen, 3, check.Commentf("There should be 3 running container, got %d", len(lines)))
  355. for _, line := range lines {
  356. f := strings.Fields(line)
  357. switch f[0] {
  358. case id1:
  359. c.Assert(f[1], checker.Equals, "busybox", check.Commentf("Expected %s tag for id %s, got %s", "busybox", id1, f[1]))
  360. case id2:
  361. c.Assert(f[1], checker.Equals, tag, check.Commentf("Expected %s tag for id %s, got %s", tag, id2, f[1]))
  362. case id3:
  363. c.Assert(f[1], checker.Equals, imageID, check.Commentf("Expected %s imageID for id %s, got %s", tag, id3, f[1]))
  364. default:
  365. c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3)
  366. }
  367. }
  368. }
  369. func (s *DockerSuite) TestPsLinkedWithNoTrunc(c *check.C) {
  370. testRequires(c, DaemonIsLinux)
  371. dockerCmd(c, "run", "--name=first", "-d", "busybox", "top")
  372. dockerCmd(c, "run", "--name=second", "--link=first:first", "-d", "busybox", "top")
  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. testRequires(c, DaemonIsLinux)
  387. portRange := "3800-3900"
  388. dockerCmd(c, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top")
  389. out, _ := dockerCmd(c, "ps")
  390. c.Assert(string(out), checker.Contains, portRange, check.Commentf("docker ps output should have had the port range %q: %s", portRange, string(out)))
  391. }
  392. func (s *DockerSuite) TestPsWithSize(c *check.C) {
  393. testRequires(c, DaemonIsLinux)
  394. dockerCmd(c, "run", "-d", "--name", "sizetest", "busybox", "top")
  395. out, _ := dockerCmd(c, "ps", "--size")
  396. c.Assert(out, checker.Contains, "virtual", check.Commentf("docker ps with --size should show virtual size of container"))
  397. }
  398. func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) {
  399. testRequires(c, DaemonIsLinux)
  400. // create a container
  401. out, _ := dockerCmd(c, "create", "busybox")
  402. cID := strings.TrimSpace(out)
  403. shortCID := cID[:12]
  404. // Make sure it DOESN'T show up w/o a '-a' for normal 'ps'
  405. out, _ = dockerCmd(c, "ps", "-q")
  406. c.Assert(out, checker.Not(checker.Contains), shortCID, check.Commentf("Should have not seen '%s' in ps output:\n%s", shortCID, out))
  407. // Make sure it DOES show up as 'Created' for 'ps -a'
  408. out, _ = dockerCmd(c, "ps", "-a")
  409. hits := 0
  410. for _, line := range strings.Split(out, "\n") {
  411. if !strings.Contains(line, shortCID) {
  412. continue
  413. }
  414. hits++
  415. c.Assert(line, checker.Contains, "Created", check.Commentf("Missing 'Created' on '%s'", line))
  416. }
  417. c.Assert(hits, checker.Equals, 1, check.Commentf("Should have seen '%s' in ps -a output once:%d\n%s", shortCID, hits, out))
  418. // filter containers by 'create' - note, no -a needed
  419. out, _ = dockerCmd(c, "ps", "-q", "-f", "status=created")
  420. containerOut := strings.TrimSpace(out)
  421. c.Assert(cID, checker.HasPrefix, containerOut)
  422. }
  423. func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) {
  424. testRequires(c, DaemonIsLinux)
  425. //create 2 containers and link them
  426. dockerCmd(c, "run", "--name=child", "-d", "busybox", "top")
  427. dockerCmd(c, "run", "--name=parent", "--link=child:linkedone", "-d", "busybox", "top")
  428. //use the new format capabilities to only list the names and --no-trunc to get all names
  429. out, _ := dockerCmd(c, "ps", "--format", "{{.Names}}", "--no-trunc")
  430. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  431. expected := []string{"parent", "child,parent/linkedone"}
  432. var names []string
  433. for _, l := range lines {
  434. names = append(names, l)
  435. }
  436. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with non-truncated names: %v, got: %v", expected, names))
  437. //now list without turning off truncation and make sure we only get the non-link names
  438. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}}")
  439. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  440. expected = []string{"parent", "child"}
  441. var truncNames []string
  442. for _, l := range lines {
  443. truncNames = append(truncNames, l)
  444. }
  445. c.Assert(expected, checker.DeepEquals, truncNames, check.Commentf("Expected array with truncated names: %v, got: %v", expected, truncNames))
  446. }
  447. func (s *DockerSuite) TestPsFormatHeaders(c *check.C) {
  448. testRequires(c, DaemonIsLinux)
  449. // make sure no-container "docker ps" still prints the header row
  450. out, _ := dockerCmd(c, "ps", "--format", "table {{.ID}}")
  451. c.Assert(out, checker.Equals, "CONTAINER ID\n", check.Commentf(`Expected 'CONTAINER ID\n', got %v`, out))
  452. // verify that "docker ps" with a container still prints the header row also
  453. dockerCmd(c, "run", "--name=test", "-d", "busybox", "top")
  454. out, _ = dockerCmd(c, "ps", "--format", "table {{.Names}}")
  455. c.Assert(out, checker.Equals, "NAMES\ntest\n", check.Commentf(`Expected 'NAMES\ntest\n', got %v`, out))
  456. }
  457. func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) {
  458. testRequires(c, DaemonIsLinux)
  459. config := `{
  460. "psFormat": "{{ .ID }} default"
  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, _ := dockerCmd(c, "run", "--name=test", "-d", "busybox", "top")
  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. testRequires(c, DaemonIsLinux)
  475. originalImageName := "busybox:TestPsImageIDAfterUpdate-original"
  476. updatedImageName := "busybox:TestPsImageIDAfterUpdate-updated"
  477. runCmd := exec.Command(dockerBinary, "tag", "busybox:latest", originalImageName)
  478. out, _, err := runCommandWithOutput(runCmd)
  479. c.Assert(err, checker.IsNil)
  480. originalImageID, err := getIDByName(originalImageName)
  481. c.Assert(err, checker.IsNil)
  482. runCmd = exec.Command(dockerBinary, "run", "-d", originalImageName, "top")
  483. out, _, err = runCommandWithOutput(runCmd)
  484. c.Assert(err, checker.IsNil)
  485. containerID := strings.TrimSpace(out)
  486. linesOut, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  487. c.Assert(err, checker.IsNil)
  488. lines := strings.Split(strings.TrimSpace(string(linesOut)), "\n")
  489. // skip header
  490. lines = lines[1:]
  491. c.Assert(len(lines), checker.Equals, 1)
  492. for _, line := range lines {
  493. f := strings.Fields(line)
  494. c.Assert(f[1], checker.Equals, originalImageName)
  495. }
  496. runCmd = exec.Command(dockerBinary, "commit", containerID, updatedImageName)
  497. out, _, err = runCommandWithOutput(runCmd)
  498. c.Assert(err, checker.IsNil)
  499. runCmd = exec.Command(dockerBinary, "tag", "-f", updatedImageName, originalImageName)
  500. out, _, err = runCommandWithOutput(runCmd)
  501. c.Assert(err, checker.IsNil)
  502. linesOut, err = exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  503. c.Assert(err, checker.IsNil)
  504. lines = strings.Split(strings.TrimSpace(string(linesOut)), "\n")
  505. // skip header
  506. lines = lines[1:]
  507. c.Assert(len(lines), checker.Equals, 1)
  508. for _, line := range lines {
  509. f := strings.Fields(line)
  510. c.Assert(f[1], checker.Equals, originalImageID)
  511. }
  512. }