2020-09-19 22:29:08 +02:00
|
|
|
package custom
|
2020-09-05 06:01:12 +02:00
|
|
|
|
|
|
|
import (
|
2022-06-08 01:37:42 +02:00
|
|
|
"fmt"
|
2021-12-03 07:44:17 +01:00
|
|
|
"io"
|
2021-12-03 05:10:21 +01:00
|
|
|
"net/http"
|
2020-09-05 06:01:12 +02:00
|
|
|
"testing"
|
2021-12-03 04:15:51 +01:00
|
|
|
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/alert"
|
|
|
|
"github.com/TwiN/gatus/v5/client"
|
|
|
|
"github.com/TwiN/gatus/v5/core"
|
|
|
|
"github.com/TwiN/gatus/v5/test"
|
2020-09-05 06:01:12 +02:00
|
|
|
)
|
|
|
|
|
2020-09-19 22:29:08 +02:00
|
|
|
func TestAlertProvider_IsValid(t *testing.T) {
|
2022-09-08 00:19:20 +02:00
|
|
|
t.Run("invalid-provider", func(t *testing.T) {
|
|
|
|
invalidProvider := AlertProvider{URL: ""}
|
|
|
|
if invalidProvider.IsValid() {
|
|
|
|
t.Error("provider shouldn't have been valid")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("valid-provider", func(t *testing.T) {
|
|
|
|
validProvider := AlertProvider{URL: "https://example.com"}
|
|
|
|
if validProvider.ClientConfig != nil {
|
|
|
|
t.Error("provider client config should have been nil prior to IsValid() being executed")
|
|
|
|
}
|
|
|
|
if !validProvider.IsValid() {
|
|
|
|
t.Error("provider should've been valid")
|
|
|
|
}
|
|
|
|
if validProvider.ClientConfig == nil {
|
|
|
|
t.Error("provider client config should have been set after IsValid() was executed")
|
|
|
|
}
|
|
|
|
})
|
2020-09-19 22:22:12 +02:00
|
|
|
}
|
|
|
|
|
2021-12-03 05:10:21 +01:00
|
|
|
func TestAlertProvider_Send(t *testing.T) {
|
|
|
|
defer client.InjectHTTPClient(nil)
|
|
|
|
firstDescription := "description-1"
|
|
|
|
secondDescription := "description-2"
|
|
|
|
scenarios := []struct {
|
|
|
|
Name string
|
|
|
|
Provider AlertProvider
|
|
|
|
Alert alert.Alert
|
|
|
|
Resolved bool
|
|
|
|
MockRoundTripper test.MockRoundTripper
|
|
|
|
ExpectedError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "triggered",
|
|
|
|
Provider: AlertProvider{},
|
|
|
|
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
|
|
|
Resolved: false,
|
|
|
|
MockRoundTripper: test.MockRoundTripper(func(r *http.Request) *http.Response {
|
|
|
|
return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody}
|
|
|
|
}),
|
|
|
|
ExpectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "triggered-error",
|
|
|
|
Provider: AlertProvider{},
|
|
|
|
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
|
|
|
Resolved: false,
|
|
|
|
MockRoundTripper: test.MockRoundTripper(func(r *http.Request) *http.Response {
|
|
|
|
return &http.Response{StatusCode: http.StatusInternalServerError, Body: http.NoBody}
|
|
|
|
}),
|
|
|
|
ExpectedError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "resolved",
|
|
|
|
Provider: AlertProvider{},
|
|
|
|
Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
|
|
|
Resolved: true,
|
|
|
|
MockRoundTripper: test.MockRoundTripper(func(r *http.Request) *http.Response {
|
|
|
|
return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody}
|
|
|
|
}),
|
|
|
|
ExpectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "resolved-error",
|
|
|
|
Provider: AlertProvider{},
|
|
|
|
Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
|
|
|
Resolved: true,
|
|
|
|
MockRoundTripper: test.MockRoundTripper(func(r *http.Request) *http.Response {
|
|
|
|
return &http.Response{StatusCode: http.StatusInternalServerError, Body: http.NoBody}
|
|
|
|
}),
|
|
|
|
ExpectedError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
|
|
|
t.Run(scenario.Name, func(t *testing.T) {
|
|
|
|
client.InjectHTTPClient(&http.Client{Transport: scenario.MockRoundTripper})
|
|
|
|
err := scenario.Provider.Send(
|
|
|
|
&core.Endpoint{Name: "endpoint-name"},
|
|
|
|
&scenario.Alert,
|
|
|
|
&core.Result{
|
|
|
|
ConditionResults: []*core.ConditionResult{
|
|
|
|
{Condition: "[CONNECTED] == true", Success: scenario.Resolved},
|
|
|
|
{Condition: "[STATUS] == 200", Success: scenario.Resolved},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
scenario.Resolved,
|
|
|
|
)
|
|
|
|
if scenario.ExpectedError && err == nil {
|
|
|
|
t.Error("expected error, got none")
|
|
|
|
}
|
|
|
|
if !scenario.ExpectedError && err != nil {
|
|
|
|
t.Error("expected no error, got", err.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 01:37:42 +02:00
|
|
|
func TestAlertProvider_buildHTTPRequest(t *testing.T) {
|
2020-09-19 22:29:08 +02:00
|
|
|
customAlertProvider := &AlertProvider{
|
2022-06-08 01:37:42 +02:00
|
|
|
URL: "https://example.com/[ENDPOINT_GROUP]/[ENDPOINT_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]&url=[ENDPOINT_URL]",
|
|
|
|
Body: "[ENDPOINT_NAME],[ENDPOINT_GROUP],[ALERT_DESCRIPTION],[ENDPOINT_URL],[ALERT_TRIGGERED_OR_RESOLVED]",
|
2020-09-05 06:01:12 +02:00
|
|
|
}
|
2022-06-08 01:37:42 +02:00
|
|
|
alertDescription := "alert-description"
|
|
|
|
scenarios := []struct {
|
|
|
|
AlertProvider *AlertProvider
|
|
|
|
Resolved bool
|
|
|
|
ExpectedURL string
|
|
|
|
ExpectedBody string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
AlertProvider: customAlertProvider,
|
|
|
|
Resolved: true,
|
|
|
|
ExpectedURL: "https://example.com/endpoint-group/endpoint-name?event=RESOLVED&description=alert-description&url=https://example.com",
|
|
|
|
ExpectedBody: "endpoint-name,endpoint-group,alert-description,https://example.com,RESOLVED",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AlertProvider: customAlertProvider,
|
|
|
|
Resolved: false,
|
|
|
|
ExpectedURL: "https://example.com/endpoint-group/endpoint-name?event=TRIGGERED&description=alert-description&url=https://example.com",
|
|
|
|
ExpectedBody: "endpoint-name,endpoint-group,alert-description,https://example.com,TRIGGERED",
|
|
|
|
},
|
2020-09-05 06:01:12 +02:00
|
|
|
}
|
2022-06-08 01:37:42 +02:00
|
|
|
for _, scenario := range scenarios {
|
|
|
|
t.Run(fmt.Sprintf("resolved-%v-with-default-placeholders", scenario.Resolved), func(t *testing.T) {
|
|
|
|
request := customAlertProvider.buildHTTPRequest(
|
|
|
|
&core.Endpoint{Name: "endpoint-name", Group: "endpoint-group", URL: "https://example.com"},
|
|
|
|
&alert.Alert{Description: &alertDescription},
|
|
|
|
scenario.Resolved,
|
|
|
|
)
|
|
|
|
if request.URL.String() != scenario.ExpectedURL {
|
|
|
|
t.Error("expected URL to be", scenario.ExpectedURL, "got", request.URL.String())
|
|
|
|
}
|
|
|
|
body, _ := io.ReadAll(request.Body)
|
|
|
|
if string(body) != scenario.ExpectedBody {
|
|
|
|
t.Error("expected body to be", scenario.ExpectedBody, "got", string(body))
|
|
|
|
}
|
|
|
|
})
|
2020-09-05 06:01:12 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-22 03:18:06 +02:00
|
|
|
|
2021-02-17 12:39:17 +01:00
|
|
|
func TestAlertProvider_buildHTTPRequestWithCustomPlaceholder(t *testing.T) {
|
|
|
|
customAlertProvider := &AlertProvider{
|
2022-06-08 01:37:42 +02:00
|
|
|
URL: "https://example.com/[ENDPOINT_GROUP]/[ENDPOINT_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]",
|
|
|
|
Body: "[ENDPOINT_NAME],[ENDPOINT_GROUP],[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": {
|
2022-06-08 01:37:42 +02:00
|
|
|
"RESOLVED": "fixed",
|
|
|
|
"TRIGGERED": "boom",
|
2021-02-17 12:39:17 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-06-08 01:37:42 +02:00
|
|
|
alertDescription := "alert-description"
|
|
|
|
scenarios := []struct {
|
|
|
|
AlertProvider *AlertProvider
|
|
|
|
Resolved bool
|
|
|
|
ExpectedURL string
|
|
|
|
ExpectedBody string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
AlertProvider: customAlertProvider,
|
|
|
|
Resolved: true,
|
|
|
|
ExpectedURL: "https://example.com/endpoint-group/endpoint-name?event=fixed&description=alert-description",
|
|
|
|
ExpectedBody: "endpoint-name,endpoint-group,alert-description,fixed",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AlertProvider: customAlertProvider,
|
|
|
|
Resolved: false,
|
|
|
|
ExpectedURL: "https://example.com/endpoint-group/endpoint-name?event=boom&description=alert-description",
|
|
|
|
ExpectedBody: "endpoint-name,endpoint-group,alert-description,boom",
|
|
|
|
},
|
2021-02-17 12:39:17 +01:00
|
|
|
}
|
2022-06-08 01:37:42 +02:00
|
|
|
for _, scenario := range scenarios {
|
|
|
|
t.Run(fmt.Sprintf("resolved-%v-with-custom-placeholders", scenario.Resolved), func(t *testing.T) {
|
|
|
|
request := customAlertProvider.buildHTTPRequest(
|
|
|
|
&core.Endpoint{Name: "endpoint-name", Group: "endpoint-group"},
|
|
|
|
&alert.Alert{Description: &alertDescription},
|
|
|
|
scenario.Resolved,
|
|
|
|
)
|
|
|
|
if request.URL.String() != scenario.ExpectedURL {
|
|
|
|
t.Error("expected URL to be", scenario.ExpectedURL, "got", request.URL.String())
|
|
|
|
}
|
|
|
|
body, _ := io.ReadAll(request.Body)
|
|
|
|
if string(body) != scenario.ExpectedBody {
|
|
|
|
t.Error("expected body to be", scenario.ExpectedBody, "got", string(body))
|
|
|
|
}
|
|
|
|
})
|
2021-02-17 12:39:17 +01:00
|
|
|
}
|
|
|
|
}
|
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{
|
2022-06-08 01:37:42 +02:00
|
|
|
URL: "https://example.com/[ENDPOINT_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]",
|
|
|
|
Body: "[ENDPOINT_NAME],[ENDPOINT_GROUP],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]",
|
2021-02-17 13:45:22 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
|
2021-12-03 04:15:51 +01:00
|
|
|
func TestAlertProvider_GetDefaultAlert(t *testing.T) {
|
2024-02-08 02:09:45 +01:00
|
|
|
if (&AlertProvider{DefaultAlert: &alert.Alert{}}).GetDefaultAlert() == nil {
|
2021-12-03 04:15:51 +01:00
|
|
|
t.Error("expected default alert to be not nil")
|
|
|
|
}
|
2024-02-08 02:09:45 +01:00
|
|
|
if (&AlertProvider{DefaultAlert: nil}).GetDefaultAlert() != nil {
|
2021-12-03 04:15:51 +01:00
|
|
|
t.Error("expected default alert to be nil")
|
|
|
|
}
|
|
|
|
}
|