daemon/logger/jsonfilelog/jsonlog/jsonlogbytes_test.go
e7a9a0be
 package jsonlog
 
 import (
 	"bytes"
7de92de6
 	"encoding/json"
e7a9a0be
 	"regexp"
 	"testing"
7de92de6
 	"time"
 
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
e7a9a0be
 )
 
5220f3b5
 func TestJSONLogsMarshalJSONBuf(t *testing.T) {
 	logs := map[*JSONLogs]string{
7de92de6
 		{Log: []byte(`"A log line with \\"`)}:                  `^{\"log\":\"\\\"A log line with \\\\\\\\\\\"\",\"time\":`,
 		{Log: []byte("A log line")}:                            `^{\"log\":\"A log line\",\"time\":`,
 		{Log: []byte("A log line with \r")}:                    `^{\"log\":\"A log line with \\r\",\"time\":`,
 		{Log: []byte("A log line with & < >")}:                 `^{\"log\":\"A log line with \\u0026 \\u003c \\u003e\",\"time\":`,
 		{Log: []byte("A log line with utf8 : 🚀 ψ ω β")}:        `^{\"log\":\"A log line with utf8 : 🚀 ψ ω β\",\"time\":`,
 		{Stream: "stdout"}:                                     `^{\"stream\":\"stdout\",\"time\":`,
 		{Stream: "stdout", Log: []byte("A log line")}:          `^{\"log\":\"A log line\",\"stream\":\"stdout\",\"time\":`,
 		{Created: time.Date(2017, 9, 1, 1, 1, 1, 1, time.UTC)}: `^{\"time\":"2017-09-01T01:01:01.000000001Z"}$`,
 
 		{}: `^{\"time\":"0001-01-01T00:00:00Z"}$`,
e7a9a0be
 		// These ones are a little weird
7de92de6
 		{Log: []byte("\u2028 \u2029")}: `^{\"log\":\"\\u2028 \\u2029\",\"time\":`,
 		{Log: []byte{0xaF}}:            `^{\"log\":\"\\ufffd\",\"time\":`,
 		{Log: []byte{0x7F}}:            `^{\"log\":\"\x7f\",\"time\":`,
0083f6e9
 		// with raw attributes
7de92de6
 		{Log: []byte("A log line"), RawAttrs: []byte(`{"hello":"world","value":1234}`)}: `^{\"log\":\"A log line\",\"attrs\":{\"hello\":\"world\",\"value\":1234},\"time\":`,
5f50f4f5
 		// with Tag set
e77267c5
 		{Log: []byte("A log line with tag"), RawAttrs: []byte(`{"hello":"world","value":1234}`)}: `^{\"log\":\"A log line with tag\",\"attrs\":{\"hello\":\"world\",\"value\":1234},\"time\":`,
e7a9a0be
 	}
 	for jsonLog, expression := range logs {
 		var buf bytes.Buffer
7de92de6
 		err := jsonLog.MarshalJSONBuf(&buf)
 		require.NoError(t, err)
 		assert.Regexp(t, regexp.MustCompile(expression), buf.String())
 		assert.NoError(t, json.Unmarshal(buf.Bytes(), &map[string]interface{}{}))
e7a9a0be
 	}
 }