github.com/mssola/docker@v1.8.1/daemon/logger/jsonfilelog/jsonfilelog_test.go (about) 1 package jsonfilelog 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strconv" 8 "testing" 9 "time" 10 11 "github.com/docker/docker/daemon/logger" 12 "github.com/docker/docker/pkg/jsonlog" 13 ) 14 15 func TestJSONFileLogger(t *testing.T) { 16 cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657" 17 tmp, err := ioutil.TempDir("", "docker-logger-") 18 if err != nil { 19 t.Fatal(err) 20 } 21 defer os.RemoveAll(tmp) 22 filename := filepath.Join(tmp, "container.log") 23 l, err := New(logger.Context{ 24 ContainerID: cid, 25 LogPath: filename, 26 }) 27 if err != nil { 28 t.Fatal(err) 29 } 30 defer l.Close() 31 32 if err := l.Log(&logger.Message{ContainerID: cid, Line: []byte("line1"), Source: "src1"}); err != nil { 33 t.Fatal(err) 34 } 35 if err := l.Log(&logger.Message{ContainerID: cid, Line: []byte("line2"), Source: "src2"}); err != nil { 36 t.Fatal(err) 37 } 38 if err := l.Log(&logger.Message{ContainerID: cid, Line: []byte("line3"), Source: "src3"}); err != nil { 39 t.Fatal(err) 40 } 41 res, err := ioutil.ReadFile(filename) 42 if err != nil { 43 t.Fatal(err) 44 } 45 expected := `{"log":"line1\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 46 {"log":"line2\n","stream":"src2","time":"0001-01-01T00:00:00Z"} 47 {"log":"line3\n","stream":"src3","time":"0001-01-01T00:00:00Z"} 48 ` 49 50 if string(res) != expected { 51 t.Fatalf("Wrong log content: %q, expected %q", res, expected) 52 } 53 } 54 55 func BenchmarkJSONFileLogger(b *testing.B) { 56 cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657" 57 tmp, err := ioutil.TempDir("", "docker-logger-") 58 if err != nil { 59 b.Fatal(err) 60 } 61 defer os.RemoveAll(tmp) 62 filename := filepath.Join(tmp, "container.log") 63 l, err := New(logger.Context{ 64 ContainerID: cid, 65 LogPath: filename, 66 }) 67 if err != nil { 68 b.Fatal(err) 69 } 70 defer l.Close() 71 72 testLine := "Line that thinks that it is log line from docker\n" 73 msg := &logger.Message{ContainerID: cid, Line: []byte(testLine), Source: "stderr", Timestamp: time.Now().UTC()} 74 jsonlog, err := (&jsonlog.JSONLog{Log: string(msg.Line) + "\n", Stream: msg.Source, Created: msg.Timestamp}).MarshalJSON() 75 if err != nil { 76 b.Fatal(err) 77 } 78 b.SetBytes(int64(len(jsonlog)+1) * 30) 79 b.ResetTimer() 80 for i := 0; i < b.N; i++ { 81 for j := 0; j < 30; j++ { 82 if err := l.Log(msg); err != nil { 83 b.Fatal(err) 84 } 85 } 86 } 87 } 88 89 func TestJSONFileLoggerWithOpts(t *testing.T) { 90 cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657" 91 tmp, err := ioutil.TempDir("", "docker-logger-") 92 if err != nil { 93 t.Fatal(err) 94 } 95 defer os.RemoveAll(tmp) 96 filename := filepath.Join(tmp, "container.log") 97 config := map[string]string{"max-file": "2", "max-size": "1k"} 98 l, err := New(logger.Context{ 99 ContainerID: cid, 100 LogPath: filename, 101 Config: config, 102 }) 103 if err != nil { 104 t.Fatal(err) 105 } 106 defer l.Close() 107 for i := 0; i < 20; i++ { 108 if err := l.Log(&logger.Message{ContainerID: cid, Line: []byte("line" + strconv.Itoa(i)), Source: "src1"}); err != nil { 109 t.Fatal(err) 110 } 111 } 112 res, err := ioutil.ReadFile(filename) 113 if err != nil { 114 t.Fatal(err) 115 } 116 penUlt, err := ioutil.ReadFile(filename + ".1") 117 if err != nil { 118 t.Fatal(err) 119 } 120 121 expectedPenultimate := `{"log":"line0\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 122 {"log":"line1\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 123 {"log":"line2\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 124 {"log":"line3\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 125 {"log":"line4\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 126 {"log":"line5\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 127 {"log":"line6\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 128 {"log":"line7\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 129 {"log":"line8\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 130 {"log":"line9\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 131 {"log":"line10\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 132 {"log":"line11\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 133 {"log":"line12\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 134 {"log":"line13\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 135 {"log":"line14\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 136 {"log":"line15\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 137 ` 138 expected := `{"log":"line16\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 139 {"log":"line17\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 140 {"log":"line18\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 141 {"log":"line19\n","stream":"src1","time":"0001-01-01T00:00:00Z"} 142 ` 143 144 if string(res) != expected { 145 t.Fatalf("Wrong log content: %q, expected %q", res, expected) 146 } 147 if string(penUlt) != expectedPenultimate { 148 t.Fatalf("Wrong log content: %q, expected %q", penUlt, expectedPenultimate) 149 } 150 151 }