2021-05-15 21:31:32 -04:00
package provider
import (
"testing"
2025-08-25 20:31:23 -07:00
"time"
2021-05-15 21:31:32 -04:00
2022-12-06 01:41:09 -05:00
"github.com/TwiN/gatus/v5/alerting/alert"
2021-05-15 21:31:32 -04:00
)
func TestParseWithDefaultAlert ( t * testing . T ) {
type Scenario struct {
2021-10-23 16:47:12 -04:00
Name string
DefaultAlert , EndpointAlert , ExpectedOutputAlert * alert . Alert
2021-05-15 21:31:32 -04:00
}
enabled := true
disabled := false
firstDescription := "description-1"
secondDescription := "description-2"
scenarios := [] Scenario {
{
2021-10-23 16:47:12 -04:00
Name : "endpoint-alert-type-only" ,
2021-05-18 22:29:15 -04:00
DefaultAlert : & alert . Alert {
2021-05-15 21:31:32 -04:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & firstDescription ,
FailureThreshold : 5 ,
SuccessThreshold : 10 ,
2025-08-25 20:31:23 -07:00
MinimumReminderInterval : 30 * time . Second ,
2021-05-15 21:31:32 -04:00
},
2021-10-23 16:47:12 -04:00
EndpointAlert : & alert . Alert {
2021-05-18 22:29:15 -04:00
Type : alert . TypeDiscord ,
2021-05-15 21:31:32 -04:00
},
2021-05-18 22:29:15 -04:00
ExpectedOutputAlert : & alert . Alert {
Type : alert . TypeDiscord ,
2021-05-15 21:31:32 -04:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & firstDescription ,
FailureThreshold : 5 ,
SuccessThreshold : 10 ,
2025-08-25 20:31:23 -07:00
MinimumReminderInterval : 30 * time . Second ,
2021-05-15 21:31:32 -04:00
},
},
{
2021-10-23 16:47:12 -04:00
Name : "endpoint-alert-overwrites-default-alert" ,
2021-05-18 22:29:15 -04:00
DefaultAlert : & alert . Alert {
2021-05-15 21:31:32 -04:00
Enabled : & disabled ,
SendOnResolved : & disabled ,
Description : & firstDescription ,
FailureThreshold : 5 ,
SuccessThreshold : 10 ,
},
2021-10-23 16:47:12 -04:00
EndpointAlert : & alert . Alert {
2021-05-18 22:29:15 -04:00
Type : alert . TypeTelegram ,
2021-05-15 21:31:32 -04:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & secondDescription ,
FailureThreshold : 6 ,
SuccessThreshold : 11 ,
},
2021-05-18 22:29:15 -04:00
ExpectedOutputAlert : & alert . Alert {
Type : alert . TypeTelegram ,
2021-05-15 21:31:32 -04:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & secondDescription ,
FailureThreshold : 6 ,
SuccessThreshold : 11 ,
},
},
{
2021-10-23 16:47:12 -04:00
Name : "endpoint-alert-partially-overwrites-default-alert" ,
2021-05-18 22:29:15 -04:00
DefaultAlert : & alert . Alert {
2021-05-15 21:31:32 -04:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & firstDescription ,
FailureThreshold : 5 ,
SuccessThreshold : 10 ,
},
2021-10-23 16:47:12 -04:00
EndpointAlert : & alert . Alert {
2021-05-18 22:29:15 -04:00
Type : alert . TypeDiscord ,
2021-05-15 21:31:32 -04:00
Enabled : nil ,
SendOnResolved : nil ,
FailureThreshold : 6 ,
SuccessThreshold : 11 ,
},
2021-05-18 22:29:15 -04:00
ExpectedOutputAlert : & alert . Alert {
Type : alert . TypeDiscord ,
2021-05-15 21:31:32 -04:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & firstDescription ,
FailureThreshold : 6 ,
SuccessThreshold : 11 ,
},
},
{
Name : "default-alert-type-should-be-ignored" ,
2021-05-18 22:29:15 -04:00
DefaultAlert : & alert . Alert {
Type : alert . TypeTelegram ,
2021-05-15 21:31:32 -04:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & firstDescription ,
FailureThreshold : 5 ,
SuccessThreshold : 10 ,
},
2021-10-23 16:47:12 -04:00
EndpointAlert : & alert . Alert {
2021-05-18 22:29:15 -04:00
Type : alert . TypeDiscord ,
2021-05-15 21:31:32 -04:00
},
2021-05-18 22:29:15 -04:00
ExpectedOutputAlert : & alert . Alert {
Type : alert . TypeDiscord ,
2021-05-15 21:31:32 -04:00
Enabled : & enabled ,
SendOnResolved : & enabled ,
Description : & firstDescription ,
FailureThreshold : 5 ,
SuccessThreshold : 10 ,
},
},
{
Name : "no-default-alert" ,
2021-05-18 22:29:15 -04:00
DefaultAlert : & alert . Alert {
Type : alert . TypeDiscord ,
2021-05-15 21:31:32 -04:00
Enabled : nil ,
SendOnResolved : nil ,
Description : & firstDescription ,
FailureThreshold : 2 ,
SuccessThreshold : 5 ,
},
2021-10-23 16:47:12 -04:00
EndpointAlert : nil ,
2021-05-15 21:31:32 -04:00
ExpectedOutputAlert : nil ,
},
}
for _ , scenario := range scenarios {
t . Run ( scenario . Name , func ( t * testing . T ) {
2024-12-16 20:32:13 -05:00
MergeProviderDefaultAlertIntoEndpointAlert ( scenario . DefaultAlert , scenario . EndpointAlert )
2021-05-15 21:31:32 -04:00
if scenario . ExpectedOutputAlert == nil {
2021-10-23 16:47:12 -04:00
if scenario . EndpointAlert != nil {
2021-05-15 21:31:32 -04:00
t . Fail ()
}
return
}
2021-10-23 16:47:12 -04: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-15 21:31:32 -04:00
}
2021-10-23 16:47:12 -04: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-15 21:31:32 -04:00
}
2021-10-23 16:47:12 -04: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-15 21:31:32 -04:00
}
2021-10-23 16:47:12 -04: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-15 21:31:32 -04:00
}
2021-10-23 16:47:12 -04: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-15 21:31:32 -04:00
}
2025-08-25 20:31:23 -07:00
if int ( scenario . EndpointAlert . MinimumReminderInterval ) != int ( scenario . ExpectedOutputAlert . MinimumReminderInterval ) {
t . Errorf ( "expected EndpointAlert.MinimumReminderInterval to be %v, got %v" , scenario . ExpectedOutputAlert . MinimumReminderInterval , scenario . EndpointAlert . MinimumReminderInterval )
}
2021-05-15 21:31:32 -04:00
})
}
}