github.com/pmorton/docker@v1.5.0/pkg/jsonlog/jsonlog_test.go (about) 1 package jsonlog 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "io/ioutil" 7 "regexp" 8 "strings" 9 "testing" 10 "time" 11 12 "github.com/docker/docker/pkg/timeutils" 13 ) 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 41 func BenchmarkWriteLog(b *testing.B) { 42 var buf bytes.Buffer 43 e := json.NewEncoder(&buf) 44 testLine := "Line that thinks that it is log line from docker\n" 45 for i := 0; i < 30; i++ { 46 e.Encode(JSONLog{Log: testLine, Stream: "stdout", Created: time.Now()}) 47 } 48 r := bytes.NewReader(buf.Bytes()) 49 w := ioutil.Discard 50 format := timeutils.RFC3339NanoFixed 51 b.SetBytes(int64(r.Len())) 52 b.ResetTimer() 53 for i := 0; i < b.N; i++ { 54 if err := WriteLog(r, w, format); err != nil { 55 b.Fatal(err) 56 } 57 b.StopTimer() 58 r.Seek(0, 0) 59 b.StartTimer() 60 } 61 }