docker_cli_ps_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/docker/docker/pkg/integration/checker"
  12. "github.com/go-check/check"
  13. "sort"
  14. "github.com/docker/docker/pkg/stringid"
  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. // FIXME: this should test paused, but it makes things hang and its wonky
  134. // this is because paused containers can't be controlled by signals
  135. // start exited container
  136. out, _ := dockerCmd(c, "run", "-d", "busybox")
  137. firstID := strings.TrimSpace(out)
  138. // make sure the exited cintainer is not running
  139. dockerCmd(c, "wait", firstID)
  140. // start running container
  141. out, _ = dockerCmd(c, "run", "-itd", "busybox")
  142. secondID := strings.TrimSpace(out)
  143. // filter containers by exited
  144. out, _ = dockerCmd(c, "ps", "-q", "--filter=status=exited")
  145. containerOut := strings.TrimSpace(out)
  146. c.Assert(containerOut, checker.Equals, firstID[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out))
  147. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=status=running")
  148. containerOut = strings.TrimSpace(out)
  149. c.Assert(containerOut, checker.Equals, secondID[:12], check.Commentf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out))
  150. out, _, _ = dockerCmdWithTimeout(time.Second*60, "ps", "-a", "-q", "--filter=status=rubbish")
  151. c.Assert(out, checker.Contains, "Unrecognised filter value for status", check.Commentf("Expected error response due to invalid status filter output: %q", out))
  152. }
  153. func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) {
  154. testRequires(c, DaemonIsLinux)
  155. // start container
  156. out, _ := dockerCmd(c, "run", "-d", "busybox")
  157. firstID := strings.TrimSpace(out)
  158. // start another container
  159. dockerCmd(c, "run", "-d", "busybox", "top")
  160. // filter containers by id
  161. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=id="+firstID)
  162. containerOut := strings.TrimSpace(out)
  163. c.Assert(containerOut, checker.Equals, firstID[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out))
  164. }
  165. func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) {
  166. testRequires(c, DaemonIsLinux)
  167. // start container
  168. out, _ := dockerCmd(c, "run", "-d", "--name=a_name_to_match", "busybox")
  169. firstID := strings.TrimSpace(out)
  170. // start another container
  171. dockerCmd(c, "run", "-d", "--name=b_name_to_match", "busybox", "top")
  172. // filter containers by name
  173. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=name=a_name_to_match")
  174. containerOut := strings.TrimSpace(out)
  175. c.Assert(containerOut, checker.Equals, firstID[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out))
  176. }
  177. // Test for the ancestor filter for ps.
  178. // There is also the same test but with image:tag@digest in docker_cli_by_digest_test.go
  179. //
  180. // What the test setups :
  181. // - Create 2 image based on busybox using the same repository but different tags
  182. // - Create an image based on the previous image (images_ps_filter_test2)
  183. // - Run containers for each of those image (busybox, images_ps_filter_test1, images_ps_filter_test2)
  184. // - Filter them out :P
  185. func (s *DockerSuite) TestPsListContainersFilterAncestorImage(c *check.C) {
  186. testRequires(c, DaemonIsLinux)
  187. // Build images
  188. imageName1 := "images_ps_filter_test1"
  189. imageID1, err := buildImage(imageName1,
  190. `FROM busybox
  191. LABEL match me 1`, true)
  192. c.Assert(err, checker.IsNil)
  193. imageName1Tagged := "images_ps_filter_test1:tag"
  194. imageID1Tagged, err := buildImage(imageName1Tagged,
  195. `FROM busybox
  196. LABEL match me 1 tagged`, true)
  197. c.Assert(err, checker.IsNil)
  198. imageName2 := "images_ps_filter_test2"
  199. imageID2, err := buildImage(imageName2,
  200. fmt.Sprintf(`FROM %s
  201. LABEL match me 2`, imageName1), true)
  202. c.Assert(err, checker.IsNil)
  203. // start containers
  204. out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "hello")
  205. firstID := strings.TrimSpace(out)
  206. // start another container
  207. out, _ = dockerCmd(c, "run", "-d", "busybox", "echo", "hello")
  208. secondID := strings.TrimSpace(out)
  209. // start third container
  210. out, _ = dockerCmd(c, "run", "-d", imageName1, "echo", "hello")
  211. thirdID := strings.TrimSpace(out)
  212. // start fourth container
  213. out, _ = dockerCmd(c, "run", "-d", imageName1Tagged, "echo", "hello")
  214. fourthID := strings.TrimSpace(out)
  215. // start fifth container
  216. out, _ = dockerCmd(c, "run", "-d", imageName2, "echo", "hello")
  217. fifthID := strings.TrimSpace(out)
  218. var filterTestSuite = []struct {
  219. filterName string
  220. expectedIDs []string
  221. }{
  222. // non existent stuff
  223. {"nonexistent", []string{}},
  224. {"nonexistent:tag", []string{}},
  225. // image
  226. {"busybox", []string{firstID, secondID, thirdID, fourthID, fifthID}},
  227. {imageName1, []string{thirdID, fifthID}},
  228. {imageName2, []string{fifthID}},
  229. // image:tag
  230. {fmt.Sprintf("%s:latest", imageName1), []string{thirdID, fifthID}},
  231. {imageName1Tagged, []string{fourthID}},
  232. // short-id
  233. {stringid.TruncateID(imageID1), []string{thirdID, fifthID}},
  234. {stringid.TruncateID(imageID2), []string{fifthID}},
  235. // full-id
  236. {imageID1, []string{thirdID, fifthID}},
  237. {imageID1Tagged, []string{fourthID}},
  238. {imageID2, []string{fifthID}},
  239. }
  240. for _, filter := range filterTestSuite {
  241. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+filter.filterName)
  242. checkPsAncestorFilterOutput(c, out, filter.filterName, filter.expectedIDs)
  243. }
  244. // Multiple ancestor filter
  245. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+imageName2, "--filter=ancestor="+imageName1Tagged)
  246. checkPsAncestorFilterOutput(c, out, imageName2+","+imageName1Tagged, []string{fourthID, fifthID})
  247. }
  248. func checkPsAncestorFilterOutput(c *check.C, out string, filterName string, expectedIDs []string) {
  249. actualIDs := []string{}
  250. if out != "" {
  251. actualIDs = strings.Split(out[:len(out)-1], "\n")
  252. }
  253. sort.Strings(actualIDs)
  254. sort.Strings(expectedIDs)
  255. 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))
  256. if len(expectedIDs) > 0 {
  257. same := true
  258. for i := range expectedIDs {
  259. if actualIDs[i] != expectedIDs[i] {
  260. c.Logf("%s, %s", actualIDs[i], expectedIDs[i])
  261. same = false
  262. break
  263. }
  264. }
  265. c.Assert(same, checker.Equals, true, check.Commentf("Expected filtered container(s) for %s ancestor filter to be %v, got %v", filterName, expectedIDs, actualIDs))
  266. }
  267. }
  268. func (s *DockerSuite) TestPsListContainersFilterLabel(c *check.C) {
  269. testRequires(c, DaemonIsLinux)
  270. // start container
  271. out, _ := dockerCmd(c, "run", "-d", "-l", "match=me", "-l", "second=tag", "busybox")
  272. firstID := strings.TrimSpace(out)
  273. // start another container
  274. out, _ = dockerCmd(c, "run", "-d", "-l", "match=me too", "busybox")
  275. secondID := strings.TrimSpace(out)
  276. // start third container
  277. out, _ = dockerCmd(c, "run", "-d", "-l", "nomatch=me", "busybox")
  278. thirdID := strings.TrimSpace(out)
  279. // filter containers by exact match
  280. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me")
  281. containerOut := strings.TrimSpace(out)
  282. c.Assert(containerOut, checker.Equals, firstID, check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out))
  283. // filter containers by two labels
  284. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag")
  285. containerOut = strings.TrimSpace(out)
  286. c.Assert(containerOut, checker.Equals, firstID, check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out))
  287. // filter containers by two labels, but expect not found because of AND behavior
  288. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no")
  289. containerOut = strings.TrimSpace(out)
  290. c.Assert(containerOut, checker.Equals, "", check.Commentf("Expected nothing, got %s for exited filter, output: %q", containerOut, out))
  291. // filter containers by exact key
  292. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match")
  293. containerOut = strings.TrimSpace(out)
  294. c.Assert(containerOut, checker.Contains, firstID)
  295. c.Assert(containerOut, checker.Contains, secondID)
  296. c.Assert(containerOut, checker.Not(checker.Contains), thirdID)
  297. }
  298. func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) {
  299. testRequires(c, DaemonIsLinux)
  300. dockerCmd(c, "run", "-d", "--name", "top", "busybox", "top")
  301. dockerCmd(c, "run", "--name", "zero1", "busybox", "true")
  302. firstZero, err := getIDByName("zero1")
  303. c.Assert(err, checker.IsNil)
  304. dockerCmd(c, "run", "--name", "zero2", "busybox", "true")
  305. secondZero, err := getIDByName("zero2")
  306. c.Assert(err, checker.IsNil)
  307. out, _, err := dockerCmdWithError("run", "--name", "nonzero1", "busybox", "false")
  308. c.Assert(err, checker.NotNil, check.Commentf("Should fail.", out, err))
  309. firstNonZero, err := getIDByName("nonzero1")
  310. c.Assert(err, checker.IsNil)
  311. out, _, err = dockerCmdWithError("run", "--name", "nonzero2", "busybox", "false")
  312. c.Assert(err, checker.NotNil, check.Commentf("Should fail.", out, err))
  313. secondNonZero, err := getIDByName("nonzero2")
  314. c.Assert(err, checker.IsNil)
  315. // filter containers by exited=0
  316. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
  317. ids := strings.Split(strings.TrimSpace(out), "\n")
  318. c.Assert(ids, checker.HasLen, 2, check.Commentf("Should be 2 zero exited containers got %d: %s", len(ids), out))
  319. c.Assert(ids[0], checker.Equals, secondZero, check.Commentf("First in list should be %q, got %q", secondZero, ids[0]))
  320. c.Assert(ids[1], checker.Equals, firstZero, check.Commentf("Second in list should be %q, got %q", firstZero, ids[1]))
  321. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
  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", len(ids)))
  324. c.Assert(ids[0], checker.Equals, secondNonZero, check.Commentf("First in list should be %q, got %q", secondNonZero, ids[0]))
  325. c.Assert(ids[1], checker.Equals, firstNonZero, check.Commentf("Second in list should be %q, got %q", firstNonZero, ids[1]))
  326. }
  327. func (s *DockerSuite) TestPsRightTagName(c *check.C) {
  328. testRequires(c, DaemonIsLinux)
  329. tag := "asybox:shmatest"
  330. dockerCmd(c, "tag", "busybox", tag)
  331. var id1 string
  332. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  333. id1 = strings.TrimSpace(string(out))
  334. var id2 string
  335. out, _ = dockerCmd(c, "run", "-d", tag, "top")
  336. id2 = strings.TrimSpace(string(out))
  337. var imageID string
  338. out, _ = dockerCmd(c, "inspect", "-f", "{{.Id}}", "busybox")
  339. imageID = strings.TrimSpace(string(out))
  340. var id3 string
  341. out, _ = dockerCmd(c, "run", "-d", imageID, "top")
  342. id3 = strings.TrimSpace(string(out))
  343. out, _ = dockerCmd(c, "ps", "--no-trunc")
  344. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  345. // skip header
  346. lines = lines[1:]
  347. c.Assert(lines, checker.HasLen, 3, check.Commentf("There should be 3 running container, got %d", len(lines)))
  348. for _, line := range lines {
  349. f := strings.Fields(line)
  350. switch f[0] {
  351. case id1:
  352. c.Assert(f[1], checker.Equals, "busybox", check.Commentf("Expected %s tag for id %s, got %s", "busybox", id1, f[1]))
  353. case id2:
  354. c.Assert(f[1], checker.Equals, tag, check.Commentf("Expected %s tag for id %s, got %s", tag, id2, f[1]))
  355. case id3:
  356. c.Assert(f[1], checker.Equals, imageID, check.Commentf("Expected %s imageID for id %s, got %s", tag, id3, f[1]))
  357. default:
  358. c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3)
  359. }
  360. }
  361. }
  362. func (s *DockerSuite) TestPsLinkedWithNoTrunc(c *check.C) {
  363. testRequires(c, DaemonIsLinux)
  364. dockerCmd(c, "run", "--name=first", "-d", "busybox", "top")
  365. dockerCmd(c, "run", "--name=second", "--link=first:first", "-d", "busybox", "top")
  366. out, _ := dockerCmd(c, "ps", "--no-trunc")
  367. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  368. // strip header
  369. lines = lines[1:]
  370. expected := []string{"second", "first,second/first"}
  371. var names []string
  372. for _, l := range lines {
  373. fields := strings.Fields(l)
  374. names = append(names, fields[len(fields)-1])
  375. }
  376. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array: %v, got: %v", expected, names))
  377. }
  378. func (s *DockerSuite) TestPsGroupPortRange(c *check.C) {
  379. testRequires(c, DaemonIsLinux)
  380. portRange := "3800-3900"
  381. dockerCmd(c, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top")
  382. out, _ := dockerCmd(c, "ps")
  383. c.Assert(string(out), checker.Contains, portRange, check.Commentf("docker ps output should have had the port range %q: %s", portRange, string(out)))
  384. }
  385. func (s *DockerSuite) TestPsWithSize(c *check.C) {
  386. testRequires(c, DaemonIsLinux)
  387. dockerCmd(c, "run", "-d", "--name", "sizetest", "busybox", "top")
  388. out, _ := dockerCmd(c, "ps", "--size")
  389. c.Assert(out, checker.Contains, "virtual", check.Commentf("docker ps with --size should show virtual size of container"))
  390. }
  391. func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) {
  392. testRequires(c, DaemonIsLinux)
  393. // create a container
  394. out, _ := dockerCmd(c, "create", "busybox")
  395. cID := strings.TrimSpace(out)
  396. shortCID := cID[:12]
  397. // Make sure it DOESN'T show up w/o a '-a' for normal 'ps'
  398. out, _ = dockerCmd(c, "ps", "-q")
  399. c.Assert(out, checker.Not(checker.Contains), shortCID, check.Commentf("Should have not seen '%s' in ps output:\n%s", shortCID, out))
  400. // Make sure it DOES show up as 'Created' for 'ps -a'
  401. out, _ = dockerCmd(c, "ps", "-a")
  402. hits := 0
  403. for _, line := range strings.Split(out, "\n") {
  404. if !strings.Contains(line, shortCID) {
  405. continue
  406. }
  407. hits++
  408. c.Assert(line, checker.Contains, "Created", check.Commentf("Missing 'Created' on '%s'", line))
  409. }
  410. c.Assert(hits, checker.Equals, 1, check.Commentf("Should have seen '%s' in ps -a output once:%d\n%s", shortCID, hits, out))
  411. // filter containers by 'create' - note, no -a needed
  412. out, _ = dockerCmd(c, "ps", "-q", "-f", "status=created")
  413. containerOut := strings.TrimSpace(out)
  414. c.Assert(cID, checker.HasPrefix, containerOut)
  415. }
  416. func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) {
  417. testRequires(c, DaemonIsLinux)
  418. //create 2 containers and link them
  419. dockerCmd(c, "run", "--name=child", "-d", "busybox", "top")
  420. dockerCmd(c, "run", "--name=parent", "--link=child:linkedone", "-d", "busybox", "top")
  421. //use the new format capabilities to only list the names and --no-trunc to get all names
  422. out, _ := dockerCmd(c, "ps", "--format", "{{.Names}}", "--no-trunc")
  423. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  424. expected := []string{"parent", "child,parent/linkedone"}
  425. var names []string
  426. for _, l := range lines {
  427. names = append(names, l)
  428. }
  429. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with non-truncated names: %v, got: %v", expected, names))
  430. //now list without turning off truncation and make sure we only get the non-link names
  431. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}}")
  432. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  433. expected = []string{"parent", "child"}
  434. var truncNames []string
  435. for _, l := range lines {
  436. truncNames = append(truncNames, l)
  437. }
  438. c.Assert(expected, checker.DeepEquals, truncNames, check.Commentf("Expected array with truncated names: %v, got: %v", expected, truncNames))
  439. }
  440. func (s *DockerSuite) TestPsFormatHeaders(c *check.C) {
  441. testRequires(c, DaemonIsLinux)
  442. // make sure no-container "docker ps" still prints the header row
  443. out, _ := dockerCmd(c, "ps", "--format", "table {{.ID}}")
  444. c.Assert(out, checker.Equals, "CONTAINER ID\n", check.Commentf(`Expected 'CONTAINER ID\n', got %v`, out))
  445. // verify that "docker ps" with a container still prints the header row also
  446. dockerCmd(c, "run", "--name=test", "-d", "busybox", "top")
  447. out, _ = dockerCmd(c, "ps", "--format", "table {{.Names}}")
  448. c.Assert(out, checker.Equals, "NAMES\ntest\n", check.Commentf(`Expected 'NAMES\ntest\n', got %v`, out))
  449. }
  450. func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) {
  451. testRequires(c, DaemonIsLinux)
  452. config := `{
  453. "psFormat": "{{ .ID }} default"
  454. }`
  455. d, err := ioutil.TempDir("", "integration-cli-")
  456. c.Assert(err, checker.IsNil)
  457. defer os.RemoveAll(d)
  458. err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
  459. c.Assert(err, checker.IsNil)
  460. out, _ := dockerCmd(c, "run", "--name=test", "-d", "busybox", "top")
  461. id := strings.TrimSpace(out)
  462. out, _ = dockerCmd(c, "--config", d, "ps", "-q")
  463. c.Assert(id, checker.HasPrefix, strings.TrimSpace(out), check.Commentf("Expected to print only the container id, got %v\n", out))
  464. }
  465. // Test for GitHub issue #12595
  466. func (s *DockerSuite) TestPsImageIDAfterUpdate(c *check.C) {
  467. testRequires(c, DaemonIsLinux)
  468. originalImageName := "busybox:TestPsImageIDAfterUpdate-original"
  469. updatedImageName := "busybox:TestPsImageIDAfterUpdate-updated"
  470. runCmd := exec.Command(dockerBinary, "tag", "busybox:latest", originalImageName)
  471. out, _, err := runCommandWithOutput(runCmd)
  472. c.Assert(err, checker.IsNil)
  473. originalImageID, err := getIDByName(originalImageName)
  474. c.Assert(err, checker.IsNil)
  475. runCmd = exec.Command(dockerBinary, "run", "-d", originalImageName, "top")
  476. out, _, err = runCommandWithOutput(runCmd)
  477. c.Assert(err, checker.IsNil)
  478. containerID := strings.TrimSpace(out)
  479. linesOut, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  480. c.Assert(err, checker.IsNil)
  481. lines := strings.Split(strings.TrimSpace(string(linesOut)), "\n")
  482. // skip header
  483. lines = lines[1:]
  484. c.Assert(len(lines), checker.Equals, 1)
  485. for _, line := range lines {
  486. f := strings.Fields(line)
  487. c.Assert(f[1], checker.Equals, originalImageName)
  488. }
  489. runCmd = exec.Command(dockerBinary, "commit", containerID, updatedImageName)
  490. out, _, err = runCommandWithOutput(runCmd)
  491. c.Assert(err, checker.IsNil)
  492. runCmd = exec.Command(dockerBinary, "tag", "-f", updatedImageName, originalImageName)
  493. out, _, err = runCommandWithOutput(runCmd)
  494. c.Assert(err, checker.IsNil)
  495. linesOut, err = exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  496. c.Assert(err, checker.IsNil)
  497. lines = strings.Split(strings.TrimSpace(string(linesOut)), "\n")
  498. // skip header
  499. lines = lines[1:]
  500. c.Assert(len(lines), checker.Equals, 1)
  501. for _, line := range lines {
  502. f := strings.Fields(line)
  503. c.Assert(f[1], checker.Equals, originalImageID)
  504. }
  505. }