Update defaults and add tests for them

This commit is contained in:
Ian Fijolek
2025-12-11 16:06:01 -08:00
parent 85afa3d9ef
commit 5c97f2e5c4
7 changed files with 105 additions and 29 deletions
+1 -1
View File
@@ -75,7 +75,7 @@ monitor "example" {
alert_up = ["log"] alert_up = ["log"]
check_interval = "1m" check_interval = "1m"
alert_after = 1 alert_after = 1
alert_every = 0 alert_every = -1
} }
``` ```
+2 -2
View File
@@ -101,14 +101,14 @@ func (alert *Alert) BuildTemplates() error {
} }
switch { switch {
case alert.commandTemplate == nil && alert.Command != nil: case alert.Command != nil:
alert.commandTemplate = []*template.Template{} alert.commandTemplate = []*template.Template{}
for i, cmdPart := range alert.Command { for i, cmdPart := range alert.Command {
alert.commandTemplate = append(alert.commandTemplate, template.Must( alert.commandTemplate = append(alert.commandTemplate, template.Must(
template.New(alert.Name+fmt.Sprint(i)).Funcs(timeFormatFuncs).Parse(cmdPart), template.New(alert.Name+fmt.Sprint(i)).Funcs(timeFormatFuncs).Parse(cmdPart),
)) ))
} }
case alert.commandShellTemplate == nil && alert.ShellCommand != "": case alert.ShellCommand != "":
shellCmd := alert.ShellCommand shellCmd := alert.ShellCommand
alert.commandShellTemplate = template.Must( alert.commandShellTemplate = template.Must(
+5
View File
@@ -47,6 +47,11 @@ func (config *Config) Init() (err error) {
config.DefaultAlertAfter = minAlertAfter config.DefaultAlertAfter = minAlertAfter
} }
if config.DefaultAlertEvery == nil {
defaultDefaultAlertEvery := -1
config.DefaultAlertEvery = &defaultDefaultAlertEvery
}
for _, monitor := range config.Monitors { for _, monitor := range config.Monitors {
if err = monitor.Init( if err = monitor.Init(
config.DefaultAlertAfter, config.DefaultAlertAfter,
+86
View File
@@ -3,6 +3,7 @@ package main_test
import ( import (
"errors" "errors"
"testing" "testing"
"time"
m "git.iamthefij.com/iamthefij/minitor-go" m "git.iamthefij.com/iamthefij/minitor-go"
) )
@@ -39,6 +40,91 @@ func TestLoadConfig(t *testing.T) {
} }
} }
func TestDefaultConfig(t *testing.T) {
cases := []struct {
configPath string
expectedResult m.Config
name string
}{
{
"./test/valid-config-default-values.hcl",
m.Config{
CheckInterval: 1 * time.Second,
DefaultAlertAfter: 2,
DefaultAlertEvery: Ptr(0),
DefaultAlertDown: []string{"log_command"},
},
"override defaults",
},
{
"./test/valid-config.hcl",
m.Config{
CheckInterval: 30 * time.Second,
DefaultAlertAfter: 1,
DefaultAlertEvery: Ptr(-1),
DefaultAlertDown: []string{},
},
"default defaults",
},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
config, err := m.LoadConfig(c.configPath)
if err != nil {
t.Errorf("Got error when loading config file %q: %s", c.configPath, err)
}
// Test Config has default values
if config.DefaultAlertAfter != c.expectedResult.DefaultAlertAfter {
t.Errorf("Got unexpected DefaultAlertAfter from file %q: expected=%v actual=%v", c.configPath, c.expectedResult.DefaultAlertAfter, config.DefaultAlertAfter)
}
if *config.DefaultAlertEvery != *c.expectedResult.DefaultAlertEvery {
t.Errorf("Got unexpected DefaultAlertEvery from file %q: expected=%v actual=%v", c.configPath, *c.expectedResult.DefaultAlertEvery, *config.DefaultAlertEvery)
}
if !m.EqualSliceString(config.DefaultAlertUp, c.expectedResult.DefaultAlertUp) {
t.Errorf("Got unexpected DefaultAlertUp from file %q: expected=%v actual=%v", c.configPath, c.expectedResult.DefaultAlertUp, config.DefaultAlertUp)
}
if !m.EqualSliceString(config.DefaultAlertDown, c.expectedResult.DefaultAlertDown) {
t.Errorf("Got unexpected DefaultAlertDown from file %q: expected=%v actual=%v", c.configPath, c.expectedResult.DefaultAlertDown, config.DefaultAlertDown)
}
// Check that monitor defaults propagate
var defaultMonitor *m.Monitor
for _, monitor := range config.Monitors {
if monitor.Name == "Default" {
defaultMonitor = monitor
}
}
if defaultMonitor == nil {
t.Errorf("failed to find default monitor in %q", c.configPath)
}
if defaultMonitor.AlertAfter != c.expectedResult.DefaultAlertAfter {
t.Errorf("Got unexpected AlertAfter from file %q: expected=%v actual=%v", c.configPath, c.expectedResult.DefaultAlertAfter, defaultMonitor.AlertAfter)
}
if *defaultMonitor.AlertEvery != *c.expectedResult.DefaultAlertEvery {
t.Errorf("Got unexpected AlertEvery from file %q: expected=%v actual=%v", c.configPath, *c.expectedResult.DefaultAlertEvery, *defaultMonitor.AlertEvery)
}
if !m.EqualSliceString(defaultMonitor.AlertUp, c.expectedResult.DefaultAlertUp) {
t.Errorf("Got unexpected AlertUp from file %q: expected=%v actual=%v", c.configPath, c.expectedResult.DefaultAlertUp, defaultMonitor.AlertUp)
}
// NOTE: Can't compare AlertDown because default is empty and that is invalid
})
}
}
// TestMultiLineConfig is a more complicated test stepping through the parsing // TestMultiLineConfig is a more complicated test stepping through the parsing
// and execution of mutli-line strings presented in YAML // and execution of mutli-line strings presented in YAML
func TestMultiLineConfig(t *testing.T) { func TestMultiLineConfig(t *testing.T) {
+6 -1
View File
@@ -1,6 +1,11 @@
check_interval = "1s" check_interval = "1s"
default_alert_down = ["log_command"] default_alert_down = ["log_command"]
default_alert_after = 1 default_alert_every = 0
default_alert_after = 2
monitor "Default" {
command = ["echo"]
}
monitor "Command" { monitor "Command" {
command = ["echo", "$PATH"] command = ["echo", "$PATH"]
+5
View File
@@ -8,6 +8,11 @@ alert "log_shell" {
shell_command = "echo \"Failure on {{.MonitorName}} User is $USER\"" shell_command = "echo \"Failure on {{.MonitorName}} User is $USER\""
} }
monitor "Default" {
command = ["echo"]
alert_down = ["log_command"]
}
monitor "Command" { monitor "Command" {
command = ["echo", "$PATH"] command = ["echo", "$PATH"]
alert_down = ["log_command", "log_shell"] alert_down = ["log_command", "log_shell"]
-25
View File
@@ -1,25 +0,0 @@
---
check_interval: 1s
monitors:
- name: Command
command: ["echo", "$PATH"]
alert_down: ["log_command", "log_shell"]
alert_every: 0
check_interval: 10s
- name: Shell
command: >
echo 'Some string with stuff';
echo 'another line';
echo $PATH;
exit 1
alert_down: ["log_command", "log_shell"]
alert_after: 5
alert_every: 0
check_interval: 1m
alerts:
log_command:
command: ["echo", "regular", '"command!!!"', "{{.MonitorName}}"]
log_shell:
command: echo "Failure on {{.MonitorName}} User is $USER"