2019-10-04 01:16:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"testing"
|
2021-05-11 17:40:54 +00:00
|
|
|
"time"
|
2019-10-04 01:16:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadConfig(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
configPath string
|
|
|
|
expectErr bool
|
|
|
|
name string
|
2020-02-20 01:31:04 +00:00
|
|
|
pyCompat bool
|
2019-10-04 01:16:03 +00:00
|
|
|
}{
|
2020-02-20 01:31:04 +00:00
|
|
|
{"./test/valid-config.yml", false, "Valid config file", false},
|
2022-12-19 17:50:44 +00:00
|
|
|
{"./test/valid-config-default-values.yml", false, "Valid config file with default values", false},
|
2020-02-20 01:31:04 +00:00
|
|
|
{"./test/valid-default-log-alert.yml", false, "Valid config file with default log alert PyCompat", true},
|
|
|
|
{"./test/valid-default-log-alert.yml", true, "Invalid config file no log alert", false},
|
|
|
|
{"./test/does-not-exist", true, "Invalid config path", false},
|
|
|
|
{"./test/invalid-config-type.yml", true, "Invalid config type for key", false},
|
|
|
|
{"./test/invalid-config-missing-alerts.yml", true, "Invalid config missing alerts", false},
|
|
|
|
{"./test/invalid-config-unknown-alert.yml", true, "Invalid config unknown alert", false},
|
2019-10-04 01:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
log.Printf("Testing case %s", c.name)
|
2020-02-20 01:31:04 +00:00
|
|
|
// Set PyCompat based on compatibility mode
|
|
|
|
PyCompat = c.pyCompat
|
2019-10-04 01:16:03 +00:00
|
|
|
_, err := LoadConfig(c.configPath)
|
|
|
|
hasErr := (err != nil)
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2019-10-04 01:16:03 +00:00
|
|
|
if hasErr != c.expectErr {
|
2020-01-07 18:28:14 +00:00
|
|
|
t.Errorf("LoadConfig(%v), expected_error=%v actual=%v", c.name, c.expectErr, err)
|
2019-10-04 01:16:03 +00:00
|
|
|
log.Printf("Case failed: %s", c.name)
|
|
|
|
}
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2020-02-20 01:31:04 +00:00
|
|
|
// Set PyCompat to default value
|
|
|
|
PyCompat = false
|
2021-05-11 17:40:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2021-05-11 17:40:54 +00:00
|
|
|
oneSecond := time.Second
|
|
|
|
tenSeconds := 10 * time.Second
|
|
|
|
oneMinute := time.Minute
|
|
|
|
|
|
|
|
// validate top level interval seconds represented as an int
|
2021-05-12 17:33:42 +00:00
|
|
|
if config.CheckInterval.Value() != oneSecond {
|
2021-05-11 17:40:54 +00:00
|
|
|
t.Errorf("Incorrectly parsed int seconds. expected=%v actual=%v", oneSecond, config.CheckInterval)
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:33:42 +00:00
|
|
|
if config.Monitors[0].CheckInterval.Value() != tenSeconds {
|
2021-05-11 17:40:54 +00:00
|
|
|
t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval)
|
2019-10-04 01:16:03 +00:00
|
|
|
}
|
2021-05-11 17:40:54 +00:00
|
|
|
|
2021-05-12 17:33:42 +00:00
|
|
|
if config.Monitors[1].CheckInterval.Value() != oneMinute {
|
2021-05-11 17:40:54 +00:00
|
|
|
t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("-----")
|
2019-10-04 01:16:03 +00:00
|
|
|
}
|
2020-01-07 18:28:14 +00:00
|
|
|
|
|
|
|
// 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")
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2020-01-07 18:28:14 +00:00
|
|
|
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)")
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2020-01-07 18:37:53 +00:00
|
|
|
expected := "echo 'Some string with stuff'; echo \"<angle brackets>\"; exit 1\n"
|
2020-02-16 21:25:11 +00:00
|
|
|
actual := config.Monitors[0].Command.ShellCommand
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2020-01-07 18:28:14 +00:00
|
|
|
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)")
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2020-01-07 18:28:14 +00:00
|
|
|
_, notice := config.Monitors[0].Check()
|
|
|
|
if notice == nil {
|
|
|
|
t.Fatalf("Did not receive an alert notice")
|
|
|
|
}
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2020-01-07 18:28:14 +00:00
|
|
|
expected = "Some string with stuff\n<angle brackets>\n"
|
|
|
|
actual = notice.LastCheckOutput
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2020-01-07 18:28:14 +00:00
|
|
|
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)")
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2020-01-07 18:28:14 +00:00
|
|
|
expected = "echo 'Some string with stuff'\necho '<angle brackets>'\n"
|
2020-02-16 21:25:11 +00:00
|
|
|
actual = config.Alerts["log_shell"].Command.ShellCommand
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2020-01-07 18:28:14 +00:00
|
|
|
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)")
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2020-01-07 18:28:14 +00:00
|
|
|
actual, err = config.Alerts["log_shell"].Send(AlertNotice{})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Execution of alert failed")
|
|
|
|
}
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2020-01-07 18:28:14 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|