jsonlog_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package jsonlog
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/docker/docker/pkg/timeutils"
  12. )
  13. // Invalid json should return an error
  14. func TestWriteLogWithInvalidJSON(t *testing.T) {
  15. json := strings.NewReader("Invalid json")
  16. w := bytes.NewBuffer(nil)
  17. if err := WriteLog(json, w, "json", time.Time{}); err == nil {
  18. t.Fatalf("Expected an error, got [%v]", w.String())
  19. }
  20. }
  21. // Any format is valid, it will just print it
  22. func TestWriteLogWithInvalidFormat(t *testing.T) {
  23. testLine := "Line that thinks that it is log line from docker\n"
  24. var buf bytes.Buffer
  25. e := json.NewEncoder(&buf)
  26. for i := 0; i < 35; i++ {
  27. e.Encode(JSONLog{Log: testLine, Stream: "stdout", Created: time.Now()})
  28. }
  29. w := bytes.NewBuffer(nil)
  30. if err := WriteLog(&buf, w, "invalid format", time.Time{}); err != nil {
  31. t.Fatal(err)
  32. }
  33. res := w.String()
  34. t.Logf("Result of WriteLog: %q", res)
  35. lines := strings.Split(strings.TrimSpace(res), "\n")
  36. expression := "^invalid format Line that thinks that it is log line from docker$"
  37. logRe := regexp.MustCompile(expression)
  38. expectedLines := 35
  39. if len(lines) != expectedLines {
  40. t.Fatalf("Must be %v lines but got %d", expectedLines, len(lines))
  41. }
  42. for _, l := range lines {
  43. if !logRe.MatchString(l) {
  44. t.Fatalf("Log line not in expected format [%v]: %q", expression, l)
  45. }
  46. }
  47. }
  48. // Having multiple Log/Stream element
  49. func TestWriteLogWithMultipleStreamLog(t *testing.T) {
  50. testLine := "Line that thinks that it is log line from docker\n"
  51. var buf bytes.Buffer
  52. e := json.NewEncoder(&buf)
  53. for i := 0; i < 35; i++ {
  54. e.Encode(JSONLog{Log: testLine, Stream: "stdout", Created: time.Now()})
  55. }
  56. w := bytes.NewBuffer(nil)
  57. if err := WriteLog(&buf, w, "invalid format", time.Time{}); err != nil {
  58. t.Fatal(err)
  59. }
  60. res := w.String()
  61. t.Logf("Result of WriteLog: %q", res)
  62. lines := strings.Split(strings.TrimSpace(res), "\n")
  63. expression := "^invalid format Line that thinks that it is log line from docker$"
  64. logRe := regexp.MustCompile(expression)
  65. expectedLines := 35
  66. if len(lines) != expectedLines {
  67. t.Fatalf("Must be %v lines but got %d", expectedLines, len(lines))
  68. }
  69. for _, l := range lines {
  70. if !logRe.MatchString(l) {
  71. t.Fatalf("Log line not in expected format [%v]: %q", expression, l)
  72. }
  73. }
  74. }
  75. // Write log with since after created, it won't print anything
  76. func TestWriteLogWithDate(t *testing.T) {
  77. created, _ := time.Parse("YYYY-MM-dd", "2015-01-01")
  78. var buf bytes.Buffer
  79. testLine := "Line that thinks that it is log line from docker\n"
  80. jsonLog := JSONLog{Log: testLine, Stream: "stdout", Created: created}
  81. if err := json.NewEncoder(&buf).Encode(jsonLog); err != nil {
  82. t.Fatal(err)
  83. }
  84. w := bytes.NewBuffer(nil)
  85. if err := WriteLog(&buf, w, "json", time.Now()); err != nil {
  86. t.Fatal(err)
  87. }
  88. res := w.String()
  89. if res != "" {
  90. t.Fatalf("Expected empty log, got [%v]", res)
  91. }
  92. }
  93. // Happy path :)
  94. func TestWriteLog(t *testing.T) {
  95. testLine := "Line that thinks that it is log line from docker\n"
  96. format := timeutils.RFC3339NanoFixed
  97. logs := map[string][]string{
  98. "": {"35", "^Line that thinks that it is log line from docker$"},
  99. "json": {"1", `^{\"log\":\"Line that thinks that it is log line from docker\\n\",\"stream\":\"stdout\",\"time\":.{30,}\"}$`},
  100. // 30+ symbols, five more can come from system timezone
  101. format: {"35", `.{30,} Line that thinks that it is log line from docker`},
  102. }
  103. for givenFormat, expressionAndLines := range logs {
  104. expectedLines, _ := strconv.Atoi(expressionAndLines[0])
  105. expression := expressionAndLines[1]
  106. var buf bytes.Buffer
  107. e := json.NewEncoder(&buf)
  108. for i := 0; i < 35; i++ {
  109. e.Encode(JSONLog{Log: testLine, Stream: "stdout", Created: time.Now()})
  110. }
  111. w := bytes.NewBuffer(nil)
  112. if err := WriteLog(&buf, w, givenFormat, time.Time{}); err != nil {
  113. t.Fatal(err)
  114. }
  115. res := w.String()
  116. t.Logf("Result of WriteLog: %q", res)
  117. lines := strings.Split(strings.TrimSpace(res), "\n")
  118. if len(lines) != expectedLines {
  119. t.Fatalf("Must be %v lines but got %d", expectedLines, len(lines))
  120. }
  121. logRe := regexp.MustCompile(expression)
  122. for _, l := range lines {
  123. if !logRe.MatchString(l) {
  124. t.Fatalf("Log line not in expected format [%v]: %q", expression, l)
  125. }
  126. }
  127. }
  128. }
  129. func BenchmarkWriteLog(b *testing.B) {
  130. var buf bytes.Buffer
  131. e := json.NewEncoder(&buf)
  132. testLine := "Line that thinks that it is log line from docker\n"
  133. for i := 0; i < 30; i++ {
  134. e.Encode(JSONLog{Log: testLine, Stream: "stdout", Created: time.Now()})
  135. }
  136. r := bytes.NewReader(buf.Bytes())
  137. w := ioutil.Discard
  138. format := timeutils.RFC3339NanoFixed
  139. b.SetBytes(int64(r.Len()))
  140. b.ResetTimer()
  141. for i := 0; i < b.N; i++ {
  142. if err := WriteLog(r, w, format, time.Time{}); err != nil {
  143. b.Fatal(err)
  144. }
  145. b.StopTimer()
  146. r.Seek(0, 0)
  147. b.StartTimer()
  148. }
  149. }