2020-09-19 16:29:08 -04:00
package custom
2020-09-05 00:01:12 -04:00
import (
2022-06-07 19:37:42 -04:00
"fmt"
2021-12-03 14:44:17 +08:00
"io"
2021-12-02 23:10:21 -05:00
"net/http"
2020-09-05 00:01:12 -04:00
"testing"
2021-12-02 22:15:51 -05:00
2022-12-06 01:41:09 -05:00
"github.com/TwiN/gatus/v5/alerting/alert"
"github.com/TwiN/gatus/v5/client"
2024-05-09 22:56:16 -04:00
"github.com/TwiN/gatus/v5/config/endpoint"
2022-12-06 01:41:09 -05:00
"github.com/TwiN/gatus/v5/test"
2020-09-05 00:01:12 -04:00
)
2024-12-16 20:32:13 -05:00
func TestAlertProvider_Validate ( t * testing . T ) {
2022-09-07 18:19:20 -04:00
t . Run ( "invalid-provider" , func ( t * testing . T ) {
2024-12-16 20:32:13 -05:00
invalidProvider := AlertProvider { DefaultConfig : Config { URL : "" }}
if err := invalidProvider . Validate (); err == nil {
2022-09-07 18:19:20 -04:00
t . Error ( "provider shouldn't have been valid" )
}
})
t . Run ( "valid-provider" , func ( t * testing . T ) {
2024-12-16 20:32:13 -05:00
validProvider := AlertProvider { DefaultConfig : Config { URL : "https://example.com" }}
if err := validProvider . Validate (); err != nil {
2022-09-07 18:19:20 -04:00
t . Error ( "provider should've been valid" )
}
})
2020-09-19 16:22:12 -04:00
}
2021-12-02 23:10:21 -05: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" ,
2024-12-16 20:32:13 -05:00
Provider : AlertProvider { DefaultConfig : Config { URL : "https://example.com" }},
2021-12-02 23:10:21 -05:00
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" ,
2024-12-16 20:32:13 -05:00
Provider : AlertProvider { DefaultConfig : Config { URL : "https://example.com" }},
2021-12-02 23:10:21 -05:00
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" ,
2024-12-16 20:32:13 -05:00
Provider : AlertProvider { DefaultConfig : Config { URL : "https://example.com" }},
2021-12-02 23:10:21 -05:00
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" ,
2024-12-16 20:32:13 -05:00
Provider : AlertProvider { DefaultConfig : Config { URL : "https://example.com" }},
2021-12-02 23:10:21 -05:00
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 (
2024-05-09 22:56:16 -04:00
& endpoint . Endpoint { Name : "endpoint-name" },
2021-12-02 23:10:21 -05:00
& scenario . Alert ,
2024-05-09 22:56:16 -04:00
& endpoint . Result {
ConditionResults : [] * endpoint . ConditionResult {
2021-12-02 23:10:21 -05:00
{ 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-07 19:37:42 -04:00
func TestAlertProvider_buildHTTPRequest ( t * testing . T ) {
2024-12-16 20:32:13 -05:00
alertProvider := & AlertProvider {
DefaultConfig : Config {
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 00:01:12 -04:00
}
2022-06-07 19:37:42 -04:00
alertDescription := "alert-description"
scenarios := [] struct {
AlertProvider * AlertProvider
Resolved bool
ExpectedURL string
ExpectedBody string
}{
{
2024-12-16 20:32:13 -05:00
AlertProvider : alertProvider ,
2022-06-07 19:37:42 -04:00
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" ,
},
{
2024-12-16 20:32:13 -05:00
AlertProvider : alertProvider ,
2022-06-07 19:37:42 -04:00
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 00:01:12 -04:00
}
2022-06-07 19:37:42 -04:00
for _ , scenario := range scenarios {
t . Run ( fmt . Sprintf ( "resolved-%v-with-default-placeholders" , scenario . Resolved ), func ( t * testing . T ) {
2024-12-16 20:32:13 -05:00
request := alertProvider . buildHTTPRequest (
& alertProvider . DefaultConfig ,
2024-05-09 22:56:16 -04:00
& endpoint . Endpoint { Name : "endpoint-name" , Group : "endpoint-group" , URL : "https://example.com" },
2022-06-07 19:37:42 -04:00
& alert . Alert { Description : & alertDescription },
2024-09-18 10:26:21 +08:00
& endpoint . Result { Errors : [] string {}},
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 ))
}
})
}
}
func TestAlertProviderWithResultErrors_buildHTTPRequest ( t * testing . T ) {
2024-12-16 20:32:13 -05:00
alertProvider := & AlertProvider {
DefaultConfig : Config {
URL : "https://example.com/[ENDPOINT_GROUP]/[ENDPOINT_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]&url=[ENDPOINT_URL]&error=[RESULT_ERRORS]" ,
Body : "[ENDPOINT_NAME],[ENDPOINT_GROUP],[ALERT_DESCRIPTION],[ENDPOINT_URL],[ALERT_TRIGGERED_OR_RESOLVED],[RESULT_ERRORS]" ,
},
2024-09-18 10:26:21 +08:00
}
alertDescription := "alert-description"
scenarios := [] struct {
AlertProvider * AlertProvider
Resolved bool
ExpectedURL string
ExpectedBody string
Errors [] string
}{
{
2024-12-16 20:32:13 -05:00
AlertProvider : alertProvider ,
2024-09-18 10:26:21 +08:00
Resolved : true ,
ExpectedURL : "https://example.com/endpoint-group/endpoint-name?event=RESOLVED&description=alert-description&url=https://example.com&error=" ,
ExpectedBody : "endpoint-name,endpoint-group,alert-description,https://example.com,RESOLVED," ,
},
{
2024-12-16 20:32:13 -05:00
AlertProvider : alertProvider ,
2024-09-18 10:26:21 +08:00
Resolved : false ,
ExpectedURL : "https://example.com/endpoint-group/endpoint-name?event=TRIGGERED&description=alert-description&url=https://example.com&error=error1,error2" ,
ExpectedBody : "endpoint-name,endpoint-group,alert-description,https://example.com,TRIGGERED,error1,error2" ,
Errors : [] string { "error1" , "error2" },
},
2025-05-13 23:55:35 +01:00
{
AlertProvider : alertProvider ,
Resolved : false ,
ExpectedURL : "https://example.com/endpoint-group/endpoint-name?event=TRIGGERED&description=alert-description&url=https://example.com&error=test \\\"error with quotes\\\"" ,
ExpectedBody : "endpoint-name,endpoint-group,alert-description,https://example.com,TRIGGERED,test \\\"error with quotes\\\"" ,
Errors : [] string { "test \"error with quotes\"" },
},
2024-09-18 10:26:21 +08:00
}
for _ , scenario := range scenarios {
t . Run ( fmt . Sprintf ( "resolved-%v-with-default-placeholders-and-result-errors" , scenario . Resolved ), func ( t * testing . T ) {
2024-12-16 20:32:13 -05:00
request := alertProvider . buildHTTPRequest (
& alertProvider . DefaultConfig ,
2024-09-18 10:26:21 +08:00
& endpoint . Endpoint { Name : "endpoint-name" , Group : "endpoint-group" , URL : "https://example.com" },
& alert . Alert { Description : & alertDescription },
& endpoint . Result { Errors : scenario . Errors },
2022-06-07 19:37:42 -04:00
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 00:01:12 -04:00
}
}
2020-10-21 21:18:06 -04:00
2021-02-17 12:39:17 +01:00
func TestAlertProvider_buildHTTPRequestWithCustomPlaceholder ( t * testing . T ) {
2024-12-16 20:32:13 -05:00
alertProvider := & AlertProvider {
DefaultConfig : Config {
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]" ,
Headers : nil ,
Placeholders : map [ string ] map [ string ] string {
"ALERT_TRIGGERED_OR_RESOLVED" : {
"RESOLVED" : "fixed" ,
"TRIGGERED" : "boom" ,
},
2021-02-17 12:39:17 +01:00
},
},
}
2022-06-07 19:37:42 -04:00
alertDescription := "alert-description"
scenarios := [] struct {
AlertProvider * AlertProvider
Resolved bool
ExpectedURL string
ExpectedBody string
}{
{
2024-12-16 20:32:13 -05:00
AlertProvider : alertProvider ,
2022-06-07 19:37:42 -04:00
Resolved : true ,
ExpectedURL : "https://example.com/endpoint-group/endpoint-name?event=fixed&description=alert-description" ,
ExpectedBody : "endpoint-name,endpoint-group,alert-description,fixed" ,
},
{
2024-12-16 20:32:13 -05:00
AlertProvider : alertProvider ,
2022-06-07 19:37:42 -04:00
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-07 19:37:42 -04:00
for _ , scenario := range scenarios {
t . Run ( fmt . Sprintf ( "resolved-%v-with-custom-placeholders" , scenario . Resolved ), func ( t * testing . T ) {
2024-12-16 20:32:13 -05:00
request := alertProvider . buildHTTPRequest (
& alertProvider . DefaultConfig ,
2024-05-09 22:56:16 -04:00
& endpoint . Endpoint { Name : "endpoint-name" , Group : "endpoint-group" },
2022-06-07 19:37:42 -04:00
& alert . Alert { Description : & alertDescription },
2024-09-18 10:26:21 +08:00
& endpoint . Result {},
2022-06-07 19:37:42 -04:00
scenario . Resolved ,
)
if request . URL . String () != scenario . ExpectedURL {
t . Error ( "expected URL to be" , scenario . ExpectedURL , "got" , request . URL . String ())
2025-10-06 18:22:38 +02:00
}
body , _ := io . ReadAll ( request . Body )
if string ( body ) != scenario . ExpectedBody {
t . Error ( "expected body to be" , scenario . ExpectedBody , "got" , string ( body ))
}
})
}
}
func TestAlertProvider_buildHTTPRequestWithCustomPlaceholderAndResultConditions ( t * testing . T ) {
alertProvider := & AlertProvider {
DefaultConfig : Config {
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],[RESULT_CONDITIONS]" ,
Headers : nil ,
Placeholders : map [ string ] map [ string ] string {
"ALERT_TRIGGERED_OR_RESOLVED" : {
"RESOLVED" : "fixed" ,
"TRIGGERED" : "boom" ,
},
},
},
}
alertDescription := "alert-description"
scenarios := [] struct {
AlertProvider * AlertProvider
Resolved bool
ExpectedURL string
ExpectedBody string
NoConditions bool
}{
{
AlertProvider : alertProvider ,
Resolved : true ,
ExpectedURL : "https://example.com/endpoint-group/endpoint-name?event=fixed&description=alert-description" ,
ExpectedBody : "endpoint-name,endpoint-group,alert-description,fixed,✅ - `[CONNECTED] == true`, ✅ - `[STATUS] == 200`" ,
},
{
AlertProvider : alertProvider ,
Resolved : false ,
ExpectedURL : "https://example.com/endpoint-group/endpoint-name?event=boom&description=alert-description" ,
ExpectedBody : "endpoint-name,endpoint-group,alert-description,boom,❌ - `[CONNECTED] == true`, ❌ - `[STATUS] == 200`" ,
},
}
for _ , scenario := range scenarios {
t . Run ( fmt . Sprintf ( "resolved-%v-with-custom-placeholders" , scenario . Resolved ), func ( t * testing . T ) {
var conditionResults [] * endpoint . ConditionResult
if ! scenario . NoConditions {
conditionResults = [] * endpoint . ConditionResult {
{ Condition : "[CONNECTED] == true" , Success : scenario . Resolved },
{ Condition : "[STATUS] == 200" , Success : scenario . Resolved },
}
}
request := alertProvider . buildHTTPRequest (
& alertProvider . DefaultConfig ,
& endpoint . Endpoint { Name : "endpoint-name" , Group : "endpoint-group" },
& alert . Alert { Description : & alertDescription },
& endpoint . Result { ConditionResults : conditionResults },
scenario . Resolved ,
)
if request . URL . String () != scenario . ExpectedURL {
t . Error ( "expected URL to be" , scenario . ExpectedURL , "got" , request . URL . String ())
2022-06-07 19:37:42 -04:00
}
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 ) {
2024-12-16 20:32:13 -05:00
alertProvider := & AlertProvider {
DefaultConfig : Config {
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
}
2024-12-16 20:32:13 -05:00
if alertProvider . GetAlertStatePlaceholderValue ( & alertProvider . DefaultConfig , true ) != "RESOLVED" {
t . Error ( "expected RESOLVED, got" , alertProvider . GetAlertStatePlaceholderValue ( & alertProvider . DefaultConfig , true ))
2021-02-17 13:45:22 +01:00
}
2024-12-16 20:32:13 -05:00
if alertProvider . GetAlertStatePlaceholderValue ( & alertProvider . DefaultConfig , false ) != "TRIGGERED" {
t . Error ( "expected TRIGGERED, got" , alertProvider . GetAlertStatePlaceholderValue ( & alertProvider . DefaultConfig , false ))
2021-02-17 13:45:22 +01:00
}
}
2021-10-23 16:47:12 -04:00
2021-12-02 22:15:51 -05:00
func TestAlertProvider_GetDefaultAlert ( t * testing . T ) {
2024-02-07 20:09:45 -05:00
if ( & AlertProvider { DefaultAlert : & alert . Alert {}}). GetDefaultAlert () == nil {
2021-12-02 22:15:51 -05:00
t . Error ( "expected default alert to be not nil" )
}
2024-02-07 20:09:45 -05:00
if ( & AlertProvider { DefaultAlert : nil }). GetDefaultAlert () != nil {
2021-12-02 22:15:51 -05:00
t . Error ( "expected default alert to be nil" )
}
}
2024-12-16 20:32:13 -05:00
func TestAlertProvider_GetConfig ( t * testing . T ) {
scenarios := [] struct {
Name string
Provider AlertProvider
InputGroup string
InputAlert alert . Alert
ExpectedOutput Config
}{
{
Name : "provider-no-override-specify-no-group-should-default" ,
Provider : AlertProvider {
DefaultConfig : Config { URL : "http://example.com" , Body : "default-body" },
Overrides : nil ,
},
InputGroup : "" ,
InputAlert : alert . Alert {},
ExpectedOutput : Config { URL : "http://example.com" , Body : "default-body" },
},
{
Name : "provider-no-override-specify-group-should-default" ,
Provider : AlertProvider {
DefaultConfig : Config { URL : "http://example.com" },
Overrides : nil ,
},
InputGroup : "group" ,
InputAlert : alert . Alert {},
ExpectedOutput : Config { URL : "http://example.com" },
},
{
Name : "provider-with-override-specify-no-group-should-default" ,
Provider : AlertProvider {
DefaultConfig : Config { URL : "http://example.com" },
Overrides : [] Override {
{
Group : "group" ,
Config : Config { URL : "http://group-example.com" , Headers : map [ string ] string { "Cache" : "true" }},
},
},
},
InputGroup : "" ,
InputAlert : alert . Alert {},
ExpectedOutput : Config { URL : "http://example.com" , Headers : map [ string ] string { "Cache" : "true" }},
},
{
Name : "provider-with-override-specify-group-should-override" ,
Provider : AlertProvider {
DefaultConfig : Config { URL : "http://example.com" , Body : "default-body" },
Overrides : [] Override {
{
Group : "group" ,
Config : Config { URL : "http://group-example.com" , Body : "group-body" },
},
},
},
InputGroup : "group" ,
InputAlert : alert . Alert {},
ExpectedOutput : Config { URL : "http://group-example.com" , Body : "group-body" },
},
{
Name : "provider-with-group-override-and-alert-override--alert-override-should-take-precedence" ,
Provider : AlertProvider {
DefaultConfig : Config { URL : "http://example.com" },
Overrides : [] Override {
{
Group : "group" ,
Config : Config { URL : "http://group-example.com" },
},
},
},
InputGroup : "group" ,
InputAlert : alert . Alert { ProviderOverride : map [ string ] any { "url" : "http://alert-example.com" , "body" : "alert-body" }},
ExpectedOutput : Config { URL : "http://alert-example.com" , Body : "alert-body" },
},
{
Name : "provider-with-partial-overrides" ,
Provider : AlertProvider {
DefaultConfig : Config { URL : "http://example.com" },
Overrides : [] Override {
{
Group : "group" ,
Config : Config { Method : "POST" },
},
},
},
InputGroup : "group" ,
InputAlert : alert . Alert { ProviderOverride : map [ string ] any { "body" : "alert-body" }},
ExpectedOutput : Config { URL : "http://example.com" , Body : "alert-body" , Method : "POST" },
},
}
for _ , scenario := range scenarios {
t . Run ( scenario . Name , func ( t * testing . T ) {
got , err := scenario . Provider . GetConfig ( scenario . InputGroup , & scenario . InputAlert )
if err != nil {
t . Fatalf ( "unexpected error: %s" , err )
}
if got . URL != scenario . ExpectedOutput . URL {
t . Errorf ( "expected webhook URL to be %s, got %s" , scenario . ExpectedOutput . URL , got . URL )
}
if got . Body != scenario . ExpectedOutput . Body {
t . Errorf ( "expected body to be %s, got %s" , scenario . ExpectedOutput . Body , got . Body )
}
if got . Headers != nil {
for key , value := range scenario . ExpectedOutput . Headers {
if got . Headers [ key ] != value {
t . Errorf ( "expected header %s to be %s, got %s" , key , value , got . Headers [ key ])
}
}
}
// Test ValidateOverrides as well, since it really just calls GetConfig
if err = scenario . Provider . ValidateOverrides ( scenario . InputGroup , & scenario . InputAlert ); err != nil {
t . Errorf ( "unexpected error: %s" , err )
}
})
}
}