2022-12-16 05:32:04 +01:00
package github
import (
"net/http"
"strings"
"testing"
"github.com/TwiN/gatus/v5/alerting/alert"
"github.com/TwiN/gatus/v5/client"
2024-05-10 04:56:16 +02:00
"github.com/TwiN/gatus/v5/config/endpoint"
2022-12-16 05:32:04 +01:00
"github.com/TwiN/gatus/v5/test"
"github.com/google/go-github/v48/github"
)
2024-12-17 02:32:13 +01:00
func TestAlertProvider_Validate ( t * testing . T ) {
2022-12-16 05:32:04 +01:00
scenarios := [ ] struct {
2024-12-17 02:32:13 +01:00
Name string
Provider AlertProvider
ExpectedError bool
2022-12-16 05:32:04 +01:00
} {
{
2024-12-17 02:32:13 +01:00
Name : "invalid" ,
Provider : AlertProvider { DefaultConfig : Config { RepositoryURL : "" , Token : "" } } ,
ExpectedError : true ,
2022-12-16 05:32:04 +01:00
} ,
{
2024-12-17 02:32:13 +01:00
Name : "invalid-token" ,
Provider : AlertProvider { DefaultConfig : Config { RepositoryURL : "https://github.com/TwiN/test" , Token : "12345" } } ,
ExpectedError : true ,
2022-12-16 05:32:04 +01:00
} ,
{
2024-12-17 02:32:13 +01:00
Name : "missing-repository-name" ,
Provider : AlertProvider { DefaultConfig : Config { RepositoryURL : "https://github.com/TwiN" , Token : "12345" } } ,
ExpectedError : true ,
2022-12-16 05:32:04 +01:00
} ,
{
2024-12-17 02:32:13 +01:00
Name : "enterprise-client" ,
Provider : AlertProvider { DefaultConfig : Config { RepositoryURL : "https://github.example.com/TwiN/test" , Token : "12345" } } ,
ExpectedError : true ,
2022-12-16 05:32:04 +01:00
} ,
{
2024-12-17 02:32:13 +01:00
Name : "invalid-url" ,
Provider : AlertProvider { DefaultConfig : Config { RepositoryURL : "github.com/TwiN/test" , Token : "12345" } } ,
ExpectedError : true ,
2022-12-16 05:32:04 +01:00
} ,
}
for _ , scenario := range scenarios {
t . Run ( scenario . Name , func ( t * testing . T ) {
2024-12-17 02:32:13 +01:00
err := scenario . Provider . Validate ( )
if scenario . ExpectedError && err == nil {
t . Error ( "expected error, got none" )
}
if ! scenario . ExpectedError && err != nil && ! strings . Contains ( err . Error ( ) , "user does not exist" ) && ! strings . Contains ( err . Error ( ) , "no such host" ) {
t . Error ( "expected no error, got" , err . Error ( ) )
2022-12-16 05:32:04 +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-error" ,
2024-12-17 02:32:13 +01:00
Provider : AlertProvider { DefaultConfig : Config { RepositoryURL : "https://github.com/TwiN/test" , Token : "12345" } } ,
2022-12-16 05:32:04 +01:00
Alert : alert . Alert { Description : & firstDescription , SuccessThreshold : 5 , FailureThreshold : 3 } ,
Resolved : false ,
ExpectedError : true ,
} ,
{
Name : "resolved-error" ,
2024-12-17 02:32:13 +01:00
Provider : AlertProvider { DefaultConfig : Config { RepositoryURL : "https://github.com/TwiN/test" , Token : "12345" } } ,
2022-12-16 05:32:04 +01:00
Alert : alert . Alert { Description : & secondDescription , SuccessThreshold : 5 , FailureThreshold : 3 } ,
Resolved : true ,
ExpectedError : true ,
} ,
}
for _ , scenario := range scenarios {
t . Run ( scenario . Name , func ( t * testing . T ) {
2024-12-17 02:32:13 +01:00
cfg , err := scenario . Provider . GetConfig ( "" , & scenario . Alert )
if err != nil && ! strings . Contains ( err . Error ( ) , "failed to retrieve GitHub user" ) && ! strings . Contains ( err . Error ( ) , "no such host" ) {
t . Error ( "expected no error, got" , err . Error ( ) )
}
cfg . githubClient = github . NewClient ( nil )
2022-12-16 05:32:04 +01:00
client . InjectHTTPClient ( & http . Client { Transport : scenario . MockRoundTripper } )
2024-12-17 02:32:13 +01:00
err = scenario . Provider . Send (
2024-05-10 04:56:16 +02:00
& endpoint . Endpoint { Name : "endpoint-name" , Group : "endpoint-group" } ,
2022-12-16 05:32:04 +01:00
& scenario . Alert ,
2024-05-10 04:56:16 +02:00
& endpoint . Result {
ConditionResults : [ ] * endpoint . ConditionResult {
2022-12-16 05:32:04 +01: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 ( ) )
}
} )
}
}
func TestAlertProvider_buildRequestBody ( t * testing . T ) {
firstDescription := "description-1"
scenarios := [ ] struct {
Name string
2024-05-10 04:56:16 +02:00
Endpoint endpoint . Endpoint
2022-12-16 05:32:04 +01:00
Provider AlertProvider
Alert alert . Alert
2024-04-11 02:46:17 +02:00
NoConditions bool
2022-12-16 05:32:04 +01:00
ExpectedBody string
} {
{
Name : "triggered" ,
2024-05-10 04:56:16 +02:00
Endpoint : endpoint . Endpoint { Name : "endpoint-name" , URL : "https://example.org" } ,
2022-12-16 05:32:04 +01:00
Provider : AlertProvider { } ,
Alert : alert . Alert { Description : & firstDescription , FailureThreshold : 3 } ,
ExpectedBody : "An alert for **endpoint-name** has been triggered due to having failed 3 time(s) in a row:\n> description-1\n\n## Condition results\n- :white_check_mark: - `[CONNECTED] == true`\n- :x: - `[STATUS] == 200`" ,
} ,
{
2024-04-11 02:46:17 +02:00
Name : "triggered-with-no-description" ,
2024-05-10 04:56:16 +02:00
Endpoint : endpoint . Endpoint { Name : "endpoint-name" , URL : "https://example.org" } ,
2022-12-16 05:32:04 +01:00
Provider : AlertProvider { } ,
Alert : alert . Alert { FailureThreshold : 10 } ,
ExpectedBody : "An alert for **endpoint-name** has been triggered due to having failed 10 time(s) in a row\n\n## Condition results\n- :white_check_mark: - `[CONNECTED] == true`\n- :x: - `[STATUS] == 200`" ,
} ,
2024-04-11 02:46:17 +02:00
{
Name : "triggered-with-no-conditions" ,
NoConditions : true ,
2024-05-10 04:56:16 +02:00
Endpoint : endpoint . Endpoint { Name : "endpoint-name" , URL : "https://example.org" } ,
2024-04-11 02:46:17 +02:00
Provider : AlertProvider { } ,
Alert : alert . Alert { Description : & firstDescription , FailureThreshold : 10 } ,
ExpectedBody : "An alert for **endpoint-name** has been triggered due to having failed 10 time(s) in a row:\n> description-1" ,
} ,
2022-12-16 05:32:04 +01:00
}
for _ , scenario := range scenarios {
t . Run ( scenario . Name , func ( t * testing . T ) {
2024-05-10 04:56:16 +02:00
var conditionResults [ ] * endpoint . ConditionResult
2024-04-11 02:46:17 +02:00
if ! scenario . NoConditions {
2024-05-10 04:56:16 +02:00
conditionResults = [ ] * endpoint . ConditionResult {
2024-04-11 02:46:17 +02:00
{ Condition : "[CONNECTED] == true" , Success : true } ,
{ Condition : "[STATUS] == 200" , Success : false } ,
}
}
2022-12-16 05:32:04 +01:00
body := scenario . Provider . buildIssueBody (
& scenario . Endpoint ,
& scenario . Alert ,
2024-05-10 04:56:16 +02:00
& endpoint . Result { ConditionResults : conditionResults } ,
2022-12-16 05:32:04 +01:00
)
if strings . TrimSpace ( body ) != strings . TrimSpace ( scenario . ExpectedBody ) {
t . Errorf ( "expected:\n%s\ngot:\n%s" , scenario . ExpectedBody , body )
}
} )
}
}
func TestAlertProvider_GetDefaultAlert ( t * testing . T ) {
2024-02-08 02:09:45 +01:00
if ( & AlertProvider { DefaultAlert : & alert . Alert { } } ) . GetDefaultAlert ( ) == nil {
2022-12-16 05:32:04 +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 {
2022-12-16 05:32:04 +01:00
t . Error ( "expected default alert to be nil" )
}
}
2024-12-17 02:32:13 +01:00
func TestAlertProvider_GetConfig ( t * testing . T ) {
scenarios := [ ] struct {
Name string
Provider AlertProvider
InputAlert alert . Alert
ExpectedOutput Config
} {
{
Name : "provider-no-override-should-default" ,
Provider : AlertProvider {
DefaultConfig : Config { RepositoryURL : "https://github.com/TwiN/test" , Token : "12345" } ,
} ,
InputAlert : alert . Alert { } ,
ExpectedOutput : Config { RepositoryURL : "https://github.com/TwiN/test" , Token : "12345" } ,
} ,
{
Name : "provider-with-alert-override" ,
Provider : AlertProvider {
DefaultConfig : Config { RepositoryURL : "https://github.com/TwiN/test" , Token : "12345" } ,
} ,
InputAlert : alert . Alert { ProviderOverride : map [ string ] any { "repository-url" : "https://github.com/TwiN/alert-test" , "token" : "54321" } } ,
ExpectedOutput : Config { RepositoryURL : "https://github.com/TwiN/alert-test" , Token : "54321" } ,
} ,
}
for _ , scenario := range scenarios {
t . Run ( scenario . Name , func ( t * testing . T ) {
got , err := scenario . Provider . GetConfig ( "" , & scenario . InputAlert )
if err != nil && ! strings . Contains ( err . Error ( ) , "failed to retrieve GitHub user" ) && ! strings . Contains ( err . Error ( ) , "no such host" ) {
t . Fatalf ( "unexpected error: %s" , err )
}
if got . RepositoryURL != scenario . ExpectedOutput . RepositoryURL {
t . Errorf ( "expected repository URL %s, got %s" , scenario . ExpectedOutput . RepositoryURL , got . RepositoryURL )
}
if got . Token != scenario . ExpectedOutput . Token {
t . Errorf ( "expected token %s, got %s" , scenario . ExpectedOutput . Token , got . Token )
}
// Test ValidateOverrides as well, since it really just calls GetConfig
if err = scenario . Provider . ValidateOverrides ( "" , & scenario . InputAlert ) ; err != nil && ! strings . Contains ( err . Error ( ) , "failed to retrieve GitHub user" ) {
t . Errorf ( "unexpected error: %s" , err )
}
} )
}
}