docker_cli_ps_test.go 13 KB

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