Improve test coverage

This commit is contained in:
TwinProduction 2021-05-15 22:09:58 -04:00
parent 77de4c4742
commit 758428b312
3 changed files with 39 additions and 25 deletions

View File

@ -16,27 +16,6 @@ type AlertProvider struct {
DefaultAlert *core.Alert `yaml:"default-alert"` DefaultAlert *core.Alert `yaml:"default-alert"`
} }
//func (provider *AlertProvider) ParseWithDefaultAlert(alert *core.Alert) {
// if provider.DefaultAlert == nil {
// return
// }
// if alert.Enabled == nil {
// alert.Enabled = provider.DefaultAlert.Enabled
// }
// if alert.SendOnResolved == nil {
// alert.SendOnResolved = provider.DefaultAlert.SendOnResolved
// }
// if len(alert.Description) == 0 {
// alert.Description = provider.DefaultAlert.Description
// }
// if alert.FailureThreshold == 0 {
// alert.FailureThreshold = provider.DefaultAlert.FailureThreshold
// }
// if alert.SuccessThreshold == 0 {
// alert.SuccessThreshold = provider.DefaultAlert.SuccessThreshold
// }
//}
// IsValid returns whether the provider's configuration is valid // IsValid returns whether the provider's configuration is valid
func (provider *AlertProvider) IsValid() bool { func (provider *AlertProvider) IsValid() bool {
return len(provider.WebhookURL) > 0 return len(provider.WebhookURL) > 0

View File

@ -47,7 +47,7 @@ func TestParseWithDefaultAlert(t *testing.T) {
SuccessThreshold: 10, SuccessThreshold: 10,
}, },
ServiceAlert: &core.Alert{ ServiceAlert: &core.Alert{
Type: core.DiscordAlert, Type: core.TelegramAlert,
Enabled: &enabled, Enabled: &enabled,
SendOnResolved: &enabled, SendOnResolved: &enabled,
Description: &secondDescription, Description: &secondDescription,
@ -55,7 +55,7 @@ func TestParseWithDefaultAlert(t *testing.T) {
SuccessThreshold: 11, SuccessThreshold: 11,
}, },
ExpectedOutputAlert: &core.Alert{ ExpectedOutputAlert: &core.Alert{
Type: core.DiscordAlert, Type: core.TelegramAlert,
Enabled: &enabled, Enabled: &enabled,
SendOnResolved: &enabled, SendOnResolved: &enabled,
Description: &secondDescription, Description: &secondDescription,

View File

@ -507,6 +507,8 @@ services:
func TestParseAndValidateConfigBytesWithAlertingAndDefaultAlert(t *testing.T) { func TestParseAndValidateConfigBytesWithAlertingAndDefaultAlert(t *testing.T) {
config, err := parseAndValidateConfigBytes([]byte(` config, err := parseAndValidateConfigBytes([]byte(`
debug: true
alerting: alerting:
slack: slack:
webhook-url: "http://example.com" webhook-url: "http://example.com"
@ -582,19 +584,36 @@ services:
if config.Alerting.Slack == nil || !config.Alerting.Slack.IsValid() { if config.Alerting.Slack == nil || !config.Alerting.Slack.IsValid() {
t.Fatal("Slack alerting config should've been valid") t.Fatal("Slack alerting config should've been valid")
} }
if config.Alerting.Slack.GetDefaultAlert() == nil {
t.Fatal("Slack.GetDefaultAlert() shouldn't have returned nil")
}
if config.Alerting.Slack.WebhookURL != "http://example.com" { if config.Alerting.Slack.WebhookURL != "http://example.com" {
t.Errorf("Slack webhook should've been %s, but was %s", "http://example.com", config.Alerting.Slack.WebhookURL) t.Errorf("Slack webhook should've been %s, but was %s", "http://example.com", config.Alerting.Slack.WebhookURL)
} }
if config.Alerting.PagerDuty == nil || !config.Alerting.PagerDuty.IsValid() { if config.Alerting.PagerDuty == nil || !config.Alerting.PagerDuty.IsValid() {
t.Fatal("PagerDuty alerting config should've been valid") t.Fatal("PagerDuty alerting config should've been valid")
} }
if config.Alerting.PagerDuty.GetDefaultAlert() == nil {
t.Fatal("PagerDuty.GetDefaultAlert() shouldn't have returned nil")
}
if config.Alerting.PagerDuty.IntegrationKey != "00000000000000000000000000000000" { if config.Alerting.PagerDuty.IntegrationKey != "00000000000000000000000000000000" {
t.Errorf("PagerDuty integration key should've been %s, but was %s", "00000000000000000000000000000000", config.Alerting.PagerDuty.IntegrationKey) t.Errorf("PagerDuty integration key should've been %s, but was %s", "00000000000000000000000000000000", config.Alerting.PagerDuty.IntegrationKey)
} }
if config.Alerting.Mattermost == nil || !config.Alerting.Mattermost.IsValid() {
t.Fatal("Mattermost alerting config should've been valid")
}
if config.Alerting.Mattermost.GetDefaultAlert() == nil {
t.Fatal("Mattermost.GetDefaultAlert() shouldn't have returned nil")
}
if config.Alerting.Messagebird == nil || !config.Alerting.Messagebird.IsValid() { if config.Alerting.Messagebird == nil || !config.Alerting.Messagebird.IsValid() {
t.Fatal("Messagebird alerting config should've been valid") t.Fatal("Messagebird alerting config should've been valid")
} }
if config.Alerting.Messagebird.GetDefaultAlert() == nil {
t.Fatal("Messagebird.GetDefaultAlert() shouldn't have returned nil")
}
if config.Alerting.Messagebird.AccessKey != "1" { if config.Alerting.Messagebird.AccessKey != "1" {
t.Errorf("Messagebird access key should've been %s, but was %s", "1", config.Alerting.Messagebird.AccessKey) t.Errorf("Messagebird access key should've been %s, but was %s", "1", config.Alerting.Messagebird.AccessKey)
} }
@ -608,18 +627,34 @@ services:
if config.Alerting.Discord == nil || !config.Alerting.Discord.IsValid() { if config.Alerting.Discord == nil || !config.Alerting.Discord.IsValid() {
t.Fatal("Discord alerting config should've been valid") t.Fatal("Discord alerting config should've been valid")
} }
if config.Alerting.Discord.GetDefaultAlert() == nil {
t.Fatal("Discord.GetDefaultAlert() shouldn't have returned nil")
}
if config.Alerting.Discord.WebhookURL != "http://example.org" { if config.Alerting.Discord.WebhookURL != "http://example.org" {
t.Errorf("Discord webhook should've been %s, but was %s", "http://example.org", config.Alerting.Discord.WebhookURL) t.Errorf("Discord webhook should've been %s, but was %s", "http://example.org", config.Alerting.Discord.WebhookURL)
} }
if GetAlertingProviderByAlertType(config, core.DiscordAlert) != config.Alerting.Discord {
t.Error("expected discord configuration")
}
if config.Alerting.Telegram == nil || !config.Alerting.Telegram.IsValid() {
t.Fatal("Telegram alerting config should've been valid")
}
if config.Alerting.Telegram.GetDefaultAlert() == nil {
t.Fatal("Telegram.GetDefaultAlert() shouldn't have returned nil")
}
if config.Alerting.Telegram.Token != "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11" { if config.Alerting.Telegram.Token != "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11" {
t.Errorf("Telegram token should've been %s, but was %s", "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11", config.Alerting.Telegram.Token) t.Errorf("Telegram token should've been %s, but was %s", "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11", config.Alerting.Telegram.Token)
} }
if config.Alerting.Telegram.ID != "0123456789" { if config.Alerting.Telegram.ID != "0123456789" {
t.Errorf("Telegram ID should've been %s, but was %s", "012345689", config.Alerting.Telegram.ID) t.Errorf("Telegram ID should've been %s, but was %s", "012345689", config.Alerting.Telegram.ID)
} }
if GetAlertingProviderByAlertType(config, core.DiscordAlert) != config.Alerting.Discord {
t.Error("expected discord configuration") if config.Alerting.Twilio == nil || !config.Alerting.Twilio.IsValid() {
t.Fatal("Twilio alerting config should've been valid")
}
if config.Alerting.Twilio.GetDefaultAlert() == nil {
t.Fatal("Twilio.GetDefaultAlert() shouldn't have returned nil")
} }
// Services // Services
if len(config.Services) != 1 { if len(config.Services) != 1 {