docker_cli_ps_test.go 24 KB

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