diff --git a/config.go b/config.go index 0a044db..0e2e953 100644 --- a/config.go +++ b/config.go @@ -4,7 +4,6 @@ import ( "errors" "io/ioutil" "log" - "os" "gopkg.in/yaml.v2" ) @@ -78,9 +77,7 @@ func LoadConfig(filePath string) (config Config, err error) { return } - // TODO: Decide if this is better expanded here, or only when executing - envExpanded := os.ExpandEnv(string(data)) - err = yaml.Unmarshal([]byte(envExpanded), &config) + err = yaml.Unmarshal(data, &config) if err != nil { return } diff --git a/config_test.go b/config_test.go index 7143332..2618971 100644 --- a/config_test.go +++ b/config_test.go @@ -23,9 +23,74 @@ func TestLoadConfig(t *testing.T) { _, err := LoadConfig(c.configPath) hasErr := (err != nil) if hasErr != c.expectErr { - t.Errorf("LoadConfig(%v), expected=%v actual=%v", c.name, "Err", err) + t.Errorf("LoadConfig(%v), expected_error=%v actual=%v", c.name, c.expectErr, err) log.Printf("Case failed: %s", c.name) } 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) { + log.Println("Testing multi-line string config") + config, err := LoadConfig("./test/valid-verify-multi-line.yml") + if err != nil { + t.Fatalf("TestMultiLineConfig(load), expected=no_error actual=%v", err) + } + + log.Println("-----") + log.Println("TestMultiLineConfig(parse > string)") + expected := "echo 'Some string with stuff'; echo ''; exit 1\n" + actual := config.Monitors[0].CommandShell + if expected != actual { + t.Errorf("TestMultiLineConfig(>) failed") + t.Logf("string expected=`%v`", expected) + t.Logf("string actual =`%v`", actual) + t.Logf("bytes expected=%v", []byte(expected)) + t.Logf("bytes actual =%v", []byte(actual)) + } + + log.Println("-----") + log.Println("TestMultiLineConfig(execute > string)") + _, notice := config.Monitors[0].Check() + if notice == nil { + t.Fatalf("Did not receive an alert notice") + } + expected = "Some string with stuff\n\n" + actual = notice.LastCheckOutput + if expected != actual { + t.Errorf("TestMultiLineConfig(execute > string) check failed") + t.Logf("string expected=`%v`", expected) + t.Logf("string actual =`%v`", actual) + t.Logf("bytes expected=%v", []byte(expected)) + t.Logf("bytes actual =%v", []byte(actual)) + } + + log.Println("-----") + log.Println("TestMultiLineConfig(parse | string)") + expected = "echo 'Some string with stuff'\necho ''\n" + actual = config.Alerts["log_shell"].CommandShell + if expected != actual { + t.Errorf("TestMultiLineConfig(|) failed") + t.Logf("string expected=`%v`", expected) + t.Logf("string actual =`%v`", actual) + t.Logf("bytes expected=%v", []byte(expected)) + t.Logf("bytes actual =%v", []byte(actual)) + } + + log.Println("-----") + log.Println("TestMultiLineConfig(execute | string)") + actual, err = config.Alerts["log_shell"].Send(AlertNotice{}) + if err != nil { + t.Errorf("Execution of alert failed") + } + expected = "Some string with stuff\n\n" + if expected != actual { + t.Errorf("TestMultiLineConfig(execute | string) check failed") + t.Logf("string expected=`%v`", expected) + t.Logf("string actual =`%v`", actual) + t.Logf("bytes expected=%v", []byte(expected)) + t.Logf("bytes actual =%v", []byte(actual)) + } +} diff --git a/monitor.go b/monitor.go index 2b7b4ef..8e83bc6 100644 --- a/monitor.go +++ b/monitor.go @@ -155,18 +155,16 @@ func (monitor Monitor) getAlertAfter() int16 { // Zero is one! if monitor.AlertAfter == 0 { return 1 - } else { - return monitor.AlertAfter } + return monitor.AlertAfter } // GetAlertNames gives a list of alert names for a given monitor status func (monitor Monitor) GetAlertNames(up bool) []string { if up { return monitor.AlertUp - } else { - return monitor.AlertDown } + return monitor.AlertDown } func (monitor Monitor) createAlertNotice(isUp bool) *AlertNotice { diff --git a/test/valid-config.yml b/test/valid-config.yml index b7abcdf..23b763e 100644 --- a/test/valid-config.yml +++ b/test/valid-config.yml @@ -1,9 +1,10 @@ +--- check_interval: 1 monitors: - name: Command command: ['echo', '$PATH'] - alert_down: [ 'log_command', 'log_shell' ] + alert_down: ['log_command', 'log_shell'] alert_every: 0 - name: Shell command_shell: > @@ -11,12 +12,12 @@ monitors: 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 alerts: log_command: - command: [ 'echo', 'regular', '"command!!!"', "{{.MonitorName}}" ] + command: ['echo', 'regular', '"command!!!"', "{{.MonitorName}}"] log_shell: command_shell: echo "Failure on {{.MonitorName}} User is $USER" diff --git a/test/valid-verify-multi-line.yml b/test/valid-verify-multi-line.yml new file mode 100644 index 0000000..df76b58 --- /dev/null +++ b/test/valid-verify-multi-line.yml @@ -0,0 +1,18 @@ +--- +check_interval: 1 + +monitors: + - name: Shell + command_shell: > + echo 'Some string with stuff'; + echo ''; + exit 1 + alert_down: ['log_shell'] + alert_after: 1 + alert_every: 0 + +alerts: + log_shell: + command_shell: | + echo 'Some string with stuff' + echo ''