@@ -133,8 +133,8 @@ func TestAlert(t *testing.T) {
133
133
t .Errorf ("expected %s, but got %s" , expected , actual )
134
134
}
135
135
136
- actualStatus := string ( alert .Status () )
137
- expectedStatus := "firing"
136
+ actualStatus := alert .Status ()
137
+ expectedStatus := AlertStatus ( "firing" )
138
138
139
139
if actualStatus != expectedStatus {
140
140
t .Errorf ("expected alertStatus %s, but got %s" , expectedStatus , actualStatus )
@@ -150,19 +150,55 @@ func TestAlert(t *testing.T) {
150
150
EndsAt : ts2 ,
151
151
}
152
152
153
+ if ! alert .Resolved () {
154
+ t .Error ("expected alert to be resolved, but it was not" )
155
+ }
156
+
153
157
actual = fmt .Sprint (alert )
154
158
expected = "[d181d0f][resolved]"
155
159
156
160
if actual != expected {
157
161
t .Errorf ("expected %s, but got %s" , expected , actual )
158
162
}
159
163
160
- actualStatus = string ( alert .Status () )
164
+ actualStatus = alert .Status ()
161
165
expectedStatus = "resolved"
162
166
163
167
if actualStatus != expectedStatus {
164
168
t .Errorf ("expected alertStatus %s, but got %s" , expectedStatus , actualStatus )
165
169
}
170
+
171
+ // Verifying that ResolvedAt works for different times
172
+ if alert .ResolvedAt (ts1 ) {
173
+ t .Error ("unexpected alert was resolved at start time" )
174
+ }
175
+ if alert .ResolvedAt (ts2 .Add (- time .Millisecond )) {
176
+ t .Error ("unexpected alert was resolved before it ended" )
177
+ }
178
+ if ! alert .ResolvedAt (ts2 ) {
179
+ t .Error ("expected alert to be resolved at end time" )
180
+ }
181
+ if ! alert .ResolvedAt (ts2 .Add (time .Millisecond )) {
182
+ t .Error ("expected alert to be resolved after it ended" )
183
+ }
184
+
185
+ // Verifying that StatusAt works for different times
186
+ actualStatus = alert .StatusAt (ts1 )
187
+ if actualStatus != "firing" {
188
+ t .Errorf ("expected alert to be firing at start time, but got %s" , actualStatus )
189
+ }
190
+ actualStatus = alert .StatusAt (ts1 .Add (- time .Millisecond ))
191
+ if actualStatus != "firing" {
192
+ t .Errorf ("expected alert to be firing before it ended, but got %s" , actualStatus )
193
+ }
194
+ actualStatus = alert .StatusAt (ts2 )
195
+ if actualStatus != "resolved" {
196
+ t .Errorf ("expected alert to be resolved at end time, but got %s" , actualStatus )
197
+ }
198
+ actualStatus = alert .StatusAt (ts2 .Add (time .Millisecond ))
199
+ if actualStatus != "resolved" {
200
+ t .Errorf ("expected alert to be resolved after it ended, but got %s" , actualStatus )
201
+ }
166
202
}
167
203
168
204
func TestSortAlerts (t * testing.T ) {
@@ -228,18 +264,19 @@ func TestSortAlerts(t *testing.T) {
228
264
}
229
265
230
266
func TestAlertsStatus (t * testing.T ) {
267
+ ts := time .Now ()
231
268
firingAlerts := Alerts {
232
269
{
233
270
Labels : LabelSet {
234
271
"foo" : "bar" ,
235
272
},
236
- StartsAt : time . Now () ,
273
+ StartsAt : ts ,
237
274
},
238
275
{
239
276
Labels : LabelSet {
240
277
"bar" : "baz" ,
241
278
},
242
- StartsAt : time . Now () ,
279
+ StartsAt : ts ,
243
280
},
244
281
}
245
282
@@ -250,7 +287,12 @@ func TestAlertsStatus(t *testing.T) {
250
287
t .Errorf ("expected status %s, but got %s" , expectedStatus , actualStatus )
251
288
}
252
289
253
- ts := time .Now ()
290
+ actualStatus = firingAlerts .StatusAt (ts )
291
+ if actualStatus != expectedStatus {
292
+ t .Errorf ("expected status %s, but got %s" , expectedStatus , actualStatus )
293
+ }
294
+
295
+ ts = time .Now ()
254
296
resolvedAlerts := Alerts {
255
297
{
256
298
Labels : LabelSet {
@@ -270,7 +312,48 @@ func TestAlertsStatus(t *testing.T) {
270
312
271
313
actualStatus = resolvedAlerts .Status ()
272
314
expectedStatus = AlertResolved
315
+ if actualStatus != expectedStatus {
316
+ t .Errorf ("expected status %s, but got %s" , expectedStatus , actualStatus )
317
+ }
318
+
319
+ actualStatus = resolvedAlerts .StatusAt (ts )
320
+ expectedStatus = AlertResolved
321
+ if actualStatus != expectedStatus {
322
+ t .Errorf ("expected status %s, but got %s" , expectedStatus , actualStatus )
323
+ }
273
324
325
+ ts = time .Now ()
326
+ mixedAlerts := Alerts {
327
+ {
328
+ Labels : LabelSet {
329
+ "foo" : "bar" ,
330
+ },
331
+ StartsAt : ts .Add (- 1 * time .Minute ),
332
+ EndsAt : ts .Add (5 * time .Minute ),
333
+ },
334
+ {
335
+ Labels : LabelSet {
336
+ "bar" : "baz" ,
337
+ },
338
+ StartsAt : ts .Add (- 1 * time .Minute ),
339
+ EndsAt : ts ,
340
+ },
341
+ }
342
+
343
+ actualStatus = mixedAlerts .Status ()
344
+ expectedStatus = AlertFiring
345
+ if actualStatus != expectedStatus {
346
+ t .Errorf ("expected status %s, but got %s" , expectedStatus , actualStatus )
347
+ }
348
+
349
+ actualStatus = mixedAlerts .StatusAt (ts )
350
+ expectedStatus = AlertFiring
351
+ if actualStatus != expectedStatus {
352
+ t .Errorf ("expected status %s, but got %s" , expectedStatus , actualStatus )
353
+ }
354
+
355
+ actualStatus = mixedAlerts .StatusAt (ts .Add (5 * time .Minute ))
356
+ expectedStatus = AlertResolved
274
357
if actualStatus != expectedStatus {
275
358
t .Errorf ("expected status %s, but got %s" , expectedStatus , actualStatus )
276
359
}
0 commit comments