2019-09-06 04:02:00 +00:00
package config
2019-09-09 01:07:08 +00:00
import (
2020-10-14 23:22:58 +00:00
"fmt"
2020-11-11 23:05:18 +00:00
"strings"
2019-09-09 01:07:08 +00:00
"testing"
"time"
2020-11-11 23:05:18 +00:00
2021-03-05 02:26:17 +00:00
"github.com/TwinProduction/gatus/alerting"
"github.com/TwinProduction/gatus/alerting/provider/custom"
"github.com/TwinProduction/gatus/alerting/provider/discord"
"github.com/TwinProduction/gatus/alerting/provider/mattermost"
"github.com/TwinProduction/gatus/alerting/provider/messagebird"
"github.com/TwinProduction/gatus/alerting/provider/pagerduty"
"github.com/TwinProduction/gatus/alerting/provider/slack"
2021-03-30 23:38:34 +00:00
"github.com/TwinProduction/gatus/alerting/provider/telegram"
2021-03-05 02:26:17 +00:00
"github.com/TwinProduction/gatus/alerting/provider/twilio"
2020-11-11 23:05:18 +00:00
"github.com/TwinProduction/gatus/core"
"github.com/TwinProduction/gatus/k8stest"
v1 "k8s.io/api/core/v1"
2019-09-09 01:07:08 +00:00
)
2019-09-06 04:02:00 +00:00
2020-10-22 02:56:35 +00:00
func TestGetBeforeConfigIsLoaded ( t * testing . T ) {
defer func ( ) { recover ( ) } ( )
Get ( )
t . Fatal ( "Should've panicked because the configuration hasn't been loaded yet" )
}
2021-01-10 03:55:36 +00:00
func TestSet ( t * testing . T ) {
if config != nil {
t . Fatal ( "config should've been nil" )
}
Set ( & Config { } )
if config == nil {
t . Fatal ( "config shouldn't have been nil" )
}
}
2020-10-22 02:56:35 +00:00
func TestLoadFileThatDoesNotExist ( t * testing . T ) {
err := Load ( "file-that-does-not-exist.yaml" )
if err == nil {
t . Error ( "Should've returned an error, because the file specified doesn't exist" )
}
}
func TestLoadDefaultConfigurationFile ( t * testing . T ) {
err := LoadDefaultConfiguration ( )
if err == nil {
t . Error ( "Should've returned an error, because there's no configuration files at the default path nor the default fallback path" )
}
}
2019-10-20 02:03:55 +00:00
func TestParseAndValidateConfigBytes ( t * testing . T ) {
2021-02-03 04:06:34 +00:00
file := t . TempDir ( ) + "/test.db"
config , err := parseAndValidateConfigBytes ( [ ] byte ( fmt . Sprintf ( `
storage :
file : % s
2019-09-06 04:02:00 +00:00
services :
- name : twinnation
2020-11-20 02:10:42 +00:00
url : https : //twinnation.org/health
2019-09-09 01:07:08 +00:00
interval : 15 s
2019-09-06 04:02:00 +00:00
conditions :
2019-12-04 22:27:27 +00:00
- "[STATUS] == 200"
2019-09-06 04:02:00 +00:00
- name : github
2019-09-09 01:07:08 +00:00
url : https : //api.github.com/healthz
2019-09-06 04:02:00 +00:00
conditions :
2019-12-04 22:27:27 +00:00
- "[STATUS] != 400"
- "[STATUS] != 500"
2021-02-03 04:06:34 +00:00
` , file ) ) )
2019-10-20 02:03:55 +00:00
if err != nil {
2021-02-03 04:06:34 +00:00
t . Error ( "expected no error, got" , err . Error ( ) )
2019-10-20 02:03:55 +00:00
}
2020-08-22 18:15:21 +00:00
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
2019-09-06 04:02:00 +00:00
if len ( config . Services ) != 2 {
t . Error ( "Should have returned two services" )
}
2020-11-20 02:10:42 +00:00
if config . Services [ 0 ] . URL != "https://twinnation.org/health" {
t . Errorf ( "URL should have been %s" , "https://twinnation.org/health" )
2019-09-06 04:02:00 +00:00
}
2020-10-23 20:29:20 +00:00
if config . Services [ 1 ] . URL != "https://api.github.com/healthz" {
2019-09-09 01:07:08 +00:00
t . Errorf ( "URL should have been %s" , "https://api.github.com/healthz" )
2019-09-06 04:02:00 +00:00
}
2020-10-22 01:56:07 +00:00
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" )
}
2019-09-09 01:07:08 +00:00
if config . Services [ 0 ] . Interval != 15 * time . Second {
t . Errorf ( "Interval should have been %s" , 15 * time . Second )
2019-09-06 04:02:00 +00:00
}
2020-09-01 16:46:23 +00: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 04:02:00 +00: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 02:21:18 +00:00
2019-10-20 02:03:55 +00:00
func TestParseAndValidateConfigBytesDefault ( t * testing . T ) {
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
2019-09-10 02:21:18 +00:00
services :
- name : twinnation
2020-11-20 02:10:42 +00:00
url : https : //twinnation.org/health
2019-09-10 02:21:18 +00:00
conditions :
2019-12-04 22:27:27 +00:00
- "[STATUS] == 200"
2019-09-10 02:21:18 +00:00
` ) )
2019-10-20 02:03:55 +00:00
if err != nil {
2021-02-03 04:06:34 +00:00
t . Error ( "expected no error, got" , err . Error ( ) )
2019-10-20 02:03:55 +00:00
}
2020-08-22 18:15:21 +00:00
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
2019-11-16 20:47:56 +00:00
if config . Metrics {
t . Error ( "Metrics should've been false by default" )
}
2020-11-20 02:10:42 +00:00
if config . Services [ 0 ] . URL != "https://twinnation.org/health" {
t . Errorf ( "URL should have been %s" , "https://twinnation.org/health" )
2019-11-16 20:47:56 +00:00
}
2020-09-01 16:46:23 +00: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 20:47:56 +00:00
}
2020-11-19 18:39:48 +00:00
if config . Web . Address != DefaultAddress {
t . Errorf ( "Bind address should have been %s, because it is the default value" , DefaultAddress )
}
if config . Web . Port != DefaultPort {
t . Errorf ( "Port should have been %d, because it is the default value" , DefaultPort )
}
}
func TestParseAndValidateConfigBytesWithAddress ( t * testing . T ) {
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
web :
address : 127.0 .0 .1
services :
- name : twinnation
url : https : //twinnation.org/actuator/health
conditions :
- "[STATUS] == 200"
` ) )
if err != nil {
2021-02-03 04:06:34 +00:00
t . Error ( "expected no error, got" , err . Error ( ) )
2020-11-19 18:39:48 +00:00
}
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 . 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 != 60 * time . Second {
t . Errorf ( "Interval should have been %s, because it is the default value" , 60 * time . Second )
}
if config . Web . Address != "127.0.0.1" {
t . Errorf ( "Bind address should have been %s, because it is specified in config" , "127.0.0.1" )
}
if config . Web . Port != DefaultPort {
t . Errorf ( "Port should have been %d, because it is the default value" , DefaultPort )
}
}
func TestParseAndValidateConfigBytesWithPort ( t * testing . T ) {
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
web :
port : 12345
services :
- name : twinnation
2021-01-17 21:38:22 +00:00
url : https : //twinnation.org/health
2020-11-19 18:39:48 +00:00
conditions :
- "[STATUS] == 200"
` ) )
if err != nil {
2021-02-03 04:06:34 +00:00
t . Error ( "expected no error, got" , err . Error ( ) )
2020-11-19 18:39:48 +00:00
}
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
if config . Metrics {
t . Error ( "Metrics should've been false by default" )
}
2021-01-17 21:38:22 +00:00
if config . Services [ 0 ] . URL != "https://twinnation.org/health" {
t . Errorf ( "URL should have been %s" , "https://twinnation.org/health" )
2020-11-19 18:39:48 +00: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 )
}
if config . Web . Address != DefaultAddress {
t . Errorf ( "Bind address should have been %s, because it is the default value" , DefaultAddress )
}
if config . Web . Port != 12345 {
t . Errorf ( "Port should have been %d, because it is specified in config" , 12345 )
}
}
func TestParseAndValidateConfigBytesWithPortAndHost ( t * testing . T ) {
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
web :
port : 12345
address : 127.0 .0 .1
services :
- name : twinnation
2020-11-20 22:40:57 +00:00
url : https : //twinnation.org/health
2020-11-19 18:39:48 +00:00
conditions :
- "[STATUS] == 200"
` ) )
if err != nil {
2021-02-03 04:06:34 +00:00
t . Error ( "expected no error, got" , err . Error ( ) )
2020-11-19 18:39:48 +00:00
}
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
if config . Metrics {
t . Error ( "Metrics should've been false by default" )
}
2020-11-20 22:40:57 +00:00
if config . Services [ 0 ] . URL != "https://twinnation.org/health" {
t . Errorf ( "URL should have been %s" , "https://twinnation.org/health" )
2020-11-19 18:39:48 +00: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 )
}
if config . Web . Address != "127.0.0.1" {
t . Errorf ( "Bind address should have been %s, because it is specified in config" , "127.0.0.1" )
}
if config . Web . Port != 12345 {
t . Errorf ( "Port should have been %d, because it is specified in config" , 12345 )
}
2019-11-16 20:47:56 +00:00
}
2020-11-19 19:03:30 +00:00
func TestParseAndValidateConfigBytesWithInvalidPort ( t * testing . T ) {
defer func ( ) { recover ( ) } ( )
2020-11-20 22:40:57 +00:00
_ , _ = parseAndValidateConfigBytes ( [ ] byte ( `
2020-11-19 19:03:30 +00:00
web :
port : 65536
address : 127.0 .0 .1
services :
- name : twinnation
2020-11-20 22:40:57 +00:00
url : https : //twinnation.org/health
2020-11-19 19:03:30 +00:00
conditions :
- "[STATUS] == 200"
` ) )
t . Fatal ( "Should've panicked because the configuration specifies an invalid port value" )
2019-11-16 20:47:56 +00:00
}
2020-12-31 01:07:07 +00:00
func TestParseAndValidateConfigBytesWithMetricsAndCustomUserAgentHeader ( t * testing . T ) {
2019-11-16 20:47:56 +00:00
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
metrics : true
services :
- name : twinnation
2020-11-20 02:10:42 +00:00
url : https : //twinnation.org/health
2020-12-31 01:07:07 +00:00
headers :
User - Agent : Test / 2.0
2019-11-16 20:47:56 +00:00
conditions :
2019-12-04 22:27:27 +00:00
- "[STATUS] == 200"
2019-11-16 20:47:56 +00:00
` ) )
if err != nil {
2021-02-03 04:06:34 +00:00
t . Error ( "expected no error, got" , err . Error ( ) )
2019-11-16 20:47:56 +00:00
}
2020-08-22 18:15:21 +00:00
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
2019-11-16 20:47:56 +00:00
if ! config . Metrics {
t . Error ( "Metrics should have been true" )
}
2020-11-20 02:10:42 +00:00
if config . Services [ 0 ] . URL != "https://twinnation.org/health" {
t . Errorf ( "URL should have been %s" , "https://twinnation.org/health" )
2019-09-10 02:21:18 +00:00
}
2020-09-01 16:46:23 +00: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 02:21:18 +00:00
}
2020-11-19 18:39:48 +00:00
if config . Web . Address != DefaultAddress {
2020-11-20 05:59:43 +00:00
t . Errorf ( "Bind address should have been %s, because it is the default value" , DefaultAddress )
2020-11-19 18:39:48 +00:00
}
if config . Web . Port != DefaultPort {
2020-11-20 05:59:43 +00:00
t . Errorf ( "Port should have been %d, because it is the default value" , DefaultPort )
2020-11-19 18:39:48 +00:00
}
2020-12-31 01:07:07 +00:00
if userAgent := config . Services [ 0 ] . Headers [ "User-Agent" ] ; userAgent != "Test/2.0" {
t . Errorf ( "User-Agent should've been %s, got %s" , "Test/2.0" , userAgent )
}
2020-11-19 18:39:48 +00:00
}
func TestParseAndValidateConfigBytesWithMetricsAndHostAndPort ( t * testing . T ) {
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
metrics : true
2020-12-31 01:07:07 +00:00
web :
address : 192.168 .0 .1
port : 9090
2020-11-19 18:39:48 +00:00
services :
- name : twinnation
2020-11-20 22:40:57 +00:00
url : https : //twinnation.org/health
2020-11-19 18:39:48 +00:00
conditions :
- "[STATUS] == 200"
` ) )
if err != nil {
2021-02-03 04:06:34 +00:00
t . Error ( "expected no error, got" , err . Error ( ) )
2020-11-19 18:39:48 +00:00
}
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
if ! config . Metrics {
t . Error ( "Metrics should have been true" )
}
2020-12-31 01:07:07 +00:00
if config . Web . Address != "192.168.0.1" {
t . Errorf ( "Bind address should have been %s, because it is the default value" , "192.168.0.1" )
}
if config . Web . Port != 9090 {
t . Errorf ( "Port should have been %d, because it is specified in config" , 9090 )
}
2020-11-20 22:40:57 +00:00
if config . Services [ 0 ] . URL != "https://twinnation.org/health" {
t . Errorf ( "URL should have been %s" , "https://twinnation.org/health" )
2020-11-19 18:39:48 +00: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-12-31 01:07:07 +00:00
if userAgent := config . Services [ 0 ] . Headers [ "User-Agent" ] ; userAgent != core . GatusUserAgent {
t . Errorf ( "User-Agent should've been %s because it's the default value, got %s" , core . GatusUserAgent , userAgent )
2020-11-19 18:39:48 +00:00
}
2019-09-10 02:21:18 +00:00
}
2019-10-20 02:03:55 +00: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 18:15:21 +00:00
func TestParseAndValidateConfigBytesWithAlerting ( t * testing . T ) {
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
2020-11-11 23:13:39 +00:00
debug : true
2020-08-22 18:15:21 +00:00
alerting :
2020-09-19 20:22:12 +00:00
slack :
webhook - url : "http://example.com"
2021-03-05 02:26:17 +00:00
discord :
webhook - url : "http://example.org"
2020-09-24 23:52:59 +00:00
pagerduty :
integration - key : "00000000000000000000000000000000"
2021-05-16 01:31:32 +00:00
mattermost :
webhook - url : "http://example.com"
2020-11-23 21:20:06 +00:00
messagebird :
2020-11-23 21:29:57 +00:00
access - key : "1"
2020-11-24 23:31:51 +00:00
originator : "31619191918"
recipients : "31619191919"
2021-03-30 23:38:34 +00:00
telegram :
token : 123456 : ABC - DEF1234ghIkl - zyx57W2v1u123ew11
id : 01234567 89
2021-05-16 01:31:32 +00:00
twilio :
sid : "1234"
token : "5678"
from : "+1-234-567-8901"
to : "+1-234-567-8901"
2020-08-22 18:15:21 +00:00
services :
- name : twinnation
2020-11-20 02:10:42 +00:00
url : https : //twinnation.org/health
2020-08-22 18:15:21 +00:00
alerts :
- type : slack
enabled : true
2020-10-22 01:56:07 +00:00
- type : pagerduty
enabled : true
2020-09-17 00:22:33 +00:00
failure - threshold : 7
2020-10-22 01:56:07 +00:00
success - threshold : 5
2020-08-22 18:15:21 +00:00
description : "Healthcheck failed 7 times in a row"
2021-05-16 01:31:32 +00:00
- type : mattermost
enabled : true
2020-11-23 21:20:06 +00:00
- type : messagebird
2021-03-05 02:26:17 +00:00
- type : discord
2020-11-23 21:20:06 +00:00
enabled : true
2021-03-05 02:26:17 +00:00
failure - threshold : 10
2021-03-30 23:38:34 +00:00
- type : telegram
enabled : true
2021-05-16 01:31:32 +00:00
- type : twilio
enabled : true
failure - threshold : 12
success - threshold : 15
2020-08-22 18:15:21 +00:00
conditions :
- "[STATUS] == 200"
2021-05-16 01:31:32 +00:00
` ) )
if err != nil {
t . Error ( "expected no error, got" , err . Error ( ) )
}
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
// Alerting providers
if config . Alerting == nil {
t . Fatal ( "config.Alerting shouldn't have been nil" )
}
if config . Alerting . Slack == nil || ! config . Alerting . Slack . IsValid ( ) {
t . Fatal ( "Slack alerting config should've been valid" )
}
// Services
if len ( config . Services ) != 1 {
t . Error ( "There should've been 1 service" )
}
if config . Services [ 0 ] . URL != "https://twinnation.org/health" {
t . Errorf ( "URL should have been %s" , "https://twinnation.org/health" )
}
if config . Services [ 0 ] . Interval != 60 * time . Second {
t . Errorf ( "Interval should have been %s, because it is the default value" , 60 * time . Second )
}
if len ( config . Services [ 0 ] . Alerts ) != 7 {
t . Fatal ( "There should've been 7 alerts configured" )
}
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 ] . IsEnabled ( ) {
t . Error ( "The alert should've been enabled" )
}
if config . Services [ 0 ] . Alerts [ 0 ] . FailureThreshold != 3 {
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 ] . SuccessThreshold != 2 {
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 ] . Type != core . PagerDutyAlert {
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 ] . GetDescription ( ) != "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 [ 1 ] . GetDescription ( ) )
}
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 [ 2 ] . Type != core . MattermostAlert {
t . Errorf ( "The type of the alert should've been %s, but it was %s" , core . MattermostAlert , config . Services [ 0 ] . Alerts [ 2 ] . Type )
}
if ! config . Services [ 0 ] . Alerts [ 2 ] . IsEnabled ( ) {
t . Error ( "The alert should've been enabled" )
}
if config . Services [ 0 ] . Alerts [ 2 ] . FailureThreshold != 3 {
t . Errorf ( "The default failure threshold of the alert should've been %d, but it was %d" , 3 , config . Services [ 0 ] . Alerts [ 2 ] . FailureThreshold )
}
if config . Services [ 0 ] . Alerts [ 2 ] . SuccessThreshold != 2 {
t . Errorf ( "The default success threshold of the alert should've been %d, but it was %d" , 2 , config . Services [ 0 ] . Alerts [ 2 ] . SuccessThreshold )
}
if config . Services [ 0 ] . Alerts [ 3 ] . Type != core . MessagebirdAlert {
t . Errorf ( "The type of the alert should've been %s, but it was %s" , core . MessagebirdAlert , config . Services [ 0 ] . Alerts [ 3 ] . Type )
}
if config . Services [ 0 ] . Alerts [ 3 ] . IsEnabled ( ) {
t . Error ( "The alert should've been disabled" )
}
if config . Services [ 0 ] . Alerts [ 4 ] . Type != core . DiscordAlert {
t . Errorf ( "The type of the alert should've been %s, but it was %s" , core . DiscordAlert , config . Services [ 0 ] . Alerts [ 4 ] . Type )
}
if ! config . Services [ 0 ] . Alerts [ 4 ] . IsEnabled ( ) {
t . Error ( "The alert should've been enabled" )
}
if config . Services [ 0 ] . Alerts [ 4 ] . FailureThreshold != 10 {
t . Errorf ( "The failure threshold of the alert should've been %d, but it was %d" , 10 , config . Services [ 0 ] . Alerts [ 4 ] . FailureThreshold )
}
if config . Services [ 0 ] . Alerts [ 4 ] . SuccessThreshold != 2 {
t . Errorf ( "The default success threshold of the alert should've been %d, but it was %d" , 2 , config . Services [ 0 ] . Alerts [ 4 ] . SuccessThreshold )
}
if config . Services [ 0 ] . Alerts [ 5 ] . Type != core . TelegramAlert {
t . Errorf ( "The type of the alert should've been %s, but it was %s" , core . TelegramAlert , config . Services [ 0 ] . Alerts [ 5 ] . Type )
}
if ! config . Services [ 0 ] . Alerts [ 5 ] . IsEnabled ( ) {
t . Error ( "The alert should've been enabled" )
}
if config . Services [ 0 ] . Alerts [ 5 ] . FailureThreshold != 3 {
t . Errorf ( "The default failure threshold of the alert should've been %d, but it was %d" , 3 , config . Services [ 0 ] . Alerts [ 5 ] . FailureThreshold )
}
if config . Services [ 0 ] . Alerts [ 5 ] . SuccessThreshold != 2 {
t . Errorf ( "The default success threshold of the alert should've been %d, but it was %d" , 2 , config . Services [ 0 ] . Alerts [ 5 ] . SuccessThreshold )
}
if config . Services [ 0 ] . Alerts [ 6 ] . Type != core . TwilioAlert {
t . Errorf ( "The type of the alert should've been %s, but it was %s" , core . TwilioAlert , config . Services [ 0 ] . Alerts [ 6 ] . Type )
}
if ! config . Services [ 0 ] . Alerts [ 6 ] . IsEnabled ( ) {
t . Error ( "The alert should've been enabled" )
}
if config . Services [ 0 ] . Alerts [ 6 ] . FailureThreshold != 12 {
t . Errorf ( "The default failure threshold of the alert should've been %d, but it was %d" , 12 , config . Services [ 0 ] . Alerts [ 6 ] . FailureThreshold )
}
if config . Services [ 0 ] . Alerts [ 6 ] . SuccessThreshold != 15 {
t . Errorf ( "The default success threshold of the alert should've been %d, but it was %d" , 15 , config . Services [ 0 ] . Alerts [ 6 ] . SuccessThreshold )
}
}
func TestParseAndValidateConfigBytesWithAlertingAndDefaultAlert ( t * testing . T ) {
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
alerting :
slack :
webhook - url : "http://example.com"
default - alert :
enabled : true
discord :
webhook - url : "http://example.org"
default - alert :
enabled : true
failure - threshold : 10
success - threshold : 1
pagerduty :
integration - key : "00000000000000000000000000000000"
default - alert :
enabled : true
description : default description
failure - threshold : 7
success - threshold : 5
mattermost :
webhook - url : "http://example.com"
default - alert :
enabled : true
messagebird :
access - key : "1"
originator : "31619191918"
recipients : "31619191919"
default - alert :
enabled : false
send - on - resolved : true
telegram :
token : 123456 : ABC - DEF1234ghIkl - zyx57W2v1u123ew11
id : 01234567 89
default - alert :
enabled : true
twilio :
sid : "1234"
token : "5678"
from : "+1-234-567-8901"
to : "+1-234-567-8901"
default - alert :
enabled : true
failure - threshold : 12
success - threshold : 15
services :
- name : twinnation
url : https : //twinnation.org/health
alerts :
- type : slack
- type : pagerduty
- type : mattermost
- type : messagebird
- type : discord
success - threshold : 2 # test service alert override
- type : telegram
- type : twilio
conditions :
- "[STATUS] == 200"
2020-08-22 18:15:21 +00:00
` ) )
if err != nil {
2021-02-03 04:06:34 +00:00
t . Error ( "expected no error, got" , err . Error ( ) )
2020-08-22 18:15:21 +00:00
}
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
if config . Metrics {
t . Error ( "Metrics should've been false by default" )
}
2021-03-05 02:26:17 +00:00
// Alerting providers
2020-08-22 18:15:21 +00:00
if config . Alerting == nil {
2020-09-24 23:52:59 +00:00
t . Fatal ( "config.Alerting shouldn't have been nil" )
}
if config . Alerting . Slack == nil || ! config . Alerting . Slack . IsValid ( ) {
t . Fatal ( "Slack alerting config should've been valid" )
2020-08-22 18:15:21 +00:00
}
2020-10-23 20:29:20 +00:00
if config . Alerting . Slack . WebhookURL != "http://example.com" {
t . Errorf ( "Slack webhook should've been %s, but was %s" , "http://example.com" , config . Alerting . Slack . WebhookURL )
2020-09-24 23:52:59 +00:00
}
if config . Alerting . PagerDuty == nil || ! config . Alerting . PagerDuty . IsValid ( ) {
t . Fatal ( "PagerDuty alerting config should've been valid" )
}
if config . Alerting . PagerDuty . IntegrationKey != "00000000000000000000000000000000" {
t . Errorf ( "PagerDuty integration key should've been %s, but was %s" , "00000000000000000000000000000000" , config . Alerting . PagerDuty . IntegrationKey )
2020-08-22 18:15:21 +00:00
}
2021-03-30 23:38:34 +00:00
if config . Alerting . Messagebird == nil || ! config . Alerting . Messagebird . IsValid ( ) {
t . Fatal ( "Messagebird alerting config should've been valid" )
}
2020-11-23 21:20:06 +00:00
if config . Alerting . Messagebird . AccessKey != "1" {
t . Errorf ( "Messagebird access key should've been %s, but was %s" , "1" , config . Alerting . Messagebird . AccessKey )
}
2020-11-24 23:30:23 +00:00
if config . Alerting . Messagebird . Originator != "31619191918" {
t . Errorf ( "Messagebird originator field should've been %s, but was %s" , "31619191918" , config . Alerting . Messagebird . Originator )
2020-11-23 21:20:06 +00:00
}
2020-11-24 23:30:23 +00:00
if config . Alerting . Messagebird . Recipients != "31619191919" {
t . Errorf ( "Messagebird to recipients should've been %s, but was %s" , "31619191919" , config . Alerting . Messagebird . Recipients )
2020-11-23 21:20:06 +00:00
}
2021-03-30 23:38:34 +00:00
2021-03-05 02:26:17 +00:00
if config . Alerting . Discord == nil || ! config . Alerting . Discord . IsValid ( ) {
t . Fatal ( "Discord alerting config should've been valid" )
}
if config . Alerting . Discord . WebhookURL != "http://example.org" {
t . Errorf ( "Discord webhook should've been %s, but was %s" , "http://example.org" , config . Alerting . Discord . WebhookURL )
}
2021-03-30 23:38:34 +00:00
if config . Alerting . Telegram . Token != "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11" {
t . Errorf ( "Telegram token should've been %s, but was %s" , "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11" , config . Alerting . Telegram . Token )
}
if config . Alerting . Telegram . ID != "0123456789" {
t . Errorf ( "Telegram ID should've been %s, but was %s" , "012345689" , config . Alerting . Telegram . ID )
}
2021-03-05 02:26:17 +00:00
if GetAlertingProviderByAlertType ( config , core . DiscordAlert ) != config . Alerting . Discord {
t . Error ( "expected discord configuration" )
}
// Services
2020-08-22 18:15:21 +00:00
if len ( config . Services ) != 1 {
t . Error ( "There should've been 1 service" )
}
2020-11-20 02:10:42 +00:00
if config . Services [ 0 ] . URL != "https://twinnation.org/health" {
t . Errorf ( "URL should have been %s" , "https://twinnation.org/health" )
2020-08-22 18:15:21 +00:00
}
2020-09-01 16:46:23 +00: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 18:15:21 +00:00
}
2021-05-16 01:31:32 +00:00
if len ( config . Services [ 0 ] . Alerts ) != 7 {
t . Fatal ( "There should've been 7 alerts configured" )
2020-08-22 18:15:21 +00:00
}
2021-03-05 02:26:17 +00: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 )
2020-08-22 18:15:21 +00:00
}
2021-05-16 01:31:32 +00:00
if ! config . Services [ 0 ] . Alerts [ 0 ] . IsEnabled ( ) {
2020-08-22 18:15:21 +00:00
t . Error ( "The alert should've been enabled" )
}
2020-10-22 01:56:07 +00:00
if config . Services [ 0 ] . Alerts [ 0 ] . FailureThreshold != 3 {
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 ] . SuccessThreshold != 2 {
t . Errorf ( "The default success threshold of the alert should've been %d, but it was %d" , 2 , config . Services [ 0 ] . Alerts [ 0 ] . SuccessThreshold )
2020-09-17 00:22:33 +00:00
}
2021-03-05 02:26:17 +00:00
if config . Services [ 0 ] . Alerts [ 1 ] . Type != core . PagerDutyAlert {
t . Errorf ( "The type of the alert should've been %s, but it was %s" , core . PagerDutyAlert , config . Services [ 0 ] . Alerts [ 1 ] . Type )
}
2021-05-16 01:31:32 +00:00
if config . Services [ 0 ] . Alerts [ 1 ] . GetDescription ( ) != "default description" {
t . Errorf ( "The description of the alert should've been %s, but it was %s" , "default description" , config . Services [ 0 ] . Alerts [ 1 ] . GetDescription ( ) )
}
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 )
2021-03-05 02:26:17 +00:00
}
2020-10-22 01:56:07 +00:00
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 )
2020-08-22 18:15:21 +00:00
}
2021-03-05 02:26:17 +00:00
2021-05-16 01:31:32 +00:00
if config . Services [ 0 ] . Alerts [ 2 ] . Type != core . MattermostAlert {
t . Errorf ( "The type of the alert should've been %s, but it was %s" , core . MattermostAlert , config . Services [ 0 ] . Alerts [ 2 ] . Type )
}
if ! config . Services [ 0 ] . Alerts [ 2 ] . IsEnabled ( ) {
t . Error ( "The alert should've been enabled" )
}
if config . Services [ 0 ] . Alerts [ 2 ] . FailureThreshold != 3 {
t . Errorf ( "The default failure threshold of the alert should've been %d, but it was %d" , 3 , config . Services [ 0 ] . Alerts [ 2 ] . FailureThreshold )
2020-08-22 18:15:21 +00:00
}
2021-05-16 01:31:32 +00:00
if config . Services [ 0 ] . Alerts [ 2 ] . SuccessThreshold != 2 {
t . Errorf ( "The default success threshold of the alert should've been %d, but it was %d" , 2 , config . Services [ 0 ] . Alerts [ 2 ] . SuccessThreshold )
}
if config . Services [ 0 ] . Alerts [ 3 ] . Type != core . MessagebirdAlert {
t . Errorf ( "The type of the alert should've been %s, but it was %s" , core . MessagebirdAlert , config . Services [ 0 ] . Alerts [ 3 ] . Type )
}
if config . Services [ 0 ] . Alerts [ 3 ] . IsEnabled ( ) {
2021-03-05 02:26:17 +00:00
t . Error ( "The alert should've been disabled" )
2020-10-22 01:56:07 +00:00
}
2021-05-16 01:31:32 +00:00
if ! config . Services [ 0 ] . Alerts [ 3 ] . IsSendingOnResolved ( ) {
t . Error ( "The alert should be sending on resolve" )
}
if config . Services [ 0 ] . Alerts [ 4 ] . Type != core . DiscordAlert {
t . Errorf ( "The type of the alert should've been %s, but it was %s" , core . DiscordAlert , config . Services [ 0 ] . Alerts [ 4 ] . Type )
}
if ! config . Services [ 0 ] . Alerts [ 4 ] . IsEnabled ( ) {
t . Error ( "The alert should've been enabled" )
}
if config . Services [ 0 ] . Alerts [ 4 ] . FailureThreshold != 10 {
t . Errorf ( "The failure threshold of the alert should've been %d, but it was %d" , 10 , config . Services [ 0 ] . Alerts [ 4 ] . FailureThreshold )
}
if config . Services [ 0 ] . Alerts [ 4 ] . SuccessThreshold != 2 {
t . Errorf ( "The default success threshold of the alert should've been %d, but it was %d" , 2 , config . Services [ 0 ] . Alerts [ 4 ] . SuccessThreshold )
}
2021-03-05 02:26:17 +00:00
2021-05-16 01:31:32 +00:00
if config . Services [ 0 ] . Alerts [ 5 ] . Type != core . TelegramAlert {
t . Errorf ( "The type of the alert should've been %s, but it was %s" , core . TelegramAlert , config . Services [ 0 ] . Alerts [ 5 ] . Type )
}
if ! config . Services [ 0 ] . Alerts [ 5 ] . IsEnabled ( ) {
t . Error ( "The alert should've been enabled" )
}
if config . Services [ 0 ] . Alerts [ 5 ] . FailureThreshold != 3 {
t . Errorf ( "The default failure threshold of the alert should've been %d, but it was %d" , 3 , config . Services [ 0 ] . Alerts [ 5 ] . FailureThreshold )
}
if config . Services [ 0 ] . Alerts [ 5 ] . SuccessThreshold != 2 {
t . Errorf ( "The default success threshold of the alert should've been %d, but it was %d" , 2 , config . Services [ 0 ] . Alerts [ 5 ] . SuccessThreshold )
}
if config . Services [ 0 ] . Alerts [ 6 ] . Type != core . TwilioAlert {
t . Errorf ( "The type of the alert should've been %s, but it was %s" , core . TwilioAlert , config . Services [ 0 ] . Alerts [ 6 ] . Type )
}
if ! config . Services [ 0 ] . Alerts [ 6 ] . IsEnabled ( ) {
t . Error ( "The alert should've been enabled" )
}
if config . Services [ 0 ] . Alerts [ 6 ] . FailureThreshold != 12 {
t . Errorf ( "The default failure threshold of the alert should've been %d, but it was %d" , 12 , config . Services [ 0 ] . Alerts [ 6 ] . FailureThreshold )
}
if config . Services [ 0 ] . Alerts [ 6 ] . SuccessThreshold != 15 {
t . Errorf ( "The default success threshold of the alert should've been %d, but it was %d" , 15 , config . Services [ 0 ] . Alerts [ 6 ] . SuccessThreshold )
}
}
func TestParseAndValidateConfigBytesWithAlertingAndDefaultAlertAndMultipleAlertsOfSameTypeWithOverriddenParameters ( t * testing . T ) {
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
alerting :
slack :
webhook - url : "http://example.com"
default - alert :
enabled : true
description : "description"
services :
- name : twinnation
url : https : //twinnation.org/health
alerts :
- type : slack
failure - threshold : 10
- type : slack
failure - threshold : 20
description : "wow"
- type : slack
enabled : false
failure - threshold : 30
conditions :
- "[STATUS] == 200"
` ) )
if err != nil {
t . Error ( "expected no error, got" , err . Error ( ) )
}
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
// Alerting providers
if config . Alerting == nil {
t . Fatal ( "config.Alerting shouldn't have been nil" )
}
if config . Alerting . Slack == nil || ! config . Alerting . Slack . IsValid ( ) {
t . Fatal ( "Slack alerting config should've been valid" )
}
// Services
if len ( config . Services ) != 1 {
t . Error ( "There should've been 2 services" )
}
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 [ 1 ] . Type != core . SlackAlert {
t . Errorf ( "The type of the alert should've been %s, but it was %s" , core . SlackAlert , config . Services [ 0 ] . Alerts [ 1 ] . Type )
}
if config . Services [ 0 ] . Alerts [ 2 ] . Type != core . SlackAlert {
t . Errorf ( "The type of the alert should've been %s, but it was %s" , core . SlackAlert , config . Services [ 0 ] . Alerts [ 2 ] . Type )
}
if ! config . Services [ 0 ] . Alerts [ 0 ] . IsEnabled ( ) {
t . Error ( "The alert should've been enabled" )
}
if ! config . Services [ 0 ] . Alerts [ 1 ] . IsEnabled ( ) {
t . Error ( "The alert should've been enabled" )
}
if config . Services [ 0 ] . Alerts [ 2 ] . IsEnabled ( ) {
t . Error ( "The alert should've been disabled" )
}
if config . Services [ 0 ] . Alerts [ 0 ] . GetDescription ( ) != "description" {
t . Errorf ( "The description of the alert should've been %s, but it was %s" , "description" , config . Services [ 0 ] . Alerts [ 0 ] . GetDescription ( ) )
}
if config . Services [ 0 ] . Alerts [ 1 ] . GetDescription ( ) != "wow" {
t . Errorf ( "The description of the alert should've been %s, but it was %s" , "description" , config . Services [ 0 ] . Alerts [ 1 ] . GetDescription ( ) )
}
if config . Services [ 0 ] . Alerts [ 2 ] . GetDescription ( ) != "description" {
t . Errorf ( "The description of the alert should've been %s, but it was %s" , "description" , config . Services [ 0 ] . Alerts [ 2 ] . GetDescription ( ) )
}
if config . Services [ 0 ] . Alerts [ 0 ] . FailureThreshold != 10 {
t . Errorf ( "The failure threshold of the alert should've been %d, but it was %d" , 10 , config . Services [ 0 ] . Alerts [ 0 ] . FailureThreshold )
2020-08-22 18:15:21 +00:00
}
2021-05-16 01:31:32 +00:00
if config . Services [ 0 ] . Alerts [ 1 ] . FailureThreshold != 20 {
t . Errorf ( "The failure threshold of the alert should've been %d, but it was %d" , 20 , config . Services [ 0 ] . Alerts [ 1 ] . FailureThreshold )
2021-03-05 02:26:17 +00:00
}
2021-05-16 01:31:32 +00:00
if config . Services [ 0 ] . Alerts [ 2 ] . FailureThreshold != 30 {
t . Errorf ( "The failure threshold of the alert should've been %d, but it was %d" , 30 , config . Services [ 0 ] . Alerts [ 2 ] . FailureThreshold )
2020-11-23 21:20:06 +00:00
}
2020-08-22 18:15:21 +00:00
}
2020-09-24 23:52:59 +00:00
func TestParseAndValidateConfigBytesWithInvalidPagerDutyAlertingConfig ( t * testing . T ) {
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
alerting :
pagerduty :
integration - key : "INVALID_KEY"
services :
- name : twinnation
2020-11-20 02:10:42 +00:00
url : https : //twinnation.org/health
2020-10-22 01:56:07 +00:00
alerts :
- type : pagerduty
2020-09-24 23:52:59 +00:00
conditions :
- "[STATUS] == 200"
` ) )
if err != nil {
2021-02-03 04:06:34 +00:00
t . Error ( "expected no error, got" , err . Error ( ) )
2020-09-24 23:52:59 +00:00
}
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
if config . Alerting == nil {
t . Fatal ( "config.Alerting shouldn't have been nil" )
}
if config . Alerting . PagerDuty == nil {
t . Fatal ( "PagerDuty alerting config shouldn't have been nil" )
}
if config . Alerting . PagerDuty . IsValid ( ) {
t . Fatal ( "PagerDuty alerting config should've been invalid" )
}
}
2020-10-14 23:22:58 +00:00
2020-11-13 20:01:21 +00:00
func TestParseAndValidateConfigBytesWithCustomAlertingConfig ( t * testing . T ) {
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
alerting :
custom :
url : "https://example.com"
body : |
{
"text" : "[ALERT_TRIGGERED_OR_RESOLVED]: [SERVICE_NAME] - [ALERT_DESCRIPTION]"
}
services :
- name : twinnation
2020-11-20 02:10:42 +00:00
url : https : //twinnation.org/health
2020-11-13 20:01:21 +00:00
alerts :
- type : custom
conditions :
- "[STATUS] == 200"
` ) )
if err != nil {
2021-02-03 04:06:34 +00:00
t . Error ( "expected no error, got" , err . Error ( ) )
2020-11-13 20:01:21 +00:00
}
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
if config . Alerting == nil {
t . Fatal ( "config.Alerting shouldn't have been nil" )
}
if config . Alerting . Custom == nil {
t . Fatal ( "PagerDuty alerting config shouldn't have been nil" )
}
if ! config . Alerting . Custom . IsValid ( ) {
t . Fatal ( "Custom alerting config should've been valid" )
}
2021-02-19 04:17:51 +00:00
if config . Alerting . Custom . GetAlertStatePlaceholderValue ( true ) != "RESOLVED" {
t . Fatal ( "ALERT_TRIGGERED_OR_RESOLVED placeholder value for RESOLVED should've been 'RESOLVED', got" , config . Alerting . Custom . GetAlertStatePlaceholderValue ( true ) )
}
if config . Alerting . Custom . GetAlertStatePlaceholderValue ( false ) != "TRIGGERED" {
t . Fatal ( "ALERT_TRIGGERED_OR_RESOLVED placeholder value for TRIGGERED should've been 'TRIGGERED', got" , config . Alerting . Custom . GetAlertStatePlaceholderValue ( false ) )
}
2020-11-13 20:01:21 +00:00
if config . Alerting . Custom . Insecure {
t . Fatal ( "config.Alerting.Custom.Insecure shouldn't have been true" )
}
}
2021-02-19 04:17:51 +00:00
func TestParseAndValidateConfigBytesWithCustomAlertingConfigAndCustomPlaceholderValues ( t * testing . T ) {
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
alerting :
custom :
placeholders :
ALERT_TRIGGERED_OR_RESOLVED :
TRIGGERED : "partial_outage"
RESOLVED : "operational"
url : "https://example.com"
insecure : true
body : "[ALERT_TRIGGERED_OR_RESOLVED]: [SERVICE_NAME] - [ALERT_DESCRIPTION]"
services :
- name : twinnation
url : https : //twinnation.org/health
alerts :
- type : custom
conditions :
- "[STATUS] == 200"
` ) )
if err != nil {
t . Error ( "expected no error, got" , err . Error ( ) )
}
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
if config . Alerting == nil {
t . Fatal ( "config.Alerting shouldn't have been nil" )
}
if config . Alerting . Custom == nil {
t . Fatal ( "PagerDuty alerting config shouldn't have been nil" )
}
if ! config . Alerting . Custom . IsValid ( ) {
t . Fatal ( "Custom alerting config should've been valid" )
}
if config . Alerting . Custom . GetAlertStatePlaceholderValue ( true ) != "operational" {
t . Fatal ( "ALERT_TRIGGERED_OR_RESOLVED placeholder value for RESOLVED should've been 'operational'" )
}
if config . Alerting . Custom . GetAlertStatePlaceholderValue ( false ) != "partial_outage" {
t . Fatal ( "ALERT_TRIGGERED_OR_RESOLVED placeholder value for TRIGGERED should've been 'partial_outage'" )
}
if ! config . Alerting . Custom . Insecure {
t . Fatal ( "config.Alerting.Custom.Insecure shouldn't have been true" )
}
}
func TestParseAndValidateConfigBytesWithCustomAlertingConfigAndOneCustomPlaceholderValue ( t * testing . T ) {
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
alerting :
custom :
placeholders :
ALERT_TRIGGERED_OR_RESOLVED :
TRIGGERED : "partial_outage"
url : "https://example.com"
insecure : true
body : "[ALERT_TRIGGERED_OR_RESOLVED]: [SERVICE_NAME] - [ALERT_DESCRIPTION]"
services :
- name : twinnation
url : https : //twinnation.org/health
alerts :
- type : custom
conditions :
- "[STATUS] == 200"
` ) )
if err != nil {
t . Error ( "expected no error, got" , err . Error ( ) )
}
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
if config . Alerting == nil {
t . Fatal ( "config.Alerting shouldn't have been nil" )
}
if config . Alerting . Custom == nil {
t . Fatal ( "PagerDuty alerting config shouldn't have been nil" )
}
if ! config . Alerting . Custom . IsValid ( ) {
t . Fatal ( "Custom alerting config should've been valid" )
}
if config . Alerting . Custom . GetAlertStatePlaceholderValue ( true ) != "RESOLVED" {
t . Fatal ( "ALERT_TRIGGERED_OR_RESOLVED placeholder value for RESOLVED should've been 'RESOLVED'" )
}
if config . Alerting . Custom . GetAlertStatePlaceholderValue ( false ) != "partial_outage" {
t . Fatal ( "ALERT_TRIGGERED_OR_RESOLVED placeholder value for TRIGGERED should've been 'partial_outage'" )
}
if ! config . Alerting . Custom . Insecure {
t . Fatal ( "config.Alerting.Custom.Insecure shouldn't have been true" )
}
}
2020-11-13 20:01:21 +00:00
func TestParseAndValidateConfigBytesWithCustomAlertingConfigThatHasInsecureSetToTrue ( t * testing . T ) {
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
alerting :
custom :
url : "https://example.com"
method : "POST"
insecure : true
body : |
{
"text" : "[ALERT_TRIGGERED_OR_RESOLVED]: [SERVICE_NAME] - [ALERT_DESCRIPTION]"
}
services :
- name : twinnation
2020-11-20 02:10:42 +00:00
url : https : //twinnation.org/health
2020-11-13 20:01:21 +00:00
alerts :
- type : custom
conditions :
- "[STATUS] == 200"
` ) )
if err != nil {
2021-02-03 04:06:34 +00:00
t . Error ( "expected no error, got" , err . Error ( ) )
2020-11-13 20:01:21 +00:00
}
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
if config . Alerting == nil {
t . Fatal ( "config.Alerting shouldn't have been nil" )
}
if config . Alerting . Custom == nil {
t . Fatal ( "PagerDuty alerting config shouldn't have been nil" )
}
if ! config . Alerting . Custom . IsValid ( ) {
t . Error ( "Custom alerting config should've been valid" )
}
if config . Alerting . Custom . Method != "POST" {
t . Error ( "config.Alerting.Custom.Method should've been POST" )
}
if ! config . Alerting . Custom . Insecure {
t . Error ( "config.Alerting.Custom.Insecure shouldn't have been true" )
}
}
2020-10-14 23:22:58 +00:00
func TestParseAndValidateConfigBytesWithInvalidSecurityConfig ( t * testing . T ) {
defer func ( ) { recover ( ) } ( )
_ , _ = parseAndValidateConfigBytes ( [ ] byte ( `
security :
basic :
username : "admin"
password - sha512 : "invalid-sha512-hash"
services :
- name : twinnation
2020-11-20 02:10:42 +00:00
url : https : //twinnation.org/health
2020-10-14 23:22:58 +00:00
conditions :
- "[STATUS] == 200"
` ) )
t . Error ( "Function should've panicked" )
}
func TestParseAndValidateConfigBytesWithValidSecurityConfig ( t * testing . T ) {
const expectedUsername = "admin"
const expectedPasswordHash = "6b97ed68d14eb3f1aa959ce5d49c7dc612e1eb1dafd73b1e705847483fd6a6c809f2ceb4e8df6ff9984c6298ff0285cace6614bf8daa9f0070101b6c89899e22"
config , err := parseAndValidateConfigBytes ( [ ] byte ( fmt . Sprintf ( `
security :
basic :
username : "%s"
password - sha512 : "%s"
services :
- name : twinnation
2020-11-20 02:10:42 +00:00
url : https : //twinnation.org/health
2020-10-14 23:22:58 +00:00
conditions :
- "[STATUS] == 200"
` , expectedUsername , expectedPasswordHash ) ) )
if err != nil {
2021-02-03 04:06:34 +00:00
t . Error ( "expected no error, got" , err . Error ( ) )
2020-10-14 23:22:58 +00:00
}
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
if config . Security == nil {
t . Fatal ( "config.Security shouldn't have been nil" )
}
if ! config . Security . IsValid ( ) {
t . Error ( "Security config should've been valid" )
}
if config . Security . Basic == nil {
t . Fatal ( "config.Security.Basic shouldn't have been nil" )
}
if config . Security . Basic . Username != expectedUsername {
t . Errorf ( "config.Security.Basic.Username should've been %s, but was %s" , expectedUsername , config . Security . Basic . Username )
}
if config . Security . Basic . PasswordSha512Hash != expectedPasswordHash {
t . Errorf ( "config.Security.Basic.PasswordSha512Hash should've been %s, but was %s" , expectedPasswordHash , config . Security . Basic . PasswordSha512Hash )
}
}
2020-11-11 23:05:18 +00:00
func TestParseAndValidateConfigBytesWithNoServicesOrAutoDiscovery ( t * testing . T ) {
_ , err := parseAndValidateConfigBytes ( [ ] byte ( ` ` ) )
if err != ErrNoServiceInConfig {
t . Error ( "The error returned should have been of type ErrNoServiceInConfig" )
}
}
func TestParseAndValidateConfigBytesWithKubernetesAutoDiscovery ( t * testing . T ) {
var kubernetesServices [ ] v1 . Service
2020-11-12 00:47:53 +00:00
kubernetesServices = append ( kubernetesServices , k8stest . CreateTestServices ( "service-1" , "default" , 8080 ) )
kubernetesServices = append ( kubernetesServices , k8stest . CreateTestServices ( "service-2" , "default" , 8080 ) )
kubernetesServices = append ( kubernetesServices , k8stest . CreateTestServices ( "service-2-canary" , "default" , 8080 ) )
kubernetesServices = append ( kubernetesServices , k8stest . CreateTestServices ( "service-3" , "kube-system" , 8080 ) )
kubernetesServices = append ( kubernetesServices , k8stest . CreateTestServices ( "service-4" , "tools" , 8080 ) )
kubernetesServices = append ( kubernetesServices , k8stest . CreateTestServices ( "service-5" , "tools" , 8080 ) )
kubernetesServices = append ( kubernetesServices , k8stest . CreateTestServices ( "service-6" , "tools" , 8080 ) )
kubernetesServices = append ( kubernetesServices , k8stest . CreateTestServices ( "service-7" , "metrics" , 8080 ) )
kubernetesServices = append ( kubernetesServices , k8stest . CreateTestServices ( "service-7-canary" , "metrics" , 8080 ) )
2020-11-11 23:05:18 +00:00
k8stest . InitializeMockedKubernetesClient ( kubernetesServices )
config , err := parseAndValidateConfigBytes ( [ ] byte ( `
debug : true
kubernetes :
cluster - mode : "mock"
auto - discover : true
excluded - service - suffixes :
- canary
service - template :
interval : 29 s
conditions :
- "[STATUS] == 200"
namespaces :
- name : default
hostname - suffix : ".default.svc.cluster.local"
target - path : "/health"
- name : tools
hostname - suffix : ".tools.svc.cluster.local"
target - path : "/health"
excluded - services :
- service - 6
- name : metrics
hostname - suffix : ".metrics.svc.cluster.local"
target - path : "/health"
` ) )
if err != nil {
2021-02-03 04:06:34 +00:00
t . Error ( "expected no error, got" , err . Error ( ) )
2020-11-11 23:05:18 +00:00
}
if config == nil {
t . Fatal ( "Config shouldn't have been nil" )
}
if config . Kubernetes == nil {
t . Fatal ( "Kuberbetes config shouldn't have been nil" )
}
if len ( config . Services ) != 5 {
t . Error ( "Expected 5 services to have been added through k8s auto discovery, got" , len ( config . Services ) )
}
for _ , service := range config . Services {
if service . Name == "service-2-canary" || service . Name == "service-7-canary" {
t . Errorf ( "service '%s' should've been excluded because excluded-service-suffixes has 'canary'" , service . Name )
} else if service . Name == "service-6" {
t . Errorf ( "service '%s' should've been excluded because excluded-services has 'service-6'" , service . Name )
} else if service . Name == "service-3" {
t . Errorf ( "service '%s' should've been excluded because the namespace 'kube-system' is not configured for auto discovery" , service . Name )
} else {
if service . Interval != 29 * time . Second {
t . Errorf ( "service '%s' should've had an interval of 29s, because the template is configured for it" , service . Name )
}
if len ( service . Conditions ) != 1 {
t . Errorf ( "service '%s' should've had 1 condition" , service . Name )
}
if len ( service . Conditions ) == 1 && * service . Conditions [ 0 ] != "[STATUS] == 200" {
t . Errorf ( "service '%s' should've had the condition '[STATUS] == 200', because the template is configured for it" , service . Name )
}
2020-11-12 00:47:53 +00:00
if ! strings . HasSuffix ( service . URL , ".svc.cluster.local:8080/health" ) {
t . Errorf ( "service '%s' should've had an URL with the suffix '.svc.cluster.local:8080/health'" , service . Name )
2020-11-11 23:05:18 +00:00
}
}
}
2020-11-11 23:11:30 +00:00
}
2020-11-11 23:05:18 +00:00
2020-11-11 23:11:30 +00:00
func TestParseAndValidateConfigBytesWithKubernetesAutoDiscoveryButNoServiceTemplate ( t * testing . T ) {
defer func ( ) { recover ( ) } ( )
_ , _ = parseAndValidateConfigBytes ( [ ] byte ( `
kubernetes :
cluster - mode : "mock"
auto - discover : true
namespaces :
- name : default
hostname - suffix : ".default.svc.cluster.local"
target - path : "/health"
` ) )
t . Error ( "Function should've panicked because providing a service-template is mandatory" )
2020-11-11 23:05:18 +00:00
}
2020-11-11 23:20:52 +00:00
func TestParseAndValidateConfigBytesWithKubernetesAutoDiscoveryUsingClusterModeIn ( t * testing . T ) {
defer func ( ) { recover ( ) } ( )
_ , _ = parseAndValidateConfigBytes ( [ ] byte ( `
kubernetes :
cluster - mode : "in"
auto - discover : true
service - template :
interval : 30 s
conditions :
- "[STATUS] == 200"
namespaces :
- name : default
hostname - suffix : ".default.svc.cluster.local"
target - path : "/health"
` ) )
// TODO: find a way to test this?
t . Error ( "Function should've panicked because testing with ClusterModeIn isn't supported" )
}
2021-03-05 02:26:17 +00:00
func TestGetAlertingProviderByAlertType ( t * testing . T ) {
cfg := & Config {
Alerting : & alerting . Config {
Custom : & custom . AlertProvider { } ,
Discord : & discord . AlertProvider { } ,
Mattermost : & mattermost . AlertProvider { } ,
Messagebird : & messagebird . AlertProvider { } ,
PagerDuty : & pagerduty . AlertProvider { } ,
Slack : & slack . AlertProvider { } ,
2021-03-30 23:38:34 +00:00
Telegram : & telegram . AlertProvider { } ,
2021-03-05 02:26:17 +00:00
Twilio : & twilio . AlertProvider { } ,
} ,
}
if GetAlertingProviderByAlertType ( cfg , core . CustomAlert ) != cfg . Alerting . Custom {
t . Error ( "expected Custom configuration" )
}
if GetAlertingProviderByAlertType ( cfg , core . DiscordAlert ) != cfg . Alerting . Discord {
t . Error ( "expected Discord configuration" )
}
if GetAlertingProviderByAlertType ( cfg , core . MattermostAlert ) != cfg . Alerting . Mattermost {
t . Error ( "expected Mattermost configuration" )
}
if GetAlertingProviderByAlertType ( cfg , core . MessagebirdAlert ) != cfg . Alerting . Messagebird {
t . Error ( "expected Messagebird configuration" )
}
if GetAlertingProviderByAlertType ( cfg , core . PagerDutyAlert ) != cfg . Alerting . PagerDuty {
t . Error ( "expected PagerDuty configuration" )
}
if GetAlertingProviderByAlertType ( cfg , core . SlackAlert ) != cfg . Alerting . Slack {
t . Error ( "expected Slack configuration" )
}
2021-03-30 23:38:34 +00:00
if GetAlertingProviderByAlertType ( cfg , core . TelegramAlert ) != cfg . Alerting . Telegram {
t . Error ( "expected Telegram configuration" )
}
2021-03-05 02:26:17 +00:00
if GetAlertingProviderByAlertType ( cfg , core . TwilioAlert ) != cfg . Alerting . Twilio {
t . Error ( "expected Twilio configuration" )
}
}