From 8c61044e7cc92c57306bd7babdbdfc3341d8a244 Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Wed, 21 Oct 2020 22:35:37 -0400 Subject: [PATCH] Improve alerting providers test coverage --- alerting/provider/custom/custom_test.go | 12 +++++------- alerting/provider/slack/slack_test.go | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/alerting/provider/custom/custom_test.go b/alerting/provider/custom/custom_test.go index e1906e44..976d78d6 100644 --- a/alerting/provider/custom/custom_test.go +++ b/alerting/provider/custom/custom_test.go @@ -19,12 +19,11 @@ func TestAlertProvider_IsValid(t *testing.T) { func TestAlertProvider_buildRequestWhenResolved(t *testing.T) { const ( - ExpectedUrl = "http://example.com/service-name" + ExpectedUrl = "http://example.com/service-name?event=RESOLVED&description=alert-description" ExpectedBody = "service-name,alert-description,RESOLVED" ) customAlertProvider := &AlertProvider{ - Url: "http://example.com/[SERVICE_NAME]", - Method: "GET", + Url: "http://example.com/[SERVICE_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]", Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]", Headers: nil, } @@ -40,14 +39,13 @@ func TestAlertProvider_buildRequestWhenResolved(t *testing.T) { func TestAlertProvider_buildRequestWhenTriggered(t *testing.T) { const ( - ExpectedUrl = "http://example.com/service-name" + ExpectedUrl = "http://example.com/service-name?event=TRIGGERED&description=alert-description" ExpectedBody = "service-name,alert-description,TRIGGERED" ) customAlertProvider := &AlertProvider{ - Url: "http://example.com/[SERVICE_NAME]", - Method: "GET", + Url: "http://example.com/[SERVICE_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]", Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]", - Headers: nil, + Headers: map[string]string{"Authorization": "Basic hunter2"}, } request := customAlertProvider.buildRequest("service-name", "alert-description", false) if request.URL.String() != ExpectedUrl { diff --git a/alerting/provider/slack/slack_test.go b/alerting/provider/slack/slack_test.go index 1715376a..0dc4785a 100644 --- a/alerting/provider/slack/slack_test.go +++ b/alerting/provider/slack/slack_test.go @@ -2,6 +2,7 @@ package slack import ( "github.com/TwinProduction/gatus/core" + "strings" "testing" ) @@ -16,10 +17,24 @@ func TestAlertProvider_IsValid(t *testing.T) { } } -func TestAlertProvider_ToCustomAlertProvider(t *testing.T) { +func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) { provider := AlertProvider{WebhookUrl: "http://example.com"} customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true) if customAlertProvider == nil { t.Error("customAlertProvider shouldn't have been nil") } + if !strings.Contains(customAlertProvider.Body, "resolved") { + t.Error("customAlertProvider.Body should've contained the substring resolved") + } +} + +func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) { + provider := AlertProvider{WebhookUrl: "http://example.com"} + customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, false) + if customAlertProvider == nil { + t.Error("customAlertProvider shouldn't have been nil") + } + if !strings.Contains(customAlertProvider.Body, "triggered") { + t.Error("customAlertProvider.Body should've contained the substring triggered") + } }