minitor-go/main_test.go
Ian Fijolek 25c5179d3d
All checks were successful
continuous-integration/drone/push Build is passing
Switch to a single key for command and command shell
This makes the configuration more similar to Minitor-py and
docker-compose. If a string is passed, it will be executed in a shell.
If an array is passed, it will be executed in as a command directly.

This breaks compatiblity with previous versions of Minitor-go, but
closer to compatiblity with Minitor-py.
2020-02-19 17:38:06 -08:00

115 lines
2.4 KiB
Go

package main
import "testing"
func TestCheckMonitors(t *testing.T) {
cases := []struct {
config Config
expectErr bool
name string
}{
{
config: Config{},
expectErr: false,
name: "Empty",
},
{
config: Config{
Monitors: []*Monitor{
&Monitor{
Name: "Success",
Command: CommandOrShell{Command: []string{"true"}},
},
},
},
expectErr: false,
name: "Monitor success, no alerts",
},
{
config: Config{
Monitors: []*Monitor{
&Monitor{
Name: "Failure",
Command: CommandOrShell{Command: []string{"false"}},
AlertAfter: 1,
},
&Monitor{
Name: "Failure",
Command: CommandOrShell{Command: []string{"false"}},
AlertDown: []string{"unknown"},
AlertAfter: 1,
},
},
},
expectErr: false,
name: "Monitor failure, no and unknown alerts",
},
{
config: Config{
Monitors: []*Monitor{
&Monitor{
Name: "Success",
Command: CommandOrShell{Command: []string{"ls"}},
alertCount: 1,
},
&Monitor{
Name: "Success",
Command: CommandOrShell{Command: []string{"true"}},
AlertUp: []string{"unknown"},
alertCount: 1,
},
},
},
expectErr: false,
name: "Monitor recovery, no alerts",
},
{
config: Config{
Monitors: []*Monitor{
&Monitor{
Name: "Failure",
Command: CommandOrShell{Command: []string{"false"}},
AlertDown: []string{"good"},
AlertAfter: 1,
},
},
Alerts: map[string]*Alert{
"good": &Alert{
Command: CommandOrShell{Command: []string{"true"}},
},
},
},
expectErr: false,
name: "Monitor failure, successful alert",
},
{
config: Config{
Monitors: []*Monitor{
&Monitor{
Name: "Failure",
Command: CommandOrShell{Command: []string{"false"}},
AlertDown: []string{"bad"},
AlertAfter: 1,
},
},
Alerts: map[string]*Alert{
"bad": &Alert{
Name: "bad",
Command: CommandOrShell{Command: []string{"false"}},
},
},
},
expectErr: true,
name: "Monitor failure, bad alert",
},
}
for _, c := range cases {
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)
}
}
}