-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook_test.go
220 lines (191 loc) · 5.86 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package logrus_rollbar
import (
"net/http"
"runtime"
"strings"
"sync"
"testing"
"github.com/rollbar/rollbar-go"
"github.com/sirupsen/logrus"
)
type mockSender struct {
sync.Mutex
calls []senderParams
}
type senderParams struct {
severity string
req *http.Request
error error
fields map[string]interface{}
}
func (s *mockSender) RequestError(severity string, req *http.Request, err error, fields map[string]interface{}) {
s.Lock()
defer s.Unlock()
s.calls = append(s.calls, senderParams{severity: severity, req: req, error: err, fields: fields})
}
func (s *mockSender) Error(severity string, err error, fields map[string]interface{}) {
s.Lock()
defer s.Unlock()
s.calls = append(s.calls, senderParams{severity: severity, error: err, fields: fields})
}
func TestHook_Fire(t *testing.T) {
sender := &mockSender{}
hook := hook{Sender: sender}
logger := logrus.New()
entry := logrus.NewEntry(logger)
entry.Message = "line of log"
err := hook.Fire(entry)
if err != nil {
t.Errorf("expected nil error, got %v", err)
}
if len(sender.calls) != 1 {
t.Errorf("expected 1 call, got %v", len(sender.calls))
}
params := sender.calls[0]
if params.severity != rollbar.ERR {
t.Errorf("expected severity error, got %v", params.severity)
}
if params.error.Error() != "line of log" {
t.Errorf("expected '%v' error, got '%v'", entry.Message, params.error.Error())
}
}
func TestHook_FireWithReq(t *testing.T) {
sender := &mockSender{}
hook := hook{Sender: sender}
logger := logrus.New()
entry := logrus.NewEntry(logger)
entry.Data["msg"] = "line of log"
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add("Authorization", "password")
entry.Data["req"] = req
err := hook.Fire(entry)
if err != nil {
t.Errorf("expected nil error, got '%v'", err)
}
params := sender.calls[0]
if params.req.Header.Get("Authorization") != "" {
t.Errorf("expected Authorization header to be cleared, got %v", params.req.Header.Get("Authorization"))
}
}
func TestHook_WithExtraField(t *testing.T) {
sender := &mockSender{}
hook := hook{Sender: sender}
logger := logrus.New()
entry := logrus.NewEntry(logger)
entry.Data["msg"] = "line of log"
entry.Data["extra"] = "extrafield"
err := hook.Fire(entry)
if err != nil {
t.Errorf("expected nil error, got '%v'", err)
}
params := sender.calls[0]
if len(params.fields) != 1 {
t.Errorf("expected 1 extra field, got %v", len(params.fields))
}
extra, ok := params.fields["extra"]
if !ok {
t.Errorf("expected extra field string, got %t", params.fields["extra"])
}
if extra != "extrafield" {
t.Errorf("expected 'extrafield' value for extra field, got %v", extra)
}
}
// Test when a request object is used multiple times in error handling (example multiple goroutine)
// This test case aims at testing race conditions
func TestHook_FireWithReq_Concurrent(t *testing.T) {
sender := &mockSender{}
hook := hook{Sender: sender}
logger := logrus.New()
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add("Authorization", "password")
// Entries are only created and sent once, it is not safe to send twice the
// same entry through the hook
wg := &sync.WaitGroup{}
wg.Add(2)
go func() {
entry := logrus.NewEntry(logger)
entry.Data["msg"] = "line of log"
entry.Data["req"] = req
hook.Fire(entry)
wg.Done()
}()
go func() {
entry := logrus.NewEntry(logger)
entry.Data["msg"] = "line of log"
entry.Data["req"] = req
hook.Fire(entry)
wg.Done()
}()
wg.Wait()
if len(sender.calls) != 2 {
t.Errorf("expected 2 call, got %v", len(sender.calls))
}
}
// Test that the stack is correctly sent when an errors from github.com/pkg/errors is sent
func TestHook_WithPkgErrors(t *testing.T) {
sender := &mockSender{}
hook := hook{Sender: sender}
logger := logrus.New()
entry := logrus.NewEntry(logger)
entry.Data["error"] = errorsFoo()
err := hook.Fire(entry)
if err != nil {
t.Errorf("expected nil error, got '%v'", err)
}
if len(sender.calls) != 1 {
t.Errorf("expected 1 call, got %v", len(sender.calls))
}
params := sender.calls[0]
// github.com/Scalingo/logrus-rollbar.errorsBar
// /home/leo/Projects/Go/src/github.com/Scalingo/logrus-rollbar/hook_pkgerrors_test.go:12
// github.com/Scalingo/logrus-rollbar.errorsFoo
// /home/leo/Projects/Go/src/github.com/Scalingo/logrus-rollbar/hook_pkgerrors_test.go:8
// github.com/Scalingo/logrus-rollbar.TestHook_WithPkgErrors
// /home/leo/Projects/Go/src/github.com/Scalingo/logrus-rollbar/hook_test.go:155
// testing.tRunner
// /opt/go/src/testing/testing.go:657
// runtime.goexit
// /opt/go/src/runtime/asm_amd64.s:2197
werr, ok := params.error.(rollbar.CauseStacker)
if !ok {
t.Fatalf("%v should be a rollbar.CauseStacker", params.error)
}
// We generate the stack here while usually it is generated by the rollbar
// lib, so we got 4 extra levels of execution when we call Stack() (goexit -> trunner -> trunner -> test)
stack := werr.Stack()
if len(stack) != 5 {
t.Fatalf("expecting a stack of 5 levels, got %v: \n===\n%v", len(stack), stack)
}
expectedStack := []runtime.Frame{
{
Line: 12,
Function: "errorsBar",
File: "hook_pkgerrors_test.go",
}, {
Line: 8,
Function: "errorsFoo",
File: "hook_pkgerrors_test.go",
}, {
Line: 154,
Function: "TestHook_WithPkgErrors",
File: "hook_test.go",
}, {
Function: "tRunner",
File: "testing.go",
}, {
Function: "goexit",
File: "asm_amd64.s",
},
}
for i, _ := range expectedStack {
if stack[i].Function != expectedStack[i].Function {
t.Errorf("expected method %v, got %v", expectedStack[i].Function, stack[i].Function)
}
if !strings.HasSuffix(stack[i].File, expectedStack[i].File) {
t.Errorf("expected filename %v, got %v", expectedStack[i].File, stack[i].File)
}
if expectedStack[i].Line != 0 && stack[i].Line != expectedStack[i].Line {
t.Errorf("expected line %v, got %v", expectedStack[i].Line, stack[i].Line)
}
}
}