2019-10-04 22:46:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func TestCheckMonitors(t *testing.T) {
|
|
|
|
cases := []struct {
|
2019-10-04 23:17:36 +00:00
|
|
|
config Config
|
|
|
|
expectErr bool
|
|
|
|
name string
|
2019-10-04 22:46:49 +00:00
|
|
|
}{
|
|
|
|
{
|
2019-10-04 23:17:36 +00:00
|
|
|
config: Config{},
|
|
|
|
expectErr: false,
|
|
|
|
name: "Empty",
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
config: Config{
|
|
|
|
Monitors: []*Monitor{
|
|
|
|
&Monitor{
|
|
|
|
Name: "Success",
|
2020-02-16 21:25:11 +00:00
|
|
|
Command: CommandOrShell{Command: []string{"true"}},
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-10-04 23:17:36 +00:00
|
|
|
expectErr: false,
|
|
|
|
name: "Monitor success, no alerts",
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
config: Config{
|
|
|
|
Monitors: []*Monitor{
|
|
|
|
&Monitor{
|
|
|
|
Name: "Failure",
|
2020-02-16 21:25:11 +00:00
|
|
|
Command: CommandOrShell{Command: []string{"false"}},
|
2019-10-04 22:46:49 +00:00
|
|
|
AlertAfter: 1,
|
|
|
|
},
|
|
|
|
&Monitor{
|
|
|
|
Name: "Failure",
|
2020-02-16 21:25:11 +00:00
|
|
|
Command: CommandOrShell{Command: []string{"false"}},
|
2019-10-04 22:46:49 +00:00
|
|
|
AlertDown: []string{"unknown"},
|
|
|
|
AlertAfter: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-10-04 23:17:36 +00:00
|
|
|
expectErr: false,
|
|
|
|
name: "Monitor failure, no and unknown alerts",
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
config: Config{
|
|
|
|
Monitors: []*Monitor{
|
|
|
|
&Monitor{
|
|
|
|
Name: "Success",
|
2020-02-16 21:25:11 +00:00
|
|
|
Command: CommandOrShell{Command: []string{"ls"}},
|
2019-10-04 22:46:49 +00:00
|
|
|
alertCount: 1,
|
|
|
|
},
|
|
|
|
&Monitor{
|
|
|
|
Name: "Success",
|
2020-02-16 21:25:11 +00:00
|
|
|
Command: CommandOrShell{Command: []string{"true"}},
|
2019-10-04 22:46:49 +00:00
|
|
|
AlertUp: []string{"unknown"},
|
|
|
|
alertCount: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-10-04 23:17:36 +00:00
|
|
|
expectErr: false,
|
|
|
|
name: "Monitor recovery, no alerts",
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
config: Config{
|
|
|
|
Monitors: []*Monitor{
|
|
|
|
&Monitor{
|
|
|
|
Name: "Failure",
|
2020-02-16 21:25:11 +00:00
|
|
|
Command: CommandOrShell{Command: []string{"false"}},
|
2019-10-04 22:46:49 +00:00
|
|
|
AlertDown: []string{"good"},
|
|
|
|
AlertAfter: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Alerts: map[string]*Alert{
|
|
|
|
"good": &Alert{
|
2020-02-16 21:25:11 +00:00
|
|
|
Command: CommandOrShell{Command: []string{"true"}},
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-10-04 23:17:36 +00:00
|
|
|
expectErr: false,
|
|
|
|
name: "Monitor failure, successful alert",
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
config: Config{
|
|
|
|
Monitors: []*Monitor{
|
|
|
|
&Monitor{
|
|
|
|
Name: "Failure",
|
2020-02-16 21:25:11 +00:00
|
|
|
Command: CommandOrShell{Command: []string{"false"}},
|
2019-10-04 22:46:49 +00:00
|
|
|
AlertDown: []string{"bad"},
|
|
|
|
AlertAfter: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Alerts: map[string]*Alert{
|
|
|
|
"bad": &Alert{
|
|
|
|
Name: "bad",
|
2020-02-16 21:25:11 +00:00
|
|
|
Command: CommandOrShell{Command: []string{"false"}},
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-10-04 23:17:36 +00:00
|
|
|
expectErr: true,
|
|
|
|
name: "Monitor failure, bad alert",
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2019-10-04 23:17:36 +00:00
|
|
|
c.config.Init()
|
|
|
|
err := checkMonitors(&c.config)
|
|
|
|
if err == nil && c.expectErr {
|
|
|
|
t.Errorf("checkMonitors(%s): Expected panic, the code did not panic", c.name)
|
|
|
|
}
|
2019-10-04 22:46:49 +00:00
|
|
|
}
|
|
|
|
}
|