2024-11-15 19:30:34 +00:00
|
|
|
package main_test
|
2019-10-03 23:30:49 +00:00
|
|
|
|
|
|
|
import (
|
2024-11-16 00:36:45 +00:00
|
|
|
"errors"
|
2019-10-03 23:30:49 +00:00
|
|
|
"testing"
|
2024-11-15 19:30:34 +00:00
|
|
|
|
|
|
|
m "git.iamthefij.com/iamthefij/minitor-go"
|
2019-10-03 23:30:49 +00:00
|
|
|
)
|
|
|
|
|
2024-11-16 00:36:45 +00:00
|
|
|
func TestAlertValidate(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-10-03 23:30:49 +00:00
|
|
|
cases := []struct {
|
2024-11-15 19:30:34 +00:00
|
|
|
alert m.Alert
|
2024-11-16 00:36:45 +00:00
|
|
|
expected error
|
2019-10-03 23:30:49 +00:00
|
|
|
name string
|
|
|
|
}{
|
2024-11-16 00:36:45 +00:00
|
|
|
{m.Alert{Command: []string{"echo", "test"}}, nil, "Command only"},
|
|
|
|
{m.Alert{ShellCommand: "echo test"}, nil, "CommandShell only"},
|
|
|
|
{m.Alert{Command: []string{"echo", "test"}, ShellCommand: "echo test"}, m.ErrInvalidAlert, "Both commands"},
|
|
|
|
{m.Alert{}, m.ErrInvalidAlert, "No commands"},
|
2019-10-03 23:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2024-11-14 19:35:26 +00:00
|
|
|
c := c
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2024-11-14 19:35:26 +00:00
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2024-11-16 00:36:45 +00:00
|
|
|
actual := c.alert.Validate()
|
|
|
|
hasErr := (actual != nil)
|
|
|
|
expectErr := (c.expected != nil)
|
|
|
|
|
|
|
|
if hasErr != expectErr || !errors.Is(actual, c.expected) {
|
2024-11-14 19:35:26 +00:00
|
|
|
t.Errorf("expected=%t actual=%t", c.expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2019-10-03 23:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlertSend(t *testing.T) {
|
|
|
|
cases := []struct {
|
2024-11-15 19:30:34 +00:00
|
|
|
alert m.Alert
|
|
|
|
notice m.AlertNotice
|
2019-10-03 23:30:49 +00:00
|
|
|
expectedOutput string
|
|
|
|
expectErr bool
|
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
m.Alert{Command: []string{"echo", "{{.MonitorName}}"}},
|
|
|
|
m.AlertNotice{MonitorName: "test"},
|
2019-10-03 23:30:49 +00:00
|
|
|
"test\n",
|
|
|
|
false,
|
|
|
|
"Command with template",
|
|
|
|
},
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
m.Alert{ShellCommand: "echo {{.MonitorName}}"},
|
|
|
|
m.AlertNotice{MonitorName: "test"},
|
2019-10-03 23:30:49 +00:00
|
|
|
"test\n",
|
|
|
|
false,
|
|
|
|
"Command shell with template",
|
|
|
|
},
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
m.Alert{Command: []string{"echo", "{{.Bad}}"}},
|
|
|
|
m.AlertNotice{MonitorName: "test"},
|
2019-10-03 23:30:49 +00:00
|
|
|
"",
|
|
|
|
true,
|
|
|
|
"Command with bad template",
|
|
|
|
},
|
|
|
|
{
|
2024-11-15 19:30:34 +00:00
|
|
|
m.Alert{ShellCommand: "echo {{.Bad}}"},
|
|
|
|
m.AlertNotice{MonitorName: "test"},
|
2019-10-03 23:30:49 +00:00
|
|
|
"",
|
|
|
|
true,
|
|
|
|
"Command shell with bad template",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2024-11-14 19:35:26 +00:00
|
|
|
c := c
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2024-11-14 19:35:26 +00:00
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2024-11-14 19:35:26 +00:00
|
|
|
err := c.alert.BuildTemplates()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Send(%v output), error building templates: %v", c.name, err)
|
|
|
|
}
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2024-11-14 19:35:26 +00:00
|
|
|
output, err := c.alert.Send(c.notice)
|
|
|
|
hasErr := (err != nil)
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2024-11-14 19:35:26 +00:00
|
|
|
if output != c.expectedOutput {
|
|
|
|
t.Errorf("Send(%v output), expected=%v actual=%v", c.name, c.expectedOutput, output)
|
|
|
|
}
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2024-11-14 19:35:26 +00:00
|
|
|
if hasErr != c.expectErr {
|
|
|
|
t.Errorf("Send(%v err), expected=%v actual=%v", c.name, "Err", err)
|
|
|
|
}
|
|
|
|
})
|
2019-10-03 23:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlertSendNoTemplates(t *testing.T) {
|
2024-11-15 19:30:34 +00:00
|
|
|
alert := m.Alert{}
|
|
|
|
notice := m.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlertBuildTemplate(t *testing.T) {
|
|
|
|
cases := []struct {
|
2024-11-15 19:30:34 +00:00
|
|
|
alert m.Alert
|
2019-10-03 23:30:49 +00:00
|
|
|
expectErr bool
|
|
|
|
name string
|
|
|
|
}{
|
2024-11-15 19:30:34 +00:00
|
|
|
{m.Alert{Command: []string{"echo", "test"}}, false, "Command only"},
|
|
|
|
{m.Alert{ShellCommand: "echo test"}, false, "CommandShell only"},
|
|
|
|
{m.Alert{}, true, "No commands"},
|
2019-10-03 23:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2024-11-14 19:35:26 +00:00
|
|
|
c := c
|
|
|
|
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
err := c.alert.BuildTemplates()
|
|
|
|
hasErr := (err != nil)
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2024-11-14 19:35:26 +00:00
|
|
|
if hasErr != c.expectErr {
|
|
|
|
t.Errorf("IsValid(%v), expected=%t actual=%t", c.name, c.expectErr, err)
|
|
|
|
}
|
2021-01-08 23:31:22 +00:00
|
|
|
|
2024-11-14 19:35:26 +00:00
|
|
|
})
|
2019-10-03 23:30:49 +00:00
|
|
|
}
|
|
|
|
}
|