Merge pull request #28035 from thaJeztah/add-test-for-long-loglines
add tests for long log-lines and trailing lines
This commit is contained in:
commit
9f7cc7d032
1 changed files with 90 additions and 4 deletions
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -33,6 +34,9 @@ func (l *TestLoggerJSON) Name() string { return "json" }
|
|||
func TestCopier(t *testing.T) {
|
||||
stdoutLine := "Line that thinks that it is log line from docker stdout"
|
||||
stderrLine := "Line that thinks that it is log line from docker stderr"
|
||||
stdoutTrailingLine := "stdout trailing line"
|
||||
stderrTrailingLine := "stderr trailing line"
|
||||
|
||||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
for i := 0; i < 30; i++ {
|
||||
|
@ -44,6 +48,14 @@ func TestCopier(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// Test remaining lines without line-endings
|
||||
if _, err := stdout.WriteString(stdoutTrailingLine); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := stderr.WriteString(stderrTrailingLine); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var jsonBuf bytes.Buffer
|
||||
|
||||
jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf)}
|
||||
|
@ -78,13 +90,87 @@ func TestCopier(t *testing.T) {
|
|||
t.Fatalf("Wrong Source: %q, should be %q or %q", msg.Source, "stdout", "stderr")
|
||||
}
|
||||
if msg.Source == "stdout" {
|
||||
if string(msg.Line) != stdoutLine {
|
||||
t.Fatalf("Wrong Line: %q, expected %q", msg.Line, stdoutLine)
|
||||
if string(msg.Line) != stdoutLine && string(msg.Line) != stdoutTrailingLine {
|
||||
t.Fatalf("Wrong Line: %q, expected %q or %q", msg.Line, stdoutLine, stdoutTrailingLine)
|
||||
}
|
||||
}
|
||||
if msg.Source == "stderr" {
|
||||
if string(msg.Line) != stderrLine {
|
||||
t.Fatalf("Wrong Line: %q, expected %q", msg.Line, stderrLine)
|
||||
if string(msg.Line) != stderrLine && string(msg.Line) != stderrTrailingLine {
|
||||
t.Fatalf("Wrong Line: %q, expected %q or %q", msg.Line, stderrLine, stderrTrailingLine)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestCopierLongLines tests long lines without line breaks
|
||||
func TestCopierLongLines(t *testing.T) {
|
||||
// Long lines (should be split at "bufSize")
|
||||
const bufSize = 16 * 1024
|
||||
stdoutLongLine := strings.Repeat("a", bufSize)
|
||||
stderrLongLine := strings.Repeat("b", bufSize)
|
||||
stdoutTrailingLine := "stdout trailing line"
|
||||
stderrTrailingLine := "stderr trailing line"
|
||||
|
||||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
if _, err := stdout.WriteString(stdoutLongLine); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := stderr.WriteString(stderrLongLine); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := stdout.WriteString(stdoutTrailingLine); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := stderr.WriteString(stderrTrailingLine); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var jsonBuf bytes.Buffer
|
||||
|
||||
jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf)}
|
||||
|
||||
c := NewCopier(
|
||||
map[string]io.Reader{
|
||||
"stdout": &stdout,
|
||||
"stderr": &stderr,
|
||||
},
|
||||
jsonLog)
|
||||
c.Run()
|
||||
wait := make(chan struct{})
|
||||
go func() {
|
||||
c.Wait()
|
||||
close(wait)
|
||||
}()
|
||||
select {
|
||||
case <-time.After(1 * time.Second):
|
||||
t.Fatal("Copier failed to do its work in 1 second")
|
||||
case <-wait:
|
||||
}
|
||||
dec := json.NewDecoder(&jsonBuf)
|
||||
for {
|
||||
var msg Message
|
||||
if err := dec.Decode(&msg); err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
t.Fatal(err)
|
||||
}
|
||||
if msg.Source != "stdout" && msg.Source != "stderr" {
|
||||
t.Fatalf("Wrong Source: %q, should be %q or %q", msg.Source, "stdout", "stderr")
|
||||
}
|
||||
if msg.Source == "stdout" {
|
||||
if string(msg.Line) != stdoutLongLine && string(msg.Line) != stdoutTrailingLine {
|
||||
t.Fatalf("Wrong Line: %q, expected 'stdoutLongLine' or 'stdoutTrailingLine'", msg.Line)
|
||||
}
|
||||
}
|
||||
if msg.Source == "stderr" {
|
||||
if string(msg.Line) != stderrLongLine && string(msg.Line) != stderrTrailingLine {
|
||||
t.Fatalf("Wrong Line: %q, expected 'stderrLongLine' or 'stderrTrailingLine'", msg.Line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue