docker_cli_ps_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. baseOut, _, err := runCommandWithOutput(cmd)
  210. baseLines := strings.Split(strings.Trim(baseOut, "\n "), "\n")
  211. baseSizeIndex := strings.Index(baseLines[0], "SIZE")
  212. baseFoundsize := baseLines[1][baseSizeIndex:]
  213. baseBytes, err := strconv.Atoi(strings.Split(baseFoundsize, " ")[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. if len(lines) != 2 {
  243. t.Fatalf("Expected 2 lines for 'ps -s -n=1' output, got %d", len(lines))
  244. }
  245. sizeIndex := strings.Index(lines[0], "SIZE")
  246. idIndex := strings.Index(lines[0], "CONTAINER ID")
  247. foundID := lines[1][idIndex : idIndex+12]
  248. if foundID != id[:12] {
  249. t.Fatalf("Expected id %s, got %s", id[:12], foundID)
  250. }
  251. expectedSize := fmt.Sprintf("%d B", (2 + baseBytes))
  252. foundSize := lines[1][sizeIndex:]
  253. if foundSize != expectedSize {
  254. t.Fatalf("Expected size %q, got %q", expectedSize, foundSize)
  255. }
  256. logDone("ps - test ps size")
  257. }
  258. func TestPsListContainersFilterStatus(t *testing.T) {
  259. // FIXME: this should test paused, but it makes things hang and its wonky
  260. // this is because paused containers can't be controlled by signals
  261. defer deleteAllContainers()
  262. // start exited container
  263. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
  264. out, _, err := runCommandWithOutput(runCmd)
  265. if err != nil {
  266. t.Fatal(out, err)
  267. }
  268. firstID := stripTrailingCharacters(out)
  269. // make sure the exited cintainer is not running
  270. runCmd = exec.Command(dockerBinary, "wait", firstID)
  271. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  272. t.Fatal(out, err)
  273. }
  274. // start running container
  275. runCmd = exec.Command(dockerBinary, "run", "-itd", "busybox")
  276. out, _, err = runCommandWithOutput(runCmd)
  277. if err != nil {
  278. t.Fatal(out, err)
  279. }
  280. secondID := stripTrailingCharacters(out)
  281. // filter containers by exited
  282. runCmd = exec.Command(dockerBinary, "ps", "-q", "--filter=status=exited")
  283. out, _, err = runCommandWithOutput(runCmd)
  284. if err != nil {
  285. t.Fatal(out, err)
  286. }
  287. containerOut := strings.TrimSpace(out)
  288. if containerOut != firstID[:12] {
  289. t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  290. }
  291. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=status=running")
  292. out, _, err = runCommandWithOutput(runCmd)
  293. if err != nil {
  294. t.Fatal(out, err)
  295. }
  296. containerOut = strings.TrimSpace(out)
  297. if containerOut != secondID[:12] {
  298. t.Fatalf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out)
  299. }
  300. logDone("ps - test ps filter status")
  301. }
  302. func TestPsListContainersFilterID(t *testing.T) {
  303. defer deleteAllContainers()
  304. // start container
  305. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
  306. out, _, err := runCommandWithOutput(runCmd)
  307. if err != nil {
  308. t.Fatal(out, err)
  309. }
  310. firstID := stripTrailingCharacters(out)
  311. // start another container
  312. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 360")
  313. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  314. t.Fatal(out, err)
  315. }
  316. // filter containers by id
  317. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=id="+firstID)
  318. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  319. t.Fatal(out, err)
  320. }
  321. containerOut := strings.TrimSpace(out)
  322. if containerOut != firstID[:12] {
  323. t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  324. }
  325. logDone("ps - test ps filter id")
  326. }
  327. func TestPsListContainersFilterName(t *testing.T) {
  328. defer deleteAllContainers()
  329. // start container
  330. runCmd := exec.Command(dockerBinary, "run", "-d", "--name=a_name_to_match", "busybox")
  331. out, _, err := runCommandWithOutput(runCmd)
  332. if err != nil {
  333. t.Fatal(out, err)
  334. }
  335. firstID := stripTrailingCharacters(out)
  336. // start another container
  337. runCmd = exec.Command(dockerBinary, "run", "-d", "--name=b_name_to_match", "busybox", "sh", "-c", "sleep 360")
  338. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  339. t.Fatal(out, err)
  340. }
  341. // filter containers by name
  342. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=name=a_name_to_match")
  343. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  344. t.Fatal(out, err)
  345. }
  346. containerOut := strings.TrimSpace(out)
  347. if containerOut != firstID[:12] {
  348. t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  349. }
  350. logDone("ps - test ps filter name")
  351. }
  352. func TestPsListContainersFilterLabel(t *testing.T) {
  353. // start container
  354. runCmd := exec.Command(dockerBinary, "run", "-d", "-l", "match=me", "-l", "second=tag", "busybox")
  355. out, _, err := runCommandWithOutput(runCmd)
  356. if err != nil {
  357. t.Fatal(out, err)
  358. }
  359. firstID := stripTrailingCharacters(out)
  360. // start another container
  361. runCmd = exec.Command(dockerBinary, "run", "-d", "-l", "match=me too", "busybox")
  362. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  363. t.Fatal(out, err)
  364. }
  365. secondID := stripTrailingCharacters(out)
  366. // start third container
  367. runCmd = exec.Command(dockerBinary, "run", "-d", "-l", "nomatch=me", "busybox")
  368. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  369. t.Fatal(out, err)
  370. }
  371. thirdID := stripTrailingCharacters(out)
  372. // filter containers by exact match
  373. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me")
  374. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  375. t.Fatal(out, err)
  376. }
  377. containerOut := strings.TrimSpace(out)
  378. if containerOut != firstID {
  379. t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
  380. }
  381. // filter containers by two labels
  382. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag")
  383. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  384. t.Fatal(out, err)
  385. }
  386. containerOut = strings.TrimSpace(out)
  387. if containerOut != firstID {
  388. t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
  389. }
  390. // filter containers by two labels, but expect not found because of AND behavior
  391. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no")
  392. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  393. t.Fatal(out, err)
  394. }
  395. containerOut = strings.TrimSpace(out)
  396. if containerOut != "" {
  397. t.Fatalf("Expected nothing, got %s for exited filter, output: %q", containerOut, out)
  398. }
  399. // filter containers by exact key
  400. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match")
  401. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  402. t.Fatal(out, err)
  403. }
  404. containerOut = strings.TrimSpace(out)
  405. if (!strings.Contains(containerOut, firstID) || !strings.Contains(containerOut, secondID)) || strings.Contains(containerOut, thirdID) {
  406. t.Fatalf("Expected ids %s,%s, got %s for exited filter, output: %q", firstID, secondID, containerOut, out)
  407. }
  408. deleteAllContainers()
  409. logDone("ps - test ps filter label")
  410. }
  411. func TestPsListContainersFilterExited(t *testing.T) {
  412. defer deleteAllContainers()
  413. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
  414. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  415. t.Fatal(out, err)
  416. }
  417. runCmd = exec.Command(dockerBinary, "run", "--name", "zero1", "busybox", "true")
  418. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  419. t.Fatal(out, err)
  420. }
  421. firstZero, err := getIDByName("zero1")
  422. if err != nil {
  423. t.Fatal(err)
  424. }
  425. runCmd = exec.Command(dockerBinary, "run", "--name", "zero2", "busybox", "true")
  426. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  427. t.Fatal(out, err)
  428. }
  429. secondZero, err := getIDByName("zero2")
  430. if err != nil {
  431. t.Fatal(err)
  432. }
  433. runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero1", "busybox", "false")
  434. if out, _, err := runCommandWithOutput(runCmd); err == nil {
  435. t.Fatal("Should fail.", out, err)
  436. }
  437. firstNonZero, err := getIDByName("nonzero1")
  438. if err != nil {
  439. t.Fatal(err)
  440. }
  441. runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero2", "busybox", "false")
  442. if out, _, err := runCommandWithOutput(runCmd); err == nil {
  443. t.Fatal("Should fail.", out, err)
  444. }
  445. secondNonZero, err := getIDByName("nonzero2")
  446. if err != nil {
  447. t.Fatal(err)
  448. }
  449. // filter containers by exited=0
  450. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
  451. out, _, err := runCommandWithOutput(runCmd)
  452. if err != nil {
  453. t.Fatal(out, err)
  454. }
  455. ids := strings.Split(strings.TrimSpace(out), "\n")
  456. if len(ids) != 2 {
  457. t.Fatalf("Should be 2 zero exited containerst got %d", len(ids))
  458. }
  459. if ids[0] != secondZero {
  460. t.Fatalf("First in list should be %q, got %q", secondZero, ids[0])
  461. }
  462. if ids[1] != firstZero {
  463. t.Fatalf("Second in list should be %q, got %q", firstZero, ids[1])
  464. }
  465. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
  466. out, _, err = runCommandWithOutput(runCmd)
  467. if err != nil {
  468. t.Fatal(out, err)
  469. }
  470. ids = strings.Split(strings.TrimSpace(out), "\n")
  471. if len(ids) != 2 {
  472. t.Fatalf("Should be 2 zero exited containerst got %d", len(ids))
  473. }
  474. if ids[0] != secondNonZero {
  475. t.Fatalf("First in list should be %q, got %q", secondNonZero, ids[0])
  476. }
  477. if ids[1] != firstNonZero {
  478. t.Fatalf("Second in list should be %q, got %q", firstNonZero, ids[1])
  479. }
  480. logDone("ps - test ps filter exited")
  481. }
  482. func TestPsRightTagName(t *testing.T) {
  483. tag := "asybox:shmatest"
  484. defer deleteAllContainers()
  485. defer deleteImages(tag)
  486. if out, err := exec.Command(dockerBinary, "tag", "busybox", tag).CombinedOutput(); err != nil {
  487. t.Fatalf("Failed to tag image: %s, out: %q", err, out)
  488. }
  489. var id1 string
  490. if out, err := exec.Command(dockerBinary, "run", "-d", "busybox", "top").CombinedOutput(); err != nil {
  491. t.Fatalf("Failed to run container: %s, out: %q", err, out)
  492. } else {
  493. id1 = strings.TrimSpace(string(out))
  494. }
  495. var id2 string
  496. if out, err := exec.Command(dockerBinary, "run", "-d", tag, "top").CombinedOutput(); err != nil {
  497. t.Fatalf("Failed to run container: %s, out: %q", err, out)
  498. } else {
  499. id2 = strings.TrimSpace(string(out))
  500. }
  501. out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  502. if err != nil {
  503. t.Fatalf("Failed to run 'ps': %s, out: %q", err, out)
  504. }
  505. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  506. // skip header
  507. lines = lines[1:]
  508. if len(lines) != 2 {
  509. t.Fatalf("There should be 2 running container, got %d", len(lines))
  510. }
  511. for _, line := range lines {
  512. f := strings.Fields(line)
  513. switch f[0] {
  514. case id1:
  515. if f[1] != "busybox:latest" {
  516. t.Fatalf("Expected %s tag for id %s, got %s", "busybox", id1, f[1])
  517. }
  518. case id2:
  519. if f[1] != tag {
  520. t.Fatalf("Expected %s tag for id %s, got %s", tag, id1, f[1])
  521. }
  522. default:
  523. t.Fatalf("Unexpected id %s, expected %s and %s", f[0], id1, id2)
  524. }
  525. }
  526. logDone("ps - right tags for containers")
  527. }
  528. func TestPsLinkedWithNoTrunc(t *testing.T) {
  529. defer deleteAllContainers()
  530. if out, err := exec.Command(dockerBinary, "run", "--name=first", "-d", "busybox", "top").CombinedOutput(); err != nil {
  531. t.Fatalf("Output: %s, err: %s", out, err)
  532. }
  533. if out, err := exec.Command(dockerBinary, "run", "--name=second", "--link=first:first", "-d", "busybox", "top").CombinedOutput(); err != nil {
  534. t.Fatalf("Output: %s, err: %s", out, err)
  535. }
  536. out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  537. if err != nil {
  538. t.Fatalf("Output: %s, err: %s", out, err)
  539. }
  540. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  541. // strip header
  542. lines = lines[1:]
  543. expected := []string{"second", "first,second/first"}
  544. var names []string
  545. for _, l := range lines {
  546. fields := strings.Fields(l)
  547. names = append(names, fields[len(fields)-1])
  548. }
  549. if !reflect.DeepEqual(expected, names) {
  550. t.Fatalf("Expected array: %v, got: %v", expected, names)
  551. }
  552. }
  553. func TestPsGroupPortRange(t *testing.T) {
  554. defer deleteAllContainers()
  555. portRange := "3800-3900"
  556. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top"))
  557. if err != nil {
  558. t.Fatal(out, err)
  559. }
  560. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "ps"))
  561. if err != nil {
  562. t.Fatal(out, err)
  563. }
  564. // check that the port range is in the output
  565. if !strings.Contains(string(out), portRange) {
  566. t.Fatalf("docker ps output should have had the port range %q: %s", portRange, string(out))
  567. }
  568. logDone("ps - port range")
  569. }