docker_cli_ps_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "testing"
  6. )
  7. func TestListContainers(t *testing.T) {
  8. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  9. out, _, err := runCommandWithOutput(runCmd)
  10. errorOut(err, t, out)
  11. firstID := stripTrailingCharacters(out)
  12. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  13. out, _, err = runCommandWithOutput(runCmd)
  14. errorOut(err, t, out)
  15. secondID := stripTrailingCharacters(out)
  16. // not long running
  17. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  18. out, _, err = runCommandWithOutput(runCmd)
  19. errorOut(err, t, out)
  20. thirdID := stripTrailingCharacters(out)
  21. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  22. out, _, err = runCommandWithOutput(runCmd)
  23. errorOut(err, t, out)
  24. fourthID := stripTrailingCharacters(out)
  25. // make sure third one is not running
  26. runCmd = exec.Command(dockerBinary, "wait", thirdID)
  27. out, _, err = runCommandWithOutput(runCmd)
  28. errorOut(err, t, out)
  29. // all
  30. runCmd = exec.Command(dockerBinary, "ps", "-a")
  31. out, _, err = runCommandWithOutput(runCmd)
  32. errorOut(err, t, out)
  33. if !assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}) {
  34. t.Error("Container list is not in the correct order")
  35. }
  36. // running
  37. runCmd = exec.Command(dockerBinary, "ps")
  38. out, _, err = runCommandWithOutput(runCmd)
  39. errorOut(err, t, out)
  40. if !assertContainerList(out, []string{fourthID, secondID, firstID}) {
  41. t.Error("Container list is not in the correct order")
  42. }
  43. // from here all flag '-a' is ignored
  44. // limit
  45. runCmd = exec.Command(dockerBinary, "ps", "-n=2", "-a")
  46. out, _, err = runCommandWithOutput(runCmd)
  47. errorOut(err, t, out)
  48. expected := []string{fourthID, thirdID}
  49. if !assertContainerList(out, expected) {
  50. t.Error("Container list is not in the correct order")
  51. }
  52. runCmd = exec.Command(dockerBinary, "ps", "-n=2")
  53. out, _, err = runCommandWithOutput(runCmd)
  54. errorOut(err, t, out)
  55. if !assertContainerList(out, expected) {
  56. t.Error("Container list is not in the correct order")
  57. }
  58. // since
  59. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-a")
  60. out, _, err = runCommandWithOutput(runCmd)
  61. errorOut(err, t, out)
  62. expected = []string{fourthID, thirdID, secondID}
  63. if !assertContainerList(out, expected) {
  64. t.Error("Container list is not in the correct order")
  65. }
  66. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID)
  67. out, _, err = runCommandWithOutput(runCmd)
  68. errorOut(err, t, out)
  69. if !assertContainerList(out, expected) {
  70. t.Error("Container list is not in the correct order")
  71. }
  72. // before
  73. runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID, "-a")
  74. out, _, err = runCommandWithOutput(runCmd)
  75. errorOut(err, t, out)
  76. expected = []string{secondID, firstID}
  77. if !assertContainerList(out, expected) {
  78. t.Error("Container list is not in the correct order")
  79. }
  80. runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID)
  81. out, _, err = runCommandWithOutput(runCmd)
  82. errorOut(err, t, out)
  83. if !assertContainerList(out, expected) {
  84. t.Error("Container list is not in the correct order")
  85. }
  86. // since & before
  87. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-a")
  88. out, _, err = runCommandWithOutput(runCmd)
  89. errorOut(err, t, out)
  90. expected = []string{thirdID, secondID}
  91. if !assertContainerList(out, expected) {
  92. t.Error("Container list is not in the correct order")
  93. }
  94. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID)
  95. out, _, err = runCommandWithOutput(runCmd)
  96. errorOut(err, t, out)
  97. if !assertContainerList(out, expected) {
  98. t.Error("Container list is not in the correct order")
  99. }
  100. // since & limit
  101. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2", "-a")
  102. out, _, err = runCommandWithOutput(runCmd)
  103. errorOut(err, t, out)
  104. expected = []string{fourthID, thirdID}
  105. if !assertContainerList(out, expected) {
  106. t.Error("Container list is not in the correct order")
  107. }
  108. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2")
  109. out, _, err = runCommandWithOutput(runCmd)
  110. errorOut(err, t, out)
  111. if !assertContainerList(out, expected) {
  112. t.Error("Container list is not in the correct order")
  113. }
  114. // before & limit
  115. runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1", "-a")
  116. out, _, err = runCommandWithOutput(runCmd)
  117. errorOut(err, t, out)
  118. expected = []string{thirdID}
  119. if !assertContainerList(out, expected) {
  120. t.Error("Container list is not in the correct order")
  121. }
  122. runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1")
  123. out, _, err = runCommandWithOutput(runCmd)
  124. errorOut(err, t, out)
  125. if !assertContainerList(out, expected) {
  126. t.Error("Container list is not in the correct order")
  127. }
  128. // since & before & limit
  129. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a")
  130. out, _, err = runCommandWithOutput(runCmd)
  131. errorOut(err, t, out)
  132. expected = []string{thirdID}
  133. if !assertContainerList(out, expected) {
  134. t.Error("Container list is not in the correct order")
  135. }
  136. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1")
  137. out, _, err = runCommandWithOutput(runCmd)
  138. errorOut(err, t, out)
  139. if !assertContainerList(out, expected) {
  140. t.Error("Container list is not in the correct order")
  141. }
  142. deleteAllContainers()
  143. logDone("ps - test ps options")
  144. }
  145. func assertContainerList(out string, expected []string) bool {
  146. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  147. if len(lines)-1 != len(expected) {
  148. return false
  149. }
  150. containerIdIndex := strings.Index(lines[0], "CONTAINER ID")
  151. for i := 0; i < len(expected); i++ {
  152. foundID := lines[i+1][containerIdIndex : containerIdIndex+12]
  153. if foundID != expected[i][:12] {
  154. return false
  155. }
  156. }
  157. return true
  158. }