-
Notifications
You must be signed in to change notification settings - Fork 3
/
alert_test.go
155 lines (127 loc) · 3.22 KB
/
alert_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 (
"github.com/hashicorp/consul/api"
"reflect"
"sync"
"testing"
"time"
)
const testAlertKVPath = "test"
// Set up some test config with a test handler configured. Returns the config and
// a channel that the handler will send to when alerting
func testAlertConfig() (*Config, chan *AlertState) {
alertCh := make(chan *AlertState, 1)
config := &Config{
Handlers: map[string]AlertHandler{
"test": testHandler{alertCh},
},
}
return config, alertCh
}
// Make sure we can properly serialize an AlertState struct to the KV store
// and read it back
func TestAlert_setGetAlert(t *testing.T) {
client, server := testConsul(t)
defer server.Stop()
expected := &AlertState{
Status: "passing",
Details: "test",
}
err := setAlertState(testAlertKVPath, expected, client)
if err != nil {
t.Fatal(err)
}
alert, err := getAlertState(testAlertKVPath, client)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(alert, expected) {
t.Errorf("expected \n%#v\n\n, got \n\n%#v\n\n", expected, alert)
}
}
// Set up an alert and make sure it gets sent to our handler
func TestAlert_tryAlert(t *testing.T) {
client, server := testConsul(t)
defer server.Stop()
config, alertCh := testAlertConfig()
go tryAlert(testAlertKVPath, AlertState{
Status: api.HealthCritical,
}, &WatchOptions{
client: client,
config: config,
alertLock: &sync.Mutex{},
})
select {
case <-alertCh:
case <-time.After(1 * time.Second):
t.Error("didn't get alert")
}
}
// Set up two handlers but only add one to DefaultHandlers
func TestAlert_defaultHandler(t *testing.T) {
client, server := testConsul(t)
defer server.Stop()
alertCh := make(chan *AlertState)
ignoredCh := make(chan *AlertState)
config := &Config{
DefaultHandlers: []string{"test"},
Handlers: map[string]AlertHandler{
"test": testHandler{alertCh},
"test_ignored": testHandler{ignoredCh},
},
}
go tryAlert(testAlertKVPath, AlertState{
Status: api.HealthCritical,
}, &WatchOptions{
client: client,
config: config,
alertLock: &sync.Mutex{},
})
select {
case <-alertCh:
case <-time.After(1 * time.Second):
t.Error("didn't get alert")
}
select {
case <-ignoredCh:
t.Error("got unexpected alert on ignored alert handler")
case <-time.After(1 * time.Second):
}
}
// Set up two handlers but configure the service to only alert on one
func TestAlert_serviceHandler(t *testing.T) {
client, server := testConsul(t)
defer server.Stop()
alertCh := make(chan *AlertState)
ignoredCh := make(chan *AlertState)
config := &Config{
Services: map[string]ServiceConfig{
testServiceName: ServiceConfig{
Name: testServiceName,
Handlers: []string{"test"},
},
},
Handlers: map[string]AlertHandler{
"test": testHandler{alertCh},
"test_ignored": testHandler{ignoredCh},
},
}
go tryAlert(testAlertKVPath, AlertState{
Status: api.HealthCritical,
}, &WatchOptions{
service: testServiceName,
client: client,
config: config,
alertLock: &sync.Mutex{},
})
select {
case <-alertCh:
case <-time.After(1 * time.Second):
t.Error("didn't get alert")
}
select {
case <-ignoredCh:
t.Error("got unexpected alert on ignored alert handler")
case <-time.After(1 * time.Second):
}
}