forked from kaplanelad/veneur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentry_test.go
115 lines (99 loc) · 2.94 KB
/
sentry_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
package veneur
import (
"errors"
"testing"
"time"
"github.com/getsentry/raven-go"
"github.com/sirupsen/logrus"
)
// returns the result of calling recover() after s.ConsumePanic()
func consumeAndCatchPanic(s *Server) (result interface{}) {
defer func() {
result = recover()
}()
ConsumePanic(s.Sentry, s.TraceClient, s.Hostname, "panic")
return
}
func TestConsumePanicWithoutSentry(t *testing.T) {
s := &Server{}
// does nothing
ConsumePanic(s.Sentry, s.TraceClient, s.Hostname, nil)
recovered := consumeAndCatchPanic(s)
if recovered != "panic" {
t.Error("ConsumePanic should panic", recovered)
}
}
type fakeSentryTransport struct {
packets []*raven.Packet
}
func (t *fakeSentryTransport) Send(url string, authHeader string, packet *raven.Packet) error {
t.packets = append(t.packets, packet)
return nil
}
func TestConsumePanicWithSentry(t *testing.T) {
s := &Server{}
var err error
s.Sentry, err = raven.NewClient("", nil)
if err != nil {
t.Fatal("failed to create sentry client:", err)
}
fakeTransport := &fakeSentryTransport{}
s.Sentry.Transport = fakeTransport
// nil does nothing
ConsumePanic(s.Sentry, s.TraceClient, s.Hostname, nil)
if len(fakeTransport.packets) != 0 {
t.Error("ConsumePanic(nil) should not send data:", fakeTransport.packets)
}
recovered := consumeAndCatchPanic(s)
if recovered != "panic" {
t.Error("ConsumePanic should panic", recovered)
}
if len(fakeTransport.packets) != 1 {
t.Error("expected 1 packet:", fakeTransport.packets)
}
}
func TestHookWithoutSentry(t *testing.T) {
// hook with a nil sentry client is used when sentry is disabled
hook := &sentryHook{}
entry := &logrus.Entry{}
// must use Fatal so the call to Fire blocks and we can check the result
entry.Level = logrus.FatalLevel
err := hook.Fire(entry)
if err != nil {
t.Error("Fire returned an error:", err)
}
}
func TestHook(t *testing.T) {
hook := &sentryHook{}
var err error
hook.c, err = raven.NewClient("", nil)
if err != nil {
t.Fatal("error creating sentry client:", err)
}
fakeTransport := &fakeSentryTransport{}
hook.c.Transport = fakeTransport
// entry without any tags
entry := &logrus.Entry{}
// must use Fatal so the call to Fire blocks and we can check the result
entry.Level = logrus.FatalLevel
entry.Time = time.Now()
entry.Message = "received zero-length trace packet"
hook.Fire(entry)
if len(fakeTransport.packets) != 1 {
t.Fatal("expected 1 packet", fakeTransport.packets)
}
if len(fakeTransport.packets[0].Extra) != 0 {
t.Error("expected no extra tags", fakeTransport.packets[0].Extra)
}
fakeTransport.packets = nil
// entry with an error tag that gets stripped
entry.Data = map[string]interface{}{logrus.ErrorKey: errors.New("some error")}
hook.Fire(entry)
if len(fakeTransport.packets) != 1 {
t.Fatal("expected 1 packet", fakeTransport.packets)
}
if len(fakeTransport.packets[0].Extra) != 0 {
t.Error("expected no extra tags", fakeTransport.packets[0].Extra)
}
fakeTransport.packets = nil
}