Improve test coverage

This commit is contained in:
TwinProduction 2021-01-10 00:24:31 -05:00
parent 1669f91a2d
commit 6f8a728c5f
2 changed files with 39 additions and 1 deletions

View File

@ -61,7 +61,6 @@ func handleAlertsToTrigger(service *core.Service, result *core.Result, cfg *conf
} else {
alert.Triggered = true
}
} else {
log.Printf("[watchdog][handleAlertsToResolve] Not sending alert of type=%s despite being TRIGGERED, because the provider wasn't configured properly", alert.Type)
}

View File

@ -245,3 +245,42 @@ func TestHandleAlertingWithoutSendingAlertOnResolve(t *testing.T) {
t.Fatal("The alert shouldn't have triggered, because the provider wasn't configured properly")
}
}
func TestHandleAlertingWhenTriggeredAlertIsAlmostResolvedButServiceStartFailingAgain(t *testing.T) {
cfg := &config.Config{
Debug: true,
Alerting: &alerting.Config{
Custom: &custom.AlertProvider{
URL: "https://twinnation.org/health",
Method: "GET",
},
},
}
config.Set(cfg)
service := &core.Service{
URL: "http://example.com",
Alerts: []*core.Alert{
{
Type: core.CustomAlert,
Enabled: true,
FailureThreshold: 2,
SuccessThreshold: 3,
SendOnResolved: true,
Triggered: true,
},
},
NumberOfFailuresInARow: 1,
}
// This test simulate an alert that was already triggered
HandleAlerting(service, &core.Result{Success: false})
if service.NumberOfFailuresInARow != 2 {
t.Fatal("service.NumberOfFailuresInARow should've increased from 1 to 2, got", service.NumberOfFailuresInARow)
}
if service.NumberOfSuccessesInARow != 0 {
t.Fatal("service.NumberOfSuccessesInARow should've stayed at 0, got", service.NumberOfSuccessesInARow)
}
if !service.Alerts[0].Triggered {
t.Fatal("The alert was already triggered at the beginning of this test")
}
}