2024-11-15 19:30:34 +00:00
|
|
|
package main_test
|
2019-10-04 22:46:49 +00:00
|
|
|
|
2024-11-15 19:30:34 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
m "git.iamthefij.com/iamthefij/minitor-go"
|
|
|
|
)
|
2019-10-04 22:46:49 +00:00
|
|
|
|
2022-01-27 00:34:31 +00:00
|
|
|
func Ptr[T any](v T) *T {
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
|
2024-11-15 19:30:34 +00:00
|
|
|
// TestCheckConfig tests the checkConfig function
|
|
|
|
// It also tests results for potentially invalid configuration. For example, no alerts
|
2019-10-04 22:46:49 +00:00
|
|
|
func TestCheckMonitors(t *testing.T) {
|
|
|
|
cases := []struct {
|
2024-11-15 19:30:34 +00:00
|
|
|
config m.Config
|
|
|
|
expectFailureError bool
|
|
|
|
expectRecoverError bool
|
|
|
|
name string
|
2019-10-04 22:46:49 +00:00
|
|
|
}{
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
config: m.Config{
|
2022-01-27 00:34:31 +00:00
|
|
|
CheckIntervalStr: "1s",
|
2024-11-15 19:30:34 +00:00
|
|
|
Monitors: []*m.Monitor{
|
2022-01-22 04:39:28 +00:00
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
Name: "Success",
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-11-15 19:30:34 +00:00
|
|
|
expectFailureError: false,
|
|
|
|
expectRecoverError: false,
|
|
|
|
name: "No alerts",
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
config: m.Config{
|
2022-01-27 00:34:31 +00:00
|
|
|
CheckIntervalStr: "1s",
|
2024-11-15 19:30:34 +00:00
|
|
|
Monitors: []*m.Monitor{
|
2022-01-22 04:39:28 +00:00
|
|
|
{
|
2021-01-08 23:31:22 +00:00
|
|
|
Name: "Failure",
|
|
|
|
AlertDown: []string{"unknown"},
|
2019-10-04 22:46:49 +00:00
|
|
|
AlertUp: []string{"unknown"},
|
2024-11-15 19:30:34 +00:00
|
|
|
AlertAfter: 1,
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-11-15 19:30:34 +00:00
|
|
|
expectFailureError: true,
|
|
|
|
expectRecoverError: true,
|
|
|
|
name: "Unknown alerts",
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
config: m.Config{
|
2022-01-27 00:34:31 +00:00
|
|
|
CheckIntervalStr: "1s",
|
2024-11-15 19:30:34 +00:00
|
|
|
Monitors: []*m.Monitor{
|
2022-01-22 04:39:28 +00:00
|
|
|
{
|
2019-10-04 22:46:49 +00:00
|
|
|
Name: "Failure",
|
|
|
|
AlertDown: []string{"good"},
|
2024-11-15 19:30:34 +00:00
|
|
|
AlertUp: []string{"good"},
|
|
|
|
AlertAfter: 1,
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
},
|
2024-11-15 19:30:34 +00:00
|
|
|
Alerts: []*m.Alert{{
|
2022-01-27 00:34:31 +00:00
|
|
|
Name: "good",
|
|
|
|
Command: []string{"true"},
|
|
|
|
}},
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
2024-11-15 19:30:34 +00:00
|
|
|
expectFailureError: false,
|
|
|
|
expectRecoverError: false,
|
|
|
|
name: "Successful alert",
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
config: m.Config{
|
2022-01-27 00:34:31 +00:00
|
|
|
CheckIntervalStr: "1s",
|
2024-11-15 19:30:34 +00:00
|
|
|
Monitors: []*m.Monitor{
|
2022-01-22 04:39:28 +00:00
|
|
|
{
|
2019-10-04 22:46:49 +00:00
|
|
|
Name: "Failure",
|
|
|
|
AlertDown: []string{"bad"},
|
2024-11-15 19:30:34 +00:00
|
|
|
AlertUp: []string{"bad"},
|
|
|
|
AlertAfter: 1,
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
},
|
2024-11-15 19:30:34 +00:00
|
|
|
Alerts: []*m.Alert{{
|
2022-01-27 00:34:31 +00:00
|
|
|
Name: "bad",
|
|
|
|
Command: []string{"false"},
|
|
|
|
}},
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
2024-11-15 19:30:34 +00:00
|
|
|
expectFailureError: true,
|
|
|
|
expectRecoverError: true,
|
|
|
|
name: "Failing alert",
|
2019-10-04 22:46:49 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-11-15 19:30:34 +00:00
|
|
|
for _, check := range []struct {
|
|
|
|
shellCmd string
|
|
|
|
name string
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{"false", "Failure", c.expectFailureError}, {"true", "Success", c.expectRecoverError},
|
|
|
|
} {
|
|
|
|
// Set the shell command for this check
|
|
|
|
c.config.Monitors[0].ShellCommand = check.shellCmd
|
|
|
|
|
|
|
|
// Run the check
|
|
|
|
err = m.CheckMonitors(&c.config)
|
|
|
|
|
|
|
|
// Check the results
|
|
|
|
if err == nil && check.expectErr {
|
|
|
|
t.Errorf("checkMonitors(%s:%s): Expected error, the code did not error", c.name, check.name)
|
|
|
|
} else if err != nil && !check.expectErr {
|
|
|
|
t.Errorf("checkMonitors(%s:%s): Did not expect an error, but we got one anyway: %v", c.name, check.name, err)
|
|
|
|
}
|
2024-11-14 19:35:26 +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 {
|
2024-11-15 19:30:34 +00:00
|
|
|
config m.Config
|
2024-04-03 19:03:17 +00:00
|
|
|
expectErr bool
|
|
|
|
startupAlerts []string
|
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
config: m.Config{
|
|
|
|
CheckIntervalStr: "1s",
|
|
|
|
},
|
2024-04-03 19:03:17 +00:00
|
|
|
expectErr: true,
|
|
|
|
startupAlerts: []string{"missing"},
|
|
|
|
name: "Unknown",
|
|
|
|
},
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
config: m.Config{
|
|
|
|
CheckIntervalStr: "1s",
|
|
|
|
Alerts: []*m.Alert{
|
2022-01-27 00:34:31 +00:00
|
|
|
{
|
|
|
|
Name: "good",
|
|
|
|
Command: []string{"true"},
|
2024-04-03 19:03:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
startupAlerts: []string{"good"},
|
|
|
|
name: "Successful alert",
|
|
|
|
},
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
config: m.Config{
|
|
|
|
CheckIntervalStr: "1s",
|
|
|
|
Alerts: []*m.Alert{
|
2022-01-27 00:34:31 +00:00
|
|
|
{
|
2024-04-03 19:03:17 +00:00
|
|
|
Name: "bad",
|
2022-01-27 00:34:31 +00:00
|
|
|
Command: []string{"false"},
|
2024-04-03 19:03:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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-04-03 19:03:17 +00:00
|
|
|
|
2024-11-15 19:30:34 +00:00
|
|
|
err = m.SendStartupAlerts(&c.config, c.startupAlerts)
|
2024-11-14 19:35:26 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2024-04-03 19:03:17 +00:00
|
|
|
}
|
|
|
|
}
|