-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathsentry_setter_test.go
177 lines (149 loc) · 3.7 KB
/
sentry_setter_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
package logrus_sentry
import (
"fmt"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestSetDefaultLoggerName(t *testing.T) {
const name = "my_logger"
a := assert.New(t)
WithTestDSN(t, func(dsn string, pch <-chan *resultPacket) {
logger := getTestLogger()
hook, err := NewSentryHook(dsn, []logrus.Level{
logrus.ErrorLevel,
})
a.NoError(err, "NewSentryHook should be NoError")
hook.SetDefaultLoggerName(name)
logger.Hooks.Add(hook)
logger.Error(message)
packet := <-pch
a.Equal(name, packet.Logger, "logger must be set")
})
}
func TestSetEnvironment(t *testing.T) {
const env = "test"
a := assert.New(t)
WithTestDSN(t, func(dsn string, pch <-chan *resultPacket) {
logger := getTestLogger()
hook, err := NewSentryHook(dsn, []logrus.Level{
logrus.ErrorLevel,
})
a.NoError(err, "NewSentryHook should be NoError")
hook.SetEnvironment(env)
logger.Hooks.Add(hook)
logger.Error(message)
packet := <-pch
a.Equal(env, packet.Environment, "environment must be set")
})
}
func TestSetIgnoreErrors(t *testing.T) {
a := assert.New(t)
tests := []struct {
errString string
isSuccess bool
}{
{"", true},
{"aaa", true},
{"jskljdasidjiaoklzmxcasifjiklmzx9eijodfsklcmzx", true},
{"[0-9]+", true},
{"[0-9", false},
{"+", false},
}
WithTestDSN(t, func(dsn string, pch <-chan *resultPacket) {
hook, err := NewSentryHook(dsn, []logrus.Level{
logrus.ErrorLevel,
})
a.NoError(err, "NewSentryHook should be NoError")
for _, tt := range tests {
target := fmt.Sprintf("%+v", tt)
err := hook.SetIgnoreErrors(tt.errString)
switch {
case !tt.isSuccess:
a.Error(err, target)
case tt.isSuccess:
a.NoError(err, target)
}
}
})
}
func TestSetIncludePaths(t *testing.T) {
a := assert.New(t)
paths := []string{
"aaa",
"bbb",
"ccc",
}
WithTestDSN(t, func(dsn string, pch <-chan *resultPacket) {
hook, err := NewSentryHook(dsn, []logrus.Level{
logrus.ErrorLevel,
})
a.NoError(err, "NewSentryHook should be NoError")
hook.SetIncludePaths(paths)
a.Equal(paths, hook.client.IncludePaths(), "includePaths must be set")
})
}
func TestSetRelease(t *testing.T) {
const releaseVer = "v0.1.0"
a := assert.New(t)
WithTestDSN(t, func(dsn string, pch <-chan *resultPacket) {
logger := getTestLogger()
hook, err := NewSentryHook(dsn, []logrus.Level{
logrus.ErrorLevel,
})
a.NoError(err, "NewSentryHook should be NoError")
hook.SetRelease(releaseVer)
logger.Hooks.Add(hook)
logger.Error(message)
packet := <-pch
a.Equal(releaseVer, packet.Release, "release version must be set")
})
}
func TestSetSampleRate(t *testing.T) {
a := assert.New(t)
tests := []struct {
rate float32
isSuccess bool
}{
{0.0, true},
{0.1, true},
{0.5, true},
{0.9, true},
{1.0, true},
{-0.1, false},
{-2, false},
{1.1, false},
{2.0, false},
}
WithTestDSN(t, func(dsn string, pch <-chan *resultPacket) {
hook, err := NewSentryHook(dsn, []logrus.Level{
logrus.ErrorLevel,
})
a.NoError(err, "NewSentryHook should be NoError")
for _, tt := range tests {
target := fmt.Sprintf("%+v", tt)
err := hook.SetSampleRate(tt.rate)
switch {
case !tt.isSuccess:
a.Error(err, target)
case tt.isSuccess:
a.NoError(err, target)
}
}
})
}
func TestSetServerName(t *testing.T) {
a := assert.New(t)
WithTestDSN(t, func(dsn string, pch <-chan *resultPacket) {
logger := getTestLogger()
hook, err := NewSentryHook(dsn, []logrus.Level{
logrus.ErrorLevel,
})
a.NoError(err, "NewSentryHook should be NoError")
hook.SetServerName(server_name)
logger.Hooks.Add(hook)
logger.Error(message)
packet := <-pch
a.Equal(server_name, packet.ServerName, "server name must be set")
})
}