-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_hooks_test.go
138 lines (111 loc) · 3.17 KB
/
logger_hooks_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
package narada
import (
"errors"
"io/ioutil"
"testing"
"time"
"gopkg.in/h2non/gock.v1"
"github.com/spf13/viper"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestNewLogrusSentryHook(t *testing.T) {
t.Run("It should work with empty sentry", func(t *testing.T) {
t.Run("Sending error", func(t *testing.T) {
h := NewLogrusSentryHook(nil)
err := h.Fire(&logrus.Entry{
Data: logrus.Fields{
"error": errors.New("some unknown error"),
},
})
assert.NotNil(t, h)
assert.NoError(t, err)
})
t.Run("Sending message", func(t *testing.T) {
h := NewLogrusSentryHook(nil)
err := h.Fire(&logrus.Entry{
Message: "i am unknown message",
})
assert.NotNil(t, h)
assert.NoError(t, err)
})
})
t.Run("Levels should be only: error, panic, fatal", func(t *testing.T) {
h := NewLogrusSentryHook(nil)
assert.NotNil(t, h)
assert.Equal(t, []logrus.Level{logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel}, h.Levels())
})
}
func TestNewLogrusSlackHook(t *testing.T) {
t.Run("Disabled by default", func(t *testing.T) {
hook := NewLogrusSlackHook(viper.New())
assert.Nil(t, hook.Fire(nil))
})
t.Run("Sending message to Slack", func(t *testing.T) {
//hwe, err := ioutil.ReadFile("./fixtures/slack/hook_with_error.json")
//assert.NoError(t, err)
//hwm, err := ioutil.ReadFile("./fixtures/slack/hook_with_message.json")
//assert.NoError(t, err)
t.Run("With error attached", func(t *testing.T) {
defer gock.Off()
done := make(chan bool, 1)
defer gock.Off()
gock.New("http://your_token.slack.com").
Post("/").
MatchType("json").
BodyString("").
ReplyFunc(func(response *gock.Response) {
done <- true
response.Status(200)
})
cfg := viper.New()
cfg.Set("logger.slack", true)
cfg.Set("logger.slack_url", "http://your_token.slack.com")
cfg.Set("logger.slack_channel", "general")
cfg.Set("logger.extra", map[string]interface{}{
"question": 42,
})
cfg.Set("app.name", "default")
logger := logrus.New()
logger.Out = ioutil.Discard
hook := NewLogrusSlackHook(cfg)
logger.AddHook(hook)
logger.WithError(errors.New("unknown error")).Error("error for slack")
select {
case <-done:
case <-time.After(time.Second * 5):
t.Fatalf("timeout exceeded")
}
})
t.Run("With error only in message", func(t *testing.T) {
done := make(chan bool, 1)
defer gock.Off()
gock.New("http://your_token.slack.com").
Post("/").
MatchType("json").
BodyString("").
ReplyFunc(func(response *gock.Response) {
done <- true
response.Status(200)
})
if gock.HasUnmatchedRequest() {
t.Fatalf("Unmatched requests from gock")
}
cfg := viper.New()
cfg.Set("logger.slack", true)
cfg.Set("logger.slack_url", "http://your_token.slack.com")
cfg.Set("logger.slack_channel", "general")
cfg.Set("app.name", "default")
logger := logrus.New()
logger.Out = ioutil.Discard
hook := NewLogrusSlackHook(cfg)
logger.AddHook(hook)
logger.Errorf("error for slack: %v", errors.New("unknown error"))
select {
case <-done:
case <-time.After(time.Second * 5):
t.Fatalf("timeout exceeded")
}
})
})
}