Add tests for alert configuration

This commit is contained in:
TwinProduction 2020-08-22 14:15:21 -04:00
parent 9a3c9e4d61
commit 22fef4e9aa

View File

@ -2,6 +2,7 @@ package config
import ( import (
"fmt" "fmt"
"github.com/TwinProduction/gatus/core"
"testing" "testing"
"time" "time"
) )
@ -23,6 +24,9 @@ services:
if err != nil { if err != nil {
t.Error("No error should've been returned") t.Error("No error should've been returned")
} }
if config == nil {
t.Fatal("Config shouldn't have been nil")
}
if len(config.Services) != 2 { if len(config.Services) != 2 {
t.Error("Should have returned two services") t.Error("Should have returned two services")
} }
@ -58,6 +62,9 @@ services:
if err != nil { if err != nil {
t.Error("No error should've been returned") t.Error("No error should've been returned")
} }
if config == nil {
t.Fatal("Config shouldn't have been nil")
}
if config.Metrics { if config.Metrics {
t.Error("Metrics should've been false by default") t.Error("Metrics should've been false by default")
} }
@ -81,6 +88,9 @@ services:
if err != nil { if err != nil {
t.Error("No error should've been returned") t.Error("No error should've been returned")
} }
if config == nil {
t.Fatal("Config shouldn't have been nil")
}
if !config.Metrics { if !config.Metrics {
t.Error("Metrics should have been true") t.Error("Metrics should have been true")
} }
@ -107,3 +117,62 @@ badconfig:
t.Error("The error returned should have been of type ErrNoServiceInConfig") t.Error("The error returned should have been of type ErrNoServiceInConfig")
} }
} }
func TestParseAndValidateConfigBytesWithAlerting(t *testing.T) {
config, err := parseAndValidateConfigBytes([]byte(`
alerting:
slack: "http://example.com"
services:
- name: twinnation
url: https://twinnation.org/actuator/health
alerts:
- type: slack
enabled: true
threshold: 7
description: "Healthcheck failed 7 times in a row"
conditions:
- "[STATUS] == 200"
`))
if err != nil {
t.Error("No error should've been returned")
}
if config == nil {
t.Fatal("Config shouldn't have been nil")
}
if config.Metrics {
t.Error("Metrics should've been false by default")
}
if config.Alerting == nil {
t.Fatal("config.Alerting shouldn't have been nil")
}
if config.Alerting.Slack != "http://example.com" {
t.Errorf("Slack webhook should've been %s, but was %s", "http://example.com", config.Alerting.Slack)
}
if len(config.Services) != 1 {
t.Error("There should've been 1 service")
}
if config.Services[0].Url != "https://twinnation.org/actuator/health" {
t.Errorf("URL should have been %s", "https://twinnation.org/actuator/health")
}
if config.Services[0].Interval != 10*time.Second {
t.Errorf("Interval should have been %s, because it is the default value", 10*time.Second)
}
if config.Services[0].Alerts == nil {
t.Fatal("The service alerts shouldn't have been nil")
}
if len(config.Services[0].Alerts) != 1 {
t.Fatal("There should've been 1 alert configured")
}
if !config.Services[0].Alerts[0].Enabled {
t.Error("The alert should've been enabled")
}
if config.Services[0].Alerts[0].Threshold != 7 {
t.Errorf("The threshold of the alert should've been %d, but it was %d", 7, config.Services[0].Alerts[0].Threshold)
}
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)
}
if config.Services[0].Alerts[0].Description != "Healthcheck failed 7 times in a row" {
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)
}
}