jsonfilelog_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package jsonfilelog // import "github.com/docker/docker/daemon/logger/jsonfilelog"
  2. import (
  3. "bytes"
  4. "compress/gzip"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "reflect"
  11. "strconv"
  12. "testing"
  13. "time"
  14. "github.com/docker/docker/daemon/logger"
  15. "github.com/docker/docker/daemon/logger/jsonfilelog/jsonlog"
  16. "gotest.tools/assert"
  17. is "gotest.tools/assert/cmp"
  18. "gotest.tools/fs"
  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. if err := l.Log(&logger.Message{Line: []byte("line1"), Source: "src1"}); err != nil {
  37. t.Fatal(err)
  38. }
  39. if err := l.Log(&logger.Message{Line: []byte("line2"), Source: "src2"}); err != nil {
  40. t.Fatal(err)
  41. }
  42. if err := l.Log(&logger.Message{Line: []byte("line3"), Source: "src3"}); err != nil {
  43. t.Fatal(err)
  44. }
  45. res, err := ioutil.ReadFile(filename)
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. expected := `{"log":"line1\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  50. {"log":"line2\n","stream":"src2","time":"0001-01-01T00:00:00Z"}
  51. {"log":"line3\n","stream":"src3","time":"0001-01-01T00:00:00Z"}
  52. `
  53. if string(res) != expected {
  54. t.Fatalf("Wrong log content: %q, expected %q", res, expected)
  55. }
  56. }
  57. func TestJSONFileLoggerWithTags(t *testing.T) {
  58. cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
  59. cname := "test-container"
  60. tmp, err := ioutil.TempDir("", "docker-logger-")
  61. assert.NilError(t, err)
  62. defer os.RemoveAll(tmp)
  63. filename := filepath.Join(tmp, "container.log")
  64. l, err := New(logger.Info{
  65. Config: map[string]string{
  66. "tag": "{{.ID}}/{{.Name}}", // first 12 characters of ContainerID and full ContainerName
  67. },
  68. ContainerID: cid,
  69. ContainerName: cname,
  70. LogPath: filename,
  71. })
  72. assert.NilError(t, err)
  73. defer l.Close()
  74. err = l.Log(&logger.Message{Line: []byte("line1"), Source: "src1"})
  75. assert.NilError(t, err)
  76. err = l.Log(&logger.Message{Line: []byte("line2"), Source: "src2"})
  77. assert.NilError(t, err)
  78. err = l.Log(&logger.Message{Line: []byte("line3"), Source: "src3"})
  79. assert.NilError(t, err)
  80. res, err := ioutil.ReadFile(filename)
  81. assert.NilError(t, err)
  82. expected := `{"log":"line1\n","stream":"src1","attrs":{"tag":"a7317399f3f8/test-container"},"time":"0001-01-01T00:00:00Z"}
  83. {"log":"line2\n","stream":"src2","attrs":{"tag":"a7317399f3f8/test-container"},"time":"0001-01-01T00:00:00Z"}
  84. {"log":"line3\n","stream":"src3","attrs":{"tag":"a7317399f3f8/test-container"},"time":"0001-01-01T00:00:00Z"}
  85. `
  86. assert.Check(t, is.Equal(expected, string(res)))
  87. }
  88. func BenchmarkJSONFileLoggerLog(b *testing.B) {
  89. tmp := fs.NewDir(b, "bench-jsonfilelog")
  90. defer tmp.Remove()
  91. jsonlogger, err := New(logger.Info{
  92. ContainerID: "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657",
  93. LogPath: tmp.Join("container.log"),
  94. Config: map[string]string{
  95. "labels": "first,second",
  96. "max-file": "10",
  97. "compress": "true",
  98. "max-size": "20m",
  99. },
  100. ContainerLabels: map[string]string{
  101. "first": "label_value",
  102. "second": "label_foo",
  103. },
  104. })
  105. assert.NilError(b, err)
  106. defer jsonlogger.Close()
  107. t := time.Now().UTC()
  108. for _, data := range [][]byte{
  109. []byte(""),
  110. []byte("a short string"),
  111. bytes.Repeat([]byte("a long string"), 100),
  112. bytes.Repeat([]byte("a really long string"), 10000),
  113. } {
  114. b.Run(fmt.Sprintf("%d", len(data)), func(b *testing.B) {
  115. testMsg := &logger.Message{
  116. Line: data,
  117. Source: "stderr",
  118. Timestamp: t,
  119. }
  120. buf := bytes.NewBuffer(nil)
  121. assert.NilError(b, marshalMessage(testMsg, nil, buf))
  122. b.SetBytes(int64(buf.Len()))
  123. b.ResetTimer()
  124. for i := 0; i < b.N; i++ {
  125. msg := logger.NewMessage()
  126. msg.Line = testMsg.Line
  127. msg.Timestamp = testMsg.Timestamp
  128. msg.Source = testMsg.Source
  129. if err := jsonlogger.Log(msg); err != nil {
  130. b.Fatal(err)
  131. }
  132. }
  133. })
  134. }
  135. }
  136. func TestJSONFileLoggerWithOpts(t *testing.T) {
  137. cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
  138. tmp, err := ioutil.TempDir("", "docker-logger-")
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. defer os.RemoveAll(tmp)
  143. filename := filepath.Join(tmp, "container.log")
  144. config := map[string]string{"max-file": "3", "max-size": "1k", "compress": "true"}
  145. l, err := New(logger.Info{
  146. ContainerID: cid,
  147. LogPath: filename,
  148. Config: config,
  149. })
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. defer l.Close()
  154. for i := 0; i < 36; i++ {
  155. if err := l.Log(&logger.Message{Line: []byte("line" + strconv.Itoa(i)), Source: "src1"}); err != nil {
  156. t.Fatal(err)
  157. }
  158. }
  159. res, err := ioutil.ReadFile(filename)
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. penUlt, err := ioutil.ReadFile(filename + ".1")
  164. if err != nil {
  165. if !os.IsNotExist(err) {
  166. t.Fatal(err)
  167. }
  168. file, err := os.Open(filename + ".1.gz")
  169. defer file.Close()
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. zipReader, err := gzip.NewReader(file)
  174. defer zipReader.Close()
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. penUlt, err = ioutil.ReadAll(zipReader)
  179. if err != nil {
  180. t.Fatal(err)
  181. }
  182. }
  183. file, err := os.Open(filename + ".2.gz")
  184. defer file.Close()
  185. if err != nil {
  186. t.Fatal(err)
  187. }
  188. zipReader, err := gzip.NewReader(file)
  189. defer zipReader.Close()
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. antepenult, err := ioutil.ReadAll(zipReader)
  194. if err != nil {
  195. t.Fatal(err)
  196. }
  197. expectedAntepenultimate := `{"log":"line0\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  198. {"log":"line1\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  199. {"log":"line2\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  200. {"log":"line3\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  201. {"log":"line4\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  202. {"log":"line5\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  203. {"log":"line6\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  204. {"log":"line7\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  205. {"log":"line8\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  206. {"log":"line9\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  207. {"log":"line10\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  208. {"log":"line11\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  209. {"log":"line12\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  210. {"log":"line13\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  211. {"log":"line14\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  212. {"log":"line15\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  213. `
  214. expectedPenultimate := `{"log":"line16\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  215. {"log":"line17\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  216. {"log":"line18\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  217. {"log":"line19\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  218. {"log":"line20\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  219. {"log":"line21\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  220. {"log":"line22\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  221. {"log":"line23\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  222. {"log":"line24\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  223. {"log":"line25\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  224. {"log":"line26\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  225. {"log":"line27\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  226. {"log":"line28\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  227. {"log":"line29\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  228. {"log":"line30\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  229. {"log":"line31\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  230. `
  231. expected := `{"log":"line32\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  232. {"log":"line33\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  233. {"log":"line34\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  234. {"log":"line35\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
  235. `
  236. if string(res) != expected {
  237. t.Fatalf("Wrong log content: %q, expected %q", res, expected)
  238. }
  239. if string(penUlt) != expectedPenultimate {
  240. t.Fatalf("Wrong log content: %q, expected %q", penUlt, expectedPenultimate)
  241. }
  242. if string(antepenult) != expectedAntepenultimate {
  243. t.Fatalf("Wrong log content: %q, expected %q", antepenult, expectedAntepenultimate)
  244. }
  245. }
  246. func TestJSONFileLoggerWithLabelsEnv(t *testing.T) {
  247. cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
  248. tmp, err := ioutil.TempDir("", "docker-logger-")
  249. if err != nil {
  250. t.Fatal(err)
  251. }
  252. defer os.RemoveAll(tmp)
  253. filename := filepath.Join(tmp, "container.log")
  254. config := map[string]string{"labels": "rack,dc", "env": "environ,debug,ssl", "env-regex": "^dc"}
  255. l, err := New(logger.Info{
  256. ContainerID: cid,
  257. LogPath: filename,
  258. Config: config,
  259. ContainerLabels: map[string]string{"rack": "101", "dc": "lhr"},
  260. ContainerEnv: []string{"environ=production", "debug=false", "port=10001", "ssl=true", "dc_region=west"},
  261. })
  262. if err != nil {
  263. t.Fatal(err)
  264. }
  265. defer l.Close()
  266. if err := l.Log(&logger.Message{Line: []byte("line"), Source: "src1"}); err != nil {
  267. t.Fatal(err)
  268. }
  269. res, err := ioutil.ReadFile(filename)
  270. if err != nil {
  271. t.Fatal(err)
  272. }
  273. var jsonLog jsonlog.JSONLogs
  274. if err := json.Unmarshal(res, &jsonLog); err != nil {
  275. t.Fatal(err)
  276. }
  277. extra := make(map[string]string)
  278. if err := json.Unmarshal(jsonLog.RawAttrs, &extra); err != nil {
  279. t.Fatal(err)
  280. }
  281. expected := map[string]string{
  282. "rack": "101",
  283. "dc": "lhr",
  284. "environ": "production",
  285. "debug": "false",
  286. "ssl": "true",
  287. "dc_region": "west",
  288. }
  289. if !reflect.DeepEqual(extra, expected) {
  290. t.Fatalf("Wrong log attrs: %q, expected %q", extra, expected)
  291. }
  292. }