From 6f8a728c5f08534b9ec6e97016b4c1c2b7495aa1 Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Sun, 10 Jan 2021 00:24:31 -0500 Subject: [PATCH] Improve test coverage --- watchdog/alerting.go | 1 - watchdog/alerting_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/watchdog/alerting.go b/watchdog/alerting.go index 60fb2b0e..f12226ff 100644 --- a/watchdog/alerting.go +++ b/watchdog/alerting.go @@ -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) } diff --git a/watchdog/alerting_test.go b/watchdog/alerting_test.go index 40b5a27e..7510a7b3 100644 --- a/watchdog/alerting_test.go +++ b/watchdog/alerting_test.go @@ -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") + } +}