2019-10-03 23:30:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAlertIsValid(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
alert Alert
|
|
|
|
expected bool
|
|
|
|
name string
|
|
|
|
}{
|
2020-02-16 21:25:11 +00:00
|
|
|
{Alert{Command: CommandOrShell{Command: []string{"echo", "test"}}}, true, "Command only"},
|
|
|
|
{Alert{Command: CommandOrShell{ShellCommand: "echo test"}}, true, "CommandShell only"},
|
2019-10-03 23:30:49 +00:00
|
|
|
{Alert{}, false, "No commands"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
log.Printf("Testing case %s", c.name)
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2019-10-03 23:30:49 +00:00
|
|
|
actual := c.alert.IsValid()
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("IsValid(%v), expected=%t actual=%t", c.name, c.expected, actual)
|
|
|
|
log.Printf("Case failed: %s", c.name)
|
|
|
|
}
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2019-10-03 23:30:49 +00:00
|
|
|
log.Println("-----")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlertSend(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
alert Alert
|
|
|
|
notice AlertNotice
|
|
|
|
expectedOutput string
|
|
|
|
expectErr bool
|
|
|
|
name string
|
2020-02-20 01:31:04 +00:00
|
|
|
pyCompat bool
|
2019-10-03 23:30:49 +00:00
|
|
|
}{
|
|
|
|
{
|
2020-02-16 21:25:11 +00:00
|
|
|
Alert{Command: CommandOrShell{Command: []string{"echo", "{{.MonitorName}}"}}},
|
2019-10-03 23:30:49 +00:00
|
|
|
AlertNotice{MonitorName: "test"},
|
|
|
|
"test\n",
|
|
|
|
false,
|
|
|
|
"Command with template",
|
2020-02-20 01:31:04 +00:00
|
|
|
false,
|
2019-10-03 23:30:49 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-16 21:25:11 +00:00
|
|
|
Alert{Command: CommandOrShell{ShellCommand: "echo {{.MonitorName}}"}},
|
2019-10-03 23:30:49 +00:00
|
|
|
AlertNotice{MonitorName: "test"},
|
|
|
|
"test\n",
|
|
|
|
false,
|
|
|
|
"Command shell with template",
|
2020-02-20 01:31:04 +00:00
|
|
|
false,
|
2019-10-03 23:30:49 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-16 21:25:11 +00:00
|
|
|
Alert{Command: CommandOrShell{Command: []string{"echo", "{{.Bad}}"}}},
|
2019-10-03 23:30:49 +00:00
|
|
|
AlertNotice{MonitorName: "test"},
|
|
|
|
"",
|
|
|
|
true,
|
|
|
|
"Command with bad template",
|
2020-02-20 01:31:04 +00:00
|
|
|
false,
|
2019-10-03 23:30:49 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-16 21:25:11 +00:00
|
|
|
Alert{Command: CommandOrShell{ShellCommand: "echo {{.Bad}}"}},
|
2019-10-03 23:30:49 +00:00
|
|
|
AlertNotice{MonitorName: "test"},
|
|
|
|
"",
|
|
|
|
true,
|
|
|
|
"Command shell with bad template",
|
2020-02-20 01:31:04 +00:00
|
|
|
false,
|
2019-10-03 23:30:49 +00:00
|
|
|
},
|
2020-02-17 02:39:55 +00:00
|
|
|
{
|
|
|
|
Alert{Command: CommandOrShell{ShellCommand: "echo {alert_message}"}},
|
|
|
|
AlertNotice{MonitorName: "test", FailureCount: 1},
|
|
|
|
"test check has failed 1 times\n",
|
|
|
|
false,
|
|
|
|
"Command shell with legacy template",
|
2020-02-20 01:31:04 +00:00
|
|
|
true,
|
2020-02-17 02:39:55 +00:00
|
|
|
},
|
2020-06-19 16:51:42 +00:00
|
|
|
// Test default log alert down
|
|
|
|
{
|
|
|
|
*NewLogAlert(),
|
|
|
|
AlertNotice{MonitorName: "Test", FailureCount: 1, IsUp: false},
|
|
|
|
"Test check has failed 1 times\n",
|
|
|
|
false,
|
|
|
|
"Default log alert down",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
// Test default log alert up
|
|
|
|
{
|
|
|
|
*NewLogAlert(),
|
|
|
|
AlertNotice{MonitorName: "Test", IsUp: true},
|
|
|
|
"Test has recovered\n",
|
|
|
|
false,
|
|
|
|
"Default log alert up",
|
|
|
|
false,
|
|
|
|
},
|
2019-10-03 23:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
log.Printf("Testing case %s", c.name)
|
2020-02-20 01:31:04 +00:00
|
|
|
// Set PyCompat to value of compat flag
|
|
|
|
PyCompat = c.pyCompat
|
2021-01-08 23:31:22 +00:00
|
|
|
|
|
|
|
err := c.alert.BuildTemplates()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Send(%v output), error building templates: %v", c.name, err)
|
|
|
|
}
|
|
|
|
|
2019-10-03 23:30:49 +00:00
|
|
|
output, err := c.alert.Send(c.notice)
|
|
|
|
hasErr := (err != nil)
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2019-10-03 23:30:49 +00:00
|
|
|
if output != c.expectedOutput {
|
|
|
|
t.Errorf("Send(%v output), expected=%v actual=%v", c.name, c.expectedOutput, output)
|
|
|
|
log.Printf("Case failed: %s", c.name)
|
|
|
|
}
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2019-10-03 23:30:49 +00:00
|
|
|
if hasErr != c.expectErr {
|
|
|
|
t.Errorf("Send(%v err), expected=%v actual=%v", c.name, "Err", err)
|
|
|
|
log.Printf("Case failed: %s", c.name)
|
|
|
|
}
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2020-02-20 01:31:04 +00:00
|
|
|
// Set PyCompat back to default value
|
|
|
|
PyCompat = false
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2019-10-03 23:30:49 +00:00
|
|
|
log.Println("-----")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlertSendNoTemplates(t *testing.T) {
|
|
|
|
alert := Alert{}
|
|
|
|
notice := AlertNotice{}
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2019-10-03 23:30:49 +00:00
|
|
|
output, err := alert.Send(notice)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Send(no template), expected=%v actual=%v", "Err", output)
|
|
|
|
}
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2019-10-03 23:30:49 +00:00
|
|
|
log.Println("-----")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlertBuildTemplate(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
alert Alert
|
|
|
|
expectErr bool
|
|
|
|
name string
|
|
|
|
}{
|
2020-02-16 21:25:11 +00:00
|
|
|
{Alert{Command: CommandOrShell{Command: []string{"echo", "test"}}}, false, "Command only"},
|
|
|
|
{Alert{Command: CommandOrShell{ShellCommand: "echo test"}}, false, "CommandShell only"},
|
2019-10-03 23:30:49 +00:00
|
|
|
{Alert{}, true, "No commands"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
log.Printf("Testing case %s", c.name)
|
|
|
|
err := c.alert.BuildTemplates()
|
|
|
|
hasErr := (err != nil)
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2019-10-03 23:30:49 +00:00
|
|
|
if hasErr != c.expectErr {
|
|
|
|
t.Errorf("IsValid(%v), expected=%t actual=%t", c.name, c.expectErr, err)
|
|
|
|
log.Printf("Case failed: %s", c.name)
|
|
|
|
}
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2019-10-03 23:30:49 +00:00
|
|
|
log.Println("-----")
|
|
|
|
}
|
|
|
|
}
|