-
Notifications
You must be signed in to change notification settings - Fork 82
/
hook_test.go
144 lines (116 loc) · 2.9 KB
/
hook_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package logrustash
import (
"bytes"
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/sirupsen/logrus"
)
type simpleFmter struct{}
func (f simpleFmter) Format(e *logrus.Entry) ([]byte, error) {
return []byte(fmt.Sprintf("msg: %#v", e.Message)), nil
}
func TestFire(t *testing.T) {
buffer := bytes.NewBuffer(nil)
h := Hook{
writer: buffer,
formatter: simpleFmter{},
}
entry := &logrus.Entry{
Message: "my message",
Data: logrus.Fields{},
}
err := h.Fire(entry)
if err != nil {
t.Error("expected Fire to not return error")
}
expected := "msg: \"my message\""
if buffer.String() != expected {
t.Errorf("expected to see '%s' in '%s'", expected, buffer.String())
}
}
type FailFmt struct{}
func (f FailFmt) Format(e *logrus.Entry) ([]byte, error) {
return nil, errors.New("")
}
func TestFireFormatError(t *testing.T) {
buffer := bytes.NewBuffer(nil)
h := Hook{
writer: buffer,
formatter: FailFmt{},
}
if err := h.Fire(&logrus.Entry{Data: logrus.Fields{}}); err == nil {
t.Error("expected Fire to return error")
}
}
type FailWrite struct{}
func (w FailWrite) Write(d []byte) (int, error) {
return 0, errors.New("")
}
func TestFireWriteError(t *testing.T) {
h := Hook{
writer: FailWrite{},
formatter: &logrus.JSONFormatter{},
}
if err := h.Fire(&logrus.Entry{Data: logrus.Fields{}}); err == nil {
t.Error("expected Fire to return error")
}
}
func TestDefaultFormatterWithFields(t *testing.T) {
format := DefaultFormatter(logrus.Fields{"ID": 123})
entry := &logrus.Entry{
Message: "msg1",
Data: logrus.Fields{"f1": "bla"},
}
res, err := format.Format(entry)
if err != nil {
t.Errorf("expected format to not return error: %s", err)
}
expected := []string{
"f1\":\"bla\"",
"ID\":123",
"message\":\"msg1\"",
}
for _, exp := range expected {
if !strings.Contains(string(res), exp) {
t.Errorf("expected to have '%s' in '%s'", exp, string(res))
}
}
}
func TestDefaultFormatterWithEmptyFields(t *testing.T) {
now := time.Now()
formatter := DefaultFormatter(logrus.Fields{})
entry := &logrus.Entry{
Message: "message bla bla",
Level: logrus.DebugLevel,
Time: now,
Data: logrus.Fields{
"Key1": "Value1",
},
}
res, err := formatter.Format(entry)
if err != nil {
t.Errorf("expected Format not to return error: %s", err)
}
expected := []string{
"\"message\":\"message bla bla\"",
"\"level\":\"debug\"",
"\"Key1\":\"Value1\"",
"\"@version\":\"1\"",
"\"type\":\"log\"",
fmt.Sprintf("\"@timestamp\":\"%s\"", now.Format(time.RFC3339Nano)),
}
for _, exp := range expected {
if !strings.Contains(string(res), exp) {
t.Errorf("expected to have '%s' in '%s'", exp, string(res))
}
}
}
func TestLogstashFieldsNotOverridden(t *testing.T) {
_ = DefaultFormatter(logrus.Fields{"user1": "11"})
if _, ok := logstashFields["user1"]; ok {
t.Errorf("expected user1 to not be in logstashFields: %#v", logstashFields)
}
}