minitor-go/main_test.go

214 lines
4.2 KiB
Go
Raw Normal View History

package main
import "testing"
2022-01-27 00:34:31 +00:00
func Ptr[T any](v T) *T {
return &v
}
func TestCheckMonitors(t *testing.T) {
cases := []struct {
2019-10-04 23:17:36 +00:00
config Config
expectErr bool
name string
}{
{
config: Config{
2022-01-27 00:34:31 +00:00
CheckIntervalStr: "1s",
Monitors: []*Monitor{
2022-01-22 04:39:28 +00:00
{
Name: "Success",
2022-01-27 00:34:31 +00:00
Command: []string{"true"},
},
},
},
2019-10-04 23:17:36 +00:00
expectErr: false,
name: "Monitor success, no alerts",
},
{
config: Config{
2022-01-27 00:34:31 +00:00
CheckIntervalStr: "1s",
Monitors: []*Monitor{
2022-01-22 04:39:28 +00:00
{
Name: "Failure",
2022-01-27 00:34:31 +00:00
Command: []string{"false"},
AlertAfter: Ptr(1),
},
},
},
2022-01-27 00:34:31 +00:00
expectErr: true,
2021-01-08 23:31:22 +00:00
name: "Monitor failure, no alerts",
},
{
config: Config{
2022-01-27 00:34:31 +00:00
CheckIntervalStr: "1s",
Monitors: []*Monitor{
2022-01-22 04:39:28 +00:00
{
Name: "Success",
2022-01-27 00:34:31 +00:00
Command: []string{"ls"},
alertCount: 1,
},
2021-01-08 23:31:22 +00:00
},
},
expectErr: false,
name: "Monitor recovery, no alerts",
},
{
config: Config{
2022-01-27 00:34:31 +00:00
CheckIntervalStr: "1s",
2021-01-08 23:31:22 +00:00
Monitors: []*Monitor{
2022-01-22 04:39:28 +00:00
{
2021-01-08 23:31:22 +00:00
Name: "Failure",
2022-01-27 00:34:31 +00:00
Command: []string{"false"},
2021-01-08 23:31:22 +00:00
AlertDown: []string{"unknown"},
2022-01-27 00:34:31 +00:00
AlertAfter: Ptr(1),
2021-01-08 23:31:22 +00:00
},
},
},
expectErr: true,
name: "Monitor failure, unknown alerts",
},
{
config: Config{
2022-01-27 00:34:31 +00:00
CheckIntervalStr: "1s",
2021-01-08 23:31:22 +00:00
Monitors: []*Monitor{
2022-01-22 04:39:28 +00:00
{
Name: "Success",
2022-01-27 00:34:31 +00:00
Command: []string{"true"},
AlertUp: []string{"unknown"},
alertCount: 1,
},
},
},
2021-01-08 23:31:22 +00:00
expectErr: true,
name: "Monitor recovery, unknown alerts",
},
{
config: Config{
2022-01-27 00:34:31 +00:00
CheckIntervalStr: "1s",
Monitors: []*Monitor{
2022-01-22 04:39:28 +00:00
{
Name: "Failure",
2022-01-27 00:34:31 +00:00
Command: []string{"false"},
AlertDown: []string{"good"},
2022-01-27 00:34:31 +00:00
AlertAfter: Ptr(1),
},
},
2022-01-27 00:34:31 +00:00
Alerts: []*Alert{{
Name: "good",
Command: []string{"true"},
}},
},
2019-10-04 23:17:36 +00:00
expectErr: false,
name: "Monitor failure, successful alert",
},
{
config: Config{
2022-01-27 00:34:31 +00:00
CheckIntervalStr: "1s",
Monitors: []*Monitor{
2022-01-22 04:39:28 +00:00
{
Name: "Failure",
2022-01-27 00:34:31 +00:00
Command: []string{"false"},
AlertDown: []string{"bad"},
2022-01-27 00:34:31 +00:00
AlertAfter: Ptr(1),
},
},
2022-01-27 00:34:31 +00:00
Alerts: []*Alert{{
Name: "bad",
Command: []string{"false"},
}},
},
2019-10-04 23:17:36 +00:00
expectErr: true,
name: "Monitor failure, bad alert",
},
}
for _, c := range cases {
2024-11-14 19:35:26 +00:00
c := c
2021-01-08 23:31:22 +00:00
2024-11-14 19:35:26 +00:00
t.Run(c.name, func(t *testing.T) {
t.Parallel()
err := c.config.Init()
if err != nil {
t.Errorf("checkMonitors(%s): unexpected error reading config: %v", c.name, err)
}
err = checkMonitors(&c.config)
if err == nil && c.expectErr {
t.Errorf("checkMonitors(%s): Expected panic, the code did not panic", c.name)
} else if err != nil && !c.expectErr {
t.Errorf("checkMonitors(%s): Did not expect an error, but we got one anyway: %v", c.name, err)
}
})
}
}
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{
2022-01-27 00:34:31 +00:00
Alerts: []*Alert{
{
Name: "good",
Command: []string{"true"},
},
},
},
expectErr: false,
startupAlerts: []string{"good"},
name: "Successful alert",
},
{
config: Config{
2022-01-27 00:34:31 +00:00
Alerts: []*Alert{
{
Name: "bad",
2022-01-27 00:34:31 +00:00
Command: []string{"false"},
},
},
},
expectErr: true,
startupAlerts: []string{"bad"},
name: "Failed alert",
},
}
for _, c := range cases {
2024-11-14 19:35:26 +00:00
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
err := c.config.Init()
if err != nil {
t.Errorf("sendFirstRunAlerts(%s): unexpected error reading config: %v", c.name, err)
}
2024-11-14 19:35:26 +00:00
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)
}
})
}
}