2019-09-06 06:02:00 +02:00
|
|
|
package config
|
|
|
|
|
2019-09-09 03:07:08 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2020-08-22 20:15:21 +02:00
|
|
|
"github.com/TwinProduction/gatus/core"
|
2019-09-09 03:07:08 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
2019-09-06 06:02:00 +02:00
|
|
|
|
2019-10-20 04:03:55 +02:00
|
|
|
func TestParseAndValidateConfigBytes(t *testing.T) {
|
|
|
|
config, err := parseAndValidateConfigBytes([]byte(`
|
2019-09-06 06:02:00 +02:00
|
|
|
services:
|
|
|
|
- name: twinnation
|
|
|
|
url: https://twinnation.org/actuator/health
|
2019-09-09 03:07:08 +02:00
|
|
|
interval: 15s
|
2019-09-06 06:02:00 +02:00
|
|
|
conditions:
|
2019-12-04 23:27:27 +01:00
|
|
|
- "[STATUS] == 200"
|
2019-09-06 06:02:00 +02:00
|
|
|
- name: github
|
2019-09-09 03:07:08 +02:00
|
|
|
url: https://api.github.com/healthz
|
2019-09-06 06:02:00 +02:00
|
|
|
conditions:
|
2019-12-04 23:27:27 +01:00
|
|
|
- "[STATUS] != 400"
|
|
|
|
- "[STATUS] != 500"
|
2019-09-06 06:02:00 +02:00
|
|
|
`))
|
2019-10-20 04:03:55 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error("No error should've been returned")
|
|
|
|
}
|
2020-08-22 20:15:21 +02:00
|
|
|
if config == nil {
|
|
|
|
t.Fatal("Config shouldn't have been nil")
|
|
|
|
}
|
2019-09-06 06:02:00 +02:00
|
|
|
if len(config.Services) != 2 {
|
|
|
|
t.Error("Should have returned two services")
|
|
|
|
}
|
|
|
|
if config.Services[0].Url != "https://twinnation.org/actuator/health" {
|
|
|
|
t.Errorf("URL should have been %s", "https://twinnation.org/actuator/health")
|
|
|
|
}
|
2019-09-09 03:07:08 +02:00
|
|
|
if config.Services[1].Url != "https://api.github.com/healthz" {
|
|
|
|
t.Errorf("URL should have been %s", "https://api.github.com/healthz")
|
2019-09-06 06:02:00 +02:00
|
|
|
}
|
2019-09-09 03:07:08 +02:00
|
|
|
fmt.Println(config.Services[0].Interval)
|
|
|
|
if config.Services[0].Interval != 15*time.Second {
|
|
|
|
t.Errorf("Interval should have been %s", 15*time.Second)
|
2019-09-06 06:02:00 +02:00
|
|
|
}
|
2020-09-01 18:46:23 +02:00
|
|
|
if config.Services[1].Interval != 60*time.Second {
|
|
|
|
t.Errorf("Interval should have been %s, because it is the default value", 60*time.Second)
|
2019-09-06 06:02:00 +02:00
|
|
|
}
|
|
|
|
if len(config.Services[0].Conditions) != 1 {
|
|
|
|
t.Errorf("There should have been %d conditions", 1)
|
|
|
|
}
|
|
|
|
if len(config.Services[1].Conditions) != 2 {
|
|
|
|
t.Errorf("There should have been %d conditions", 2)
|
|
|
|
}
|
|
|
|
}
|
2019-09-10 04:21:18 +02:00
|
|
|
|
2019-10-20 04:03:55 +02:00
|
|
|
func TestParseAndValidateConfigBytesDefault(t *testing.T) {
|
|
|
|
config, err := parseAndValidateConfigBytes([]byte(`
|
2019-09-10 04:21:18 +02:00
|
|
|
services:
|
|
|
|
- name: twinnation
|
|
|
|
url: https://twinnation.org/actuator/health
|
|
|
|
conditions:
|
2019-12-04 23:27:27 +01:00
|
|
|
- "[STATUS] == 200"
|
2019-09-10 04:21:18 +02:00
|
|
|
`))
|
2019-10-20 04:03:55 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error("No error should've been returned")
|
|
|
|
}
|
2020-08-22 20:15:21 +02:00
|
|
|
if config == nil {
|
|
|
|
t.Fatal("Config shouldn't have been nil")
|
|
|
|
}
|
2019-11-16 21:47:56 +01:00
|
|
|
if config.Metrics {
|
|
|
|
t.Error("Metrics should've been false by default")
|
|
|
|
}
|
|
|
|
if config.Services[0].Url != "https://twinnation.org/actuator/health" {
|
|
|
|
t.Errorf("URL should have been %s", "https://twinnation.org/actuator/health")
|
|
|
|
}
|
2020-09-01 18:46:23 +02:00
|
|
|
if config.Services[0].Interval != 60*time.Second {
|
|
|
|
t.Errorf("Interval should have been %s, because it is the default value", 60*time.Second)
|
2019-11-16 21:47:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndValidateConfigBytesWithMetrics(t *testing.T) {
|
|
|
|
config, err := parseAndValidateConfigBytes([]byte(`
|
|
|
|
metrics: true
|
|
|
|
services:
|
|
|
|
- name: twinnation
|
|
|
|
url: https://twinnation.org/actuator/health
|
|
|
|
conditions:
|
2019-12-04 23:27:27 +01:00
|
|
|
- "[STATUS] == 200"
|
2019-11-16 21:47:56 +01:00
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Error("No error should've been returned")
|
|
|
|
}
|
2020-08-22 20:15:21 +02:00
|
|
|
if config == nil {
|
|
|
|
t.Fatal("Config shouldn't have been nil")
|
|
|
|
}
|
2019-11-16 21:47:56 +01:00
|
|
|
if !config.Metrics {
|
|
|
|
t.Error("Metrics should have been true")
|
|
|
|
}
|
2019-09-10 04:21:18 +02:00
|
|
|
if config.Services[0].Url != "https://twinnation.org/actuator/health" {
|
|
|
|
t.Errorf("URL should have been %s", "https://twinnation.org/actuator/health")
|
|
|
|
}
|
2020-09-01 18:46:23 +02:00
|
|
|
if config.Services[0].Interval != 60*time.Second {
|
|
|
|
t.Errorf("Interval should have been %s, because it is the default value", 60*time.Second)
|
2019-09-10 04:21:18 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 04:03:55 +02:00
|
|
|
|
|
|
|
func TestParseAndValidateBadConfigBytes(t *testing.T) {
|
|
|
|
_, err := parseAndValidateConfigBytes([]byte(`
|
|
|
|
badconfig:
|
|
|
|
- asdsa: w0w
|
|
|
|
usadasdrl: asdxzczxc
|
|
|
|
asdas:
|
|
|
|
- soup
|
|
|
|
`))
|
|
|
|
if err == nil {
|
|
|
|
t.Error("An error should've been returned")
|
|
|
|
}
|
|
|
|
if err != ErrNoServiceInConfig {
|
|
|
|
t.Error("The error returned should have been of type ErrNoServiceInConfig")
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 20:15:21 +02:00
|
|
|
|
|
|
|
func TestParseAndValidateConfigBytesWithAlerting(t *testing.T) {
|
|
|
|
config, err := parseAndValidateConfigBytes([]byte(`
|
|
|
|
alerting:
|
2020-09-19 22:22:12 +02:00
|
|
|
slack:
|
|
|
|
webhook-url: "http://example.com"
|
2020-08-22 20:15:21 +02:00
|
|
|
services:
|
|
|
|
- name: twinnation
|
|
|
|
url: https://twinnation.org/actuator/health
|
|
|
|
alerts:
|
|
|
|
- type: slack
|
|
|
|
enabled: true
|
2020-09-17 02:22:33 +02:00
|
|
|
failure-threshold: 7
|
2020-08-22 20:15:21 +02:00
|
|
|
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 {
|
2020-08-28 04:23:21 +02:00
|
|
|
t.Fatal("config.AlertingConfig shouldn't have been nil")
|
2020-08-22 20:15:21 +02:00
|
|
|
}
|
2020-09-19 22:22:12 +02:00
|
|
|
if config.Alerting.Slack.WebhookUrl != "http://example.com" {
|
2020-08-22 20:15:21 +02:00
|
|
|
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")
|
|
|
|
}
|
2020-09-01 18:46:23 +02:00
|
|
|
if config.Services[0].Interval != 60*time.Second {
|
|
|
|
t.Errorf("Interval should have been %s, because it is the default value", 60*time.Second)
|
2020-08-22 20:15:21 +02:00
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
2020-09-17 02:22:33 +02:00
|
|
|
if config.Services[0].Alerts[0].FailureThreshold != 7 {
|
|
|
|
t.Errorf("The failure threshold of the alert should've been %d, but it was %d", 7, config.Services[0].Alerts[0].FailureThreshold)
|
|
|
|
}
|
|
|
|
if config.Services[0].Alerts[0].FailureThreshold != 7 {
|
|
|
|
t.Errorf("The success threshold of the alert should've been %d, but it was %d", 2, config.Services[0].Alerts[0].SuccessThreshold)
|
2020-08-22 20:15:21 +02:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|