From 22fef4e9aa44325331325d396650811e5d3120b3 Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Sat, 22 Aug 2020 14:15:21 -0400 Subject: [PATCH] Add tests for alert configuration --- config/config_test.go | 69 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/config/config_test.go b/config/config_test.go index c8f5cc25..a6970ca8 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "github.com/TwinProduction/gatus/core" "testing" "time" ) @@ -23,6 +24,9 @@ services: if err != nil { t.Error("No error should've been returned") } + if config == nil { + t.Fatal("Config shouldn't have been nil") + } if len(config.Services) != 2 { t.Error("Should have returned two services") } @@ -58,6 +62,9 @@ services: 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") } @@ -81,6 +88,9 @@ services: 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 have been true") } @@ -107,3 +117,62 @@ badconfig: 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) + } +}