docker_cli_ps_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. }
  195. func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) {
  196. // start container
  197. out, _ := dockerCmd(c, "run", "-d", "busybox")
  198. firstID := strings.TrimSpace(out)
  199. // start another container
  200. dockerCmd(c, "run", "-d", "busybox", "top")
  201. // filter containers by id
  202. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=id="+firstID)
  203. containerOut := strings.TrimSpace(out)
  204. if containerOut != firstID[:12] {
  205. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  206. }
  207. }
  208. func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) {
  209. // start container
  210. out, _ := dockerCmd(c, "run", "-d", "--name=a_name_to_match", "busybox")
  211. firstID := strings.TrimSpace(out)
  212. // start another container
  213. dockerCmd(c, "run", "-d", "--name=b_name_to_match", "busybox", "top")
  214. // filter containers by name
  215. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=name=a_name_to_match")
  216. containerOut := strings.TrimSpace(out)
  217. if containerOut != firstID[:12] {
  218. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  219. }
  220. }
  221. func (s *DockerSuite) TestPsListContainersFilterLabel(c *check.C) {
  222. // start container
  223. out, _ := dockerCmd(c, "run", "-d", "-l", "match=me", "-l", "second=tag", "busybox")
  224. firstID := strings.TrimSpace(out)
  225. // start another container
  226. out, _ = dockerCmd(c, "run", "-d", "-l", "match=me too", "busybox")
  227. secondID := strings.TrimSpace(out)
  228. // start third container
  229. out, _ = dockerCmd(c, "run", "-d", "-l", "nomatch=me", "busybox")
  230. thirdID := strings.TrimSpace(out)
  231. // filter containers by exact match
  232. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me")
  233. containerOut := strings.TrimSpace(out)
  234. if containerOut != firstID {
  235. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
  236. }
  237. // filter containers by two labels
  238. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag")
  239. containerOut = strings.TrimSpace(out)
  240. if containerOut != firstID {
  241. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
  242. }
  243. // filter containers by two labels, but expect not found because of AND behavior
  244. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no")
  245. containerOut = strings.TrimSpace(out)
  246. if containerOut != "" {
  247. c.Fatalf("Expected nothing, got %s for exited filter, output: %q", containerOut, out)
  248. }
  249. // filter containers by exact key
  250. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match")
  251. containerOut = strings.TrimSpace(out)
  252. if (!strings.Contains(containerOut, firstID) || !strings.Contains(containerOut, secondID)) || strings.Contains(containerOut, thirdID) {
  253. c.Fatalf("Expected ids %s,%s, got %s for exited filter, output: %q", firstID, secondID, containerOut, out)
  254. }
  255. }
  256. func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) {
  257. dockerCmd(c, "run", "-d", "--name", "top", "busybox", "top")
  258. dockerCmd(c, "run", "--name", "zero1", "busybox", "true")
  259. firstZero, err := getIDByName("zero1")
  260. if err != nil {
  261. c.Fatal(err)
  262. }
  263. dockerCmd(c, "run", "--name", "zero2", "busybox", "true")
  264. secondZero, err := getIDByName("zero2")
  265. if err != nil {
  266. c.Fatal(err)
  267. }
  268. runCmd := exec.Command(dockerBinary, "run", "--name", "nonzero1", "busybox", "false")
  269. if out, _, err := runCommandWithOutput(runCmd); err == nil {
  270. c.Fatal("Should fail.", out, err)
  271. }
  272. firstNonZero, err := getIDByName("nonzero1")
  273. if err != nil {
  274. c.Fatal(err)
  275. }
  276. runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero2", "busybox", "false")
  277. if out, _, err := runCommandWithOutput(runCmd); err == nil {
  278. c.Fatal("Should fail.", out, err)
  279. }
  280. secondNonZero, err := getIDByName("nonzero2")
  281. if err != nil {
  282. c.Fatal(err)
  283. }
  284. // filter containers by exited=0
  285. out, _ := dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
  286. ids := strings.Split(strings.TrimSpace(out), "\n")
  287. if len(ids) != 2 {
  288. c.Fatalf("Should be 2 zero exited containers got %d: %s", len(ids), out)
  289. }
  290. if ids[0] != secondZero {
  291. c.Fatalf("First in list should be %q, got %q", secondZero, ids[0])
  292. }
  293. if ids[1] != firstZero {
  294. c.Fatalf("Second in list should be %q, got %q", firstZero, ids[1])
  295. }
  296. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
  297. ids = strings.Split(strings.TrimSpace(out), "\n")
  298. if len(ids) != 2 {
  299. c.Fatalf("Should be 2 zero exited containers got %d", len(ids))
  300. }
  301. if ids[0] != secondNonZero {
  302. c.Fatalf("First in list should be %q, got %q", secondNonZero, ids[0])
  303. }
  304. if ids[1] != firstNonZero {
  305. c.Fatalf("Second in list should be %q, got %q", firstNonZero, ids[1])
  306. }
  307. }
  308. func (s *DockerSuite) TestPsRightTagName(c *check.C) {
  309. tag := "asybox:shmatest"
  310. if out, err := exec.Command(dockerBinary, "tag", "busybox", tag).CombinedOutput(); err != nil {
  311. c.Fatalf("Failed to tag image: %s, out: %q", err, out)
  312. }
  313. var id1 string
  314. if out, err := exec.Command(dockerBinary, "run", "-d", "busybox", "top").CombinedOutput(); err != nil {
  315. c.Fatalf("Failed to run container: %s, out: %q", err, out)
  316. } else {
  317. id1 = strings.TrimSpace(string(out))
  318. }
  319. var id2 string
  320. if out, err := exec.Command(dockerBinary, "run", "-d", tag, "top").CombinedOutput(); err != nil {
  321. c.Fatalf("Failed to run container: %s, out: %q", err, out)
  322. } else {
  323. id2 = strings.TrimSpace(string(out))
  324. }
  325. var imageID string
  326. if out, err := exec.Command(dockerBinary, "inspect", "-f", "{{.Id}}", "busybox").CombinedOutput(); err != nil {
  327. c.Fatalf("failed to get the image ID of busybox: %s, %v", out, err)
  328. } else {
  329. imageID = strings.TrimSpace(string(out))
  330. }
  331. var id3 string
  332. if out, err := exec.Command(dockerBinary, "run", "-d", imageID, "top").CombinedOutput(); err != nil {
  333. c.Fatalf("Failed to run container: %s, out: %q", err, out)
  334. } else {
  335. id3 = strings.TrimSpace(string(out))
  336. }
  337. out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  338. if err != nil {
  339. c.Fatalf("Failed to run 'ps': %s, out: %q", err, out)
  340. }
  341. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  342. // skip header
  343. lines = lines[1:]
  344. if len(lines) != 3 {
  345. c.Fatalf("There should be 3 running container, got %d", len(lines))
  346. }
  347. for _, line := range lines {
  348. f := strings.Fields(line)
  349. switch f[0] {
  350. case id1:
  351. if f[1] != "busybox" {
  352. c.Fatalf("Expected %s tag for id %s, got %s", "busybox", id1, f[1])
  353. }
  354. case id2:
  355. if f[1] != tag {
  356. c.Fatalf("Expected %s tag for id %s, got %s", tag, id2, f[1])
  357. }
  358. case id3:
  359. if f[1] != imageID {
  360. c.Fatalf("Expected %s imageID for id %s, got %s", tag, id3, f[1])
  361. }
  362. default:
  363. c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3)
  364. }
  365. }
  366. }
  367. func (s *DockerSuite) TestPsLinkedWithNoTrunc(c *check.C) {
  368. if out, err := exec.Command(dockerBinary, "run", "--name=first", "-d", "busybox", "top").CombinedOutput(); err != nil {
  369. c.Fatalf("Output: %s, err: %s", out, err)
  370. }
  371. if out, err := exec.Command(dockerBinary, "run", "--name=second", "--link=first:first", "-d", "busybox", "top").CombinedOutput(); err != nil {
  372. c.Fatalf("Output: %s, err: %s", out, err)
  373. }
  374. out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  375. if err != nil {
  376. c.Fatalf("Output: %s, err: %s", out, err)
  377. }
  378. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  379. // strip header
  380. lines = lines[1:]
  381. expected := []string{"second", "first,second/first"}
  382. var names []string
  383. for _, l := range lines {
  384. fields := strings.Fields(l)
  385. names = append(names, fields[len(fields)-1])
  386. }
  387. if !reflect.DeepEqual(expected, names) {
  388. c.Fatalf("Expected array: %v, got: %v", expected, names)
  389. }
  390. }
  391. func (s *DockerSuite) TestPsGroupPortRange(c *check.C) {
  392. portRange := "3800-3900"
  393. dockerCmd(c, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top")
  394. out, _ := dockerCmd(c, "ps")
  395. // check that the port range is in the output
  396. if !strings.Contains(string(out), portRange) {
  397. c.Fatalf("docker ps output should have had the port range %q: %s", portRange, string(out))
  398. }
  399. }
  400. func (s *DockerSuite) TestPsWithSize(c *check.C) {
  401. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "sizetest", "busybox", "top"))
  402. if err != nil {
  403. c.Fatal(out, err)
  404. }
  405. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "ps", "--size"))
  406. if err != nil {
  407. c.Fatal(out, err)
  408. }
  409. if !strings.Contains(out, "virtual") {
  410. c.Fatalf("docker ps with --size should show virtual size of container")
  411. }
  412. }
  413. func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) {
  414. // create a container
  415. createCmd := exec.Command(dockerBinary, "create", "busybox")
  416. out, _, err := runCommandWithOutput(createCmd)
  417. if err != nil {
  418. c.Fatal(out, err)
  419. }
  420. cID := strings.TrimSpace(out)
  421. shortCID := cID[:12]
  422. // Make sure it DOESN'T show up w/o a '-a' for normal 'ps'
  423. runCmd := exec.Command(dockerBinary, "ps", "-q")
  424. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  425. c.Fatal(out, err)
  426. }
  427. if strings.Contains(out, shortCID) {
  428. c.Fatalf("Should have not seen '%s' in ps output:\n%s", shortCID, out)
  429. }
  430. // Make sure it DOES show up as 'Created' for 'ps -a'
  431. runCmd = exec.Command(dockerBinary, "ps", "-a")
  432. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  433. c.Fatal(out, err)
  434. }
  435. hits := 0
  436. for _, line := range strings.Split(out, "\n") {
  437. if !strings.Contains(line, shortCID) {
  438. continue
  439. }
  440. hits++
  441. if !strings.Contains(line, "Created") {
  442. c.Fatalf("Missing 'Created' on '%s'", line)
  443. }
  444. }
  445. if hits != 1 {
  446. c.Fatalf("Should have seen '%s' in ps -a output once:%d\n%s", shortCID, hits, out)
  447. }
  448. // filter containers by 'create' - note, no -a needed
  449. runCmd = exec.Command(dockerBinary, "ps", "-q", "-f", "status=created")
  450. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  451. c.Fatal(out, err)
  452. }
  453. containerOut := strings.TrimSpace(out)
  454. if !strings.HasPrefix(cID, containerOut) {
  455. c.Fatalf("Expected id %s, got %s for filter, out: %s", cID, containerOut, out)
  456. }
  457. }