docker_cli_ps_test.go 19 KB

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