github.com/chenchun/docker@v1.3.2-0.20150629222414-20467faf132b/pkg/jsonlog/jsonlog_test.go (about)

     1  package jsonlog
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"regexp"
     8  	"strconv"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/docker/docker/pkg/timeutils"
    14  )
    15  
    16  // Invalid json should return an error
    17  func TestWriteLogWithInvalidJSON(t *testing.T) {
    18  	json := strings.NewReader("Invalid json")
    19  	w := bytes.NewBuffer(nil)
    20  	if err := WriteLog(json, w, "json", time.Time{}); err == nil {
    21  		t.Fatalf("Expected an error, got [%v]", w.String())
    22  	}
    23  }
    24  
    25  // Any format is valid, it will just print it
    26  func TestWriteLogWithInvalidFormat(t *testing.T) {
    27  	testLine := "Line that thinks that it is log line from docker\n"
    28  	var buf bytes.Buffer
    29  	e := json.NewEncoder(&buf)
    30  	for i := 0; i < 35; i++ {
    31  		e.Encode(JSONLog{Log: testLine, Stream: "stdout", Created: time.Now()})
    32  	}
    33  	w := bytes.NewBuffer(nil)
    34  	if err := WriteLog(&buf, w, "invalid format", time.Time{}); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	res := w.String()
    38  	t.Logf("Result of WriteLog: %q", res)
    39  	lines := strings.Split(strings.TrimSpace(res), "\n")
    40  	expression := "^invalid format Line that thinks that it is log line from docker$"
    41  	logRe := regexp.MustCompile(expression)
    42  	expectedLines := 35
    43  	if len(lines) != expectedLines {
    44  		t.Fatalf("Must be %v lines but got %d", expectedLines, len(lines))
    45  	}
    46  	for _, l := range lines {
    47  		if !logRe.MatchString(l) {
    48  			t.Fatalf("Log line not in expected format [%v]: %q", expression, l)
    49  		}
    50  	}
    51  }
    52  
    53  // Having multiple Log/Stream element
    54  func TestWriteLogWithMultipleStreamLog(t *testing.T) {
    55  	testLine := "Line that thinks that it is log line from docker\n"
    56  	var buf bytes.Buffer
    57  	e := json.NewEncoder(&buf)
    58  	for i := 0; i < 35; i++ {
    59  		e.Encode(JSONLog{Log: testLine, Stream: "stdout", Created: time.Now()})
    60  	}
    61  	w := bytes.NewBuffer(nil)
    62  	if err := WriteLog(&buf, w, "invalid format", time.Time{}); err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	res := w.String()
    66  	t.Logf("Result of WriteLog: %q", res)
    67  	lines := strings.Split(strings.TrimSpace(res), "\n")
    68  	expression := "^invalid format Line that thinks that it is log line from docker$"
    69  	logRe := regexp.MustCompile(expression)
    70  	expectedLines := 35
    71  	if len(lines) != expectedLines {
    72  		t.Fatalf("Must be %v lines but got %d", expectedLines, len(lines))
    73  	}
    74  	for _, l := range lines {
    75  		if !logRe.MatchString(l) {
    76  			t.Fatalf("Log line not in expected format [%v]: %q", expression, l)
    77  		}
    78  	}
    79  }
    80  
    81  // Write log with since after created, it won't print anything
    82  func TestWriteLogWithDate(t *testing.T) {
    83  	created, _ := time.Parse("YYYY-MM-dd", "2015-01-01")
    84  	var buf bytes.Buffer
    85  	testLine := "Line that thinks that it is log line from docker\n"
    86  	jsonLog := JSONLog{Log: testLine, Stream: "stdout", Created: created}
    87  	if err := json.NewEncoder(&buf).Encode(jsonLog); err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	w := bytes.NewBuffer(nil)
    91  	if err := WriteLog(&buf, w, "json", time.Now()); err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	res := w.String()
    95  	if res != "" {
    96  		t.Fatalf("Expected empty log, got [%v]", res)
    97  	}
    98  }
    99  
   100  // Happy path :)
   101  func TestWriteLog(t *testing.T) {
   102  	testLine := "Line that thinks that it is log line from docker\n"
   103  	format := timeutils.RFC3339NanoFixed
   104  	logs := map[string][]string{
   105  		"":     {"35", "^Line that thinks that it is log line from docker$"},
   106  		"json": {"1", `^{\"log\":\"Line that thinks that it is log line from docker\\n\",\"stream\":\"stdout\",\"time\":.{30,}\"}$`},
   107  		// 30+ symbols, five more can come from system timezone
   108  		format: {"35", `.{30,} Line that thinks that it is log line from docker`},
   109  	}
   110  	for givenFormat, expressionAndLines := range logs {
   111  		expectedLines, _ := strconv.Atoi(expressionAndLines[0])
   112  		expression := expressionAndLines[1]
   113  		var buf bytes.Buffer
   114  		e := json.NewEncoder(&buf)
   115  		for i := 0; i < 35; i++ {
   116  			e.Encode(JSONLog{Log: testLine, Stream: "stdout", Created: time.Now()})
   117  		}
   118  		w := bytes.NewBuffer(nil)
   119  		if err := WriteLog(&buf, w, givenFormat, time.Time{}); err != nil {
   120  			t.Fatal(err)
   121  		}
   122  		res := w.String()
   123  		t.Logf("Result of WriteLog: %q", res)
   124  		lines := strings.Split(strings.TrimSpace(res), "\n")
   125  		if len(lines) != expectedLines {
   126  			t.Fatalf("Must be %v lines but got %d", expectedLines, len(lines))
   127  		}
   128  		logRe := regexp.MustCompile(expression)
   129  		for _, l := range lines {
   130  			if !logRe.MatchString(l) {
   131  				t.Fatalf("Log line not in expected format [%v]: %q", expression, l)
   132  			}
   133  		}
   134  	}
   135  }
   136  
   137  func BenchmarkWriteLog(b *testing.B) {
   138  	var buf bytes.Buffer
   139  	e := json.NewEncoder(&buf)
   140  	testLine := "Line that thinks that it is log line from docker\n"
   141  	for i := 0; i < 30; i++ {
   142  		e.Encode(JSONLog{Log: testLine, Stream: "stdout", Created: time.Now()})
   143  	}
   144  	r := bytes.NewReader(buf.Bytes())
   145  	w := ioutil.Discard
   146  	format := timeutils.RFC3339NanoFixed
   147  	b.SetBytes(int64(r.Len()))
   148  	b.ResetTimer()
   149  	for i := 0; i < b.N; i++ {
   150  		if err := WriteLog(r, w, format, time.Time{}); err != nil {
   151  			b.Fatal(err)
   152  		}
   153  		b.StopTimer()
   154  		r.Seek(0, 0)
   155  		b.StartTimer()
   156  	}
   157  }