From 947a28f45d2ef742d2424755a78d2386c21d181b Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Wed, 21 Oct 2020 22:56:35 -0400 Subject: [PATCH] Improve test coverage --- alerting/provider/slack/slack_test.go | 4 +-- config/config_test.go | 34 +++++++++++++++----------- core/service.go | 23 ++++++++++++++---- core/service_test.go | 35 +++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 21 deletions(-) diff --git a/alerting/provider/slack/slack_test.go b/alerting/provider/slack/slack_test.go index 0dc4785a..6ba8b3db 100644 --- a/alerting/provider/slack/slack_test.go +++ b/alerting/provider/slack/slack_test.go @@ -19,7 +19,7 @@ func TestAlertProvider_IsValid(t *testing.T) { func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) { provider := AlertProvider{WebhookUrl: "http://example.com"} - customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true) + customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "SUCCESSFUL_CONDITION", Success: true}}}, true) if customAlertProvider == nil { t.Error("customAlertProvider shouldn't have been nil") } @@ -30,7 +30,7 @@ func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) { func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) { provider := AlertProvider{WebhookUrl: "http://example.com"} - customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, false) + customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "UNSUCCESSFUL_CONDITION", Success: false}}}, false) if customAlertProvider == nil { t.Error("customAlertProvider shouldn't have been nil") } diff --git a/config/config_test.go b/config/config_test.go index 2098ae48..54c2fa89 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -7,6 +7,26 @@ import ( "time" ) +func TestGetBeforeConfigIsLoaded(t *testing.T) { + defer func() { recover() }() + Get() + t.Fatal("Should've panicked because the configuration hasn't been loaded yet") +} + +func TestLoadFileThatDoesNotExist(t *testing.T) { + err := Load("file-that-does-not-exist.yaml") + if err == nil { + t.Error("Should've returned an error, because the file specified doesn't exist") + } +} + +func TestLoadDefaultConfigurationFile(t *testing.T) { + err := LoadDefaultConfiguration() + if err == nil { + t.Error("Should've returned an error, because there's no configuration files at the default path nor the default fallback path") + } +} + func TestParseAndValidateConfigBytes(t *testing.T) { config, err := parseAndValidateConfigBytes([]byte(` services: @@ -291,17 +311,3 @@ services: t.Errorf("config.Security.Basic.PasswordSha512Hash should've been %s, but was %s", expectedPasswordHash, config.Security.Basic.PasswordSha512Hash) } } - -func TestLoadFileThatDoesNotExist(t *testing.T) { - err := Load("file-that-does-not-exist.yaml") - if err == nil { - t.Error("Should've returned an error, because the file specified doesn't exist") - } -} - -func TestLoadDefaultConfigurationFile(t *testing.T) { - err := LoadDefaultConfiguration() - if err == nil { - t.Error("Should've returned an error, because there's no configuration files at the default path nor the default fallback path") - } -} diff --git a/core/service.go b/core/service.go index 1e83ebd7..91af94cb 100644 --- a/core/service.go +++ b/core/service.go @@ -14,8 +14,14 @@ import ( ) var ( - ErrNoCondition = errors.New("you must specify at least one condition per service") - ErrNoUrl = errors.New("you must specify an url for each service") + // ErrServiceWithNoCondition is the error with which gatus will panic if a service is configured with no conditions + ErrServiceWithNoCondition = errors.New("you must specify at least one condition per service") + + // ErrServiceWithNoUrl is the error with which gatus will panic if a service is configured with no url + ErrServiceWithNoUrl = errors.New("you must specify an url for each service") + + // ErrServiceWithNoName is the error with which gatus will panic if a service is configured with no name + ErrServiceWithNoName = errors.New("you must specify a name for each service") ) // Service is the configuration of a monitored endpoint @@ -50,7 +56,10 @@ type Service struct { // Insecure is whether to skip verifying the server's certificate chain and host name Insecure bool `yaml:"insecure,omitempty"` - NumberOfFailuresInARow int + // NumberOfFailuresInARow is the number of unsuccessful evaluations in a row + NumberOfFailuresInARow int + + // NumberOfFailuresInARow is the number of successful evaluations in a row NumberOfSuccessesInARow int } @@ -74,11 +83,14 @@ func (service *Service) ValidateAndSetDefaults() { alert.SuccessThreshold = 2 } } + if len(service.Name) == 0 { + panic(ErrServiceWithNoName) + } if len(service.Url) == 0 { - panic(ErrNoUrl) + panic(ErrServiceWithNoUrl) } if len(service.Conditions) == 0 { - panic(ErrNoCondition) + panic(ErrServiceWithNoCondition) } // Make sure that the request can be created @@ -107,6 +119,7 @@ func (service *Service) EvaluateHealth() *Result { return result } +// GetAlertsTriggered returns a slice of alerts that have been triggered func (service *Service) GetAlertsTriggered() []Alert { var alerts []Alert if service.NumberOfFailuresInARow == 0 { diff --git a/core/service_test.go b/core/service_test.go index d3265a54..25a9b9f5 100644 --- a/core/service_test.go +++ b/core/service_test.go @@ -37,6 +37,41 @@ func TestService_ValidateAndSetDefaults(t *testing.T) { } } +func TestService_ValidateAndSetDefaultsWithNoName(t *testing.T) { + defer func() { recover() }() + condition := Condition("[STATUS] == 200") + service := &Service{ + Name: "", + Url: "http://example.com", + Conditions: []*Condition{&condition}, + } + service.ValidateAndSetDefaults() + t.Fatal("Should've panicked because service didn't have a name, which is a mandatory field") +} + +func TestService_ValidateAndSetDefaultsWithNoUrl(t *testing.T) { + defer func() { recover() }() + condition := Condition("[STATUS] == 200") + service := &Service{ + Name: "example", + Url: "", + Conditions: []*Condition{&condition}, + } + service.ValidateAndSetDefaults() + t.Fatal("Should've panicked because service didn't have an url, which is a mandatory field") +} + +func TestService_ValidateAndSetDefaultsWithNoConditions(t *testing.T) { + defer func() { recover() }() + service := &Service{ + Name: "example", + Url: "http://example.com", + Conditions: nil, + } + service.ValidateAndSetDefaults() + t.Fatal("Should've panicked because service didn't have at least 1 condition") +} + func TestService_GetAlertsTriggered(t *testing.T) { condition := Condition("[STATUS] == 200") service := Service{