jsonfilelog_test.go 4.6 KB

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