2014-03-31 14:40:39 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2014-04-02 15:26:06 -04:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2014-03-31 14:40:39 -04:00
|
|
|
"testing"
|
2014-04-02 15:26:06 -04:00
|
|
|
"time"
|
2014-03-31 14:40:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This used to work, it test a log of PageSize-1 (gh#4851)
|
|
|
|
func TestLogsContainerSmallerThanPage(t *testing.T) {
|
|
|
|
testLen := 32767
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", 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", cleanedContainerID)
|
|
|
|
out, _, _, err = runCommandWithStdoutStderr(logsCmd)
|
|
|
|
errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
|
|
|
|
|
|
|
|
if len(out) != testLen+1 {
|
|
|
|
t.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
|
|
|
|
}
|
|
|
|
|
2014-04-03 20:22:32 -04:00
|
|
|
deleteContainer(cleanedContainerID)
|
2014-03-31 14:40:39 -04:00
|
|
|
|
|
|
|
logDone("logs - logs container running echo smaller than page size")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regression test: When going over the PageSize, it used to panic (gh#4851)
|
|
|
|
func TestLogsContainerBiggerThanPage(t *testing.T) {
|
|
|
|
testLen := 32768
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", 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", cleanedContainerID)
|
|
|
|
out, _, _, err = runCommandWithStdoutStderr(logsCmd)
|
|
|
|
errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
|
|
|
|
|
|
|
|
if len(out) != testLen+1 {
|
|
|
|
t.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
|
|
|
|
}
|
|
|
|
|
2014-04-03 20:22:32 -04:00
|
|
|
deleteContainer(cleanedContainerID)
|
2014-03-31 14:40:39 -04:00
|
|
|
|
|
|
|
logDone("logs - logs container running echo bigger than page size")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regression test: When going much over the PageSize, it used to block (gh#4851)
|
|
|
|
func TestLogsContainerMuchBiggerThanPage(t *testing.T) {
|
|
|
|
testLen := 33000
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", 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", cleanedContainerID)
|
|
|
|
out, _, _, err = runCommandWithStdoutStderr(logsCmd)
|
|
|
|
errorOut(err, t, fmt.Sprintf("failed to log container: %v %v", out, err))
|
|
|
|
|
|
|
|
if len(out) != testLen+1 {
|
|
|
|
t.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
|
|
|
|
}
|
|
|
|
|
2014-04-03 20:22:32 -04:00
|
|
|
deleteContainer(cleanedContainerID)
|
2014-03-31 14:40:39 -04:00
|
|
|
|
|
|
|
logDone("logs - logs container running echo much bigger than page size")
|
|
|
|
}
|
2014-04-02 15:26:06 -04:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2014-05-10 12:51:16 -04:00
|
|
|
ts := regexp.MustCompile(`^.* `)
|
2014-04-02 15:26:06 -04:00
|
|
|
|
|
|
|
for _, l := range lines {
|
|
|
|
if l != "" {
|
2014-05-10 12:51:16 -04:00
|
|
|
_, err := time.Parse(time.RFC3339Nano+" ", ts.FindString(l))
|
2014-04-02 15:26:06 -04:00
|
|
|
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)")
|
|
|
|
}
|
2014-06-03 07:09:33 -04:00
|
|
|
|
|
|
|
func TestLogsTail(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", "--tail", "5", 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) != 6 {
|
|
|
|
t.Fatalf("Expected log %d lines, received %d\n", 6, len(lines))
|
|
|
|
}
|
|
|
|
|
|
|
|
logsCmd = exec.Command(dockerBinary, "logs", "--tail", "all", 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))
|
|
|
|
}
|
|
|
|
|
|
|
|
logsCmd = exec.Command(dockerBinary, "logs", "--tail", "random", 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))
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteContainer(cleanedContainerID)
|
|
|
|
logDone("logs - logs tail")
|
|
|
|
}
|
2014-08-20 12:02:56 -04:00
|
|
|
|
|
|
|
func TestLogsFollowStopped(t *testing.T) {
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "hello")
|
|
|
|
|
|
|
|
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", "-f", cleanedContainerID)
|
|
|
|
if err := logsCmd.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
if err := logsCmd.Wait(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
close(c)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("Following logs is hanged")
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteContainer(cleanedContainerID)
|
|
|
|
logDone("logs - logs follow stopped container")
|
|
|
|
}
|