gopkg.in/dotcloud/docker.v1@v1.13.1/daemon/logger/jsonfilelog/jsonfilelog_test.go (about)

     1  package jsonfilelog
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"reflect"
     9  	"strconv"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/docker/docker/daemon/logger"
    14  	"github.com/docker/docker/pkg/jsonlog"
    15  )
    16  
    17  func TestJSONFileLogger(t *testing.T) {
    18  	cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
    19  	tmp, err := ioutil.TempDir("", "docker-logger-")
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	defer os.RemoveAll(tmp)
    24  	filename := filepath.Join(tmp, "container.log")
    25  	l, err := New(logger.Context{
    26  		ContainerID: cid,
    27  		LogPath:     filename,
    28  	})
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	defer l.Close()
    33  
    34  	if err := l.Log(&logger.Message{Line: []byte("line1"), Source: "src1"}); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	if err := l.Log(&logger.Message{Line: []byte("line2"), Source: "src2"}); err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	if err := l.Log(&logger.Message{Line: []byte("line3"), Source: "src3"}); err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	res, err := ioutil.ReadFile(filename)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	expected := `{"log":"line1\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
    48  {"log":"line2\n","stream":"src2","time":"0001-01-01T00:00:00Z"}
    49  {"log":"line3\n","stream":"src3","time":"0001-01-01T00:00:00Z"}
    50  `
    51  
    52  	if string(res) != expected {
    53  		t.Fatalf("Wrong log content: %q, expected %q", res, expected)
    54  	}
    55  }
    56  
    57  func BenchmarkJSONFileLogger(b *testing.B) {
    58  	cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
    59  	tmp, err := ioutil.TempDir("", "docker-logger-")
    60  	if err != nil {
    61  		b.Fatal(err)
    62  	}
    63  	defer os.RemoveAll(tmp)
    64  	filename := filepath.Join(tmp, "container.log")
    65  	l, err := New(logger.Context{
    66  		ContainerID: cid,
    67  		LogPath:     filename,
    68  	})
    69  	if err != nil {
    70  		b.Fatal(err)
    71  	}
    72  	defer l.Close()
    73  
    74  	testLine := "Line that thinks that it is log line from docker\n"
    75  	msg := &logger.Message{Line: []byte(testLine), Source: "stderr", Timestamp: time.Now().UTC()}
    76  	jsonlog, err := (&jsonlog.JSONLog{Log: string(msg.Line) + "\n", Stream: msg.Source, Created: msg.Timestamp}).MarshalJSON()
    77  	if err != nil {
    78  		b.Fatal(err)
    79  	}
    80  	b.SetBytes(int64(len(jsonlog)+1) * 30)
    81  	b.ResetTimer()
    82  	for i := 0; i < b.N; i++ {
    83  		for j := 0; j < 30; j++ {
    84  			if err := l.Log(msg); err != nil {
    85  				b.Fatal(err)
    86  			}
    87  		}
    88  	}
    89  }
    90  
    91  func TestJSONFileLoggerWithOpts(t *testing.T) {
    92  	cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
    93  	tmp, err := ioutil.TempDir("", "docker-logger-")
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	defer os.RemoveAll(tmp)
    98  	filename := filepath.Join(tmp, "container.log")
    99  	config := map[string]string{"max-file": "2", "max-size": "1k"}
   100  	l, err := New(logger.Context{
   101  		ContainerID: cid,
   102  		LogPath:     filename,
   103  		Config:      config,
   104  	})
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	defer l.Close()
   109  	for i := 0; i < 20; i++ {
   110  		if err := l.Log(&logger.Message{Line: []byte("line" + strconv.Itoa(i)), Source: "src1"}); err != nil {
   111  			t.Fatal(err)
   112  		}
   113  	}
   114  	res, err := ioutil.ReadFile(filename)
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  	penUlt, err := ioutil.ReadFile(filename + ".1")
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  
   123  	expectedPenultimate := `{"log":"line0\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   124  {"log":"line1\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   125  {"log":"line2\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   126  {"log":"line3\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   127  {"log":"line4\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   128  {"log":"line5\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   129  {"log":"line6\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   130  {"log":"line7\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   131  {"log":"line8\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   132  {"log":"line9\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   133  {"log":"line10\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   134  {"log":"line11\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   135  {"log":"line12\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   136  {"log":"line13\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   137  {"log":"line14\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   138  {"log":"line15\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   139  `
   140  	expected := `{"log":"line16\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   141  {"log":"line17\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   142  {"log":"line18\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   143  {"log":"line19\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
   144  `
   145  
   146  	if string(res) != expected {
   147  		t.Fatalf("Wrong log content: %q, expected %q", res, expected)
   148  	}
   149  	if string(penUlt) != expectedPenultimate {
   150  		t.Fatalf("Wrong log content: %q, expected %q", penUlt, expectedPenultimate)
   151  	}
   152  
   153  }
   154  
   155  func TestJSONFileLoggerWithLabelsEnv(t *testing.T) {
   156  	cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
   157  	tmp, err := ioutil.TempDir("", "docker-logger-")
   158  	if err != nil {
   159  		t.Fatal(err)
   160  	}
   161  	defer os.RemoveAll(tmp)
   162  	filename := filepath.Join(tmp, "container.log")
   163  	config := map[string]string{"labels": "rack,dc", "env": "environ,debug,ssl"}
   164  	l, err := New(logger.Context{
   165  		ContainerID:     cid,
   166  		LogPath:         filename,
   167  		Config:          config,
   168  		ContainerLabels: map[string]string{"rack": "101", "dc": "lhr"},
   169  		ContainerEnv:    []string{"environ=production", "debug=false", "port=10001", "ssl=true"},
   170  	})
   171  	if err != nil {
   172  		t.Fatal(err)
   173  	}
   174  	defer l.Close()
   175  	if err := l.Log(&logger.Message{Line: []byte("line"), Source: "src1"}); err != nil {
   176  		t.Fatal(err)
   177  	}
   178  	res, err := ioutil.ReadFile(filename)
   179  	if err != nil {
   180  		t.Fatal(err)
   181  	}
   182  
   183  	var jsonLog jsonlog.JSONLogs
   184  	if err := json.Unmarshal(res, &jsonLog); err != nil {
   185  		t.Fatal(err)
   186  	}
   187  	extra := make(map[string]string)
   188  	if err := json.Unmarshal(jsonLog.RawAttrs, &extra); err != nil {
   189  		t.Fatal(err)
   190  	}
   191  	expected := map[string]string{
   192  		"rack":    "101",
   193  		"dc":      "lhr",
   194  		"environ": "production",
   195  		"debug":   "false",
   196  		"ssl":     "true",
   197  	}
   198  	if !reflect.DeepEqual(extra, expected) {
   199  		t.Fatalf("Wrong log attrs: %q, expected %q", extra, expected)
   200  	}
   201  }
   202  
   203  func BenchmarkJSONFileLoggerWithReader(b *testing.B) {
   204  	b.StopTimer()
   205  	b.ResetTimer()
   206  	cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
   207  	dir, err := ioutil.TempDir("", "json-logger-bench")
   208  	if err != nil {
   209  		b.Fatal(err)
   210  	}
   211  	defer os.RemoveAll(dir)
   212  
   213  	l, err := New(logger.Context{
   214  		ContainerID: cid,
   215  		LogPath:     filepath.Join(dir, "container.log"),
   216  	})
   217  	if err != nil {
   218  		b.Fatal(err)
   219  	}
   220  	defer l.Close()
   221  	msg := &logger.Message{Line: []byte("line"), Source: "src1"}
   222  	jsonlog, err := (&jsonlog.JSONLog{Log: string(msg.Line) + "\n", Stream: msg.Source, Created: msg.Timestamp}).MarshalJSON()
   223  	if err != nil {
   224  		b.Fatal(err)
   225  	}
   226  	b.SetBytes(int64(len(jsonlog)+1) * 30)
   227  
   228  	b.StartTimer()
   229  
   230  	go func() {
   231  		for i := 0; i < b.N; i++ {
   232  			for j := 0; j < 30; j++ {
   233  				l.Log(msg)
   234  			}
   235  		}
   236  		l.Close()
   237  	}()
   238  
   239  	lw := l.(logger.LogReader).ReadLogs(logger.ReadConfig{Follow: true})
   240  	watchClose := lw.WatchClose()
   241  	for {
   242  		select {
   243  		case <-lw.Msg:
   244  		case <-watchClose:
   245  			return
   246  		}
   247  	}
   248  }