This repository has been archived by the owner on Jun 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logrusbadger_test.go
141 lines (126 loc) · 3.47 KB
/
logrusbadger_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
package logrusbadger
import (
"errors"
"testing"
honeybadger "github.com/honeybadger-io/honeybadger-go"
"github.com/sirupsen/logrus"
)
type Notification struct {
Message string
Extra []interface{}
}
type FakeNotifier struct {
T *testing.T
Notifications []Notification
NotifyErr error
}
func NewFakeNotifer(t *testing.T) *FakeNotifier {
return &FakeNotifier{T: t}
}
func (n *FakeNotifier) Notify(msg interface{}, extra ...interface{}) (string, error) {
s, ok := msg.(string)
if !ok {
n.T.Errorf("message must be a string")
}
notification := Notification{Message: s, Extra: extra}
n.Notifications = append(n.Notifications, notification)
return "", n.NotifyErr
}
func TestLevels(t *testing.T) {
DefaultLevels = []logrus.Level{logrus.PanicLevel}
h := NewHook()
actual := h.Levels()
if i := len(actual); i != 1 {
t.Errorf("unexpected length %d of Levels", i)
}
if actual[0] != logrus.PanicLevel {
t.Errorf("expected PanicLevel, got %v", actual[0])
}
}
func TestFire_WithErrorField(t *testing.T) {
h := NewHook()
fake := NewFakeNotifer(t)
h.notifier = fake
e := logrus.WithField("test", "value").WithError(errors.New("test error"))
e.Message = "test message"
err := h.Fire(e)
if err != nil {
t.Errorf("expected no error, got: %v", err)
}
if i := len(fake.Notifications); i != 1 {
t.Errorf("expected 1 notification, got %d", i)
}
n := fake.Notifications[0]
if expected := "test error"; n.Message != expected {
t.Errorf("expected '%s', got '%s'", expected, n.Message)
}
if i := len(n.Extra); i != 2 {
t.Errorf("expected 2 extras, got %d", i)
}
class, ok := n.Extra[0].(honeybadger.ErrorClass)
if !ok {
t.Error("expected first extra to be honeybadger.ErrorClass")
}
if expected := "test message"; class.Name != expected {
t.Errorf("expected error class '%s', got '%s'", expected, class.Name)
}
ctx, ok := n.Extra[1].(honeybadger.Context)
if !ok {
t.Error("expected second extra to be honeybadger.Context")
}
if i := len(ctx); i != 2 {
t.Errorf("expected 2 entries in context, got %d: %v", i, ctx)
}
if v := ctx["test"]; v != "value" {
t.Errorf("expected test='value' in context, got %v", v)
}
if v := ctx["error"]; v != "test error" {
t.Errorf("expected error='test error' in context, got %v", v)
}
}
func TestFire_NoErrorField(t *testing.T) {
h := NewHook()
fake := NewFakeNotifer(t)
h.notifier = fake
e := logrus.WithField("test", "value")
e.Message = "test message"
err := h.Fire(e)
if err != nil {
t.Errorf("expected no error, got: %v", err)
}
if i := len(fake.Notifications); i != 1 {
t.Errorf("expected 1 notification, got %d", i)
}
n := fake.Notifications[0]
if n.Message == "" {
t.Errorf("expected empty message, got '%s'", n.Message)
}
if i := len(n.Extra); i != 1 {
t.Errorf("expected 1 extras, got %d: %+v", i, n.Extra)
}
ctx, ok := n.Extra[0].(honeybadger.Context)
if !ok {
t.Error("expected extra to be honeybadger.Context")
}
if i := len(ctx); i != 1 {
t.Errorf("expected 1 entry in context, got %d: %v", i, ctx)
}
if v := ctx["test"]; v != "value" {
t.Errorf("expected test='value' in context, got %v", v)
}
}
func TestFire_NotifyError(t *testing.T) {
h := NewHook()
fake := NewFakeNotifer(t)
fake.NotifyErr = errors.New("test error")
h.notifier = fake
e := logrus.WithField("test", "value")
e.Message = "test message"
err := h.Fire(e)
if err == nil {
t.Error("expected error, got nil")
}
if err.Error() != "test error" {
t.Errorf("expected 'test error', got '%s'", err.Error())
}
}