Improve testing coverage

This commit is contained in:
TwinProduction 2020-10-21 21:56:07 -04:00
parent dca517e077
commit a32d98ab96
2 changed files with 48 additions and 8 deletions

View File

@ -36,6 +36,12 @@ services:
if config.Services[1].Url != "https://api.github.com/healthz" { if config.Services[1].Url != "https://api.github.com/healthz" {
t.Errorf("URL should have been %s", "https://api.github.com/healthz") t.Errorf("URL should have been %s", "https://api.github.com/healthz")
} }
if config.Services[0].Method != "GET" {
t.Errorf("Method should have been %s (default)", "GET")
}
if config.Services[1].Method != "GET" {
t.Errorf("Method should have been %s (default)", "GET")
}
if config.Services[0].Interval != 15*time.Second { if config.Services[0].Interval != 15*time.Second {
t.Errorf("Interval should have been %s", 15*time.Second) t.Errorf("Interval should have been %s", 15*time.Second)
} }
@ -130,7 +136,10 @@ services:
alerts: alerts:
- type: slack - type: slack
enabled: true enabled: true
- type: pagerduty
enabled: true
failure-threshold: 7 failure-threshold: 7
success-threshold: 5
description: "Healthcheck failed 7 times in a row" description: "Healthcheck failed 7 times in a row"
conditions: conditions:
- "[STATUS] == 200" - "[STATUS] == 200"
@ -171,23 +180,32 @@ services:
if config.Services[0].Alerts == nil { if config.Services[0].Alerts == nil {
t.Fatal("The service alerts shouldn't have been nil") t.Fatal("The service alerts shouldn't have been nil")
} }
if len(config.Services[0].Alerts) != 1 { if len(config.Services[0].Alerts) != 2 {
t.Fatal("There should've been 1 alert configured") t.Fatal("There should've been 2 alert configured")
} }
if !config.Services[0].Alerts[0].Enabled { if !config.Services[0].Alerts[0].Enabled {
t.Error("The alert should've been enabled") t.Error("The alert should've been enabled")
} }
if config.Services[0].Alerts[0].FailureThreshold != 7 { if config.Services[0].Alerts[0].FailureThreshold != 3 {
t.Errorf("The failure threshold of the alert should've been %d, but it was %d", 7, config.Services[0].Alerts[0].FailureThreshold) t.Errorf("The default failure threshold of the alert should've been %d, but it was %d", 3, config.Services[0].Alerts[0].FailureThreshold)
} }
if config.Services[0].Alerts[0].FailureThreshold != 7 { if config.Services[0].Alerts[0].SuccessThreshold != 2 {
t.Errorf("The success threshold of the alert should've been %d, but it was %d", 2, config.Services[0].Alerts[0].SuccessThreshold) t.Errorf("The default success threshold of the alert should've been %d, but it was %d", 2, config.Services[0].Alerts[0].SuccessThreshold)
}
if config.Services[0].Alerts[1].FailureThreshold != 7 {
t.Errorf("The failure threshold of the alert should've been %d, but it was %d", 7, config.Services[0].Alerts[1].FailureThreshold)
}
if config.Services[0].Alerts[1].SuccessThreshold != 5 {
t.Errorf("The success threshold of the alert should've been %d, but it was %d", 5, config.Services[0].Alerts[1].SuccessThreshold)
} }
if config.Services[0].Alerts[0].Type != core.SlackAlert { if config.Services[0].Alerts[0].Type != core.SlackAlert {
t.Errorf("The type of the alert should've been %s, but it was %s", core.SlackAlert, config.Services[0].Alerts[0].Type) t.Errorf("The type of the alert should've been %s, but it was %s", core.SlackAlert, config.Services[0].Alerts[0].Type)
} }
if config.Services[0].Alerts[0].Description != "Healthcheck failed 7 times in a row" { if config.Services[0].Alerts[1].Type != core.PagerDutyAlert {
t.Errorf("The type of the alert should've been %s, but it was %s", "Healthcheck failed 7 times in a row", config.Services[0].Alerts[0].Description) t.Errorf("The type of the alert should've been %s, but it was %s", core.PagerDutyAlert, config.Services[0].Alerts[1].Type)
}
if config.Services[0].Alerts[1].Description != "Healthcheck failed 7 times in a row" {
t.Errorf("The description of the alert should've been %s, but it was %s", "Healthcheck failed 7 times in a row", config.Services[0].Alerts[0].Description)
} }
} }
@ -199,6 +217,8 @@ alerting:
services: services:
- name: twinnation - name: twinnation
url: https://twinnation.org/actuator/health url: https://twinnation.org/actuator/health
alerts:
- type: pagerduty
conditions: conditions:
- "[STATUS] == 200" - "[STATUS] == 200"
`)) `))

View File

@ -2,6 +2,7 @@ package core
import ( import (
"testing" "testing"
"time"
) )
func TestIntegrationEvaluateHealth(t *testing.T) { func TestIntegrationEvaluateHealth(t *testing.T) {
@ -41,3 +42,22 @@ func TestIntegrationEvaluateHealthWithFailure(t *testing.T) {
t.Error("Because one of the conditions failed, success should have been false") t.Error("Because one of the conditions failed, success should have been false")
} }
} }
func TestService_ValidateAndSetDefaults(t *testing.T) {
condition := Condition("[STATUS] == 200")
service := Service{
Name: "TwiNNatioN",
Url: "https://twinnation.org/health",
Conditions: []*Condition{&condition},
}
service.ValidateAndSetDefaults()
if service.Method != "GET" {
t.Error("Service method should've defaulted to GET")
}
if service.Interval != time.Minute {
t.Error("Service interval should've defaulted to 1 minute")
}
if service.Headers == nil {
t.Error("Service headers should've defaulted to an empty map")
}
}