-
Notifications
You must be signed in to change notification settings - Fork 26
/
main_test.go
135 lines (109 loc) · 3.75 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
package main
import (
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestFormattedEventAction(t *testing.T) {
assert := assert.New(t)
event := corev2.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 := corev2.FixtureEvent("entity1", "check1")
eventKey := eventKey(event)
assert.Equal("entity1/check1", eventKey)
}
func TestEventSummary(t *testing.T) {
assert := assert.New(t)
event := corev2.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 := corev2.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 := corev2.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 := corev2.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 := corev2.FixtureEvent("entity1", "check1")
var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
expectedBody := `{"channel":"#test","attachments":[{"color":"good","fallback":"RESOLVED - entity1/check1:","title":"Description","fields":[{"title":"Status","value":"Resolved","short":false},{"title":"Entity","value":"entity1","short":true},{"title":"Check","value":"check1","short":true}],"blocks":null}],"replace_original":false,"delete_original":false}`
assert.Equal(expectedBody, string(body))
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"ok": true}`))
require.NoError(t, err)
}))
config.slackwebHookURL = apiStub.URL
config.slackChannel = "#test"
config.slackDescriptionTemplate = "{{ .Check.Output }}"
err := sendMessage(event)
assert.NoError(err)
}
func TestCheckArgs(t *testing.T) {
assert := assert.New(t)
config := HandlerConfig{}
event := corev2.FixtureEvent("entity1", "check1")
config.slackDescriptionTemplate = "Sensu Event Details"
config.slackUsername = "Dummy user"
config.slackChannel = "Test"
config.slackIconURL = "https://www.sensu.io/img/sensu-logo.png"
_ = os.Setenv("SLACK_WEBHOOK_URL", "http://example.com/webhook")
config.slackwebHookURL = os.Getenv("SLACK_WEBHOOK_URL")
assert.NoError(checkArgs(event))
}