-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler_test.go
97 lines (77 loc) · 2.07 KB
/
handler_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
package main
import (
"fmt"
"os"
"testing"
log "github.com/Sirupsen/logrus"
"github.com/Sirupsen/logrus/hooks/test"
"github.com/nlopes/slack"
)
func TestHandler_stdout(t *testing.T) {
logger, hook := test.NewNullLogger()
handler := StdoutHandler{"warn", logger}
detail1 := "detail line 1"
detail2 := "detail line 2"
alert := &AlertState{
Message: "service is failing",
Details: detail1 + "\n" + detail2,
}
handler.Alert("", alert)
if len(hook.Entries) != 3 {
t.Errorf("expected %d lines of output, got %d", 3, len(hook.Entries))
}
if hook.Entries[0].Message != alert.Message {
t.Errorf("expected message line '%s', got '%s'", alert.Message, hook.Entries[0])
}
if hook.Entries[1].Message != detail1 || hook.Entries[2].Message != detail2 {
t.Errorf("expected detail lines: '%s', '%s'; got: '%s', '%s'", detail1, detail2,
hook.Entries[1].Message, hook.Entries[2].Message)
}
if hook.LastEntry().Level != log.WarnLevel {
t.Errorf("expected loglevel %s, got %s", log.WarnLevel, hook.LastEntry().Level)
}
}
func TestHandler_slack(t *testing.T) {
token := os.Getenv("TEST_SLACK_TOKEN")
if token == "" {
t.Skip("TEST_SLACK_TOKEN not set, skipping")
}
channel := os.Getenv("TEST_SLACK_CHANNEL")
if token == "" {
t.Skip("TEST_SLACK_CHANNEL not set, skipping")
}
handler := SlackHandler{
Token: token,
ChannelName: channel,
}
hook := new(test.Hook)
log.AddHook(hook)
detail1 := "detail line 1"
detail2 := "detail line 2"
alert := &AlertState{
Message: "service is failing",
Details: detail1 + "\n" + detail2,
}
handler.Alert("", alert)
api := slack.New(token)
groups, err := api.GetGroups(true)
if err != nil {
t.Fatal(err)
}
id := ""
for _, group := range groups {
if group.Name == channel {
id = group.ID
}
}
history, err := api.GetGroupHistory(id, slack.HistoryParameters{
Count: 1,
})
if err != nil {
t.Fatal(err)
}
expected := fmt.Sprintf(slackMessageFormat, alert.Message, alert.Details)
if history.Messages[0].Text != expected {
t.Errorf("expected `%s`, got `%s`", expected, history.Messages[0].Text)
}
}