minitor-go/monitor_test.go

287 lines
8.5 KiB
Go
Raw Normal View History

package main_test
import (
"errors"
"reflect"
"testing"
"time"
m "git.iamthefij.com/iamthefij/minitor-go"
)
func TestMonitorValidate(t *testing.T) {
t.Parallel()
cases := []struct {
monitor m.Monitor
expected error
name string
}{
{m.Monitor{AlertAfter: 1, Command: []string{"echo", "test"}, AlertDown: []string{"log"}}, nil, "Command only"},
{m.Monitor{AlertAfter: 1, ShellCommand: "echo test", AlertDown: []string{"log"}}, nil, "CommandShell only"},
{m.Monitor{AlertAfter: 1, Command: []string{"echo", "test"}}, m.ErrInvalidMonitor, "No AlertDown"},
{m.Monitor{AlertAfter: 1, AlertDown: []string{"log"}}, m.ErrInvalidMonitor, "No commands"},
{m.Monitor{AlertAfter: -1, Command: []string{"echo", "test"}, AlertDown: []string{"log"}}, m.ErrInvalidMonitor, "Invalid alert threshold, -1"},
}
for _, c := range cases {
2024-11-14 19:35:26 +00:00
c := c
2021-05-11 04:00:58 +00:00
2024-11-14 19:35:26 +00:00
t.Run(c.name, func(t *testing.T) {
t.Parallel()
2021-05-11 04:00:58 +00:00
actual := c.monitor.Validate()
hasErr := (actual != nil)
expectErr := (c.expected != nil)
if hasErr != expectErr || !errors.Is(actual, c.expected) {
2024-11-14 19:35:26 +00:00
t.Errorf("IsValid(%v), expected=%t actual=%t", c.name, c.expected, actual)
}
})
}
}
// TestMonitorShouldCheck tests the Monitor.ShouldCheck()
func TestMonitorShouldCheck(t *testing.T) {
t.Parallel()
// Create a monitor that should check every second and then verify it checks with some sleeps
monitor := m.Monitor{ShellCommand: "true", CheckInterval: time.Second}
if !monitor.ShouldCheck() {
t.Errorf("New monitor should be ready to check")
}
monitor.Check()
2024-11-14 19:35:26 +00:00
if monitor.ShouldCheck() {
t.Errorf("Monitor should not be ready to check after a check")
}
2024-11-14 19:35:26 +00:00
time.Sleep(time.Second)
if !monitor.ShouldCheck() {
t.Errorf("Monitor should be ready to check after a second")
}
}
// TestMonitorIsUp tests the Monitor.IsUp()
func TestMonitorIsUp(t *testing.T) {
t.Parallel()
// Creating a monitor that should alert after 2 failures. The monitor should be considered up until we reach two failed checks
monitor := m.Monitor{ShellCommand: "false", AlertAfter: 2}
if !monitor.IsUp() {
t.Errorf("New monitor should be considered up")
}
monitor.Check()
2021-05-11 04:00:58 +00:00
if !monitor.IsUp() {
t.Errorf("Monitor should be considered up with one failure and no alerts")
}
2021-05-11 04:00:58 +00:00
monitor.Check()
if monitor.IsUp() {
t.Errorf("Monitor should be considered down with one alert")
}
}
// TestMonitorGetAlertNames tests that proper alert names are returned
func TestMonitorGetAlertNames(t *testing.T) {
cases := []struct {
monitor m.Monitor
up bool
expected []string
name string
}{
{m.Monitor{}, true, nil, "Empty up"},
{m.Monitor{}, false, nil, "Empty down"},
{m.Monitor{AlertUp: []string{"alert"}}, true, []string{"alert"}, "Return up"},
{m.Monitor{AlertDown: []string{"alert"}}, false, []string{"alert"}, "Return down"},
}
for _, c := range cases {
2024-11-14 19:35:26 +00:00
c := c
2021-05-11 04:00:58 +00:00
2024-11-14 19:35:26 +00:00
t.Run(c.name, func(t *testing.T) {
t.Parallel()
2021-05-11 04:00:58 +00:00
2024-11-14 19:35:26 +00:00
actual := c.monitor.GetAlertNames(c.up)
if !reflect.DeepEqual(actual, c.expected) {
2024-11-14 19:35:26 +00:00
t.Errorf("GetAlertNames(%v), expected=%v actual=%v", c.name, c.expected, actual)
}
})
}
}
// TestMonitorFailureAlertAfter tests that alerts will not trigger until
// hitting the threshold provided by AlertAfter
func TestMonitorFailureAlertAfter(t *testing.T) {
var alertEveryOne int = 1
cases := []struct {
monitor m.Monitor
numChecks int
expectNotice bool
name string
}{
{m.Monitor{ShellCommand: "false", AlertAfter: 1}, 1, true, "Empty After 1"}, // Defaults to true because and AlertEvery default to 0
{m.Monitor{ShellCommand: "false", AlertAfter: 1, AlertEvery: &alertEveryOne}, 1, true, "Alert after 1: first failure"},
{m.Monitor{ShellCommand: "false", AlertAfter: 1, AlertEvery: &alertEveryOne}, 2, true, "Alert after 1: second failure"},
{m.Monitor{ShellCommand: "false", AlertAfter: 20, AlertEvery: &alertEveryOne}, 1, false, "Alert after 20: first failure"},
{m.Monitor{ShellCommand: "false", AlertAfter: 20, AlertEvery: &alertEveryOne}, 20, true, "Alert after 20: 20th failure"},
{m.Monitor{ShellCommand: "false", AlertAfter: 20, AlertEvery: &alertEveryOne}, 21, true, "Alert after 20: 21st failure"},
}
for _, c := range cases {
2024-11-14 19:35:26 +00:00
c := c
2021-05-11 04:00:58 +00:00
2024-11-14 19:35:26 +00:00
t.Run(c.name, func(t *testing.T) {
t.Parallel()
2021-05-11 04:00:58 +00:00
hasNotice := false
for i := 0; i < c.numChecks; i++ {
_, notice := c.monitor.Check()
hasNotice = (notice != nil)
}
2021-05-11 04:00:58 +00:00
2024-11-14 19:35:26 +00:00
if hasNotice != c.expectNotice {
t.Errorf("failure(%v), expected=%t actual=%t", c.name, c.expectNotice, hasNotice)
}
})
}
}
// TestMonitorFailureAlertEvery tests that alerts will trigger
// on the expected intervals
func TestMonitorFailureAlertEvery(t *testing.T) {
cases := []struct {
monitor m.Monitor
expectedNotice []bool
name string
}{
{m.Monitor{ShellCommand: "false", AlertAfter: 1}, []bool{true}, "No AlertEvery set"}, // Defaults to true because AlertAfter and AlertEvery default to nil
// Alert first time only, after 1
{m.Monitor{ShellCommand: "false", AlertAfter: 1, AlertEvery: Ptr(0)}, []bool{true, false, false}, "Alert first time only after 1"},
// Alert every time, after 1
{m.Monitor{ShellCommand: "false", AlertAfter: 1, AlertEvery: Ptr(1)}, []bool{true, true, true}, "Alert every time after 1"},
// Alert every other time, after 1
{m.Monitor{ShellCommand: "false", AlertAfter: 1, AlertEvery: Ptr(2)}, []bool{true, false, true, false}, "Alert every other time after 1"},
}
for _, c := range cases {
2024-11-14 19:35:26 +00:00
c := c
2019-10-03 03:52:34 +00:00
2024-11-14 19:35:26 +00:00
t.Run(c.name, func(t *testing.T) {
t.Parallel()
2021-05-11 04:00:58 +00:00
for i, expectNotice := range c.expectedNotice {
_, notice := c.monitor.Check()
hasNotice := (notice != nil)
2021-05-11 04:00:58 +00:00
if hasNotice != expectNotice {
t.Errorf("failed %s check %d: expected=%t actual=%t", c.name, i, expectNotice, hasNotice)
}
2024-11-14 19:35:26 +00:00
}
})
2019-10-03 03:52:34 +00:00
}
}
// TestMonitorFailureExponential tests that alerts will trigger
// with an exponential backoff after repeated failures
func TestMonitorFailureExponential(t *testing.T) {
2022-01-27 00:34:31 +00:00
var alertEveryExp int = -1
2019-10-03 03:52:34 +00:00
cases := []struct {
expectNotice bool
name string
}{
{true, "Alert exponential after 1: first failure"},
{true, "Alert exponential after 1: second failure"},
{false, "Alert exponential after 1: third failure"},
{true, "Alert exponential after 1: fourth failure"},
{false, "Alert exponential after 1: fifth failure"},
{false, "Alert exponential after 1: sixth failure"},
{false, "Alert exponential after 1: seventh failure"},
{true, "Alert exponential after 1: eighth failure"},
}
// Unlike previous tests, this one requires a static Monitor with repeated
// calls to the failure method
monitor := m.Monitor{ShellCommand: "false", AlertAfter: 1, AlertEvery: &alertEveryExp}
2021-05-11 04:00:58 +00:00
2019-10-03 03:52:34 +00:00
for _, c := range cases {
2024-11-14 19:35:26 +00:00
t.Run(c.name, func(t *testing.T) {
// NOTE: These tests are not parallel because they rely on the state of the Monitor
_, notice := monitor.Check()
2024-11-14 19:35:26 +00:00
hasNotice := (notice != nil)
if hasNotice != c.expectNotice {
t.Errorf("failure(%v), expected=%t actual=%t", c.name, c.expectNotice, hasNotice)
}
})
}
}
2019-10-03 16:22:13 +00:00
// TestMonitorCheck tests successful and failed commands and shell commands
func TestMonitorCheck(t *testing.T) {
type expected struct {
isSuccess bool
hasNotice bool
lastOutput string
}
2021-05-11 04:00:58 +00:00
2019-10-03 16:22:13 +00:00
cases := []struct {
monitor m.Monitor
2019-10-03 16:22:13 +00:00
expect expected
name string
}{
{
m.Monitor{AlertAfter: 1, Command: []string{"echo", "success"}},
2019-10-03 16:22:13 +00:00
expected{isSuccess: true, hasNotice: false, lastOutput: "success\n"},
"Test successful command",
},
{
m.Monitor{AlertAfter: 1, ShellCommand: "echo success"},
2019-10-03 16:22:13 +00:00
expected{isSuccess: true, hasNotice: false, lastOutput: "success\n"},
"Test successful command shell",
},
{
m.Monitor{AlertAfter: 1, Command: []string{"total", "failure"}},
2019-10-03 16:22:13 +00:00
expected{isSuccess: false, hasNotice: true, lastOutput: ""},
"Test failed command",
},
{
m.Monitor{AlertAfter: 1, ShellCommand: "false"},
expected{isSuccess: false, hasNotice: true, lastOutput: ""},
2019-10-03 16:22:13 +00:00
"Test failed command shell",
},
}
for _, c := range cases {
2024-11-14 19:35:26 +00:00
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
isSuccess, notice := c.monitor.Check()
if isSuccess != c.expect.isSuccess {
t.Errorf("Check(%v) (success), expected=%t actual=%t", c.name, c.expect.isSuccess, isSuccess)
}
hasNotice := (notice != nil)
if hasNotice != c.expect.hasNotice {
t.Errorf("Check(%v) (notice), expected=%t actual=%t", c.name, c.expect.hasNotice, hasNotice)
}
lastOutput := c.monitor.LastOutput()
2024-11-14 19:35:26 +00:00
if lastOutput != c.expect.lastOutput {
t.Errorf("Check(%v) (output), expected=%v actual=%v", c.name, c.expect.lastOutput, lastOutput)
}
})
2019-10-03 16:22:13 +00:00
}
}