Browse code

Test for jsonlog.WriteLog

Signed-off-by: Alexandr Morozov <lk4d4@docker.com>

Alexandr Morozov authored on 2014/09/23 23:19:06
Showing 1 changed files
... ...
@@ -4,12 +4,40 @@ import (
4 4
 	"bytes"
5 5
 	"encoding/json"
6 6
 	"io/ioutil"
7
+	"regexp"
8
+	"strings"
7 9
 	"testing"
8 10
 	"time"
9 11
 
10 12
 	"github.com/docker/docker/pkg/timeutils"
11 13
 )
12 14
 
15
+func TestWriteLog(t *testing.T) {
16
+	var buf bytes.Buffer
17
+	e := json.NewEncoder(&buf)
18
+	testLine := "Line that thinks that it is log line from docker\n"
19
+	for i := 0; i < 30; i++ {
20
+		e.Encode(JSONLog{Log: testLine, Stream: "stdout", Created: time.Now()})
21
+	}
22
+	w := bytes.NewBuffer(nil)
23
+	format := timeutils.RFC3339NanoFixed
24
+	if err := WriteLog(&buf, w, format); err != nil {
25
+		t.Fatal(err)
26
+	}
27
+	res := w.String()
28
+	t.Logf("Result of WriteLog: %q", res)
29
+	lines := strings.Split(strings.TrimSpace(res), "\n")
30
+	if len(lines) != 30 {
31
+		t.Fatalf("Must be 30 lines but got %d", len(lines))
32
+	}
33
+	logRe := regexp.MustCompile(`\[.*\] Line that thinks that it is log line from docker`)
34
+	for _, l := range lines {
35
+		if !logRe.MatchString(l) {
36
+			t.Fatalf("Log line not in expected format: %q", l)
37
+		}
38
+	}
39
+}
40
+
13 41
 func BenchmarkWriteLog(b *testing.B) {
14 42
 	var buf bytes.Buffer
15 43
 	e := json.NewEncoder(&buf)