From 2add198a7abc084dd394f61ed26057ec5d639a4c Mon Sep 17 00:00:00 2001 From: Alexandr Morozov Date: Tue, 23 Sep 2014 18:19:06 +0400 Subject: [PATCH] Test for jsonlog.WriteLog Signed-off-by: Alexandr Morozov --- pkg/jsonlog/jsonlog_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pkg/jsonlog/jsonlog_test.go b/pkg/jsonlog/jsonlog_test.go index 13b8dbea5b..5ee5eda35c 100644 --- a/pkg/jsonlog/jsonlog_test.go +++ b/pkg/jsonlog/jsonlog_test.go @@ -4,12 +4,40 @@ import ( "bytes" "encoding/json" "io/ioutil" + "regexp" + "strings" "testing" "time" "github.com/docker/docker/pkg/timeutils" ) +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 + if err := WriteLog(&buf, w, format); err != nil { + 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)) + } + logRe := regexp.MustCompile(`\[.*\] Line that thinks that it is log line from docker`) + for _, l := range lines { + if !logRe.MatchString(l) { + t.Fatalf("Log line not in expected format: %q", l) + } + } +} + func BenchmarkWriteLog(b *testing.B) { var buf bytes.Buffer e := json.NewEncoder(&buf)