-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
238 lines (197 loc) · 5.77 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"testing"
"time"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/types"
"github.com/stretchr/testify/assert"
)
var (
eventWithStatus = &corev2.Event{
Check: &corev2.Check{
Output: "This is a string w/ new line\n",
Status: 10,
},
}
)
func TestTrimSuffixCheckOutput(t *testing.T) {
assert := assert.New(t)
output := trimSuffixCheckOutput(eventWithStatus)
assert.Equal("This is a string w/ new line", output)
}
func TestParseTime(t *testing.T) {
assert := assert.New(t)
timeStamp, _ := time.Parse("Jan 2, 2006 at 3:04pm (MST)", "Dec 29, 2014 at 7:54pm (SGT)")
assert.Equal("2014-12-29 19:54:00 +0000 SGT", timeStamp.String())
}
// func TestCheck(t *testing.T) {
// newCheck := corev2.FixtureCheck("check")
// newCheck.History = []corev2.CheckHistory{
// Status: 1,
// }
// assert := assert.New(t)
// seed := time.Now().UnixNano()
// popr := math_rand.New(math_rand.NewSource(seed))
// p := corev2.NewPopulatedCheckHistory(popr, false)
// // err := validateEvent(&corev2.Event{
// // Timestamp: 11231231,
// // Check: &corev2.Check{
// // Output: "This is a string w/ new line\n",
// // History: [p],
// // },
// // })
// assert.Nil(err)
// }
func TestValidateEvent(t *testing.T) {
assert := assert.New(t)
err := validateEvent(&corev2.Event{
Timestamp: 11231231,
Entity: &corev2.Entity{
EntityClass: "agent",
ObjectMeta: corev2.ObjectMeta{
Name: "fp",
Namespace: "default",
},
},
Check: &corev2.Check{
ObjectMeta: corev2.ObjectMeta{
Name: "check-name",
},
Interval: 20,
},
})
assert.Nil(err)
assert.EqualError(validateEvent(&corev2.Event{}), "timestamp is missing or must be greater than zero")
assert.EqualError(validateEvent(&corev2.Event{
Timestamp: 11231231,
}), "entity is missing from event")
assert.EqualError(validateEvent(&corev2.Event{
Timestamp: 11231231,
Entity: &corev2.Entity{},
}), "check is missing from event")
assert.EqualError(validateEvent(&corev2.Event{
Timestamp: 11231231,
Entity: &corev2.Entity{
ObjectMeta: corev2.ObjectMeta{
Name: "fp",
},
},
Check: &corev2.Check{},
}), "entity class must not be empty")
assert.EqualError(validateEvent(&corev2.Event{
Timestamp: 11231231,
Entity: &corev2.Entity{
EntityClass: "agent",
ObjectMeta: corev2.ObjectMeta{
Name: "fp",
Namespace: "default",
},
},
Check: &corev2.Check{
ObjectMeta: corev2.ObjectMeta{
Name: "check-name",
},
},
}), "check interval must be greater than or equal to 1")
}
func TestCheckArgs(t *testing.T) {
assert := assert.New(t)
err := checkArgs(&corev2.Event{
Timestamp: 11231231,
Entity: &corev2.Entity{
EntityClass: "agent",
ObjectMeta: corev2.ObjectMeta{
Name: "fp",
Namespace: "default",
},
},
Check: &corev2.Check{
Output: "This is a string w/ new line\n",
Status: 10,
Interval: 20,
ObjectMeta: corev2.ObjectMeta{
Name: "check-name",
},
},
})
assert.Nil(err)
assert.EqualError(checkArgs(&corev2.Event{}), "event does not contain check")
}
func TestStringMinifier(t *testing.T) {
assert := assert.New(t)
input := `<blockquote class='blue'> foo bar <br/>
<b>Check Name:</b> foocheck
<b>Entity:</b> entity-nmame <br/>
<b>Check output:</b> this is output <br/>
<b>History:</b> <br/>
</blockquote>`
output := stringMinifier(input)
assert.Equal(output, "<blockquote class='blue'> foo bar <br/> <b>Check Name:</b> foocheck <b>Entity:</b> entity-nmame <br/> <b>Check output:</b> this is output <br/> <b>History:</b> <br/> </blockquote>")
}
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 TestStateToEmojifier(t *testing.T) {
assert := assert.New(t)
event := types.FixtureEvent("entity1", "check1")
event.Check.Status = 0
color, state, icon, _ := stateToEmojifier(event)
assert.Equal("success", color)
assert.Equal("Resolved", state)
assert.Equal("✅", icon)
event.Check.Status = 1
color, state, icon, _ = stateToEmojifier(event)
assert.Equal("warning", color)
assert.Equal("Warning", state)
assert.Equal("️⚠️", icon)
event.Check.Status = 2
color, state, icon, _ = stateToEmojifier(event)
assert.Equal("danger", color)
assert.Equal("Critical", state)
assert.Equal("🚨", icon)
event.Check.Status = 33
color, state, icon, _ = stateToEmojifier(event)
assert.Equal("unknown", color)
assert.Equal("Unknown", state)
assert.Equal("⁉️", icon)
}