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{
|
2022-01-22 04:39:28 +00:00
|
|
|
{
|
2019-10-04 22:46:49 +00:00
|
|
|
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{
|
2022-01-22 04:39:28 +00:00
|
|
|
{
|
2019-10-04 22:46:49 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-10-04 23:17:36 +00:00
|
|
|
expectErr: false,
|
2021-01-08 23:31:22 +00:00
|
|
|
name: "Monitor failure, no alerts",
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
config: Config{
|
|
|
|
Monitors: []*Monitor{
|
2022-01-22 04:39:28 +00:00
|
|
|
{
|
2019-10-04 22:46:49 +00:00
|
|
|
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,
|
|
|
|
},
|
2021-01-08 23:31:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
name: "Monitor recovery, no alerts",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: Config{
|
|
|
|
Monitors: []*Monitor{
|
2022-01-22 04:39:28 +00:00
|
|
|
{
|
2021-01-08 23:31:22 +00:00
|
|
|
Name: "Failure",
|
|
|
|
Command: CommandOrShell{Command: []string{"false"}},
|
|
|
|
AlertDown: []string{"unknown"},
|
|
|
|
AlertAfter: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectErr: true,
|
|
|
|
name: "Monitor failure, unknown alerts",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: Config{
|
|
|
|
Monitors: []*Monitor{
|
2022-01-22 04:39:28 +00:00
|
|
|
{
|
2019-10-04 22:46:49 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-01-08 23:31:22 +00:00
|
|
|
expectErr: true,
|
|
|
|
name: "Monitor recovery, unknown alerts",
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
config: Config{
|
|
|
|
Monitors: []*Monitor{
|
2022-01-22 04:39:28 +00:00
|
|
|
{
|
2019-10-04 22:46:49 +00:00
|
|
|
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{
|
2022-01-22 04:39:28 +00:00
|
|
|
"good": {
|
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{
|
2022-01-22 04:39:28 +00:00
|
|
|
{
|
2019-10-04 22:46:49 +00:00
|
|
|
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{
|
2022-01-22 04:39:28 +00:00
|
|
|
"bad": {
|
2019-10-04 22:46:49 +00:00
|
|
|
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 {
|
2021-01-08 23:31:22 +00:00
|
|
|
err := c.config.Init()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("checkMonitors(%s): unexpected error reading config: %v", c.name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = checkMonitors(&c.config)
|
2019-10-04 23:17:36 +00:00
|
|
|
if err == nil && c.expectErr {
|
|
|
|
t.Errorf("checkMonitors(%s): Expected panic, the code did not panic", c.name)
|
2021-01-08 23:31:22 +00:00
|
|
|
} else if err != nil && !c.expectErr {
|
|
|
|
t.Errorf("checkMonitors(%s): Did not expect an error, but we got one anyway: %v", c.name, err)
|
2019-10-04 23:17:36 +00:00
|
|
|
}
|
2019-10-04 22:46:49 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-03 19:03:17 +00:00
|
|
|
|
|
|
|
func TestFirstRunAlerts(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
config Config
|
|
|
|
expectErr bool
|
|
|
|
startupAlerts []string
|
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
config: Config{},
|
|
|
|
expectErr: false,
|
|
|
|
startupAlerts: []string{},
|
|
|
|
name: "Empty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: Config{},
|
|
|
|
expectErr: true,
|
|
|
|
startupAlerts: []string{"missing"},
|
|
|
|
name: "Unknown",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: Config{
|
|
|
|
Alerts: map[string]*Alert{
|
|
|
|
"good": {
|
|
|
|
Command: CommandOrShell{Command: []string{"true"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
startupAlerts: []string{"good"},
|
|
|
|
name: "Successful alert",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: Config{
|
|
|
|
Alerts: map[string]*Alert{
|
|
|
|
"bad": {
|
|
|
|
Name: "bad",
|
|
|
|
Command: CommandOrShell{Command: []string{"false"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectErr: true,
|
|
|
|
startupAlerts: []string{"bad"},
|
|
|
|
name: "Failed alert",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
err := c.config.Init()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("sendFirstRunAlerts(%s): unexpected error reading config: %v", c.name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sendStartupAlerts(&c.config, c.startupAlerts)
|
|
|
|
if err == nil && c.expectErr {
|
|
|
|
t.Errorf("sendFirstRunAlerts(%s): Expected error, the code did not error", c.name)
|
|
|
|
} else if err != nil && !c.expectErr {
|
|
|
|
t.Errorf("sendFirstRunAlerts(%s): Did not expect an error, but we got one anyway: %v", c.name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|