docker_cli_logs_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  17. cleanedContainerID := stripTrailingCharacters(out)
  18. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  19. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  20. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  21. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  22. if len(out) != testLen+1 {
  23. t.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
  24. }
  25. deleteContainer(cleanedContainerID)
  26. logDone("logs - logs container running echo smaller than page size")
  27. }
  28. // Regression test: When going over the PageSize, it used to panic (gh#4851)
  29. func TestLogsContainerBiggerThanPage(t *testing.T) {
  30. testLen := 32768
  31. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", testLen))
  32. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  33. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  34. cleanedContainerID := stripTrailingCharacters(out)
  35. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  36. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  37. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  38. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  39. if len(out) != testLen+1 {
  40. t.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
  41. }
  42. deleteContainer(cleanedContainerID)
  43. logDone("logs - logs container running echo bigger than page size")
  44. }
  45. // Regression test: When going much over the PageSize, it used to block (gh#4851)
  46. func TestLogsContainerMuchBiggerThanPage(t *testing.T) {
  47. testLen := 33000
  48. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", testLen))
  49. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  50. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  51. cleanedContainerID := stripTrailingCharacters(out)
  52. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  53. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  54. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  55. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  56. if len(out) != testLen+1 {
  57. t.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
  58. }
  59. deleteContainer(cleanedContainerID)
  60. logDone("logs - logs container running echo much bigger than page size")
  61. }
  62. func TestLogsTimestamps(t *testing.T) {
  63. testLen := 100
  64. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen))
  65. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  66. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  67. cleanedContainerID := stripTrailingCharacters(out)
  68. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  69. logsCmd := exec.Command(dockerBinary, "logs", "-t", cleanedContainerID)
  70. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  71. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  72. lines := strings.Split(out, "\n")
  73. if len(lines) != testLen+1 {
  74. t.Fatalf("Expected log %d lines, received %d\n", testLen+1, len(lines))
  75. }
  76. ts := regexp.MustCompile(`^.* `)
  77. for _, l := range lines {
  78. if l != "" {
  79. _, err := time.Parse(timeutils.RFC3339NanoFixed+" ", ts.FindString(l))
  80. if err != nil {
  81. t.Fatalf("Failed to parse timestamp from %v: %v", l, err)
  82. }
  83. if l[29] != 'Z' { // ensure we have padded 0's
  84. t.Fatalf("Timestamp isn't padded properly: %s", l)
  85. }
  86. }
  87. }
  88. deleteContainer(cleanedContainerID)
  89. logDone("logs - logs with timestamps")
  90. }
  91. func TestLogsSeparateStderr(t *testing.T) {
  92. msg := "stderr_log"
  93. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
  94. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  95. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  96. cleanedContainerID := stripTrailingCharacters(out)
  97. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  98. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  99. stdout, stderr, _, err := runCommandWithStdoutStderr(logsCmd)
  100. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  101. if stdout != "" {
  102. t.Fatalf("Expected empty stdout stream, got %v", stdout)
  103. }
  104. stderr = strings.TrimSpace(stderr)
  105. if stderr != msg {
  106. t.Fatalf("Expected %v in stderr stream, got %v", msg, stderr)
  107. }
  108. deleteContainer(cleanedContainerID)
  109. logDone("logs - separate stderr (without pseudo-tty)")
  110. }
  111. func TestLogsStderrInStdout(t *testing.T) {
  112. msg := "stderr_log"
  113. runCmd := exec.Command(dockerBinary, "run", "-d", "-t", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
  114. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  115. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  116. cleanedContainerID := stripTrailingCharacters(out)
  117. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  118. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  119. stdout, stderr, _, err := runCommandWithStdoutStderr(logsCmd)
  120. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  121. if stderr != "" {
  122. t.Fatalf("Expected empty stderr stream, got %v", stdout)
  123. }
  124. stdout = strings.TrimSpace(stdout)
  125. if stdout != msg {
  126. t.Fatalf("Expected %v in stdout stream, got %v", msg, stdout)
  127. }
  128. deleteContainer(cleanedContainerID)
  129. logDone("logs - stderr in stdout (with pseudo-tty)")
  130. }
  131. func TestLogsTail(t *testing.T) {
  132. testLen := 100
  133. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen))
  134. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  135. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  136. cleanedContainerID := stripTrailingCharacters(out)
  137. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  138. logsCmd := exec.Command(dockerBinary, "logs", "--tail", "5", cleanedContainerID)
  139. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  140. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  141. lines := strings.Split(out, "\n")
  142. if len(lines) != 6 {
  143. t.Fatalf("Expected log %d lines, received %d\n", 6, len(lines))
  144. }
  145. logsCmd = exec.Command(dockerBinary, "logs", "--tail", "all", cleanedContainerID)
  146. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  147. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  148. lines = strings.Split(out, "\n")
  149. if len(lines) != testLen+1 {
  150. t.Fatalf("Expected log %d lines, received %d\n", testLen+1, len(lines))
  151. }
  152. logsCmd = exec.Command(dockerBinary, "logs", "--tail", "random", cleanedContainerID)
  153. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  154. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  155. lines = strings.Split(out, "\n")
  156. if len(lines) != testLen+1 {
  157. t.Fatalf("Expected log %d lines, received %d\n", testLen+1, len(lines))
  158. }
  159. deleteContainer(cleanedContainerID)
  160. logDone("logs - logs tail")
  161. }
  162. func TestLogsFollowStopped(t *testing.T) {
  163. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "hello")
  164. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  165. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  166. cleanedContainerID := stripTrailingCharacters(out)
  167. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  168. logsCmd := exec.Command(dockerBinary, "logs", "-f", cleanedContainerID)
  169. if err := logsCmd.Start(); err != nil {
  170. t.Fatal(err)
  171. }
  172. c := make(chan struct{})
  173. go func() {
  174. if err := logsCmd.Wait(); err != nil {
  175. t.Fatal(err)
  176. }
  177. close(c)
  178. }()
  179. select {
  180. case <-c:
  181. case <-time.After(1 * time.Second):
  182. t.Fatal("Following logs is hanged")
  183. }
  184. deleteContainer(cleanedContainerID)
  185. logDone("logs - logs follow stopped container")
  186. }