docker_cli_ps_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. "time"
  9. )
  10. func TestPsListContainers(t *testing.T) {
  11. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  12. out, _, err := runCommandWithOutput(runCmd)
  13. if err != nil {
  14. t.Fatal(out, err)
  15. }
  16. firstID := stripTrailingCharacters(out)
  17. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  18. out, _, err = runCommandWithOutput(runCmd)
  19. if err != nil {
  20. t.Fatal(out, err)
  21. }
  22. secondID := stripTrailingCharacters(out)
  23. // not long running
  24. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  25. out, _, err = runCommandWithOutput(runCmd)
  26. if err != nil {
  27. t.Fatal(out, err)
  28. }
  29. thirdID := stripTrailingCharacters(out)
  30. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  31. out, _, err = runCommandWithOutput(runCmd)
  32. if err != nil {
  33. t.Fatal(out, err)
  34. }
  35. fourthID := stripTrailingCharacters(out)
  36. // make sure third one is not running
  37. runCmd = exec.Command(dockerBinary, "wait", thirdID)
  38. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  39. t.Fatal(out, err)
  40. }
  41. // all
  42. runCmd = exec.Command(dockerBinary, "ps", "-a")
  43. out, _, err = runCommandWithOutput(runCmd)
  44. if err != nil {
  45. t.Fatal(out, err)
  46. }
  47. if !assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}) {
  48. t.Error("Container list is not in the correct order")
  49. }
  50. // running
  51. runCmd = exec.Command(dockerBinary, "ps")
  52. out, _, err = runCommandWithOutput(runCmd)
  53. if err != nil {
  54. t.Fatal(out, err)
  55. }
  56. if !assertContainerList(out, []string{fourthID, secondID, firstID}) {
  57. t.Error("Container list is not in the correct order")
  58. }
  59. // from here all flag '-a' is ignored
  60. // limit
  61. runCmd = exec.Command(dockerBinary, "ps", "-n=2", "-a")
  62. out, _, err = runCommandWithOutput(runCmd)
  63. if err != nil {
  64. t.Fatal(out, err)
  65. }
  66. expected := []string{fourthID, thirdID}
  67. if !assertContainerList(out, expected) {
  68. t.Error("Container list is not in the correct order")
  69. }
  70. runCmd = exec.Command(dockerBinary, "ps", "-n=2")
  71. out, _, err = runCommandWithOutput(runCmd)
  72. if err != nil {
  73. t.Fatal(out, err)
  74. }
  75. if !assertContainerList(out, expected) {
  76. t.Error("Container list is not in the correct order")
  77. }
  78. // since
  79. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-a")
  80. out, _, err = runCommandWithOutput(runCmd)
  81. if err != nil {
  82. t.Fatal(out, err)
  83. }
  84. expected = []string{fourthID, thirdID, secondID}
  85. if !assertContainerList(out, expected) {
  86. t.Error("Container list is not in the correct order")
  87. }
  88. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID)
  89. out, _, err = runCommandWithOutput(runCmd)
  90. if err != nil {
  91. t.Fatal(out, err)
  92. }
  93. if !assertContainerList(out, expected) {
  94. t.Error("Container list is not in the correct order")
  95. }
  96. // before
  97. runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID, "-a")
  98. out, _, err = runCommandWithOutput(runCmd)
  99. if err != nil {
  100. t.Fatal(out, err)
  101. }
  102. expected = []string{secondID, firstID}
  103. if !assertContainerList(out, expected) {
  104. t.Error("Container list is not in the correct order")
  105. }
  106. runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID)
  107. out, _, err = runCommandWithOutput(runCmd)
  108. if err != nil {
  109. t.Fatal(out, err)
  110. }
  111. if !assertContainerList(out, expected) {
  112. t.Error("Container list is not in the correct order")
  113. }
  114. // since & before
  115. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-a")
  116. out, _, err = runCommandWithOutput(runCmd)
  117. if err != nil {
  118. t.Fatal(out, err)
  119. }
  120. expected = []string{thirdID, secondID}
  121. if !assertContainerList(out, expected) {
  122. t.Error("Container list is not in the correct order")
  123. }
  124. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID)
  125. out, _, err = runCommandWithOutput(runCmd)
  126. if err != nil {
  127. t.Fatal(out, err)
  128. }
  129. if !assertContainerList(out, expected) {
  130. t.Error("Container list is not in the correct order")
  131. }
  132. // since & limit
  133. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2", "-a")
  134. out, _, err = runCommandWithOutput(runCmd)
  135. if err != nil {
  136. t.Fatal(out, err)
  137. }
  138. expected = []string{fourthID, thirdID}
  139. if !assertContainerList(out, expected) {
  140. t.Error("Container list is not in the correct order")
  141. }
  142. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2")
  143. out, _, err = runCommandWithOutput(runCmd)
  144. if err != nil {
  145. t.Fatal(out, err)
  146. }
  147. if !assertContainerList(out, expected) {
  148. t.Error("Container list is not in the correct order")
  149. }
  150. // before & limit
  151. runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1", "-a")
  152. out, _, err = runCommandWithOutput(runCmd)
  153. if err != nil {
  154. t.Fatal(out, err)
  155. }
  156. expected = []string{thirdID}
  157. if !assertContainerList(out, expected) {
  158. t.Error("Container list is not in the correct order")
  159. }
  160. runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1")
  161. out, _, err = runCommandWithOutput(runCmd)
  162. if err != nil {
  163. t.Fatal(out, err)
  164. }
  165. if !assertContainerList(out, expected) {
  166. t.Error("Container list is not in the correct order")
  167. }
  168. // since & before & limit
  169. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a")
  170. out, _, err = runCommandWithOutput(runCmd)
  171. if err != nil {
  172. t.Fatal(out, err)
  173. }
  174. expected = []string{thirdID}
  175. if !assertContainerList(out, expected) {
  176. t.Error("Container list is not in the correct order")
  177. }
  178. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1")
  179. out, _, err = runCommandWithOutput(runCmd)
  180. if err != nil {
  181. t.Fatal(out, err)
  182. }
  183. if !assertContainerList(out, expected) {
  184. t.Error("Container list is not in the correct order")
  185. }
  186. deleteAllContainers()
  187. logDone("ps - test ps options")
  188. }
  189. func assertContainerList(out string, expected []string) bool {
  190. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  191. if len(lines)-1 != len(expected) {
  192. return false
  193. }
  194. containerIDIndex := strings.Index(lines[0], "CONTAINER ID")
  195. for i := 0; i < len(expected); i++ {
  196. foundID := lines[i+1][containerIDIndex : containerIDIndex+12]
  197. if foundID != expected[i][:12] {
  198. return false
  199. }
  200. }
  201. return true
  202. }
  203. func TestPsListContainersSize(t *testing.T) {
  204. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "hello")
  205. runCommandWithOutput(cmd)
  206. cmd = exec.Command(dockerBinary, "ps", "-s", "-n=1")
  207. base_out, _, err := runCommandWithOutput(cmd)
  208. base_lines := strings.Split(strings.Trim(base_out, "\n "), "\n")
  209. base_sizeIndex := strings.Index(base_lines[0], "SIZE")
  210. base_foundSize := base_lines[1][base_sizeIndex:]
  211. base_bytes, err := strconv.Atoi(strings.Split(base_foundSize, " ")[0])
  212. if err != nil {
  213. t.Fatal(err)
  214. }
  215. name := "test_size"
  216. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test")
  217. out, _, err := runCommandWithOutput(runCmd)
  218. if err != nil {
  219. t.Fatal(out, err)
  220. }
  221. id, err := getIDByName(name)
  222. if err != nil {
  223. t.Fatal(err)
  224. }
  225. runCmd = exec.Command(dockerBinary, "ps", "-s", "-n=1")
  226. wait := make(chan struct{})
  227. go func() {
  228. out, _, err = runCommandWithOutput(runCmd)
  229. close(wait)
  230. }()
  231. select {
  232. case <-wait:
  233. case <-time.After(3 * time.Second):
  234. t.Fatalf("Calling \"docker ps -s\" timed out!")
  235. }
  236. if err != nil {
  237. t.Fatal(out, err)
  238. }
  239. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  240. sizeIndex := strings.Index(lines[0], "SIZE")
  241. idIndex := strings.Index(lines[0], "CONTAINER ID")
  242. foundID := lines[1][idIndex : idIndex+12]
  243. if foundID != id[:12] {
  244. t.Fatalf("Expected id %s, got %s", id[:12], foundID)
  245. }
  246. expectedSize := fmt.Sprintf("%d B", (2 + base_bytes))
  247. foundSize := lines[1][sizeIndex:]
  248. if foundSize != expectedSize {
  249. t.Fatalf("Expected size %q, got %q", expectedSize, foundSize)
  250. }
  251. deleteAllContainers()
  252. logDone("ps - test ps size")
  253. }
  254. func TestPsListContainersFilterStatus(t *testing.T) {
  255. // FIXME: this should test paused, but it makes things hang and its wonky
  256. // this is because paused containers can't be controlled by signals
  257. deleteAllContainers()
  258. // start exited container
  259. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
  260. out, _, err := runCommandWithOutput(runCmd)
  261. if err != nil {
  262. t.Fatal(out, err)
  263. }
  264. firstID := stripTrailingCharacters(out)
  265. // make sure the exited cintainer is not running
  266. runCmd = exec.Command(dockerBinary, "wait", firstID)
  267. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  268. t.Fatal(out, err)
  269. }
  270. // start running container
  271. runCmd = exec.Command(dockerBinary, "run", "-itd", "busybox")
  272. out, _, err = runCommandWithOutput(runCmd)
  273. if err != nil {
  274. t.Fatal(out, err)
  275. }
  276. secondID := stripTrailingCharacters(out)
  277. // filter containers by exited
  278. runCmd = exec.Command(dockerBinary, "ps", "-q", "--filter=status=exited")
  279. out, _, err = runCommandWithOutput(runCmd)
  280. if err != nil {
  281. t.Fatal(out, err)
  282. }
  283. containerOut := strings.TrimSpace(out)
  284. if containerOut != firstID[:12] {
  285. t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  286. }
  287. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=status=running")
  288. out, _, err = runCommandWithOutput(runCmd)
  289. if err != nil {
  290. t.Fatal(out, err)
  291. }
  292. containerOut = strings.TrimSpace(out)
  293. if containerOut != secondID[:12] {
  294. t.Fatalf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out)
  295. }
  296. deleteAllContainers()
  297. logDone("ps - test ps filter status")
  298. }
  299. func TestPsListContainersFilterID(t *testing.T) {
  300. // start container
  301. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
  302. out, _, err := runCommandWithOutput(runCmd)
  303. if err != nil {
  304. t.Fatal(out, err)
  305. }
  306. firstID := stripTrailingCharacters(out)
  307. // start another container
  308. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 360")
  309. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  310. t.Fatal(out, err)
  311. }
  312. // filter containers by id
  313. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=id="+firstID)
  314. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  315. t.Fatal(out, err)
  316. }
  317. containerOut := strings.TrimSpace(out)
  318. if containerOut != firstID[:12] {
  319. t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  320. }
  321. deleteAllContainers()
  322. logDone("ps - test ps filter id")
  323. }
  324. func TestPsListContainersFilterName(t *testing.T) {
  325. // start container
  326. runCmd := exec.Command(dockerBinary, "run", "-d", "--name=a_name_to_match", "busybox")
  327. out, _, err := runCommandWithOutput(runCmd)
  328. if err != nil {
  329. t.Fatal(out, err)
  330. }
  331. firstID := stripTrailingCharacters(out)
  332. // start another container
  333. runCmd = exec.Command(dockerBinary, "run", "-d", "--name=b_name_to_match", "busybox", "sh", "-c", "sleep 360")
  334. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  335. t.Fatal(out, err)
  336. }
  337. // filter containers by name
  338. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=name=a_name_to_match")
  339. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  340. t.Fatal(out, err)
  341. }
  342. containerOut := strings.TrimSpace(out)
  343. if containerOut != firstID[:12] {
  344. t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  345. }
  346. deleteAllContainers()
  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. }