docker_cli_ps_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  13. out, _, err := runCommandWithOutput(runCmd)
  14. if err != nil {
  15. c.Fatal(out, err)
  16. }
  17. firstID := strings.TrimSpace(out)
  18. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  19. out, _, err = runCommandWithOutput(runCmd)
  20. if err != nil {
  21. c.Fatal(out, err)
  22. }
  23. secondID := strings.TrimSpace(out)
  24. // not long running
  25. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  26. out, _, err = runCommandWithOutput(runCmd)
  27. if err != nil {
  28. c.Fatal(out, err)
  29. }
  30. thirdID := strings.TrimSpace(out)
  31. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  32. out, _, err = runCommandWithOutput(runCmd)
  33. if err != nil {
  34. c.Fatal(out, err)
  35. }
  36. fourthID := strings.TrimSpace(out)
  37. // make sure the second is running
  38. if err := waitRun(secondID); err != nil {
  39. c.Fatalf("waiting for container failed: %v", err)
  40. }
  41. // make sure third one is not running
  42. runCmd = exec.Command(dockerBinary, "wait", thirdID)
  43. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  44. c.Fatal(out, err)
  45. }
  46. // make sure the forth is running
  47. if err := waitRun(fourthID); err != nil {
  48. c.Fatalf("waiting for container failed: %v", err)
  49. }
  50. // all
  51. runCmd = exec.Command(dockerBinary, "ps", "-a")
  52. out, _, err = runCommandWithOutput(runCmd)
  53. if err != nil {
  54. c.Fatal(out, err)
  55. }
  56. if !assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}) {
  57. c.Errorf("Container list is not in the correct order: %s", out)
  58. }
  59. // running
  60. runCmd = exec.Command(dockerBinary, "ps")
  61. out, _, err = runCommandWithOutput(runCmd)
  62. if err != nil {
  63. c.Fatal(out, err)
  64. }
  65. if !assertContainerList(out, []string{fourthID, secondID, firstID}) {
  66. c.Errorf("Container list is not in the correct order: %s", out)
  67. }
  68. // from here all flag '-a' is ignored
  69. // limit
  70. runCmd = exec.Command(dockerBinary, "ps", "-n=2", "-a")
  71. out, _, err = runCommandWithOutput(runCmd)
  72. if err != nil {
  73. c.Fatal(out, err)
  74. }
  75. expected := []string{fourthID, thirdID}
  76. if !assertContainerList(out, expected) {
  77. c.Errorf("Container list is not in the correct order: %s", out)
  78. }
  79. runCmd = exec.Command(dockerBinary, "ps", "-n=2")
  80. out, _, err = runCommandWithOutput(runCmd)
  81. if err != nil {
  82. c.Fatal(out, err)
  83. }
  84. if !assertContainerList(out, expected) {
  85. c.Errorf("Container list is not in the correct order: %s", out)
  86. }
  87. // since
  88. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-a")
  89. out, _, err = runCommandWithOutput(runCmd)
  90. if err != nil {
  91. c.Fatal(out, err)
  92. }
  93. expected = []string{fourthID, thirdID, secondID}
  94. if !assertContainerList(out, expected) {
  95. c.Errorf("Container list is not in the correct order: %s", out)
  96. }
  97. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID)
  98. out, _, err = runCommandWithOutput(runCmd)
  99. if err != nil {
  100. c.Fatal(out, err)
  101. }
  102. if !assertContainerList(out, expected) {
  103. c.Errorf("Container list is not in the correct order: %s", out)
  104. }
  105. // before
  106. runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID, "-a")
  107. out, _, err = runCommandWithOutput(runCmd)
  108. if err != nil {
  109. c.Fatal(out, err)
  110. }
  111. expected = []string{secondID, firstID}
  112. if !assertContainerList(out, expected) {
  113. c.Errorf("Container list is not in the correct order: %s", out)
  114. }
  115. runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID)
  116. out, _, err = runCommandWithOutput(runCmd)
  117. if err != nil {
  118. c.Fatal(out, err)
  119. }
  120. if !assertContainerList(out, expected) {
  121. c.Errorf("Container list is not in the correct order: %s", out)
  122. }
  123. // since & before
  124. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-a")
  125. out, _, err = runCommandWithOutput(runCmd)
  126. if err != nil {
  127. c.Fatal(out, err)
  128. }
  129. expected = []string{thirdID, secondID}
  130. if !assertContainerList(out, expected) {
  131. c.Errorf("Container list is not in the correct order: %s", out)
  132. }
  133. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID)
  134. out, _, err = runCommandWithOutput(runCmd)
  135. if err != nil {
  136. c.Fatal(out, err)
  137. }
  138. if !assertContainerList(out, expected) {
  139. c.Errorf("Container list is not in the correct order: %s", out)
  140. }
  141. // since & limit
  142. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2", "-a")
  143. out, _, err = runCommandWithOutput(runCmd)
  144. if err != nil {
  145. c.Fatal(out, err)
  146. }
  147. expected = []string{fourthID, thirdID}
  148. if !assertContainerList(out, expected) {
  149. c.Errorf("Container list is not in the correct order: %s", out)
  150. }
  151. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2")
  152. out, _, err = runCommandWithOutput(runCmd)
  153. if err != nil {
  154. c.Fatal(out, err)
  155. }
  156. if !assertContainerList(out, expected) {
  157. c.Errorf("Container list is not in the correct order: %s", out)
  158. }
  159. // before & limit
  160. runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1", "-a")
  161. out, _, err = runCommandWithOutput(runCmd)
  162. if err != nil {
  163. c.Fatal(out, err)
  164. }
  165. expected = []string{thirdID}
  166. if !assertContainerList(out, expected) {
  167. c.Errorf("Container list is not in the correct order: %s", out)
  168. }
  169. runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1")
  170. out, _, err = runCommandWithOutput(runCmd)
  171. if err != nil {
  172. c.Fatal(out, err)
  173. }
  174. if !assertContainerList(out, expected) {
  175. c.Errorf("Container list is not in the correct order: %s", out)
  176. }
  177. // since & before & limit
  178. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a")
  179. out, _, err = runCommandWithOutput(runCmd)
  180. if err != nil {
  181. c.Fatal(out, err)
  182. }
  183. expected = []string{thirdID}
  184. if !assertContainerList(out, expected) {
  185. c.Errorf("Container list is not in the correct order: %s", out)
  186. }
  187. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1")
  188. out, _, err = runCommandWithOutput(runCmd)
  189. if err != nil {
  190. c.Fatal(out, err)
  191. }
  192. if !assertContainerList(out, expected) {
  193. c.Errorf("Container list is not in the correct order: %s", out)
  194. }
  195. }
  196. func assertContainerList(out string, expected []string) bool {
  197. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  198. if len(lines)-1 != len(expected) {
  199. return false
  200. }
  201. containerIDIndex := strings.Index(lines[0], "CONTAINER ID")
  202. for i := 0; i < len(expected); i++ {
  203. foundID := lines[i+1][containerIDIndex : containerIDIndex+12]
  204. if foundID != expected[i][:12] {
  205. return false
  206. }
  207. }
  208. return true
  209. }
  210. func (s *DockerSuite) TestPsListContainersSize(c *check.C) {
  211. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "hello")
  212. runCommandWithOutput(cmd)
  213. cmd = exec.Command(dockerBinary, "ps", "-s", "-n=1")
  214. baseOut, _, err := runCommandWithOutput(cmd)
  215. baseLines := strings.Split(strings.Trim(baseOut, "\n "), "\n")
  216. baseSizeIndex := strings.Index(baseLines[0], "SIZE")
  217. baseFoundsize := baseLines[1][baseSizeIndex:]
  218. baseBytes, err := strconv.Atoi(strings.Split(baseFoundsize, " ")[0])
  219. if err != nil {
  220. c.Fatal(err)
  221. }
  222. name := "test_size"
  223. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test")
  224. out, _, err := runCommandWithOutput(runCmd)
  225. if err != nil {
  226. c.Fatal(out, err)
  227. }
  228. id, err := getIDByName(name)
  229. if err != nil {
  230. c.Fatal(err)
  231. }
  232. runCmd = exec.Command(dockerBinary, "ps", "-s", "-n=1")
  233. wait := make(chan struct{})
  234. go func() {
  235. out, _, err = runCommandWithOutput(runCmd)
  236. close(wait)
  237. }()
  238. select {
  239. case <-wait:
  240. case <-time.After(3 * time.Second):
  241. c.Fatalf("Calling \"docker ps -s\" timed out!")
  242. }
  243. if err != nil {
  244. c.Fatal(out, err)
  245. }
  246. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  247. if len(lines) != 2 {
  248. c.Fatalf("Expected 2 lines for 'ps -s -n=1' output, got %d", len(lines))
  249. }
  250. sizeIndex := strings.Index(lines[0], "SIZE")
  251. idIndex := strings.Index(lines[0], "CONTAINER ID")
  252. foundID := lines[1][idIndex : idIndex+12]
  253. if foundID != id[:12] {
  254. c.Fatalf("Expected id %s, got %s", id[:12], foundID)
  255. }
  256. expectedSize := fmt.Sprintf("%d B", (2 + baseBytes))
  257. foundSize := lines[1][sizeIndex:]
  258. if foundSize != expectedSize {
  259. c.Fatalf("Expected size %q, got %q", expectedSize, foundSize)
  260. }
  261. }
  262. func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) {
  263. // FIXME: this should test paused, but it makes things hang and its wonky
  264. // this is because paused containers can't be controlled by signals
  265. // start exited container
  266. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
  267. out, _, err := runCommandWithOutput(runCmd)
  268. if err != nil {
  269. c.Fatal(out, err)
  270. }
  271. firstID := strings.TrimSpace(out)
  272. // make sure the exited cintainer is not running
  273. runCmd = exec.Command(dockerBinary, "wait", firstID)
  274. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  275. c.Fatal(out, err)
  276. }
  277. // start running container
  278. runCmd = exec.Command(dockerBinary, "run", "-itd", "busybox")
  279. out, _, err = runCommandWithOutput(runCmd)
  280. if err != nil {
  281. c.Fatal(out, err)
  282. }
  283. secondID := strings.TrimSpace(out)
  284. // filter containers by exited
  285. runCmd = exec.Command(dockerBinary, "ps", "-q", "--filter=status=exited")
  286. out, _, err = runCommandWithOutput(runCmd)
  287. if err != nil {
  288. c.Fatal(out, err)
  289. }
  290. containerOut := strings.TrimSpace(out)
  291. if containerOut != firstID[:12] {
  292. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  293. }
  294. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=status=running")
  295. out, _, err = runCommandWithOutput(runCmd)
  296. if err != nil {
  297. c.Fatal(out, err)
  298. }
  299. containerOut = strings.TrimSpace(out)
  300. if containerOut != secondID[:12] {
  301. c.Fatalf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out)
  302. }
  303. }
  304. func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) {
  305. // start container
  306. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
  307. out, _, err := runCommandWithOutput(runCmd)
  308. if err != nil {
  309. c.Fatal(out, err)
  310. }
  311. firstID := strings.TrimSpace(out)
  312. // start another container
  313. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  314. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  315. c.Fatal(out, err)
  316. }
  317. // filter containers by id
  318. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=id="+firstID)
  319. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  320. c.Fatal(out, err)
  321. }
  322. containerOut := strings.TrimSpace(out)
  323. if containerOut != firstID[:12] {
  324. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  325. }
  326. }
  327. func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) {
  328. // start container
  329. runCmd := exec.Command(dockerBinary, "run", "-d", "--name=a_name_to_match", "busybox")
  330. out, _, err := runCommandWithOutput(runCmd)
  331. if err != nil {
  332. c.Fatal(out, err)
  333. }
  334. firstID := strings.TrimSpace(out)
  335. // start another container
  336. runCmd = exec.Command(dockerBinary, "run", "-d", "--name=b_name_to_match", "busybox", "top")
  337. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  338. c.Fatal(out, err)
  339. }
  340. // filter containers by name
  341. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=name=a_name_to_match")
  342. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  343. c.Fatal(out, err)
  344. }
  345. containerOut := strings.TrimSpace(out)
  346. if containerOut != firstID[:12] {
  347. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  348. }
  349. }
  350. func (s *DockerSuite) TestPsListContainersFilterLabel(c *check.C) {
  351. // start container
  352. runCmd := exec.Command(dockerBinary, "run", "-d", "-l", "match=me", "-l", "second=tag", "busybox")
  353. out, _, err := runCommandWithOutput(runCmd)
  354. if err != nil {
  355. c.Fatal(out, err)
  356. }
  357. firstID := strings.TrimSpace(out)
  358. // start another container
  359. runCmd = exec.Command(dockerBinary, "run", "-d", "-l", "match=me too", "busybox")
  360. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  361. c.Fatal(out, err)
  362. }
  363. secondID := strings.TrimSpace(out)
  364. // start third container
  365. runCmd = exec.Command(dockerBinary, "run", "-d", "-l", "nomatch=me", "busybox")
  366. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  367. c.Fatal(out, err)
  368. }
  369. thirdID := strings.TrimSpace(out)
  370. // filter containers by exact match
  371. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me")
  372. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  373. c.Fatal(out, err)
  374. }
  375. containerOut := strings.TrimSpace(out)
  376. if containerOut != firstID {
  377. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
  378. }
  379. // filter containers by two labels
  380. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag")
  381. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  382. c.Fatal(out, err)
  383. }
  384. containerOut = strings.TrimSpace(out)
  385. if containerOut != firstID {
  386. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
  387. }
  388. // filter containers by two labels, but expect not found because of AND behavior
  389. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no")
  390. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  391. c.Fatal(out, err)
  392. }
  393. containerOut = strings.TrimSpace(out)
  394. if containerOut != "" {
  395. c.Fatalf("Expected nothing, got %s for exited filter, output: %q", containerOut, out)
  396. }
  397. // filter containers by exact key
  398. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match")
  399. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  400. c.Fatal(out, err)
  401. }
  402. containerOut = strings.TrimSpace(out)
  403. if (!strings.Contains(containerOut, firstID) || !strings.Contains(containerOut, secondID)) || strings.Contains(containerOut, thirdID) {
  404. c.Fatalf("Expected ids %s,%s, got %s for exited filter, output: %q", firstID, secondID, containerOut, out)
  405. }
  406. }
  407. func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) {
  408. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
  409. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  410. c.Fatal(out, err)
  411. }
  412. runCmd = exec.Command(dockerBinary, "run", "--name", "zero1", "busybox", "true")
  413. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  414. c.Fatal(out, err)
  415. }
  416. firstZero, err := getIDByName("zero1")
  417. if err != nil {
  418. c.Fatal(err)
  419. }
  420. runCmd = exec.Command(dockerBinary, "run", "--name", "zero2", "busybox", "true")
  421. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  422. c.Fatal(out, err)
  423. }
  424. secondZero, err := getIDByName("zero2")
  425. if err != nil {
  426. c.Fatal(err)
  427. }
  428. runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero1", "busybox", "false")
  429. if out, _, err := runCommandWithOutput(runCmd); err == nil {
  430. c.Fatal("Should fail.", out, err)
  431. }
  432. firstNonZero, err := getIDByName("nonzero1")
  433. if err != nil {
  434. c.Fatal(err)
  435. }
  436. runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero2", "busybox", "false")
  437. if out, _, err := runCommandWithOutput(runCmd); err == nil {
  438. c.Fatal("Should fail.", out, err)
  439. }
  440. secondNonZero, err := getIDByName("nonzero2")
  441. if err != nil {
  442. c.Fatal(err)
  443. }
  444. // filter containers by exited=0
  445. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
  446. out, _, err := runCommandWithOutput(runCmd)
  447. if err != nil {
  448. c.Fatal(out, err)
  449. }
  450. ids := strings.Split(strings.TrimSpace(out), "\n")
  451. if len(ids) != 2 {
  452. c.Fatalf("Should be 2 zero exited containers got %d: %s", len(ids), out)
  453. }
  454. if ids[0] != secondZero {
  455. c.Fatalf("First in list should be %q, got %q", secondZero, ids[0])
  456. }
  457. if ids[1] != firstZero {
  458. c.Fatalf("Second in list should be %q, got %q", firstZero, ids[1])
  459. }
  460. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
  461. out, _, err = runCommandWithOutput(runCmd)
  462. if err != nil {
  463. c.Fatal(out, err)
  464. }
  465. ids = strings.Split(strings.TrimSpace(out), "\n")
  466. if len(ids) != 2 {
  467. c.Fatalf("Should be 2 zero exited containers got %d", len(ids))
  468. }
  469. if ids[0] != secondNonZero {
  470. c.Fatalf("First in list should be %q, got %q", secondNonZero, ids[0])
  471. }
  472. if ids[1] != firstNonZero {
  473. c.Fatalf("Second in list should be %q, got %q", firstNonZero, ids[1])
  474. }
  475. }
  476. func (s *DockerSuite) TestPsRightTagName(c *check.C) {
  477. tag := "asybox:shmatest"
  478. if out, err := exec.Command(dockerBinary, "tag", "busybox", tag).CombinedOutput(); err != nil {
  479. c.Fatalf("Failed to tag image: %s, out: %q", err, out)
  480. }
  481. var id1 string
  482. if out, err := exec.Command(dockerBinary, "run", "-d", "busybox", "top").CombinedOutput(); err != nil {
  483. c.Fatalf("Failed to run container: %s, out: %q", err, out)
  484. } else {
  485. id1 = strings.TrimSpace(string(out))
  486. }
  487. var id2 string
  488. if out, err := exec.Command(dockerBinary, "run", "-d", tag, "top").CombinedOutput(); err != nil {
  489. c.Fatalf("Failed to run container: %s, out: %q", err, out)
  490. } else {
  491. id2 = strings.TrimSpace(string(out))
  492. }
  493. var imageID string
  494. if out, err := exec.Command(dockerBinary, "inspect", "-f", "{{.Id}}", "busybox").CombinedOutput(); err != nil {
  495. c.Fatalf("failed to get the image ID of busybox: %s, %v", out, err)
  496. } else {
  497. imageID = strings.TrimSpace(string(out))
  498. }
  499. var id3 string
  500. if out, err := exec.Command(dockerBinary, "run", "-d", imageID, "top").CombinedOutput(); err != nil {
  501. c.Fatalf("Failed to run container: %s, out: %q", err, out)
  502. } else {
  503. id3 = strings.TrimSpace(string(out))
  504. }
  505. out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  506. if err != nil {
  507. c.Fatalf("Failed to run 'ps': %s, out: %q", err, out)
  508. }
  509. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  510. // skip header
  511. lines = lines[1:]
  512. if len(lines) != 3 {
  513. c.Fatalf("There should be 3 running container, got %d", len(lines))
  514. }
  515. for _, line := range lines {
  516. f := strings.Fields(line)
  517. switch f[0] {
  518. case id1:
  519. if f[1] != "busybox" {
  520. c.Fatalf("Expected %s tag for id %s, got %s", "busybox", id1, f[1])
  521. }
  522. case id2:
  523. if f[1] != tag {
  524. c.Fatalf("Expected %s tag for id %s, got %s", tag, id2, f[1])
  525. }
  526. case id3:
  527. if f[1] != imageID {
  528. c.Fatalf("Expected %s imageID for id %s, got %s", tag, id3, f[1])
  529. }
  530. default:
  531. c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3)
  532. }
  533. }
  534. }
  535. func (s *DockerSuite) TestPsLinkedWithNoTrunc(c *check.C) {
  536. if out, err := exec.Command(dockerBinary, "run", "--name=first", "-d", "busybox", "top").CombinedOutput(); err != nil {
  537. c.Fatalf("Output: %s, err: %s", out, err)
  538. }
  539. if out, err := exec.Command(dockerBinary, "run", "--name=second", "--link=first:first", "-d", "busybox", "top").CombinedOutput(); err != nil {
  540. c.Fatalf("Output: %s, err: %s", out, err)
  541. }
  542. out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  543. if err != nil {
  544. c.Fatalf("Output: %s, err: %s", out, err)
  545. }
  546. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  547. // strip header
  548. lines = lines[1:]
  549. expected := []string{"second", "first,second/first"}
  550. var names []string
  551. for _, l := range lines {
  552. fields := strings.Fields(l)
  553. names = append(names, fields[len(fields)-1])
  554. }
  555. if !reflect.DeepEqual(expected, names) {
  556. c.Fatalf("Expected array: %v, got: %v", expected, names)
  557. }
  558. }
  559. func (s *DockerSuite) TestPsGroupPortRange(c *check.C) {
  560. portRange := "3800-3900"
  561. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top"))
  562. if err != nil {
  563. c.Fatal(out, err)
  564. }
  565. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "ps"))
  566. if err != nil {
  567. c.Fatal(out, err)
  568. }
  569. // check that the port range is in the output
  570. if !strings.Contains(string(out), portRange) {
  571. c.Fatalf("docker ps output should have had the port range %q: %s", portRange, string(out))
  572. }
  573. }