diff --git a/README.md b/README.md index 9a39203..06aa0b0 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ monitor "example" { alert_up = ["log"] check_interval = "1m" alert_after = 1 - alert_every = 0 + alert_every = -1 } ``` diff --git a/alert.go b/alert.go index 2a6c7c8..20a3b92 100644 --- a/alert.go +++ b/alert.go @@ -101,14 +101,14 @@ func (alert *Alert) BuildTemplates() error { } switch { - case alert.commandTemplate == nil && alert.Command != nil: + case alert.Command != nil: alert.commandTemplate = []*template.Template{} for i, cmdPart := range alert.Command { alert.commandTemplate = append(alert.commandTemplate, template.Must( template.New(alert.Name+fmt.Sprint(i)).Funcs(timeFormatFuncs).Parse(cmdPart), )) } - case alert.commandShellTemplate == nil && alert.ShellCommand != "": + case alert.ShellCommand != "": shellCmd := alert.ShellCommand alert.commandShellTemplate = template.Must( diff --git a/config.go b/config.go index 554dd6a..c5e0684 100644 --- a/config.go +++ b/config.go @@ -47,6 +47,11 @@ func (config *Config) Init() (err error) { config.DefaultAlertAfter = minAlertAfter } + if config.DefaultAlertEvery == nil { + defaultDefaultAlertEvery := -1 + config.DefaultAlertEvery = &defaultDefaultAlertEvery + } + for _, monitor := range config.Monitors { if err = monitor.Init( config.DefaultAlertAfter, diff --git a/config_test.go b/config_test.go index 6a6d3d4..6fd101a 100644 --- a/config_test.go +++ b/config_test.go @@ -3,6 +3,7 @@ package main_test import ( "errors" "testing" + "time" 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 // and execution of mutli-line strings presented in YAML func TestMultiLineConfig(t *testing.T) { diff --git a/test/valid-config-default-values.hcl b/test/valid-config-default-values.hcl index f743af0..2532908 100644 --- a/test/valid-config-default-values.hcl +++ b/test/valid-config-default-values.hcl @@ -1,6 +1,11 @@ check_interval = "1s" default_alert_down = ["log_command"] -default_alert_after = 1 +default_alert_every = 0 +default_alert_after = 2 + +monitor "Default" { + command = ["echo"] +} monitor "Command" { command = ["echo", "$PATH"] diff --git a/test/valid-config.hcl b/test/valid-config.hcl index 5b64931..8dad062 100644 --- a/test/valid-config.hcl +++ b/test/valid-config.hcl @@ -8,6 +8,11 @@ alert "log_shell" { shell_command = "echo \"Failure on {{.MonitorName}} User is $USER\"" } +monitor "Default" { + command = ["echo"] + alert_down = ["log_command"] +} + monitor "Command" { command = ["echo", "$PATH"] alert_down = ["log_command", "log_shell"] diff --git a/test/valid-config.yml b/test/valid-config.yml deleted file mode 100644 index da0a264..0000000 --- a/test/valid-config.yml +++ /dev/null @@ -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"