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