Add some test cases to cover main

These don't include any asserts, but they do check panics
This commit is contained in:
IamTheFij 2019-10-04 15:46:49 -07:00
parent 6aaeeb32d4
commit f99ee9891e
2 changed files with 130 additions and 4 deletions

12
main.go
View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
"log"
"time"
)
@ -12,11 +13,14 @@ func checkMonitors(config *Config) {
// Should probably consider refactoring everything below here
if alertNotice != nil {
log.Printf("DEBUG: Recieved an alert notice: %v", alertNotice)
log.Printf("DEBUG: Recieved an alert notice from %s", alertNotice.MonitorName)
alertNames := monitor.GetAlertNames(alertNotice.IsUp)
if alertNames == nil {
// TODO: Should this be a panic? Should this be validated against? Probably
log.Printf("WARNING: Found alert, but no alert mechanisms exist: %v", alertNotice)
log.Printf(
"WARNING: Recieved alert, but no alert mechanisms exist. MonitorName=%s IsUp=%t",
alertNotice.MonitorName, alertNotice.IsUp,
)
}
for _, alertName := range alertNames {
if alert, ok := config.Alerts[alertName]; ok {
@ -29,12 +33,12 @@ func checkMonitors(config *Config) {
output,
)
// TODO: Maybe return this error instead of panicking here
log.Fatalf(
panic(fmt.Errorf(
"ERROR: Unsuccessfully triggered alert '%s'. "+
"Crashing to avoid false negatives: %v",
alert.Name,
err,
)
))
}
} else {
// TODO: Maybe panic here. Also, probably validate up front

122
main_test.go Normal file
View File

@ -0,0 +1,122 @@
package main
import "testing"
func TestCheckMonitors(t *testing.T) {
cases := []struct {
config Config
expectPanic bool
name string
}{
{
config: Config{},
expectPanic: false,
name: "Empty",
},
{
config: Config{
Monitors: []*Monitor{
&Monitor{
Name: "Success",
Command: []string{"true"},
},
},
},
expectPanic: false,
name: "Monitor success, no alerts",
},
{
config: Config{
Monitors: []*Monitor{
&Monitor{
Name: "Failure",
Command: []string{"false"},
AlertAfter: 1,
},
&Monitor{
Name: "Failure",
Command: []string{"false"},
AlertDown: []string{"unknown"},
AlertAfter: 1,
},
},
},
expectPanic: false,
name: "Monitor failure, no and unknown alerts",
},
{
config: Config{
Monitors: []*Monitor{
&Monitor{
Name: "Success",
Command: []string{"ls"},
alertCount: 1,
},
&Monitor{
Name: "Success",
Command: []string{"true"},
AlertUp: []string{"unknown"},
alertCount: 1,
},
},
},
expectPanic: false,
name: "Monitor recovery, no alerts",
},
{
config: Config{
Monitors: []*Monitor{
&Monitor{
Name: "Failure",
Command: []string{"false"},
AlertDown: []string{"good"},
AlertAfter: 1,
},
},
Alerts: map[string]*Alert{
"good": &Alert{
Command: []string{"true"},
},
},
},
expectPanic: false,
name: "Monitor failure, successful alert",
},
{
config: Config{
Monitors: []*Monitor{
&Monitor{
Name: "Failure",
Command: []string{"false"},
AlertDown: []string{"bad"},
AlertAfter: 1,
},
},
Alerts: map[string]*Alert{
"bad": &Alert{
Name: "bad",
Command: []string{"false"},
},
},
},
expectPanic: true,
name: "Monitor failure, bad alert",
},
}
for _, c := range cases {
// Create new function so that deferred recovery can run at end of each test case
func() {
// Set up recover to catch panic
defer func() {
if r := recover(); r == nil {
if c.expectPanic {
t.Errorf("checkMonitors(%s): Expected panic, the code did not panic", c.name)
}
}
}()
c.config.Init()
checkMonitors(&c.config)
}()
}
}