Improve test coverage

This commit is contained in:
TwinProduction
2020-10-21 22:56:35 -04:00
parent 8c61044e7c
commit 947a28f45d
4 changed files with 75 additions and 21 deletions

View File

@ -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 {

View File

@ -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{