Add new test for multi-line YAML strings

This commit is contained in:
IamTheFij 2020-01-07 10:28:14 -08:00
parent f6ccd9a3bd
commit 0269ad3512
5 changed files with 91 additions and 12 deletions

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -78,9 +77,7 @@ func LoadConfig(filePath string) (config Config, err error) {
return return
} }
// TODO: Decide if this is better expanded here, or only when executing err = yaml.Unmarshal(data, &config)
envExpanded := os.ExpandEnv(string(data))
err = yaml.Unmarshal([]byte(envExpanded), &config)
if err != nil { if err != nil {
return return
} }

View File

@ -23,9 +23,74 @@ func TestLoadConfig(t *testing.T) {
_, err := LoadConfig(c.configPath) _, err := LoadConfig(c.configPath)
hasErr := (err != nil) hasErr := (err != nil)
if hasErr != c.expectErr { 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.Printf("Case failed: %s", c.name)
} }
log.Println("-----") 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 '<angle brackets>'; 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<angle brackets>\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 '<angle brackets>'\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<angle brackets>\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))
}
}

View File

@ -155,18 +155,16 @@ func (monitor Monitor) getAlertAfter() int16 {
// Zero is one! // Zero is one!
if monitor.AlertAfter == 0 { if monitor.AlertAfter == 0 {
return 1 return 1
} else {
return monitor.AlertAfter
} }
return monitor.AlertAfter
} }
// GetAlertNames gives a list of alert names for a given monitor status // GetAlertNames gives a list of alert names for a given monitor status
func (monitor Monitor) GetAlertNames(up bool) []string { func (monitor Monitor) GetAlertNames(up bool) []string {
if up { if up {
return monitor.AlertUp return monitor.AlertUp
} else {
return monitor.AlertDown
} }
return monitor.AlertDown
} }
func (monitor Monitor) createAlertNotice(isUp bool) *AlertNotice { func (monitor Monitor) createAlertNotice(isUp bool) *AlertNotice {

View File

@ -1,9 +1,10 @@
---
check_interval: 1 check_interval: 1
monitors: monitors:
- name: Command - name: Command
command: ['echo', '$PATH'] command: ['echo', '$PATH']
alert_down: [ 'log_command', 'log_shell' ] alert_down: ['log_command', 'log_shell']
alert_every: 0 alert_every: 0
- name: Shell - name: Shell
command_shell: > command_shell: >
@ -11,12 +12,12 @@ monitors:
echo 'another line'; echo 'another line';
echo $PATH; echo $PATH;
exit 1 exit 1
alert_down: [ 'log_command', 'log_shell' ] alert_down: ['log_command', 'log_shell']
alert_after: 5 alert_after: 5
alert_every: 0 alert_every: 0
alerts: alerts:
log_command: log_command:
command: [ 'echo', 'regular', '"command!!!"', "{{.MonitorName}}" ] command: ['echo', 'regular', '"command!!!"', "{{.MonitorName}}"]
log_shell: log_shell:
command_shell: echo "Failure on {{.MonitorName}} User is $USER" command_shell: echo "Failure on {{.MonitorName}} User is $USER"

View File

@ -0,0 +1,18 @@
---
check_interval: 1
monitors:
- name: Shell
command_shell: >
echo 'Some string with stuff';
echo '<angle brackets>';
exit 1
alert_down: ['log_shell']
alert_after: 1
alert_every: 0
alerts:
log_shell:
command_shell: |
echo 'Some string with stuff'
echo '<angle brackets>'