2014-09-23 13:55:14 +00:00
|
|
|
package jsonlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2014-09-23 14:19:06 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2014-09-23 13:55:14 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/timeutils"
|
|
|
|
)
|
|
|
|
|
2014-09-23 14:19:06 +00:00
|
|
|
func TestWriteLog(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
e := json.NewEncoder(&buf)
|
|
|
|
testLine := "Line that thinks that it is log line from docker\n"
|
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
e.Encode(JSONLog{Log: testLine, Stream: "stdout", Created: time.Now()})
|
|
|
|
}
|
|
|
|
w := bytes.NewBuffer(nil)
|
|
|
|
format := timeutils.RFC3339NanoFixed
|
2015-04-14 04:36:12 +00:00
|
|
|
if err := WriteLog(&buf, w, format, time.Time{}); err != nil {
|
2014-09-23 14:19:06 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
res := w.String()
|
|
|
|
t.Logf("Result of WriteLog: %q", res)
|
|
|
|
lines := strings.Split(strings.TrimSpace(res), "\n")
|
|
|
|
if len(lines) != 30 {
|
|
|
|
t.Fatalf("Must be 30 lines but got %d", len(lines))
|
|
|
|
}
|
2015-01-28 22:33:15 +00:00
|
|
|
// 30+ symbols, five more can come from system timezone
|
|
|
|
logRe := regexp.MustCompile(`.{30,} Line that thinks that it is log line from docker`)
|
2014-09-23 14:19:06 +00:00
|
|
|
for _, l := range lines {
|
|
|
|
if !logRe.MatchString(l) {
|
|
|
|
t.Fatalf("Log line not in expected format: %q", l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 13:55:14 +00:00
|
|
|
func BenchmarkWriteLog(b *testing.B) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
e := json.NewEncoder(&buf)
|
|
|
|
testLine := "Line that thinks that it is log line from docker\n"
|
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
e.Encode(JSONLog{Log: testLine, Stream: "stdout", Created: time.Now()})
|
|
|
|
}
|
|
|
|
r := bytes.NewReader(buf.Bytes())
|
|
|
|
w := ioutil.Discard
|
|
|
|
format := timeutils.RFC3339NanoFixed
|
|
|
|
b.SetBytes(int64(r.Len()))
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-04-14 04:36:12 +00:00
|
|
|
if err := WriteLog(r, w, format, time.Time{}); err != nil {
|
2014-09-23 13:55:14 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
b.StopTimer()
|
|
|
|
r.Seek(0, 0)
|
|
|
|
b.StartTimer()
|
|
|
|
}
|
|
|
|
}
|