docker_cli_logs_test.go 9.1 KB

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