diff --git a/config_test.go b/config_test.go index 808a194..8c5fab9 100644 --- a/config_test.go +++ b/config_test.go @@ -3,6 +3,7 @@ package main import ( "log" "testing" + "time" ) func TestLoadConfig(t *testing.T) { @@ -35,11 +36,37 @@ func TestLoadConfig(t *testing.T) { // Set PyCompat to default value PyCompat = false - - log.Println("-----") } } +func TestIntervalParsing(t *testing.T) { + log.Printf("Testing case TestIntervalParsing") + + config, err := LoadConfig("./test/valid-config.yml") + if err != nil { + t.Errorf("Failed loading config: %v", err) + } + + oneSecond := time.Second + tenSeconds := 10 * time.Second + oneMinute := time.Minute + + // validate top level interval seconds represented as an int + if config.CheckInterval != oneSecond { + t.Errorf("Incorrectly parsed int seconds. expected=%v actual=%v", oneSecond, config.CheckInterval) + } + + if config.Monitors[0].CheckInterval != tenSeconds { + t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval) + } + + if config.Monitors[1].CheckInterval != oneMinute { + t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval) + } + + log.Println("-----") +} + // 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/monitor_test.go b/monitor_test.go index cde021f..5988b91 100644 --- a/monitor_test.go +++ b/monitor_test.go @@ -45,9 +45,9 @@ func TestMonitorShouldCheck(t *testing.T) { name string }{ {Monitor{}, true, "Empty"}, - {Monitor{lastCheck: timeNow, CheckInterval: 15}, false, "Just checked"}, - {Monitor{lastCheck: timeTenSecAgo, CheckInterval: 15}, false, "-10s"}, - {Monitor{lastCheck: timeTwentySecAgo, CheckInterval: 15}, true, "-20s"}, + {Monitor{lastCheck: timeNow, CheckInterval: time.Second * 15}, false, "Just checked"}, + {Monitor{lastCheck: timeTenSecAgo, CheckInterval: time.Second * 15}, false, "-10s"}, + {Monitor{lastCheck: timeTwentySecAgo, CheckInterval: time.Second * 15}, true, "-20s"}, } for _, c := range cases { diff --git a/test/valid-config.yml b/test/valid-config.yml index e192977..3860e95 100644 --- a/test/valid-config.yml +++ b/test/valid-config.yml @@ -3,21 +3,23 @@ check_interval: 1 monitors: - name: Command - command: ['echo', '$PATH'] - alert_down: ['log_command', 'log_shell'] + 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_down: ["log_command", "log_shell"] alert_after: 5 alert_every: 0 + check_interval: 1m alerts: log_command: - command: ['echo', 'regular', '"command!!!"', "{{.MonitorName}}"] + command: ["echo", "regular", '"command!!!"', "{{.MonitorName}}"] log_shell: command: echo "Failure on {{.MonitorName}} User is $USER"