docker_cli_logs_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "regexp"
  6. "strings"
  7. "testing"
  8. "time"
  9. )
  10. // This used to work, it test a log of PageSize-1 (gh#4851)
  11. func TestLogsContainerSmallerThanPage(t *testing.T) {
  12. testLen := 32767
  13. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", testLen))
  14. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  15. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  16. cleanedContainerID := stripTrailingCharacters(out)
  17. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  18. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  19. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  20. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  21. if len(out) != testLen+1 {
  22. t.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
  23. }
  24. deleteContainer(cleanedContainerID)
  25. logDone("logs - logs container running echo smaller than page size")
  26. }
  27. // Regression test: When going over the PageSize, it used to panic (gh#4851)
  28. func TestLogsContainerBiggerThanPage(t *testing.T) {
  29. testLen := 32768
  30. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", testLen))
  31. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  32. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  33. cleanedContainerID := stripTrailingCharacters(out)
  34. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  35. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  36. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  37. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  38. if len(out) != testLen+1 {
  39. t.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
  40. }
  41. deleteContainer(cleanedContainerID)
  42. logDone("logs - logs container running echo bigger than page size")
  43. }
  44. // Regression test: When going much over the PageSize, it used to block (gh#4851)
  45. func TestLogsContainerMuchBiggerThanPage(t *testing.T) {
  46. testLen := 33000
  47. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", testLen))
  48. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  49. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  50. cleanedContainerID := stripTrailingCharacters(out)
  51. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  52. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  53. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  54. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  55. if len(out) != testLen+1 {
  56. t.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
  57. }
  58. deleteContainer(cleanedContainerID)
  59. logDone("logs - logs container running echo much bigger than page size")
  60. }
  61. func TestLogsTimestamps(t *testing.T) {
  62. testLen := 100
  63. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen))
  64. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  65. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  66. cleanedContainerID := stripTrailingCharacters(out)
  67. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  68. logsCmd := exec.Command(dockerBinary, "logs", "-t", cleanedContainerID)
  69. out, _, _, err = runCommandWithStdoutStderr(logsCmd)
  70. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  71. lines := strings.Split(out, "\n")
  72. if len(lines) != testLen+1 {
  73. t.Fatalf("Expected log %d lines, received %d\n", testLen+1, len(lines))
  74. }
  75. ts := regexp.MustCompile(`^\[.*?\]`)
  76. for _, l := range lines {
  77. if l != "" {
  78. _, err := time.Parse("["+time.StampMilli+"]", ts.FindString(l))
  79. if err != nil {
  80. t.Fatalf("Failed to parse timestamp from %v: %v", l, err)
  81. }
  82. }
  83. }
  84. deleteContainer(cleanedContainerID)
  85. logDone("logs - logs with timestamps")
  86. }
  87. func TestLogsSeparateStderr(t *testing.T) {
  88. msg := "stderr_log"
  89. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
  90. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  91. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  92. cleanedContainerID := stripTrailingCharacters(out)
  93. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  94. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  95. stdout, stderr, _, err := runCommandWithStdoutStderr(logsCmd)
  96. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  97. if stdout != "" {
  98. t.Fatalf("Expected empty stdout stream, got %v", stdout)
  99. }
  100. stderr = strings.TrimSpace(stderr)
  101. if stderr != msg {
  102. t.Fatalf("Expected %v in stderr stream, got %v", msg, stderr)
  103. }
  104. deleteContainer(cleanedContainerID)
  105. logDone("logs - separate stderr (without pseudo-tty)")
  106. }
  107. func TestLogsStderrInStdout(t *testing.T) {
  108. msg := "stderr_log"
  109. runCmd := exec.Command(dockerBinary, "run", "-d", "-t", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
  110. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  111. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  112. cleanedContainerID := stripTrailingCharacters(out)
  113. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  114. logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
  115. stdout, stderr, _, err := runCommandWithStdoutStderr(logsCmd)
  116. errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
  117. if stderr != "" {
  118. t.Fatalf("Expected empty stderr stream, got %v", stdout)
  119. }
  120. stdout = strings.TrimSpace(stdout)
  121. if stdout != msg {
  122. t.Fatalf("Expected %v in stdout stream, got %v", msg, stdout)
  123. }
  124. deleteContainer(cleanedContainerID)
  125. logDone("logs - stderr in stdout (with pseudo-tty)")
  126. }