mirror of
https://github.com/clearlinux/docker.git
synced 2026-09-06 05:31:47 +00:00
Timestamps for docker logs.
Fixes #1165 Docker-DCO-1.1-Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com> (github: LK4D4)
This commit is contained in:
@@ -3,7 +3,10 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// This used to work, it test a log of PageSize-1 (gh#4851)
|
||||
@@ -74,3 +77,95 @@ func TestLogsContainerMuchBiggerThanPage(t *testing.T) {
|
||||
|
||||
logDone("logs - logs container running echo much bigger than page size")
|
||||
}
|
||||
|
||||
func TestLogsTimestamps(t *testing.T) {
|
||||
testLen := 100
|
||||
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen))
|
||||
|
||||
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
||||
errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
|
||||
|
||||
cleanedContainerID := stripTrailingCharacters(out)
|
||||
exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
|
||||
|
||||
logsCmd := exec.Command(dockerBinary, "logs", "-t", cleanedContainerID)
|
||||
out, _, _, err = runCommandWithStdoutStderr(logsCmd)
|
||||
errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
|
||||
|
||||
lines := strings.Split(out, "\n")
|
||||
|
||||
if len(lines) != testLen+1 {
|
||||
t.Fatalf("Expected log %d lines, received %d\n", testLen+1, len(lines))
|
||||
}
|
||||
|
||||
ts := regexp.MustCompile(`^\[.*?\]`)
|
||||
|
||||
for _, l := range lines {
|
||||
if l != "" {
|
||||
_, err := time.Parse("["+time.StampMilli+"]", ts.FindString(l))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse timestamp from %v: %v", l, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deleteContainer(cleanedContainerID)
|
||||
|
||||
logDone("logs - logs with timestamps")
|
||||
}
|
||||
|
||||
func TestLogsSeparateStderr(t *testing.T) {
|
||||
msg := "stderr_log"
|
||||
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
|
||||
|
||||
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
||||
errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
|
||||
|
||||
cleanedContainerID := stripTrailingCharacters(out)
|
||||
exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
|
||||
|
||||
logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
|
||||
stdout, stderr, _, err := runCommandWithStdoutStderr(logsCmd)
|
||||
errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
|
||||
|
||||
if stdout != "" {
|
||||
t.Fatalf("Expected empty stdout stream, got %v", stdout)
|
||||
}
|
||||
|
||||
stderr = strings.TrimSpace(stderr)
|
||||
if stderr != msg {
|
||||
t.Fatalf("Expected %v in stderr stream, got %v", msg, stderr)
|
||||
}
|
||||
|
||||
deleteContainer(cleanedContainerID)
|
||||
|
||||
logDone("logs - separate stderr (without pseudo-tty)")
|
||||
}
|
||||
|
||||
func TestLogsStderrInStdout(t *testing.T) {
|
||||
msg := "stderr_log"
|
||||
runCmd := exec.Command(dockerBinary, "run", "-d", "-t", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
|
||||
|
||||
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
||||
errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
|
||||
|
||||
cleanedContainerID := stripTrailingCharacters(out)
|
||||
exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
|
||||
|
||||
logsCmd := exec.Command(dockerBinary, "logs", cleanedContainerID)
|
||||
stdout, stderr, _, err := runCommandWithStdoutStderr(logsCmd)
|
||||
errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
|
||||
|
||||
if stderr != "" {
|
||||
t.Fatalf("Expected empty stderr stream, got %v", stdout)
|
||||
}
|
||||
|
||||
stdout = strings.TrimSpace(stdout)
|
||||
if stdout != msg {
|
||||
t.Fatalf("Expected %v in stdout stream, got %v", msg, stdout)
|
||||
}
|
||||
|
||||
deleteContainer(cleanedContainerID)
|
||||
|
||||
logDone("logs - stderr in stdout (with pseudo-tty)")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user