2020-09-19 22:29:08 +02:00
|
|
|
package custom
|
2020-09-05 06:01:12 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
2020-12-11 02:00:42 +01:00
|
|
|
|
|
|
|
"github.com/TwinProduction/gatus/core"
|
2020-09-05 06:01:12 +02:00
|
|
|
)
|
|
|
|
|
2020-09-19 22:29:08 +02:00
|
|
|
func TestAlertProvider_IsValid(t *testing.T) {
|
2020-10-23 22:29:20 +02:00
|
|
|
invalidProvider := AlertProvider{URL: ""}
|
2020-09-19 22:22:12 +02:00
|
|
|
if invalidProvider.IsValid() {
|
|
|
|
t.Error("provider shouldn't have been valid")
|
|
|
|
}
|
2020-10-23 22:29:20 +02:00
|
|
|
validProvider := AlertProvider{URL: "http://example.com"}
|
2020-09-19 22:22:12 +02:00
|
|
|
if !validProvider.IsValid() {
|
|
|
|
t.Error("provider should've been valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 02:03:59 +01:00
|
|
|
func TestAlertProvider_buildHTTPRequestWhenResolved(t *testing.T) {
|
2020-09-05 06:01:12 +02:00
|
|
|
const (
|
2020-10-23 22:29:20 +02:00
|
|
|
ExpectedURL = "http://example.com/service-name?event=RESOLVED&description=alert-description"
|
2020-09-05 06:01:12 +02:00
|
|
|
ExpectedBody = "service-name,alert-description,RESOLVED"
|
|
|
|
)
|
2020-09-19 22:29:08 +02:00
|
|
|
customAlertProvider := &AlertProvider{
|
2020-10-23 22:29:20 +02:00
|
|
|
URL: "http://example.com/[SERVICE_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]",
|
2020-09-05 06:01:12 +02:00
|
|
|
Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]",
|
|
|
|
Headers: nil,
|
|
|
|
}
|
2020-12-09 02:03:59 +01:00
|
|
|
request := customAlertProvider.buildHTTPRequest("service-name", "alert-description", true)
|
2020-10-23 22:29:20 +02:00
|
|
|
if request.URL.String() != ExpectedURL {
|
|
|
|
t.Error("expected URL to be", ExpectedURL, "was", request.URL.String())
|
2020-09-05 06:01:12 +02:00
|
|
|
}
|
|
|
|
body, _ := ioutil.ReadAll(request.Body)
|
|
|
|
if string(body) != ExpectedBody {
|
|
|
|
t.Error("expected body to be", ExpectedBody, "was", string(body))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 02:03:59 +01:00
|
|
|
func TestAlertProvider_buildHTTPRequestWhenTriggered(t *testing.T) {
|
2020-09-05 06:01:12 +02:00
|
|
|
const (
|
2020-10-23 22:29:20 +02:00
|
|
|
ExpectedURL = "http://example.com/service-name?event=TRIGGERED&description=alert-description"
|
2020-09-05 06:01:12 +02:00
|
|
|
ExpectedBody = "service-name,alert-description,TRIGGERED"
|
|
|
|
)
|
2020-09-19 22:29:08 +02:00
|
|
|
customAlertProvider := &AlertProvider{
|
2020-10-23 22:29:20 +02:00
|
|
|
URL: "http://example.com/[SERVICE_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]",
|
2020-09-05 06:01:12 +02:00
|
|
|
Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]",
|
2020-10-22 04:35:37 +02:00
|
|
|
Headers: map[string]string{"Authorization": "Basic hunter2"},
|
2020-09-05 06:01:12 +02:00
|
|
|
}
|
2020-12-09 02:03:59 +01:00
|
|
|
request := customAlertProvider.buildHTTPRequest("service-name", "alert-description", false)
|
2020-10-23 22:29:20 +02:00
|
|
|
if request.URL.String() != ExpectedURL {
|
|
|
|
t.Error("expected URL to be", ExpectedURL, "was", request.URL.String())
|
2020-09-05 06:01:12 +02:00
|
|
|
}
|
|
|
|
body, _ := ioutil.ReadAll(request.Body)
|
|
|
|
if string(body) != ExpectedBody {
|
|
|
|
t.Error("expected body to be", ExpectedBody, "was", string(body))
|
|
|
|
}
|
|
|
|
}
|
2020-10-22 03:18:06 +02:00
|
|
|
|
|
|
|
func TestAlertProvider_ToCustomAlertProvider(t *testing.T) {
|
2020-10-23 22:29:20 +02:00
|
|
|
provider := AlertProvider{URL: "http://example.com"}
|
2020-10-22 03:18:06 +02:00
|
|
|
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true)
|
|
|
|
if customAlertProvider == nil {
|
2021-02-20 01:06:20 +01:00
|
|
|
t.Fatal("customAlertProvider shouldn't have been nil")
|
2020-10-22 03:18:06 +02:00
|
|
|
}
|
2021-02-20 02:34:35 +01:00
|
|
|
if customAlertProvider.URL != "http://example.com" {
|
|
|
|
t.Error("expected URL to be http://example.com, got", customAlertProvider.URL)
|
2020-10-22 03:18:06 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-17 12:39:17 +01:00
|
|
|
|
|
|
|
func TestAlertProvider_buildHTTPRequestWithCustomPlaceholder(t *testing.T) {
|
|
|
|
const (
|
2021-02-19 05:47:53 +01:00
|
|
|
ExpectedURL = "http://example.com/service-name?event=test&description=alert-description"
|
|
|
|
ExpectedBody = "service-name,alert-description,test"
|
2021-02-17 12:39:17 +01:00
|
|
|
)
|
|
|
|
customAlertProvider := &AlertProvider{
|
|
|
|
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,
|
|
|
|
Placeholders: map[string]map[string]string{
|
|
|
|
"ALERT_TRIGGERED_OR_RESOLVED": {
|
2021-02-19 05:47:53 +01:00
|
|
|
"RESOLVED": "test",
|
2021-02-17 12:39:17 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
request := customAlertProvider.buildHTTPRequest("service-name", "alert-description", true)
|
|
|
|
if request.URL.String() != ExpectedURL {
|
|
|
|
t.Error("expected URL to be", ExpectedURL, "was", request.URL.String())
|
|
|
|
}
|
|
|
|
body, _ := ioutil.ReadAll(request.Body)
|
|
|
|
if string(body) != ExpectedBody {
|
|
|
|
t.Error("expected body to be", ExpectedBody, "was", string(body))
|
|
|
|
}
|
|
|
|
}
|
2021-02-17 13:45:22 +01:00
|
|
|
|
2021-02-18 19:03:12 +01:00
|
|
|
func TestAlertProvider_GetAlertStatePlaceholderValueDefaults(t *testing.T) {
|
2021-02-17 13:45:22 +01:00
|
|
|
customAlertProvider := &AlertProvider{
|
|
|
|
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,
|
|
|
|
Placeholders: nil,
|
|
|
|
}
|
2021-02-18 19:03:12 +01:00
|
|
|
if customAlertProvider.GetAlertStatePlaceholderValue(true) != "RESOLVED" {
|
2021-02-19 05:47:53 +01:00
|
|
|
t.Error("expected RESOLVED, got", customAlertProvider.GetAlertStatePlaceholderValue(true))
|
2021-02-17 13:45:22 +01:00
|
|
|
}
|
2021-02-18 19:03:12 +01:00
|
|
|
if customAlertProvider.GetAlertStatePlaceholderValue(false) != "TRIGGERED" {
|
2021-02-19 05:47:53 +01:00
|
|
|
t.Error("expected TRIGGERED, got", customAlertProvider.GetAlertStatePlaceholderValue(false))
|
2021-02-17 13:45:22 +01:00
|
|
|
}
|
|
|
|
}
|