github.com/LazyboyChen7/engine@v17.12.1-ce-rc2+incompatible/daemon/logger/jsonfilelog/jsonfilelog_test.go (about)

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