2024-11-15 19:30:34 +00:00
|
|
|
package main_test
|
2019-10-03 01:01:55 +00:00
|
|
|
|
|
|
|
import (
|
2024-11-16 00:36:45 +00:00
|
|
|
"errors"
|
2024-11-15 19:30:34 +00:00
|
|
|
"reflect"
|
2019-10-03 01:01:55 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2024-11-15 19:30:34 +00:00
|
|
|
|
|
|
|
m "git.iamthefij.com/iamthefij/minitor-go"
|
2019-10-03 01:01:55 +00:00
|
|
|
)
|
|
|
|
|
2024-11-16 00:36:45 +00:00
|
|
|
func TestMonitorValidate(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-10-03 01:01:55 +00:00
|
|
|
cases := []struct {
|
2024-11-15 19:30:34 +00:00
|
|
|
monitor m.Monitor
|
2024-11-16 00:36:45 +00:00
|
|
|
expected error
|
2019-10-03 01:01:55 +00:00
|
|
|
name string
|
|
|
|
}{
|
2024-11-16 00:36:45 +00:00
|
|
|
{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"},
|
2019-10-03 01:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-16 00:36:45 +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)
|
|
|
|
}
|
|
|
|
})
|
2019-10-03 01:01:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestMonitorShouldCheck tests the Monitor.ShouldCheck()
|
|
|
|
func TestMonitorShouldCheck(t *testing.T) {
|
2024-11-15 19:30:34 +00:00
|
|
|
t.Parallel()
|
2019-10-03 01:01:55 +00:00
|
|
|
|
2024-11-15 19:30:34 +00:00
|
|
|
// 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")
|
2019-10-03 01:01:55 +00:00
|
|
|
}
|
|
|
|
|
2024-11-15 19:30:34 +00:00
|
|
|
monitor.Check()
|
2024-11-14 19:35:26 +00:00
|
|
|
|
2024-11-15 19:30:34 +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
|
|
|
|
2024-11-15 19:30:34 +00:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
if !monitor.ShouldCheck() {
|
|
|
|
t.Errorf("Monitor should be ready to check after a second")
|
2019-10-03 01:01:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 00:09:56 +00:00
|
|
|
// TestMonitorIsUp tests the Monitor.IsUp()
|
2019-10-03 01:01:55 +00:00
|
|
|
func TestMonitorIsUp(t *testing.T) {
|
2024-11-15 19:30:34 +00:00
|
|
|
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")
|
2019-10-03 01:01:55 +00:00
|
|
|
}
|
|
|
|
|
2024-11-15 19:30:34 +00:00
|
|
|
monitor.Check()
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2024-11-15 19:30:34 +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
|
|
|
|
2024-11-15 19:30:34 +00:00
|
|
|
monitor.Check()
|
|
|
|
|
|
|
|
if monitor.IsUp() {
|
|
|
|
t.Errorf("Monitor should be considered down with one alert")
|
2019-10-03 01:01:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-04 21:47:38 +00:00
|
|
|
|
|
|
|
// TestMonitorGetAlertNames tests that proper alert names are returned
|
|
|
|
func TestMonitorGetAlertNames(t *testing.T) {
|
|
|
|
cases := []struct {
|
2024-11-15 19:30:34 +00:00
|
|
|
monitor m.Monitor
|
2019-10-04 21:47:38 +00:00
|
|
|
up bool
|
|
|
|
expected []string
|
|
|
|
name string
|
|
|
|
}{
|
2024-11-15 19:30:34 +00:00
|
|
|
{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"},
|
2019-10-04 21:47:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-11-15 19:30:34 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2019-10-04 21:47:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-03 01:01:55 +00:00
|
|
|
|
|
|
|
// TestMonitorFailureAlertAfter tests that alerts will not trigger until
|
|
|
|
// hitting the threshold provided by AlertAfter
|
|
|
|
func TestMonitorFailureAlertAfter(t *testing.T) {
|
2024-11-15 19:30:34 +00:00
|
|
|
var alertEveryOne int = 1
|
2022-12-19 19:15:34 +00:00
|
|
|
|
2019-10-03 01:01:55 +00:00
|
|
|
cases := []struct {
|
2024-11-15 19:30:34 +00:00
|
|
|
monitor m.Monitor
|
|
|
|
numChecks int
|
2019-10-03 01:01:55 +00:00
|
|
|
expectNotice bool
|
|
|
|
name string
|
|
|
|
}{
|
2024-11-15 19:30:34 +00:00
|
|
|
{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"},
|
2019-10-03 01:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-15 19:30:34 +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)
|
|
|
|
}
|
|
|
|
})
|
2019-10-03 01:01:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestMonitorFailureAlertEvery tests that alerts will trigger
|
|
|
|
// on the expected intervals
|
|
|
|
func TestMonitorFailureAlertEvery(t *testing.T) {
|
|
|
|
cases := []struct {
|
2024-11-15 19:30:34 +00:00
|
|
|
monitor m.Monitor
|
|
|
|
expectedNotice []bool
|
|
|
|
name string
|
2019-10-03 01:01:55 +00:00
|
|
|
}{
|
2024-11-15 19:30:34 +00:00
|
|
|
{m.Monitor{ShellCommand: "false", AlertAfter: 1}, []bool{true}, "No AlertEvery set"}, // Defaults to true because AlertAfter and AlertEvery default to nil
|
2019-10-03 01:01:55 +00:00
|
|
|
// Alert first time only, after 1
|
2024-11-15 19:30:34 +00:00
|
|
|
{m.Monitor{ShellCommand: "false", AlertAfter: 1, AlertEvery: Ptr(0)}, []bool{true, false, false}, "Alert first time only after 1"},
|
2019-10-03 01:01:55 +00:00
|
|
|
// Alert every time, after 1
|
2024-11-15 19:30:34 +00:00
|
|
|
{m.Monitor{ShellCommand: "false", AlertAfter: 1, AlertEvery: Ptr(1)}, []bool{true, true, true}, "Alert every time after 1"},
|
2019-10-03 01:01:55 +00:00
|
|
|
// Alert every other time, after 1
|
2024-11-15 19:30:34 +00:00
|
|
|
{m.Monitor{ShellCommand: "false", AlertAfter: 1, AlertEvery: Ptr(2)}, []bool{true, false, true, false}, "Alert every other time after 1"},
|
2019-10-03 01:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-11-15 19:30:34 +00:00
|
|
|
for i, expectNotice := range c.expectedNotice {
|
|
|
|
_, notice := c.monitor.Check()
|
|
|
|
hasNotice := (notice != nil)
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2024-11-15 19:30:34 +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
|
2022-12-19 19:15:34 +00:00
|
|
|
|
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
|
2024-11-15 19:30:34 +00:00
|
|
|
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
|
2024-11-15 19:30:34 +00:00
|
|
|
_, 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 01:01:55 +00:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2024-11-15 19:30:34 +00:00
|
|
|
monitor m.Monitor
|
2019-10-03 16:22:13 +00:00
|
|
|
expect expected
|
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
m.Monitor{AlertAfter: 1, ShellCommand: "false"},
|
2019-10-07 22:53:41 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-11-15 19:30:34 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|