github.com/ld86/docker@v1.7.1-rc3/daemon/logger/jsonfilelog/jsonfilelog_test.go (about)

     1  package jsonfilelog
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/docker/docker/daemon/logger"
    11  	"github.com/docker/docker/pkg/jsonlog"
    12  )
    13  
    14  func TestJSONFileLogger(t *testing.T) {
    15  	cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
    16  	tmp, err := ioutil.TempDir("", "docker-logger-")
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	defer os.RemoveAll(tmp)
    21  	filename := filepath.Join(tmp, "container.log")
    22  	l, err := New(logger.Context{
    23  		ContainerID: cid,
    24  		LogPath:     filename,
    25  	})
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	defer l.Close()
    30  
    31  	if err := l.Log(&logger.Message{ContainerID: cid, Line: []byte("line1"), Source: "src1"}); err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	if err := l.Log(&logger.Message{ContainerID: cid, Line: []byte("line2"), Source: "src2"}); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	if err := l.Log(&logger.Message{ContainerID: cid, Line: []byte("line3"), Source: "src3"}); err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	res, err := ioutil.ReadFile(filename)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	expected := `{"log":"line1\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
    45  {"log":"line2\n","stream":"src2","time":"0001-01-01T00:00:00Z"}
    46  {"log":"line3\n","stream":"src3","time":"0001-01-01T00:00:00Z"}
    47  `
    48  
    49  	if string(res) != expected {
    50  		t.Fatalf("Wrong log content: %q, expected %q", res, expected)
    51  	}
    52  }
    53  
    54  func BenchmarkJSONFileLogger(b *testing.B) {
    55  	cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
    56  	tmp, err := ioutil.TempDir("", "docker-logger-")
    57  	if err != nil {
    58  		b.Fatal(err)
    59  	}
    60  	defer os.RemoveAll(tmp)
    61  	filename := filepath.Join(tmp, "container.log")
    62  	l, err := New(logger.Context{
    63  		ContainerID: cid,
    64  		LogPath:     filename,
    65  	})
    66  	if err != nil {
    67  		b.Fatal(err)
    68  	}
    69  	defer l.Close()
    70  
    71  	testLine := "Line that thinks that it is log line from docker\n"
    72  	msg := &logger.Message{ContainerID: cid, Line: []byte(testLine), Source: "stderr", Timestamp: time.Now().UTC()}
    73  	jsonlog, err := (&jsonlog.JSONLog{Log: string(msg.Line) + "\n", Stream: msg.Source, Created: msg.Timestamp}).MarshalJSON()
    74  	if err != nil {
    75  		b.Fatal(err)
    76  	}
    77  	b.SetBytes(int64(len(jsonlog)+1) * 30)
    78  	b.ResetTimer()
    79  	for i := 0; i < b.N; i++ {
    80  		for j := 0; j < 30; j++ {
    81  			if err := l.Log(msg); err != nil {
    82  				b.Fatal(err)
    83  			}
    84  		}
    85  	}
    86  }