docker_cli_ps_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 TestPsListContainersFilterExited(t *testing.T) {
  350. defer deleteAllContainers()
  351. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
  352. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  353. t.Fatal(out, err)
  354. }
  355. runCmd = exec.Command(dockerBinary, "run", "--name", "zero1", "busybox", "true")
  356. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  357. t.Fatal(out, err)
  358. }
  359. firstZero, err := getIDByName("zero1")
  360. if err != nil {
  361. t.Fatal(err)
  362. }
  363. runCmd = exec.Command(dockerBinary, "run", "--name", "zero2", "busybox", "true")
  364. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  365. t.Fatal(out, err)
  366. }
  367. secondZero, err := getIDByName("zero2")
  368. if err != nil {
  369. t.Fatal(err)
  370. }
  371. runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero1", "busybox", "false")
  372. if out, _, err := runCommandWithOutput(runCmd); err == nil {
  373. t.Fatal("Should fail.", out, err)
  374. }
  375. firstNonZero, err := getIDByName("nonzero1")
  376. if err != nil {
  377. t.Fatal(err)
  378. }
  379. runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero2", "busybox", "false")
  380. if out, _, err := runCommandWithOutput(runCmd); err == nil {
  381. t.Fatal("Should fail.", out, err)
  382. }
  383. secondNonZero, err := getIDByName("nonzero2")
  384. if err != nil {
  385. t.Fatal(err)
  386. }
  387. // filter containers by exited=0
  388. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
  389. out, _, err := runCommandWithOutput(runCmd)
  390. if err != nil {
  391. t.Fatal(out, err)
  392. }
  393. ids := strings.Split(strings.TrimSpace(out), "\n")
  394. if len(ids) != 2 {
  395. t.Fatalf("Should be 2 zero exited containerst got %d", len(ids))
  396. }
  397. if ids[0] != secondZero {
  398. t.Fatalf("First in list should be %q, got %q", secondZero, ids[0])
  399. }
  400. if ids[1] != firstZero {
  401. t.Fatalf("Second in list should be %q, got %q", firstZero, ids[1])
  402. }
  403. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
  404. out, _, err = runCommandWithOutput(runCmd)
  405. if err != nil {
  406. t.Fatal(out, err)
  407. }
  408. ids = strings.Split(strings.TrimSpace(out), "\n")
  409. if len(ids) != 2 {
  410. t.Fatalf("Should be 2 zero exited containerst got %d", len(ids))
  411. }
  412. if ids[0] != secondNonZero {
  413. t.Fatalf("First in list should be %q, got %q", secondNonZero, ids[0])
  414. }
  415. if ids[1] != firstNonZero {
  416. t.Fatalf("Second in list should be %q, got %q", firstNonZero, ids[1])
  417. }
  418. logDone("ps - test ps filter exited")
  419. }
  420. func TestPsRightTagName(t *testing.T) {
  421. tag := "asybox:shmatest"
  422. defer deleteAllContainers()
  423. defer deleteImages(tag)
  424. if out, err := exec.Command(dockerBinary, "tag", "busybox", tag).CombinedOutput(); err != nil {
  425. t.Fatalf("Failed to tag image: %s, out: %q", err, out)
  426. }
  427. var id1 string
  428. if out, err := exec.Command(dockerBinary, "run", "-d", "busybox", "top").CombinedOutput(); err != nil {
  429. t.Fatalf("Failed to run container: %s, out: %q", err, out)
  430. } else {
  431. id1 = strings.TrimSpace(string(out))
  432. }
  433. var id2 string
  434. if out, err := exec.Command(dockerBinary, "run", "-d", tag, "top").CombinedOutput(); err != nil {
  435. t.Fatalf("Failed to run container: %s, out: %q", err, out)
  436. } else {
  437. id2 = strings.TrimSpace(string(out))
  438. }
  439. out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  440. if err != nil {
  441. t.Fatalf("Failed to run 'ps': %s, out: %q", err, out)
  442. }
  443. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  444. // skip header
  445. lines = lines[1:]
  446. if len(lines) != 2 {
  447. t.Fatalf("There should be 2 running container, got %d", len(lines))
  448. }
  449. for _, line := range lines {
  450. f := strings.Fields(line)
  451. switch f[0] {
  452. case id1:
  453. if f[1] != "busybox:latest" {
  454. t.Fatalf("Expected %s tag for id %s, got %s", "busybox", id1, f[1])
  455. }
  456. case id2:
  457. if f[1] != tag {
  458. t.Fatalf("Expected %s tag for id %s, got %s", tag, id1, f[1])
  459. }
  460. default:
  461. t.Fatalf("Unexpected id %s, expected %s and %s", f[0], id1, id2)
  462. }
  463. }
  464. logDone("ps - right tags for containers")
  465. }
  466. func TestPsLinkedWithNoTrunc(t *testing.T) {
  467. defer deleteAllContainers()
  468. if out, err := exec.Command(dockerBinary, "run", "--name=first", "-d", "busybox", "top").CombinedOutput(); err != nil {
  469. t.Fatalf("Output: %s, err: %s", out, err)
  470. }
  471. if out, err := exec.Command(dockerBinary, "run", "--name=second", "--link=first:first", "-d", "busybox", "top").CombinedOutput(); err != nil {
  472. t.Fatalf("Output: %s, err: %s", out, err)
  473. }
  474. out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  475. if err != nil {
  476. t.Fatalf("Output: %s, err: %s", out, err)
  477. }
  478. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  479. // strip header
  480. lines = lines[1:]
  481. expected := []string{"second", "first,second/first"}
  482. var names []string
  483. for _, l := range lines {
  484. fields := strings.Fields(l)
  485. names = append(names, fields[len(fields)-1])
  486. }
  487. if !reflect.DeepEqual(expected, names) {
  488. t.Fatalf("Expected array: %v, got: %v", expected, names)
  489. }
  490. }
  491. func TestPsGroupPortRange(t *testing.T) {
  492. defer deleteAllContainers()
  493. portRange := "3300-3900"
  494. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top"))
  495. if err != nil {
  496. t.Fatal(out, err)
  497. }
  498. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "ps"))
  499. if err != nil {
  500. t.Fatal(out, err)
  501. }
  502. // check that the port range is in the output
  503. if !strings.Contains(string(out), portRange) {
  504. t.Fatalf("docker ps output should have had the port range %q: %s", portRange, string(out))
  505. }
  506. logDone("ps - port range")
  507. }