docker_cli_logs_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "regexp"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/docker/docker/pkg/timeutils"
  10. )
  11. // This used to work, it test a log of PageSize-1 (gh#4851)
  12. func TestLogsContainerSmallerThanPage(t *testing.T) {
  13. testLen := 32767
  14. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", testLen))
  15. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  16. if err != nil {
  17. t.Fatalf("run failed with errors: %s, %v", out, err)
  18. }
  19. cleanedContainerID := stripTrailingCharacters(out)
  20. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  21. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  22. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  23. if err != nil {
  24. t.Fatalf("failed to log container: %s, %v", out, err)
  25. }
  26. if len(out) != testLen+1 {
  27. t.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
  28. }
  29. deleteContainer(cleanedContainerID)
  30. logDone("logs - logs container running echo smaller than page size")
  31. }
  32. // Regression test: When going over the PageSize, it used to panic (gh#4851)
  33. func TestLogsContainerBiggerThanPage(t *testing.T) {
  34. testLen := 32768
  35. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", testLen))
  36. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  37. if err != nil {
  38. t.Fatalf("run failed with errors: %s, %v", out, err)
  39. }
  40. cleanedContainerID := stripTrailingCharacters(out)
  41. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  42. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  43. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  44. if err != nil {
  45. t.Fatalf("failed to log container: %s, %v", out, err)
  46. }
  47. if len(out) != testLen+1 {
  48. t.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
  49. }
  50. deleteContainer(cleanedContainerID)
  51. logDone("logs - logs container running echo bigger than page size")
  52. }
  53. // Regression test: When going much over the PageSize, it used to block (gh#4851)
  54. func TestLogsContainerMuchBiggerThanPage(t *testing.T) {
  55. testLen := 33000
  56. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", testLen))
  57. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  58. if err != nil {
  59. t.Fatalf("run failed with errors: %s, %v", out, err)
  60. }
  61. cleanedContainerID := stripTrailingCharacters(out)
  62. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  63. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  64. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  65. if err != nil {
  66. t.Fatalf("failed to log container: %s, %v", out, err)
  67. }
  68. if len(out) != testLen+1 {
  69. t.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
  70. }
  71. deleteContainer(cleanedContainerID)
  72. logDone("logs - logs container running echo much bigger than page size")
  73. }
  74. func TestLogsTimestamps(t *testing.T) {
  75. testLen := 100
  76. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen))
  77. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  78. if err != nil {
  79. t.Fatalf("run failed with errors: %s, %v", out, err)
  80. }
  81. cleanedContainerID := stripTrailingCharacters(out)
  82. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  83. logsCmd := exec.Command(dockerBinary, "logs", "-t", cleanedContainerID)
  84. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  85. if err != nil {
  86. t.Fatalf("failed to log container: %s, %v", out, err)
  87. }
  88. lines := strings.Split(out, "\n")
  89. if len(lines) != testLen+1 {
  90. t.Fatalf("Expected log %d lines, received %d\n", testLen+1, len(lines))
  91. }
  92. ts := regexp.MustCompile(`^.* `)
  93. for _, l := range lines {
  94. if l != "" {
  95. _, err := time.Parse(timeutils.RFC3339NanoFixed+" ", ts.FindString(l))
  96. if err != nil {
  97. t.Fatalf("Failed to parse timestamp from %v: %v", l, err)
  98. }
  99. if l[29] != 'Z' { // ensure we have padded 0's
  100. t.Fatalf("Timestamp isn't padded properly: %s", l)
  101. }
  102. }
  103. }
  104. deleteContainer(cleanedContainerID)
  105. logDone("logs - logs with timestamps")
  106. }
  107. func TestLogsSeparateStderr(t *testing.T) {
  108. msg := "stderr_log"
  109. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
  110. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  111. if err != nil {
  112. t.Fatalf("run failed with errors: %s, %v", out, err)
  113. }
  114. cleanedContainerID := stripTrailingCharacters(out)
  115. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  116. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  117. stdout, stderr, _, err := runCommandWithStdoutStderr(logsCmd)
  118. if err != nil {
  119. t.Fatalf("failed to log container: %s, %v", out, err)
  120. }
  121. if stdout != "" {
  122. t.Fatalf("Expected empty stdout stream, got %v", stdout)
  123. }
  124. stderr = strings.TrimSpace(stderr)
  125. if stderr != msg {
  126. t.Fatalf("Expected %v in stderr stream, got %v", msg, stderr)
  127. }
  128. deleteContainer(cleanedContainerID)
  129. logDone("logs - separate stderr (without pseudo-tty)")
  130. }
  131. func TestLogsStderrInStdout(t *testing.T) {
  132. msg := "stderr_log"
  133. runCmd := exec.Command(dockerBinary, "run", "-d", "-t", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
  134. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  135. if err != nil {
  136. t.Fatalf("run failed with errors: %s, %v", out, err)
  137. }
  138. cleanedContainerID := stripTrailingCharacters(out)
  139. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  140. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  141. stdout, stderr, _, err := runCommandWithStdoutStderr(logsCmd)
  142. if err != nil {
  143. t.Fatalf("failed to log container: %s, %v", out, err)
  144. }
  145. if stderr != "" {
  146. t.Fatalf("Expected empty stderr stream, got %v", stdout)
  147. }
  148. stdout = strings.TrimSpace(stdout)
  149. if stdout != msg {
  150. t.Fatalf("Expected %v in stdout stream, got %v", msg, stdout)
  151. }
  152. deleteContainer(cleanedContainerID)
  153. logDone("logs - stderr in stdout (with pseudo-tty)")
  154. }
  155. func TestLogsTail(t *testing.T) {
  156. testLen := 100
  157. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen))
  158. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  159. if err != nil {
  160. t.Fatalf("run failed with errors: %s, %v", out, err)
  161. }
  162. cleanedContainerID := stripTrailingCharacters(out)
  163. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  164. logsCmd := exec.Command(dockerBinary, "logs", "--tail", "5", cleanedContainerID)
  165. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  166. if err != nil {
  167. t.Fatalf("failed to log container: %s, %v", out, err)
  168. }
  169. lines := strings.Split(out, "\n")
  170. if len(lines) != 6 {
  171. t.Fatalf("Expected log %d lines, received %d\n", 6, len(lines))
  172. }
  173. logsCmd = exec.Command(dockerBinary, "logs", "--tail", "all", cleanedContainerID)
  174. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  175. if err != nil {
  176. t.Fatalf("failed to log container: %s, %v", out, err)
  177. }
  178. lines = strings.Split(out, "\n")
  179. if len(lines) != testLen+1 {
  180. t.Fatalf("Expected log %d lines, received %d\n", testLen+1, len(lines))
  181. }
  182. logsCmd = exec.Command(dockerBinary, "logs", "--tail", "random", cleanedContainerID)
  183. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  184. if err != nil {
  185. t.Fatalf("failed to log container: %s, %v", out, err)
  186. }
  187. lines = strings.Split(out, "\n")
  188. if len(lines) != testLen+1 {
  189. t.Fatalf("Expected log %d lines, received %d\n", testLen+1, len(lines))
  190. }
  191. deleteContainer(cleanedContainerID)
  192. logDone("logs - logs tail")
  193. }
  194. func TestLogsFollowStopped(t *testing.T) {
  195. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "hello")
  196. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  197. if err != nil {
  198. t.Fatalf("run failed with errors: %s, %v", out, err)
  199. }
  200. cleanedContainerID := stripTrailingCharacters(out)
  201. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  202. logsCmd := exec.Command(dockerBinary, "logs", "-f", cleanedContainerID)
  203. if err := logsCmd.Start(); err != nil {
  204. t.Fatal(err)
  205. }
  206. c := make(chan struct{})
  207. go func() {
  208. if err := logsCmd.Wait(); err != nil {
  209. t.Fatal(err)
  210. }
  211. close(c)
  212. }()
  213. select {
  214. case <-c:
  215. case <-time.After(1 * time.Second):
  216. t.Fatal("Following logs is hanged")
  217. }
  218. deleteContainer(cleanedContainerID)
  219. logDone("logs - logs follow stopped container")
  220. }
  221. // Regression test for #8832
  222. func TestLogsFollowSlowStdoutConsumer(t *testing.T) {
  223. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", `usleep 200000;yes X | head -c 200000`)
  224. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  225. if err != nil {
  226. t.Fatalf("run failed with errors: %s, %v", out, err)
  227. }
  228. cleanedContainerID := stripTrailingCharacters(out)
  229. defer deleteContainer(cleanedContainerID)
  230. stopSlowRead := make(chan bool)
  231. go func() {
  232. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  233. stopSlowRead <- true
  234. }()
  235. logCmd := exec.Command(dockerBinary, "logs", "-f", cleanedContainerID)
  236. stdout, err := logCmd.StdoutPipe()
  237. if err != nil {
  238. t.Fatal(err)
  239. }
  240. if err := logCmd.Start(); err != nil {
  241. t.Fatal(err)
  242. }
  243. // First read slowly
  244. bytes1, err := consumeWithSpeed(stdout, 10, 50*time.Millisecond, stopSlowRead)
  245. if err != nil {
  246. t.Fatal(err)
  247. }
  248. // After the container has finished we can continue reading fast
  249. bytes2, err := consumeWithSpeed(stdout, 32*1024, 0, nil)
  250. if err != nil {
  251. t.Fatal(err)
  252. }
  253. actual := bytes1 + bytes2
  254. expected := 200000
  255. if actual != expected {
  256. t.Fatalf("Invalid bytes read: %d, expected %d", actual, expected)
  257. }
  258. logDone("logs - follow slow consumer")
  259. }