Update linting and a test case
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
IamTheFij 2021-01-08 18:31:22 -05:00
parent 9e124803da
commit f0e179851f
3 changed files with 49 additions and 13 deletions

View File

@ -18,11 +18,13 @@ func TestAlertIsValid(t *testing.T) {
for _, c := range cases { for _, c := range cases {
log.Printf("Testing case %s", c.name) log.Printf("Testing case %s", c.name)
actual := c.alert.IsValid() actual := c.alert.IsValid()
if actual != c.expected { if actual != c.expected {
t.Errorf("IsValid(%v), expected=%t actual=%t", c.name, c.expected, actual) t.Errorf("IsValid(%v), expected=%t actual=%t", c.name, c.expected, actual)
log.Printf("Case failed: %s", c.name) log.Printf("Case failed: %s", c.name)
} }
log.Println("-----") log.Println("-----")
} }
} }
@ -100,17 +102,25 @@ func TestAlertSend(t *testing.T) {
log.Printf("Testing case %s", c.name) log.Printf("Testing case %s", c.name)
// Set PyCompat to value of compat flag // Set PyCompat to value of compat flag
PyCompat = c.pyCompat PyCompat = c.pyCompat
c.alert.BuildTemplates()
err := c.alert.BuildTemplates()
if err != nil {
t.Errorf("Send(%v output), error building templates: %v", c.name, err)
}
output, err := c.alert.Send(c.notice) output, err := c.alert.Send(c.notice)
hasErr := (err != nil) hasErr := (err != nil)
if output != c.expectedOutput { if output != c.expectedOutput {
t.Errorf("Send(%v output), expected=%v actual=%v", c.name, c.expectedOutput, output) t.Errorf("Send(%v output), expected=%v actual=%v", c.name, c.expectedOutput, output)
log.Printf("Case failed: %s", c.name) log.Printf("Case failed: %s", c.name)
} }
if hasErr != c.expectErr { if hasErr != c.expectErr {
t.Errorf("Send(%v err), expected=%v actual=%v", c.name, "Err", err) t.Errorf("Send(%v err), expected=%v actual=%v", c.name, "Err", err)
log.Printf("Case failed: %s", c.name) log.Printf("Case failed: %s", c.name)
} }
// Set PyCompat back to default value // Set PyCompat back to default value
PyCompat = false PyCompat = false
log.Println("-----") log.Println("-----")
@ -120,10 +130,12 @@ func TestAlertSend(t *testing.T) {
func TestAlertSendNoTemplates(t *testing.T) { func TestAlertSendNoTemplates(t *testing.T) {
alert := Alert{} alert := Alert{}
notice := AlertNotice{} notice := AlertNotice{}
output, err := alert.Send(notice) output, err := alert.Send(notice)
if err == nil { if err == nil {
t.Errorf("Send(no template), expected=%v actual=%v", "Err", output) t.Errorf("Send(no template), expected=%v actual=%v", "Err", output)
} }
log.Println("-----") log.Println("-----")
} }
@ -142,10 +154,12 @@ func TestAlertBuildTemplate(t *testing.T) {
log.Printf("Testing case %s", c.name) log.Printf("Testing case %s", c.name)
err := c.alert.BuildTemplates() err := c.alert.BuildTemplates()
hasErr := (err != nil) hasErr := (err != nil)
if hasErr != c.expectErr { if hasErr != c.expectErr {
t.Errorf("IsValid(%v), expected=%t actual=%t", c.name, c.expectErr, err) t.Errorf("IsValid(%v), expected=%t actual=%t", c.name, c.expectErr, err)
log.Printf("Case failed: %s", c.name) log.Printf("Case failed: %s", c.name)
} }
log.Println("-----") log.Println("-----")
} }
} }

View File

@ -33,16 +33,10 @@ func TestCheckMonitors(t *testing.T) {
Command: CommandOrShell{Command: []string{"false"}}, Command: CommandOrShell{Command: []string{"false"}},
AlertAfter: 1, AlertAfter: 1,
}, },
&Monitor{
Name: "Failure",
Command: CommandOrShell{Command: []string{"false"}},
AlertDown: []string{"unknown"},
AlertAfter: 1,
},
}, },
}, },
expectErr: false, expectErr: false,
name: "Monitor failure, no and unknown alerts", name: "Monitor failure, no alerts",
}, },
{ {
config: Config{ config: Config{
@ -52,6 +46,28 @@ func TestCheckMonitors(t *testing.T) {
Command: CommandOrShell{Command: []string{"ls"}}, Command: CommandOrShell{Command: []string{"ls"}},
alertCount: 1, alertCount: 1,
}, },
},
},
expectErr: false,
name: "Monitor recovery, no alerts",
},
{
config: Config{
Monitors: []*Monitor{
&Monitor{
Name: "Failure",
Command: CommandOrShell{Command: []string{"false"}},
AlertDown: []string{"unknown"},
AlertAfter: 1,
},
},
},
expectErr: true,
name: "Monitor failure, unknown alerts",
},
{
config: Config{
Monitors: []*Monitor{
&Monitor{ &Monitor{
Name: "Success", Name: "Success",
Command: CommandOrShell{Command: []string{"true"}}, Command: CommandOrShell{Command: []string{"true"}},
@ -60,8 +76,8 @@ func TestCheckMonitors(t *testing.T) {
}, },
}, },
}, },
expectErr: false, expectErr: true,
name: "Monitor recovery, no alerts", name: "Monitor recovery, unknown alerts",
}, },
{ {
config: Config{ config: Config{
@ -105,10 +121,16 @@ func TestCheckMonitors(t *testing.T) {
} }
for _, c := range cases { for _, c := range cases {
c.config.Init() err := c.config.Init()
err := checkMonitors(&c.config) if err != nil {
t.Errorf("checkMonitors(%s): unexpected error reading config: %v", c.name, err)
}
err = checkMonitors(&c.config)
if err == nil && c.expectErr { if err == nil && c.expectErr {
t.Errorf("checkMonitors(%s): Expected panic, the code did not panic", c.name) t.Errorf("checkMonitors(%s): Expected panic, the code did not panic", c.name)
} else if err != nil && !c.expectErr {
t.Errorf("checkMonitors(%s): Did not expect an error, but we got one anyway: %v", c.name, err)
} }
} }
} }

View File

@ -40,7 +40,7 @@ func (monitor Monitor) ShouldCheck() bool {
return true return true
} }
sinceLastCheck := time.Now().Sub(monitor.lastCheck).Seconds() sinceLastCheck := time.Since(monitor.lastCheck).Seconds()
return sinceLastCheck >= monitor.CheckInterval return sinceLastCheck >= monitor.CheckInterval
} }