forked from wchan2/bloodhound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerts_test.go
88 lines (70 loc) · 2.13 KB
/
alerts_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
package main_test
import (
"fmt"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/wchan2/bloodhound"
)
var _ = Describe(`TotalTrafficAlert`, func() {
var (
alert Alert
notification *notificationMock
)
JustBeforeEach(func() {
alert = NewTotalTrafficAlert(2, 2*time.Minute, notification)
})
Describe(`#Check`, func() {
BeforeEach(func() {
notification = new(notificationMock)
})
Context(`when the threshold is exceeded`, func() {
var currentTime, twoMinutesAgo time.Time
BeforeEach(func() {
currentTime = time.Now()
twoMinutesAgo = currentTime.Add(-2 * time.Minute)
})
JustBeforeEach(func() {
alert.Check(Event{Time: twoMinutesAgo})
alert.Check(Event{Time: currentTime})
})
It(`sends an alert message`, func() {
notificationMessage := fmt.Sprintf("High traffic generated an alert - hits = 2, triggered at %s", currentTime.String())
Expect(notification.message).To(Equal(notificationMessage))
})
})
Context(`when the threshold is not exceeded`, func() {
var currentTime, fourMinutesAgo time.Time
BeforeEach(func() {
currentTime = time.Now()
fourMinutesAgo = currentTime.Add(-4 * time.Minute)
})
JustBeforeEach(func() {
alert.Check(Event{Time: fourMinutesAgo})
alert.Check(Event{Time: currentTime})
})
It(`does not send an alert message`, func() {
Expect(notification.message).To(BeEmpty())
})
})
Context(`when the threshold is resolved`, func() {
var currentTime, threeMinutesAgo, fourMinutesAgo time.Time
BeforeEach(func() {
currentTime = time.Now()
threeMinutesAgo = currentTime.Add(-3 * time.Minute)
fourMinutesAgo = currentTime.Add(-4 * time.Minute)
})
JustBeforeEach(func() {
// trigger an alert
alert.Check(Event{Time: fourMinutesAgo})
alert.Check(Event{Time: threeMinutesAgo})
// resolve the alert
alert.Check(Event{Time: currentTime})
})
It(`sends a resolved message`, func() {
resolvedMessage := fmt.Sprintf("Traffic returned to normal, triggered at %s", currentTime.String())
Expect(notification.message).To(Equal(resolvedMessage))
})
})
})
})