Improve alerting providers test coverage

This commit is contained in:
TwinProduction 2020-10-21 22:35:37 -04:00
parent 89ffc5a788
commit 8c61044e7c
2 changed files with 21 additions and 8 deletions

View File

@ -19,12 +19,11 @@ func TestAlertProvider_IsValid(t *testing.T) {
func TestAlertProvider_buildRequestWhenResolved(t *testing.T) { func TestAlertProvider_buildRequestWhenResolved(t *testing.T) {
const ( const (
ExpectedUrl = "http://example.com/service-name" ExpectedUrl = "http://example.com/service-name?event=RESOLVED&description=alert-description"
ExpectedBody = "service-name,alert-description,RESOLVED" ExpectedBody = "service-name,alert-description,RESOLVED"
) )
customAlertProvider := &AlertProvider{ customAlertProvider := &AlertProvider{
Url: "http://example.com/[SERVICE_NAME]", Url: "http://example.com/[SERVICE_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]",
Method: "GET",
Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]", Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]",
Headers: nil, Headers: nil,
} }
@ -40,14 +39,13 @@ func TestAlertProvider_buildRequestWhenResolved(t *testing.T) {
func TestAlertProvider_buildRequestWhenTriggered(t *testing.T) { func TestAlertProvider_buildRequestWhenTriggered(t *testing.T) {
const ( const (
ExpectedUrl = "http://example.com/service-name" ExpectedUrl = "http://example.com/service-name?event=TRIGGERED&description=alert-description"
ExpectedBody = "service-name,alert-description,TRIGGERED" ExpectedBody = "service-name,alert-description,TRIGGERED"
) )
customAlertProvider := &AlertProvider{ customAlertProvider := &AlertProvider{
Url: "http://example.com/[SERVICE_NAME]", Url: "http://example.com/[SERVICE_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]",
Method: "GET",
Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]", 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) request := customAlertProvider.buildRequest("service-name", "alert-description", false)
if request.URL.String() != ExpectedUrl { if request.URL.String() != ExpectedUrl {

View File

@ -2,6 +2,7 @@ package slack
import ( import (
"github.com/TwinProduction/gatus/core" "github.com/TwinProduction/gatus/core"
"strings"
"testing" "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"} provider := AlertProvider{WebhookUrl: "http://example.com"}
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true) customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true)
if customAlertProvider == nil { if customAlertProvider == nil {
t.Error("customAlertProvider shouldn't have been 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")
}
} }