2015-02-03 18:41:26 -05:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2016-05-25 00:19:01 -04:00
|
|
|
"sync"
|
2015-02-03 18:41:26 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestLoggerJSON struct {
|
|
|
|
*json.Encoder
|
2016-05-25 00:19:01 -04:00
|
|
|
mu sync.Mutex
|
2016-01-15 08:42:23 -05:00
|
|
|
delay time.Duration
|
2015-02-03 18:41:26 -05:00
|
|
|
}
|
|
|
|
|
2016-01-15 08:42:23 -05:00
|
|
|
func (l *TestLoggerJSON) Log(m *Message) error {
|
|
|
|
if l.delay > 0 {
|
|
|
|
time.Sleep(l.delay)
|
|
|
|
}
|
2016-05-25 00:19:01 -04:00
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
2016-01-15 08:42:23 -05:00
|
|
|
return l.Encode(m)
|
|
|
|
}
|
2015-02-03 18:41:26 -05:00
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
func (l *TestLoggerJSON) Close() error { return nil }
|
|
|
|
|
|
|
|
func (l *TestLoggerJSON) Name() string { return "json" }
|
2015-02-03 18:41:26 -05:00
|
|
|
|
|
|
|
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"
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
if _, err := stdout.WriteString(stdoutLine + "\n"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := stderr.WriteString(stderrLine + "\n"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonBuf bytes.Buffer
|
|
|
|
|
|
|
|
jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf)}
|
|
|
|
|
2016-05-31 15:46:55 -04:00
|
|
|
c := NewCopier(
|
2015-02-03 18:41:26 -05:00
|
|
|
map[string]io.Reader{
|
|
|
|
"stdout": &stdout,
|
|
|
|
"stderr": &stderr,
|
|
|
|
},
|
|
|
|
jsonLog)
|
|
|
|
c.Run()
|
2015-03-18 14:44:14 -04:00
|
|
|
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:
|
|
|
|
}
|
2015-02-03 18:41:26 -05:00
|
|
|
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) != stdoutLine {
|
|
|
|
t.Fatalf("Wrong Line: %q, expected %q", msg.Line, stdoutLine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if msg.Source == "stderr" {
|
|
|
|
if string(msg.Line) != stderrLine {
|
|
|
|
t.Fatalf("Wrong Line: %q, expected %q", msg.Line, stderrLine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-15 08:42:23 -05:00
|
|
|
|
|
|
|
func TestCopierSlow(t *testing.T) {
|
|
|
|
stdoutLine := "Line that thinks that it is log line from docker stdout"
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
if _, err := stdout.WriteString(stdoutLine + "\n"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonBuf bytes.Buffer
|
|
|
|
//encoder := &encodeCloser{Encoder: json.NewEncoder(&jsonBuf)}
|
|
|
|
jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf), delay: 100 * time.Millisecond}
|
|
|
|
|
2016-05-31 15:46:55 -04:00
|
|
|
c := NewCopier(map[string]io.Reader{"stdout": &stdout}, jsonLog)
|
2016-01-15 08:42:23 -05:00
|
|
|
c.Run()
|
|
|
|
wait := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
c.Wait()
|
|
|
|
close(wait)
|
|
|
|
}()
|
|
|
|
<-time.After(150 * time.Millisecond)
|
|
|
|
c.Close()
|
|
|
|
select {
|
|
|
|
case <-time.After(200 * time.Millisecond):
|
|
|
|
t.Fatalf("failed to exit in time after the copier is closed")
|
|
|
|
case <-wait:
|
|
|
|
}
|
|
|
|
}
|