Add new test for multi-line YAML strings
This commit is contained in:
parent
f6ccd9a3bd
commit
0269ad3512
@ -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
|
||||
}
|
||||
|
@ -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 '<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))
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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"
|
||||
|
18
test/valid-verify-multi-line.yml
Normal file
18
test/valid-verify-multi-line.yml
Normal 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>'
|
Loading…
Reference in New Issue
Block a user