github.com/olljanat/moby@v1.13.1/pkg/jsonlog/jsonlog_marshalling_test.go (about)

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