2021-05-19 04:29:15 +02:00
|
|
|
package alert
|
2021-05-16 03:31:32 +02:00
|
|
|
|
2021-12-12 22:58:48 +01:00
|
|
|
import (
|
2024-04-09 03:00:40 +02:00
|
|
|
"errors"
|
2021-12-12 22:58:48 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAlert_ValidateAndSetDefaults(t *testing.T) {
|
|
|
|
invalidDescription := "\""
|
|
|
|
scenarios := []struct {
|
|
|
|
name string
|
|
|
|
alert Alert
|
|
|
|
expectedError error
|
|
|
|
expectedSuccessThreshold int
|
|
|
|
expectedFailureThreshold int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid-empty",
|
|
|
|
alert: Alert{
|
|
|
|
Description: nil,
|
|
|
|
FailureThreshold: 0,
|
|
|
|
SuccessThreshold: 0,
|
|
|
|
},
|
|
|
|
expectedError: nil,
|
|
|
|
expectedFailureThreshold: 3,
|
|
|
|
expectedSuccessThreshold: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid-description",
|
|
|
|
alert: Alert{
|
|
|
|
Description: &invalidDescription,
|
|
|
|
FailureThreshold: 10,
|
|
|
|
SuccessThreshold: 5,
|
|
|
|
},
|
|
|
|
expectedError: ErrAlertWithInvalidDescription,
|
|
|
|
expectedFailureThreshold: 10,
|
|
|
|
expectedSuccessThreshold: 5,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
|
|
|
t.Run(scenario.name, func(t *testing.T) {
|
2024-04-09 03:00:40 +02:00
|
|
|
if err := scenario.alert.ValidateAndSetDefaults(); !errors.Is(err, scenario.expectedError) {
|
2021-12-12 22:58:48 +01:00
|
|
|
t.Errorf("expected error %v, got %v", scenario.expectedError, err)
|
|
|
|
}
|
|
|
|
if scenario.alert.SuccessThreshold != scenario.expectedSuccessThreshold {
|
|
|
|
t.Errorf("expected success threshold %v, got %v", scenario.expectedSuccessThreshold, scenario.alert.SuccessThreshold)
|
|
|
|
}
|
|
|
|
if scenario.alert.FailureThreshold != scenario.expectedFailureThreshold {
|
|
|
|
t.Errorf("expected failure threshold %v, got %v", scenario.expectedFailureThreshold, scenario.alert.FailureThreshold)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-05-16 03:31:32 +02:00
|
|
|
|
|
|
|
func TestAlert_IsEnabled(t *testing.T) {
|
2024-04-09 03:00:40 +02:00
|
|
|
if !(&Alert{Enabled: nil}).IsEnabled() {
|
2022-12-06 05:15:19 +01:00
|
|
|
t.Error("alert.IsEnabled() should've returned true, because Enabled was set to nil")
|
2021-05-16 03:31:32 +02:00
|
|
|
}
|
2024-04-09 03:00:40 +02:00
|
|
|
if value := false; (&Alert{Enabled: &value}).IsEnabled() {
|
2021-05-16 03:31:32 +02:00
|
|
|
t.Error("alert.IsEnabled() should've returned false, because Enabled was set to false")
|
|
|
|
}
|
2024-04-09 03:00:40 +02:00
|
|
|
if value := true; !(&Alert{Enabled: &value}).IsEnabled() {
|
2021-05-16 03:31:32 +02:00
|
|
|
t.Error("alert.IsEnabled() should've returned true, because Enabled was set to true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlert_GetDescription(t *testing.T) {
|
2024-04-09 03:00:40 +02:00
|
|
|
if (&Alert{Description: nil}).GetDescription() != "" {
|
2021-05-16 03:31:32 +02:00
|
|
|
t.Error("alert.GetDescription() should've returned an empty string, because Description was set to nil")
|
|
|
|
}
|
2024-04-09 03:00:40 +02:00
|
|
|
if value := "description"; (&Alert{Description: &value}).GetDescription() != value {
|
2021-05-16 03:31:32 +02:00
|
|
|
t.Error("alert.GetDescription() should've returned false, because Description was set to 'description'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlert_IsSendingOnResolved(t *testing.T) {
|
2024-04-09 03:00:40 +02:00
|
|
|
if (&Alert{SendOnResolved: nil}).IsSendingOnResolved() {
|
2021-05-16 03:31:32 +02:00
|
|
|
t.Error("alert.IsSendingOnResolved() should've returned false, because SendOnResolved was set to nil")
|
|
|
|
}
|
2024-04-09 03:00:40 +02:00
|
|
|
if value := false; (&Alert{SendOnResolved: &value}).IsSendingOnResolved() {
|
2021-05-16 03:31:32 +02:00
|
|
|
t.Error("alert.IsSendingOnResolved() should've returned false, because SendOnResolved was set to false")
|
|
|
|
}
|
2024-04-09 03:00:40 +02:00
|
|
|
if value := true; !(&Alert{SendOnResolved: &value}).IsSendingOnResolved() {
|
2021-05-16 03:31:32 +02:00
|
|
|
t.Error("alert.IsSendingOnResolved() should've returned true, because SendOnResolved was set to true")
|
|
|
|
}
|
|
|
|
}
|