docker_cli_ps_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. errorOut(err, t, out)
  12. firstID := stripTrailingCharacters(out)
  13. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  14. out, _, err = runCommandWithOutput(runCmd)
  15. errorOut(err, t, out)
  16. secondID := stripTrailingCharacters(out)
  17. // not long running
  18. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  19. out, _, err = runCommandWithOutput(runCmd)
  20. errorOut(err, t, out)
  21. thirdID := stripTrailingCharacters(out)
  22. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  23. out, _, err = runCommandWithOutput(runCmd)
  24. errorOut(err, t, out)
  25. fourthID := stripTrailingCharacters(out)
  26. // make sure third one is not running
  27. runCmd = exec.Command(dockerBinary, "wait", thirdID)
  28. out, _, err = runCommandWithOutput(runCmd)
  29. errorOut(err, t, out)
  30. // all
  31. runCmd = exec.Command(dockerBinary, "ps", "-a")
  32. out, _, err = runCommandWithOutput(runCmd)
  33. errorOut(err, t, out)
  34. if !assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}) {
  35. t.Error("Container list is not in the correct order")
  36. }
  37. // running
  38. runCmd = exec.Command(dockerBinary, "ps")
  39. out, _, err = runCommandWithOutput(runCmd)
  40. errorOut(err, t, out)
  41. if !assertContainerList(out, []string{fourthID, secondID, firstID}) {
  42. t.Error("Container list is not in the correct order")
  43. }
  44. // from here all flag '-a' is ignored
  45. // limit
  46. runCmd = exec.Command(dockerBinary, "ps", "-n=2", "-a")
  47. out, _, err = runCommandWithOutput(runCmd)
  48. errorOut(err, t, out)
  49. expected := []string{fourthID, thirdID}
  50. if !assertContainerList(out, expected) {
  51. t.Error("Container list is not in the correct order")
  52. }
  53. runCmd = exec.Command(dockerBinary, "ps", "-n=2")
  54. out, _, err = runCommandWithOutput(runCmd)
  55. errorOut(err, t, out)
  56. if !assertContainerList(out, expected) {
  57. t.Error("Container list is not in the correct order")
  58. }
  59. // since
  60. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-a")
  61. out, _, err = runCommandWithOutput(runCmd)
  62. errorOut(err, t, out)
  63. expected = []string{fourthID, thirdID, secondID}
  64. if !assertContainerList(out, expected) {
  65. t.Error("Container list is not in the correct order")
  66. }
  67. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID)
  68. out, _, err = runCommandWithOutput(runCmd)
  69. errorOut(err, t, out)
  70. if !assertContainerList(out, expected) {
  71. t.Error("Container list is not in the correct order")
  72. }
  73. // before
  74. runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID, "-a")
  75. out, _, err = runCommandWithOutput(runCmd)
  76. errorOut(err, t, out)
  77. expected = []string{secondID, firstID}
  78. if !assertContainerList(out, expected) {
  79. t.Error("Container list is not in the correct order")
  80. }
  81. runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID)
  82. out, _, err = runCommandWithOutput(runCmd)
  83. errorOut(err, t, out)
  84. if !assertContainerList(out, expected) {
  85. t.Error("Container list is not in the correct order")
  86. }
  87. // since & before
  88. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-a")
  89. out, _, err = runCommandWithOutput(runCmd)
  90. errorOut(err, t, out)
  91. expected = []string{thirdID, secondID}
  92. if !assertContainerList(out, expected) {
  93. t.Error("Container list is not in the correct order")
  94. }
  95. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID)
  96. out, _, err = runCommandWithOutput(runCmd)
  97. errorOut(err, t, out)
  98. if !assertContainerList(out, expected) {
  99. t.Error("Container list is not in the correct order")
  100. }
  101. // since & limit
  102. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2", "-a")
  103. out, _, err = runCommandWithOutput(runCmd)
  104. errorOut(err, t, out)
  105. expected = []string{fourthID, thirdID}
  106. if !assertContainerList(out, expected) {
  107. t.Error("Container list is not in the correct order")
  108. }
  109. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2")
  110. out, _, err = runCommandWithOutput(runCmd)
  111. errorOut(err, t, out)
  112. if !assertContainerList(out, expected) {
  113. t.Error("Container list is not in the correct order")
  114. }
  115. // before & limit
  116. runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1", "-a")
  117. out, _, err = runCommandWithOutput(runCmd)
  118. errorOut(err, t, out)
  119. expected = []string{thirdID}
  120. if !assertContainerList(out, expected) {
  121. t.Error("Container list is not in the correct order")
  122. }
  123. runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1")
  124. out, _, err = runCommandWithOutput(runCmd)
  125. errorOut(err, t, out)
  126. if !assertContainerList(out, expected) {
  127. t.Error("Container list is not in the correct order")
  128. }
  129. // since & before & limit
  130. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a")
  131. out, _, err = runCommandWithOutput(runCmd)
  132. errorOut(err, t, out)
  133. expected = []string{thirdID}
  134. if !assertContainerList(out, expected) {
  135. t.Error("Container list is not in the correct order")
  136. }
  137. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1")
  138. out, _, err = runCommandWithOutput(runCmd)
  139. errorOut(err, t, out)
  140. if !assertContainerList(out, expected) {
  141. t.Error("Container list is not in the correct order")
  142. }
  143. deleteAllContainers()
  144. logDone("ps - test ps options")
  145. }
  146. func assertContainerList(out string, expected []string) bool {
  147. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  148. if len(lines)-1 != len(expected) {
  149. return false
  150. }
  151. containerIdIndex := strings.Index(lines[0], "CONTAINER ID")
  152. for i := 0; i < len(expected); i++ {
  153. foundID := lines[i+1][containerIdIndex : containerIdIndex+12]
  154. if foundID != expected[i][:12] {
  155. return false
  156. }
  157. }
  158. return true
  159. }
  160. func TestPsListContainersSize(t *testing.T) {
  161. name := "test_size"
  162. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test")
  163. out, _, err := runCommandWithOutput(runCmd)
  164. errorOut(err, t, out)
  165. id, err := getIDByName(name)
  166. if err != nil {
  167. t.Fatal(err)
  168. }
  169. runCmd = exec.Command(dockerBinary, "ps", "-s", "-n=1")
  170. wait := make(chan struct{})
  171. go func() {
  172. out, _, err = runCommandWithOutput(runCmd)
  173. close(wait)
  174. }()
  175. select {
  176. case <-wait:
  177. case <-time.After(3 * time.Second):
  178. t.Fatalf("Calling \"docker ps -s\" timed out!")
  179. }
  180. errorOut(err, t, out)
  181. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  182. sizeIndex := strings.Index(lines[0], "SIZE")
  183. idIndex := strings.Index(lines[0], "CONTAINER ID")
  184. foundID := lines[1][idIndex : idIndex+12]
  185. if foundID != id[:12] {
  186. t.Fatalf("Expected id %s, got %s", id[:12], foundID)
  187. }
  188. expectedSize := "2 B"
  189. foundSize := lines[1][sizeIndex:]
  190. if foundSize != expectedSize {
  191. t.Fatalf("Expected size %q, got %q", expectedSize, foundSize)
  192. }
  193. }