docker_cli_ps_test.go 19 KB

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