forked from sensu/sensu-slack-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
155 lines (124 loc) · 4 KB
/
main_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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/sensu/sensu-go/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFormattedEventAction(t *testing.T) {
assert := assert.New(t)
event := types.FixtureEvent("entity1", "check1")
action := formattedEventAction(event)
assert.Equal("RESOLVED", action)
event.Check.Status = 1
action = formattedEventAction(event)
assert.Equal("ALERT", action)
}
func TestChomp(t *testing.T) {
assert := assert.New(t)
trimNewline := chomp("hello\n")
assert.Equal("hello", trimNewline)
trimCarriageReturn := chomp("hello\r")
assert.Equal("hello", trimCarriageReturn)
trimBoth := chomp("hello\r\n")
assert.Equal("hello", trimBoth)
trimLots := chomp("hello\r\n\r\n\r\n")
assert.Equal("hello", trimLots)
}
func TestEventKey(t *testing.T) {
assert := assert.New(t)
event := types.FixtureEvent("entity1", "check1")
eventKey := eventKey(event)
assert.Equal("entity1/check1", eventKey)
}
func TestEventSummary(t *testing.T) {
assert := assert.New(t)
event := types.FixtureEvent("entity1", "check1")
event.Check.Output = "disk is full"
eventKey := eventSummary(event, 100)
assert.Equal("entity1/check1:disk is full", eventKey)
eventKey = eventSummary(event, 5)
assert.Equal("entity1/check1:disk ...", eventKey)
}
func TestFormattedMessage(t *testing.T) {
assert := assert.New(t)
event := types.FixtureEvent("entity1", "check1")
event.Check.Output = "disk is full"
event.Check.Status = 1
formattedMsg := formattedMessage(event)
assert.Equal("ALERT - entity1/check1:disk is full", formattedMsg)
}
func TestMessageColor(t *testing.T) {
assert := assert.New(t)
event := types.FixtureEvent("entity1", "check1")
event.Check.Status = 0
color := messageColor(event)
assert.Equal("good", color)
event.Check.Status = 1
color = messageColor(event)
assert.Equal("warning", color)
event.Check.Status = 2
color = messageColor(event)
assert.Equal("danger", color)
}
func TestMessageStatus(t *testing.T) {
assert := assert.New(t)
event := types.FixtureEvent("entity1", "check1")
event.Check.Status = 0
status := messageStatus(event)
assert.Equal("Resolved", status)
event.Check.Status = 1
status = messageStatus(event)
assert.Equal("Warning", status)
event.Check.Status = 2
status = messageStatus(event)
assert.Equal("Critical", status)
}
func TestSendMessage(t *testing.T) {
assert := assert.New(t)
event := types.FixtureEvent("entity1", "check1")
var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
expectedBody := `{"channel":"#test","attachments":[{"color":"good","fallback":"RESOLVED - entity1/check1:","title":"Description","text":"","fields":[{"title":"Status","value":"Resolved","short":false},{"title":"Entity","value":"entity1","short":true},{"title":"Check","value":"check1","short":true}]}]}`
assert.Equal(expectedBody, string(body))
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"ok": true}`))
require.NoError(t, err)
}))
config.SlackWebhookUrl.Value = apiStub.URL
config.SlackChannel.Value = "#test"
err := sendMessage(event)
assert.NoError(err)
}
func TestMain(t *testing.T) {
assert := assert.New(t)
file, _ := ioutil.TempFile(os.TempDir(), "sensu-handler-slack-")
defer func() {
_ = os.Remove(file.Name())
}()
event := types.FixtureEvent("entity1", "check1")
eventJSON, _ := json.Marshal(event)
_, err := file.WriteString(string(eventJSON))
require.NoError(t, err)
require.NoError(t, file.Sync())
_, err = file.Seek(0, 0)
require.NoError(t, err)
stdin = file
requestReceived := false
var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestReceived = true
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"ok": true}`))
require.NoError(t, err)
}))
oldArgs := os.Args
os.Args = []string{"slack", "-w", apiStub.URL}
defer func() { os.Args = oldArgs }()
main()
assert.True(requestReceived)
}