github.com/LazyboyChen7/engine@v17.12.1-ce-rc2+incompatible/daemon/logger/jsonfilelog/jsonlog/jsonlogbytes_test.go (about) 1 package jsonlog 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "regexp" 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestJSONLogsMarshalJSONBuf(t *testing.T) { 15 logs := map[*JSONLogs]string{ 16 {Log: []byte(`"A log line with \\"`)}: `^{\"log\":\"\\\"A log line with \\\\\\\\\\\"\",\"time\":`, 17 {Log: []byte("A log line")}: `^{\"log\":\"A log line\",\"time\":`, 18 {Log: []byte("A log line with \r")}: `^{\"log\":\"A log line with \\r\",\"time\":`, 19 {Log: []byte("A log line with & < >")}: `^{\"log\":\"A log line with \\u0026 \\u003c \\u003e\",\"time\":`, 20 {Log: []byte("A log line with utf8 : 🚀 ψ ω β")}: `^{\"log\":\"A log line with utf8 : 🚀 ψ ω β\",\"time\":`, 21 {Stream: "stdout"}: `^{\"stream\":\"stdout\",\"time\":`, 22 {Stream: "stdout", Log: []byte("A log line")}: `^{\"log\":\"A log line\",\"stream\":\"stdout\",\"time\":`, 23 {Created: time.Date(2017, 9, 1, 1, 1, 1, 1, time.UTC)}: `^{\"time\":"2017-09-01T01:01:01.000000001Z"}$`, 24 25 {}: `^{\"time\":"0001-01-01T00:00:00Z"}$`, 26 // These ones are a little weird 27 {Log: []byte("\u2028 \u2029")}: `^{\"log\":\"\\u2028 \\u2029\",\"time\":`, 28 {Log: []byte{0xaF}}: `^{\"log\":\"\\ufffd\",\"time\":`, 29 {Log: []byte{0x7F}}: `^{\"log\":\"\x7f\",\"time\":`, 30 // with raw attributes 31 {Log: []byte("A log line"), RawAttrs: []byte(`{"hello":"world","value":1234}`)}: `^{\"log\":\"A log line\",\"attrs\":{\"hello\":\"world\",\"value\":1234},\"time\":`, 32 } 33 for jsonLog, expression := range logs { 34 var buf bytes.Buffer 35 err := jsonLog.MarshalJSONBuf(&buf) 36 require.NoError(t, err) 37 assert.Regexp(t, regexp.MustCompile(expression), buf.String()) 38 assert.NoError(t, json.Unmarshal(buf.Bytes(), &map[string]interface{}{})) 39 } 40 }