docker_cli_ps_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "reflect"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/go-check/check"
  10. )
  11. func (s *DockerSuite) TestPsListContainers(c *check.C) {
  12. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  13. firstID := strings.TrimSpace(out)
  14. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  15. secondID := strings.TrimSpace(out)
  16. // not long running
  17. out, _ = dockerCmd(c, "run", "-d", "busybox", "true")
  18. thirdID := strings.TrimSpace(out)
  19. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  20. fourthID := strings.TrimSpace(out)
  21. // make sure the second is running
  22. if err := waitRun(secondID); err != nil {
  23. c.Fatalf("waiting for container failed: %v", err)
  24. }
  25. // make sure third one is not running
  26. dockerCmd(c, "wait", thirdID)
  27. // make sure the forth is running
  28. if err := waitRun(fourthID); err != nil {
  29. c.Fatalf("waiting for container failed: %v", err)
  30. }
  31. // all
  32. out, _ = dockerCmd(c, "ps", "-a")
  33. if !assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}) {
  34. c.Errorf("Container list is not in the correct order: %s", out)
  35. }
  36. // running
  37. out, _ = dockerCmd(c, "ps")
  38. if !assertContainerList(out, []string{fourthID, secondID, firstID}) {
  39. c.Errorf("Container list is not in the correct order: %s", out)
  40. }
  41. // from here all flag '-a' is ignored
  42. // limit
  43. out, _ = dockerCmd(c, "ps", "-n=2", "-a")
  44. expected := []string{fourthID, thirdID}
  45. if !assertContainerList(out, expected) {
  46. c.Errorf("Container list is not in the correct order: %s", out)
  47. }
  48. out, _ = dockerCmd(c, "ps", "-n=2")
  49. if !assertContainerList(out, expected) {
  50. c.Errorf("Container list is not in the correct order: %s", out)
  51. }
  52. // since
  53. out, _ = dockerCmd(c, "ps", "--since", firstID, "-a")
  54. expected = []string{fourthID, thirdID, secondID}
  55. if !assertContainerList(out, expected) {
  56. c.Errorf("Container list is not in the correct order: %s", out)
  57. }
  58. out, _ = dockerCmd(c, "ps", "--since", firstID)
  59. if !assertContainerList(out, expected) {
  60. c.Errorf("Container list is not in the correct order: %s", out)
  61. }
  62. // before
  63. out, _ = dockerCmd(c, "ps", "--before", thirdID, "-a")
  64. expected = []string{secondID, firstID}
  65. if !assertContainerList(out, expected) {
  66. c.Errorf("Container list is not in the correct order: %s", out)
  67. }
  68. out, _ = dockerCmd(c, "ps", "--before", thirdID)
  69. if !assertContainerList(out, expected) {
  70. c.Errorf("Container list is not in the correct order: %s", out)
  71. }
  72. // since & before
  73. out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-a")
  74. expected = []string{thirdID, secondID}
  75. if !assertContainerList(out, expected) {
  76. c.Errorf("Container list is not in the correct order: %s", out)
  77. }
  78. out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID)
  79. if !assertContainerList(out, expected) {
  80. c.Errorf("Container list is not in the correct order: %s", out)
  81. }
  82. // since & limit
  83. out, _ = dockerCmd(c, "ps", "--since", firstID, "-n=2", "-a")
  84. expected = []string{fourthID, thirdID}
  85. if !assertContainerList(out, expected) {
  86. c.Errorf("Container list is not in the correct order: %s", out)
  87. }
  88. out, _ = dockerCmd(c, "ps", "--since", firstID, "-n=2")
  89. if !assertContainerList(out, expected) {
  90. c.Errorf("Container list is not in the correct order: %s", out)
  91. }
  92. // before & limit
  93. out, _ = dockerCmd(c, "ps", "--before", fourthID, "-n=1", "-a")
  94. expected = []string{thirdID}
  95. if !assertContainerList(out, expected) {
  96. c.Errorf("Container list is not in the correct order: %s", out)
  97. }
  98. out, _ = dockerCmd(c, "ps", "--before", fourthID, "-n=1")
  99. if !assertContainerList(out, expected) {
  100. c.Errorf("Container list is not in the correct order: %s", out)
  101. }
  102. out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a")
  103. expected = []string{thirdID}
  104. if !assertContainerList(out, expected) {
  105. c.Errorf("Container list is not in the correct order: %s", out)
  106. }
  107. out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-n=1")
  108. if !assertContainerList(out, expected) {
  109. c.Errorf("Container list is not in the correct order: %s", out)
  110. }
  111. }
  112. func assertContainerList(out string, expected []string) bool {
  113. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  114. if len(lines)-1 != len(expected) {
  115. return false
  116. }
  117. containerIDIndex := strings.Index(lines[0], "CONTAINER ID")
  118. for i := 0; i < len(expected); i++ {
  119. foundID := lines[i+1][containerIDIndex : containerIDIndex+12]
  120. if foundID != expected[i][:12] {
  121. return false
  122. }
  123. }
  124. return true
  125. }
  126. func (s *DockerSuite) TestPsListContainersSize(c *check.C) {
  127. dockerCmd(c, "run", "-d", "busybox", "echo", "hello")
  128. baseOut, _ := dockerCmd(c, "ps", "-s", "-n=1")
  129. baseLines := strings.Split(strings.Trim(baseOut, "\n "), "\n")
  130. baseSizeIndex := strings.Index(baseLines[0], "SIZE")
  131. baseFoundsize := baseLines[1][baseSizeIndex:]
  132. baseBytes, err := strconv.Atoi(strings.Split(baseFoundsize, " ")[0])
  133. if err != nil {
  134. c.Fatal(err)
  135. }
  136. name := "test_size"
  137. out, _ := dockerCmd(c, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test")
  138. id, err := getIDByName(name)
  139. if err != nil {
  140. c.Fatal(err)
  141. }
  142. runCmd := exec.Command(dockerBinary, "ps", "-s", "-n=1")
  143. wait := make(chan struct{})
  144. go func() {
  145. out, _, err = runCommandWithOutput(runCmd)
  146. close(wait)
  147. }()
  148. select {
  149. case <-wait:
  150. case <-time.After(3 * time.Second):
  151. c.Fatalf("Calling \"docker ps -s\" timed out!")
  152. }
  153. if err != nil {
  154. c.Fatal(out, err)
  155. }
  156. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  157. if len(lines) != 2 {
  158. c.Fatalf("Expected 2 lines for 'ps -s -n=1' output, got %d", len(lines))
  159. }
  160. sizeIndex := strings.Index(lines[0], "SIZE")
  161. idIndex := strings.Index(lines[0], "CONTAINER ID")
  162. foundID := lines[1][idIndex : idIndex+12]
  163. if foundID != id[:12] {
  164. c.Fatalf("Expected id %s, got %s", id[:12], foundID)
  165. }
  166. expectedSize := fmt.Sprintf("%d B", (2 + baseBytes))
  167. foundSize := lines[1][sizeIndex:]
  168. if !strings.Contains(foundSize, expectedSize) {
  169. c.Fatalf("Expected size %q, got %q", expectedSize, foundSize)
  170. }
  171. }
  172. func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) {
  173. // FIXME: this should test paused, but it makes things hang and its wonky
  174. // this is because paused containers can't be controlled by signals
  175. // start exited container
  176. out, _ := dockerCmd(c, "run", "-d", "busybox")
  177. firstID := strings.TrimSpace(out)
  178. // make sure the exited cintainer is not running
  179. dockerCmd(c, "wait", firstID)
  180. // start running container
  181. out, _ = dockerCmd(c, "run", "-itd", "busybox")
  182. secondID := strings.TrimSpace(out)
  183. // filter containers by exited
  184. out, _ = dockerCmd(c, "ps", "-q", "--filter=status=exited")
  185. containerOut := strings.TrimSpace(out)
  186. if containerOut != firstID[:12] {
  187. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  188. }
  189. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=status=running")
  190. containerOut = strings.TrimSpace(out)
  191. if containerOut != secondID[:12] {
  192. c.Fatalf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out)
  193. }
  194. out, _, _ = dockerCmdWithTimeout(time.Second*60, "ps", "-a", "-q", "--filter=status=rubbish")
  195. if !strings.Contains(out, "Unrecognised filter value for status") {
  196. c.Fatalf("Expected error response due to invalid status filter output: %q", out)
  197. }
  198. }
  199. func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) {
  200. // start container
  201. out, _ := dockerCmd(c, "run", "-d", "busybox")
  202. firstID := strings.TrimSpace(out)
  203. // start another container
  204. dockerCmd(c, "run", "-d", "busybox", "top")
  205. // filter containers by id
  206. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=id="+firstID)
  207. containerOut := strings.TrimSpace(out)
  208. if containerOut != firstID[:12] {
  209. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  210. }
  211. }
  212. func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) {
  213. // start container
  214. out, _ := dockerCmd(c, "run", "-d", "--name=a_name_to_match", "busybox")
  215. firstID := strings.TrimSpace(out)
  216. // start another container
  217. dockerCmd(c, "run", "-d", "--name=b_name_to_match", "busybox", "top")
  218. // filter containers by name
  219. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=name=a_name_to_match")
  220. containerOut := strings.TrimSpace(out)
  221. if containerOut != firstID[:12] {
  222. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  223. }
  224. }
  225. func (s *DockerSuite) TestPsListContainersFilterLabel(c *check.C) {
  226. // start container
  227. out, _ := dockerCmd(c, "run", "-d", "-l", "match=me", "-l", "second=tag", "busybox")
  228. firstID := strings.TrimSpace(out)
  229. // start another container
  230. out, _ = dockerCmd(c, "run", "-d", "-l", "match=me too", "busybox")
  231. secondID := strings.TrimSpace(out)
  232. // start third container
  233. out, _ = dockerCmd(c, "run", "-d", "-l", "nomatch=me", "busybox")
  234. thirdID := strings.TrimSpace(out)
  235. // filter containers by exact match
  236. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me")
  237. containerOut := strings.TrimSpace(out)
  238. if containerOut != firstID {
  239. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
  240. }
  241. // filter containers by two labels
  242. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag")
  243. containerOut = strings.TrimSpace(out)
  244. if containerOut != firstID {
  245. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
  246. }
  247. // filter containers by two labels, but expect not found because of AND behavior
  248. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no")
  249. containerOut = strings.TrimSpace(out)
  250. if containerOut != "" {
  251. c.Fatalf("Expected nothing, got %s for exited filter, output: %q", containerOut, out)
  252. }
  253. // filter containers by exact key
  254. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match")
  255. containerOut = strings.TrimSpace(out)
  256. if (!strings.Contains(containerOut, firstID) || !strings.Contains(containerOut, secondID)) || strings.Contains(containerOut, thirdID) {
  257. c.Fatalf("Expected ids %s,%s, got %s for exited filter, output: %q", firstID, secondID, containerOut, out)
  258. }
  259. }
  260. func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) {
  261. dockerCmd(c, "run", "-d", "--name", "top", "busybox", "top")
  262. dockerCmd(c, "run", "--name", "zero1", "busybox", "true")
  263. firstZero, err := getIDByName("zero1")
  264. if err != nil {
  265. c.Fatal(err)
  266. }
  267. dockerCmd(c, "run", "--name", "zero2", "busybox", "true")
  268. secondZero, err := getIDByName("zero2")
  269. if err != nil {
  270. c.Fatal(err)
  271. }
  272. if out, _, err := dockerCmdWithError(c, "run", "--name", "nonzero1", "busybox", "false"); err == nil {
  273. c.Fatal("Should fail.", out, err)
  274. }
  275. firstNonZero, err := getIDByName("nonzero1")
  276. if err != nil {
  277. c.Fatal(err)
  278. }
  279. if out, _, err := dockerCmdWithError(c, "run", "--name", "nonzero2", "busybox", "false"); err == nil {
  280. c.Fatal("Should fail.", out, err)
  281. }
  282. secondNonZero, err := getIDByName("nonzero2")
  283. if err != nil {
  284. c.Fatal(err)
  285. }
  286. // filter containers by exited=0
  287. out, _ := dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
  288. ids := strings.Split(strings.TrimSpace(out), "\n")
  289. if len(ids) != 2 {
  290. c.Fatalf("Should be 2 zero exited containers got %d: %s", len(ids), out)
  291. }
  292. if ids[0] != secondZero {
  293. c.Fatalf("First in list should be %q, got %q", secondZero, ids[0])
  294. }
  295. if ids[1] != firstZero {
  296. c.Fatalf("Second in list should be %q, got %q", firstZero, ids[1])
  297. }
  298. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
  299. ids = strings.Split(strings.TrimSpace(out), "\n")
  300. if len(ids) != 2 {
  301. c.Fatalf("Should be 2 zero exited containers got %d", len(ids))
  302. }
  303. if ids[0] != secondNonZero {
  304. c.Fatalf("First in list should be %q, got %q", secondNonZero, ids[0])
  305. }
  306. if ids[1] != firstNonZero {
  307. c.Fatalf("Second in list should be %q, got %q", firstNonZero, ids[1])
  308. }
  309. }
  310. func (s *DockerSuite) TestPsRightTagName(c *check.C) {
  311. tag := "asybox:shmatest"
  312. dockerCmd(c, "tag", "busybox", tag)
  313. var id1 string
  314. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  315. id1 = strings.TrimSpace(string(out))
  316. var id2 string
  317. out, _ = dockerCmd(c, "run", "-d", tag, "top")
  318. id2 = strings.TrimSpace(string(out))
  319. var imageID string
  320. out, _ = dockerCmd(c, "inspect", "-f", "{{.Id}}", "busybox")
  321. imageID = strings.TrimSpace(string(out))
  322. var id3 string
  323. out, _ = dockerCmd(c, "run", "-d", imageID, "top")
  324. id3 = strings.TrimSpace(string(out))
  325. out, _ = dockerCmd(c, "ps", "--no-trunc")
  326. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  327. // skip header
  328. lines = lines[1:]
  329. if len(lines) != 3 {
  330. c.Fatalf("There should be 3 running container, got %d", len(lines))
  331. }
  332. for _, line := range lines {
  333. f := strings.Fields(line)
  334. switch f[0] {
  335. case id1:
  336. if f[1] != "busybox" {
  337. c.Fatalf("Expected %s tag for id %s, got %s", "busybox", id1, f[1])
  338. }
  339. case id2:
  340. if f[1] != tag {
  341. c.Fatalf("Expected %s tag for id %s, got %s", tag, id2, f[1])
  342. }
  343. case id3:
  344. if f[1] != imageID {
  345. c.Fatalf("Expected %s imageID for id %s, got %s", tag, id3, f[1])
  346. }
  347. default:
  348. c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3)
  349. }
  350. }
  351. }
  352. func (s *DockerSuite) TestPsLinkedWithNoTrunc(c *check.C) {
  353. dockerCmd(c, "run", "--name=first", "-d", "busybox", "top")
  354. dockerCmd(c, "run", "--name=second", "--link=first:first", "-d", "busybox", "top")
  355. out, _ := dockerCmd(c, "ps", "--no-trunc")
  356. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  357. // strip header
  358. lines = lines[1:]
  359. expected := []string{"second", "first,second/first"}
  360. var names []string
  361. for _, l := range lines {
  362. fields := strings.Fields(l)
  363. names = append(names, fields[len(fields)-1])
  364. }
  365. if !reflect.DeepEqual(expected, names) {
  366. c.Fatalf("Expected array: %v, got: %v", expected, names)
  367. }
  368. }
  369. func (s *DockerSuite) TestPsGroupPortRange(c *check.C) {
  370. portRange := "3800-3900"
  371. dockerCmd(c, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top")
  372. out, _ := dockerCmd(c, "ps")
  373. // check that the port range is in the output
  374. if !strings.Contains(string(out), portRange) {
  375. c.Fatalf("docker ps output should have had the port range %q: %s", portRange, string(out))
  376. }
  377. }
  378. func (s *DockerSuite) TestPsWithSize(c *check.C) {
  379. dockerCmd(c, "run", "-d", "--name", "sizetest", "busybox", "top")
  380. out, _ := dockerCmd(c, "ps", "--size")
  381. if !strings.Contains(out, "virtual") {
  382. c.Fatalf("docker ps with --size should show virtual size of container")
  383. }
  384. }
  385. func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) {
  386. // create a container
  387. out, _ := dockerCmd(c, "create", "busybox")
  388. cID := strings.TrimSpace(out)
  389. shortCID := cID[:12]
  390. // Make sure it DOESN'T show up w/o a '-a' for normal 'ps'
  391. out, _ = dockerCmd(c, "ps", "-q")
  392. if strings.Contains(out, shortCID) {
  393. c.Fatalf("Should have not seen '%s' in ps output:\n%s", shortCID, out)
  394. }
  395. // Make sure it DOES show up as 'Created' for 'ps -a'
  396. out, _ = dockerCmd(c, "ps", "-a")
  397. hits := 0
  398. for _, line := range strings.Split(out, "\n") {
  399. if !strings.Contains(line, shortCID) {
  400. continue
  401. }
  402. hits++
  403. if !strings.Contains(line, "Created") {
  404. c.Fatalf("Missing 'Created' on '%s'", line)
  405. }
  406. }
  407. if hits != 1 {
  408. c.Fatalf("Should have seen '%s' in ps -a output once:%d\n%s", shortCID, hits, out)
  409. }
  410. // filter containers by 'create' - note, no -a needed
  411. out, _ = dockerCmd(c, "ps", "-q", "-f", "status=created")
  412. containerOut := strings.TrimSpace(out)
  413. if !strings.HasPrefix(cID, containerOut) {
  414. c.Fatalf("Expected id %s, got %s for filter, out: %s", cID, containerOut, out)
  415. }
  416. }
  417. func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) {
  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. if !reflect.DeepEqual(expected, names) {
  430. c.Fatalf("Expected array with non-truncated names: %v, got: %v", expected, names)
  431. }
  432. //now list without turning off truncation and make sure we only get the non-link names
  433. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}}")
  434. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  435. expected = []string{"parent", "child"}
  436. var truncNames []string
  437. for _, l := range lines {
  438. truncNames = append(truncNames, l)
  439. }
  440. if !reflect.DeepEqual(expected, truncNames) {
  441. c.Fatalf("Expected array with truncated names: %v, got: %v", expected, truncNames)
  442. }
  443. }
  444. func (s *DockerSuite) TestPsFormatHeaders(c *check.C) {
  445. // make sure no-container "docker ps" still prints the header row
  446. out, _ := dockerCmd(c, "ps", "--format", "table {{.ID}}")
  447. if out != "CONTAINER ID\n" {
  448. c.Fatalf(`Expected 'CONTAINER ID\n', got %v`, out)
  449. }
  450. // verify that "docker ps" with a container still prints the header row also
  451. dockerCmd(c, "run", "--name=test", "-d", "busybox", "top")
  452. out, _ = dockerCmd(c, "ps", "--format", "table {{.Names}}")
  453. if out != "NAMES\ntest\n" {
  454. c.Fatalf(`Expected 'NAMES\ntest\n', got %v`, out)
  455. }
  456. }