浏览代码

Test for jsonlog.WriteLog

Signed-off-by: Alexandr Morozov <lk4d4@docker.com>
Alexandr Morozov 10 年之前
父节点
当前提交
2add198a7a
共有 1 个文件被更改,包括 28 次插入0 次删除
  1. 28 0
      pkg/jsonlog/jsonlog_test.go

+ 28 - 0
pkg/jsonlog/jsonlog_test.go

@@ -4,12 +4,40 @@ import (
 	"bytes"
 	"bytes"
 	"encoding/json"
 	"encoding/json"
 	"io/ioutil"
 	"io/ioutil"
+	"regexp"
+	"strings"
 	"testing"
 	"testing"
 	"time"
 	"time"
 
 
 	"github.com/docker/docker/pkg/timeutils"
 	"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) {
 func BenchmarkWriteLog(b *testing.B) {
 	var buf bytes.Buffer
 	var buf bytes.Buffer
 	e := json.NewEncoder(&buf)
 	e := json.NewEncoder(&buf)