forked from mperham/inspeqtor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions_test.go
193 lines (164 loc) · 5.3 KB
/
actions_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
package inspeqtor
import (
"bytes"
"strings"
"testing"
"github.com/mperham/inspeqtor/metrics"
"github.com/mperham/inspeqtor/services"
"github.com/stretchr/testify/assert"
)
func makeAction(actionName, notifType string, config map[string]string) (Action, error) {
return Actions[actionName](nil, &AlertRoute{"", notifType, config})
}
func mockService(name string) *Service {
return &Service{&Entity{name, nil, nil, nil}, nil, services.WithStatus(999, services.Up), services.MockInit()}
}
func TestRestartAlert(t *testing.T) {
t.Parallel()
s := mockService("foobar")
res, err := Actions["restart"](s, nil)
assert.Nil(t, err)
assert.NotNil(t, res)
}
func TestRestart(t *testing.T) {
t.Parallel()
s := mockService("foobar")
res, err := Actions["restart"](s, nil)
assert.Nil(t, err)
assert.NotNil(t, res)
assert.Equal(t, 999, s.Process.Pid)
assert.Equal(t, services.Up, s.Process.Status.String())
res.Trigger(nil)
assert.Equal(t, 0, s.Process.Pid)
assert.Equal(t, services.Starting, s.Process.Status.String())
}
func TestReload(t *testing.T) {
t.Parallel()
service := mockService("foobar")
res, err := Actions["reload"](service, nil)
assert.Nil(t, err)
assert.NotNil(t, res)
}
func TestGmailNotifier(t *testing.T) {
t.Parallel()
action, err := makeAction("alert", "gmail", map[string]string{
"username": "mike",
"password": "fuzzbucket",
"to_email": "[email protected]",
})
assert.Nil(t, err)
assert.NotNil(t, action)
}
func TestEmailNotifier(t *testing.T) {
t.Parallel()
action, err := makeAction("alert", "email", map[string]string{
"username": "mike",
"password": "fuzzbucket",
"smtp_server": "smtp.example.com",
"to_email": "[email protected]",
})
assert.Nil(t, err)
assert.NotNil(t, action)
}
func TestUnauthenticatedEmailNotifier(t *testing.T) {
t.Parallel()
action, err := makeAction("alert", "email", map[string]string{
"smtp_server": "smtp.example.com",
"to_email": "[email protected]",
})
assert.Nil(t, err)
assert.NotNil(t, action)
}
func TestInvalidNotifier(t *testing.T) {
t.Parallel()
action, err := makeAction("alert", "emaul", map[string]string{})
assert.NotNil(t, err)
assert.Nil(t, action)
}
func TestMissingEmailNotifier(t *testing.T) {
t.Parallel()
action, err := makeAction("alert", "email", map[string]string{
"username": "mike",
"password": "fuzzbucket",
"to_email": "[email protected]",
})
assert.NotNil(t, err)
assert.Nil(t, action)
}
func TestEmailEventRuleFailed(t *testing.T) {
t.Parallel()
alert := validRuleEvent(RuleFailed)
err := validEmailSetup().TriggerEmail(alert, func(e *EmailNotifier, doc bytes.Buffer) error {
content := doc.String()
assert.True(t, strings.Index(content, "[mysql]") > 0, "email does not contain expected content: "+content)
assert.True(t, strings.Index(content, "memory:rss") > 0, "email does not contain expected content: "+content)
return nil
})
assert.Nil(t, err)
}
func TestEmailEventProcessExists(t *testing.T) {
t.Parallel()
alert := validProcessEvent(ProcessExists)
err := validEmailSetup().TriggerEmail(alert, func(e *EmailNotifier, doc bytes.Buffer) error {
content := doc.String()
assert.True(t, strings.Index(content, "PID 100") > 0, "email does not contain expected content")
assert.True(t, strings.Index(content, "The mysql service") > 0, "email does not contain expected content")
return nil
})
assert.Nil(t, err)
}
func TestEmailEventProcessDoesNotExist(t *testing.T) {
t.Parallel()
alert := validProcessEvent(ProcessDoesNotExist)
err := validEmailSetup().TriggerEmail(alert, func(e *EmailNotifier, doc bytes.Buffer) error {
content := doc.String()
assert.True(t, strings.Index(content, "can't locate") > 0, "email does not contain expected content")
assert.True(t, strings.Index(content, "the mysql service") > 0, "email does not contain expected content")
return nil
})
assert.Nil(t, err)
}
func TestEmailEventRuleRecovered(t *testing.T) {
t.Parallel()
alert := validRuleEvent(RuleRecovered)
err := validEmailSetup().TriggerEmail(alert, func(e *EmailNotifier, doc bytes.Buffer) error {
content := doc.String()
assert.True(t, strings.Index(content, "has recovered") > 0, "email does not contain expected content")
assert.True(t, strings.Index(content, "[mysql]") > 0, "email does not contain expected content")
return nil
})
assert.Nil(t, err)
}
func validRuleEvent(etype EventType) *Event {
svc := &Service{&Entity{"mysql", nil, metrics.NewProcessStore("/proc", 15), nil}, nil, services.WithStatus(100, services.Up), nil}
return &Event{
etype, svc, &Rule{svc, "memory", "rss", GT, "64m", 64 * 1024 * 1024, 0, false, 1, 0, Ok, []Action{mockAction()}},
}
}
func validProcessEvent(etype EventType) *Event {
svc := &Service{&Entity{"mysql", nil, metrics.NewProcessStore("/proc", 15), nil}, nil, services.WithStatus(100, services.Up), nil}
return &Event{etype, svc, nil}
}
func validEmailSetup() *EmailNotifier {
return &EmailNotifier{
"mike", "fuzzbucket", "smtp.gmail.com", "[email protected]", "[email protected]", ""}
}
type TestAction struct {
events []*Event
}
func (t *TestAction) Latest() *Event {
if len(t.events) == 0 {
return nil
}
return t.events[len(t.events)-1]
}
func (t *TestAction) Size() int {
return len(t.events)
}
func (t *TestAction) Trigger(e *Event) error {
t.events = append(t.events, e)
return nil
}
func mockAction() *TestAction {
return &TestAction{}
}