github.com/glycerine/docker@v1.8.2/pkg/jsonlog/jsonlogbytes_test.go (about) 1 package jsonlog 2 3 import ( 4 "bytes" 5 "regexp" 6 "testing" 7 ) 8 9 func TestJSONLogBytesMarshalJSONBuf(t *testing.T) { 10 logs := map[*JSONLogBytes]string{ 11 &JSONLogBytes{Log: []byte(`"A log line with \\"`)}: `^{\"log\":\"\\\"A log line with \\\\\\\\\\\"\",\"time\":}$`, 12 &JSONLogBytes{Log: []byte("A log line")}: `^{\"log\":\"A log line\",\"time\":}$`, 13 &JSONLogBytes{Log: []byte("A log line with \r")}: `^{\"log\":\"A log line with \\r\",\"time\":}$`, 14 &JSONLogBytes{Log: []byte("A log line with & < >")}: `^{\"log\":\"A log line with \\u0026 \\u003c \\u003e\",\"time\":}$`, 15 &JSONLogBytes{Log: []byte("A log line with utf8 : 🚀 ψ ω β")}: `^{\"log\":\"A log line with utf8 : 🚀 ψ ω β\",\"time\":}$`, 16 &JSONLogBytes{Stream: "stdout"}: `^{\"stream\":\"stdout\",\"time\":}$`, 17 &JSONLogBytes{Stream: "stdout", Log: []byte("A log line")}: `^{\"log\":\"A log line\",\"stream\":\"stdout\",\"time\":}$`, 18 &JSONLogBytes{Created: "time"}: `^{\"time\":time}$`, 19 &JSONLogBytes{}: `^{\"time\":}$`, 20 // These ones are a little weird 21 &JSONLogBytes{Log: []byte("\u2028 \u2029")}: `^{\"log\":\"\\u2028 \\u2029\",\"time\":}$`, 22 &JSONLogBytes{Log: []byte{0xaF}}: `^{\"log\":\"\\ufffd\",\"time\":}$`, 23 &JSONLogBytes{Log: []byte{0x7F}}: `^{\"log\":\"\x7f\",\"time\":}$`, 24 } 25 for jsonLog, expression := range logs { 26 var buf bytes.Buffer 27 if err := jsonLog.MarshalJSONBuf(&buf); err != nil { 28 t.Fatal(err) 29 } 30 res := buf.String() 31 t.Logf("Result of WriteLog: %q", res) 32 logRe := regexp.MustCompile(expression) 33 if !logRe.MatchString(res) { 34 t.Fatalf("Log line not in expected format [%v]: %q", expression, res) 35 } 36 } 37 }