2021-05-16 03:31:32 +02:00
package provider
import (
"testing"
2022-06-21 03:25:14 +02:00
"github.com/TwiN/gatus/v4/alerting/alert"
2021-05-16 03:31:32 +02:00
)
func TestParseWithDefaultAlert ( t * testing . T ) {
type Scenario struct {
2021-10-23 22:47:12 +02:00
Name string
DefaultAlert , EndpointAlert , ExpectedOutputAlert * alert . Alert
2021-05-16 03:31:32 +02:00
}
enabled := true
disabled := false
firstDescription := "description-1"
secondDescription := "description-2"
scenarios := [ ] Scenario {
{
2021-10-23 22:47:12 +02:00
Name : "endpoint-alert-type-only" ,
2021-05-19 04:29:15 +02:00
DefaultAlert : & alert . Alert {
2021-05-16 03:31:32 +02:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & firstDescription ,
FailureThreshold : 5 ,
SuccessThreshold : 10 ,
} ,
2021-10-23 22:47:12 +02:00
EndpointAlert : & alert . Alert {
2021-05-19 04:29:15 +02:00
Type : alert . TypeDiscord ,
2021-05-16 03:31:32 +02:00
} ,
2021-05-19 04:29:15 +02:00
ExpectedOutputAlert : & alert . Alert {
Type : alert . TypeDiscord ,
2021-05-16 03:31:32 +02:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & firstDescription ,
FailureThreshold : 5 ,
SuccessThreshold : 10 ,
} ,
} ,
{
2021-10-23 22:47:12 +02:00
Name : "endpoint-alert-overwrites-default-alert" ,
2021-05-19 04:29:15 +02:00
DefaultAlert : & alert . Alert {
2021-05-16 03:31:32 +02:00
Enabled : & disabled ,
SendOnResolved : & disabled ,
Description : & firstDescription ,
FailureThreshold : 5 ,
SuccessThreshold : 10 ,
} ,
2021-10-23 22:47:12 +02:00
EndpointAlert : & alert . Alert {
2021-05-19 04:29:15 +02:00
Type : alert . TypeTelegram ,
2021-05-16 03:31:32 +02:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & secondDescription ,
FailureThreshold : 6 ,
SuccessThreshold : 11 ,
} ,
2021-05-19 04:29:15 +02:00
ExpectedOutputAlert : & alert . Alert {
Type : alert . TypeTelegram ,
2021-05-16 03:31:32 +02:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & secondDescription ,
FailureThreshold : 6 ,
SuccessThreshold : 11 ,
} ,
} ,
{
2021-10-23 22:47:12 +02:00
Name : "endpoint-alert-partially-overwrites-default-alert" ,
2021-05-19 04:29:15 +02:00
DefaultAlert : & alert . Alert {
2021-05-16 03:31:32 +02:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & firstDescription ,
FailureThreshold : 5 ,
SuccessThreshold : 10 ,
} ,
2021-10-23 22:47:12 +02:00
EndpointAlert : & alert . Alert {
2021-05-19 04:29:15 +02:00
Type : alert . TypeDiscord ,
2021-05-16 03:31:32 +02:00
Enabled : nil ,
SendOnResolved : nil ,
FailureThreshold : 6 ,
SuccessThreshold : 11 ,
} ,
2021-05-19 04:29:15 +02:00
ExpectedOutputAlert : & alert . Alert {
Type : alert . TypeDiscord ,
2021-05-16 03:31:32 +02:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & firstDescription ,
FailureThreshold : 6 ,
SuccessThreshold : 11 ,
} ,
} ,
{
Name : "default-alert-type-should-be-ignored" ,
2021-05-19 04:29:15 +02:00
DefaultAlert : & alert . Alert {
Type : alert . TypeTelegram ,
2021-05-16 03:31:32 +02:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & firstDescription ,
FailureThreshold : 5 ,
SuccessThreshold : 10 ,
} ,
2021-10-23 22:47:12 +02:00
EndpointAlert : & alert . Alert {
2021-05-19 04:29:15 +02:00
Type : alert . TypeDiscord ,
2021-05-16 03:31:32 +02:00
} ,
2021-05-19 04:29:15 +02:00
ExpectedOutputAlert : & alert . Alert {
Type : alert . TypeDiscord ,
2021-05-16 03:31:32 +02:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & firstDescription ,
FailureThreshold : 5 ,
SuccessThreshold : 10 ,
} ,
} ,
{
Name : "no-default-alert" ,
2021-05-19 04:29:15 +02:00
DefaultAlert : & alert . Alert {
Type : alert . TypeDiscord ,
2021-05-16 03:31:32 +02:00
Enabled : nil ,
SendOnResolved : nil ,
Description : & firstDescription ,
FailureThreshold : 2 ,
SuccessThreshold : 5 ,
} ,
2021-10-23 22:47:12 +02:00
EndpointAlert : nil ,
2021-05-16 03:31:32 +02:00
ExpectedOutputAlert : nil ,
} ,
}
for _ , scenario := range scenarios {
t . Run ( scenario . Name , func ( t * testing . T ) {
2021-10-23 22:47:12 +02:00
ParseWithDefaultAlert ( scenario . DefaultAlert , scenario . EndpointAlert )
2021-05-16 03:31:32 +02:00
if scenario . ExpectedOutputAlert == nil {
2021-10-23 22:47:12 +02:00
if scenario . EndpointAlert != nil {
2021-05-16 03:31:32 +02:00
t . Fail ( )
}
return
}
2021-10-23 22:47:12 +02:00
if scenario . EndpointAlert . IsEnabled ( ) != scenario . ExpectedOutputAlert . IsEnabled ( ) {
t . Errorf ( "expected EndpointAlert.IsEnabled() to be %v, got %v" , scenario . ExpectedOutputAlert . IsEnabled ( ) , scenario . EndpointAlert . IsEnabled ( ) )
2021-05-16 03:31:32 +02:00
}
2021-10-23 22:47:12 +02:00
if scenario . EndpointAlert . IsSendingOnResolved ( ) != scenario . ExpectedOutputAlert . IsSendingOnResolved ( ) {
t . Errorf ( "expected EndpointAlert.IsSendingOnResolved() to be %v, got %v" , scenario . ExpectedOutputAlert . IsSendingOnResolved ( ) , scenario . EndpointAlert . IsSendingOnResolved ( ) )
2021-05-16 03:31:32 +02:00
}
2021-10-23 22:47:12 +02:00
if scenario . EndpointAlert . GetDescription ( ) != scenario . ExpectedOutputAlert . GetDescription ( ) {
t . Errorf ( "expected EndpointAlert.GetDescription() to be %v, got %v" , scenario . ExpectedOutputAlert . GetDescription ( ) , scenario . EndpointAlert . GetDescription ( ) )
2021-05-16 03:31:32 +02:00
}
2021-10-23 22:47:12 +02:00
if scenario . EndpointAlert . FailureThreshold != scenario . ExpectedOutputAlert . FailureThreshold {
t . Errorf ( "expected EndpointAlert.FailureThreshold to be %v, got %v" , scenario . ExpectedOutputAlert . FailureThreshold , scenario . EndpointAlert . FailureThreshold )
2021-05-16 03:31:32 +02:00
}
2021-10-23 22:47:12 +02:00
if scenario . EndpointAlert . SuccessThreshold != scenario . ExpectedOutputAlert . SuccessThreshold {
t . Errorf ( "expected EndpointAlert.SuccessThreshold to be %v, got %v" , scenario . ExpectedOutputAlert . SuccessThreshold , scenario . EndpointAlert . SuccessThreshold )
2021-05-16 03:31:32 +02:00
}
} )
}
}