From 50435f40305e1a53206d5d1e42352469ee899a63 Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Fri, 19 Feb 2021 19:06:20 -0500 Subject: [PATCH] Improve tests for alerting providers --- alerting/provider/custom/custom_test.go | 2 +- alerting/provider/mattermost/mattermost.go | 2 +- .../provider/mattermost/mattermost_test.go | 28 ++++++++++++- .../provider/messagebird/messagebird_test.go | 28 ++++++++++++- alerting/provider/pagerduty/pagerduty_test.go | 28 ++++++++++++- alerting/provider/slack/slack.go | 2 +- alerting/provider/slack/slack_test.go | 28 ++++++++++++- alerting/provider/twilio/twilio_test.go | 39 ++++++++++++++----- 8 files changed, 136 insertions(+), 21 deletions(-) diff --git a/alerting/provider/custom/custom_test.go b/alerting/provider/custom/custom_test.go index 92ccd269..79750454 100644 --- a/alerting/provider/custom/custom_test.go +++ b/alerting/provider/custom/custom_test.go @@ -62,7 +62,7 @@ func TestAlertProvider_ToCustomAlertProvider(t *testing.T) { provider := AlertProvider{URL: "http://example.com"} customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true) if customAlertProvider == nil { - t.Error("customAlertProvider shouldn't have been nil") + t.Fatal("customAlertProvider shouldn't have been nil") } if customAlertProvider != customAlertProvider { t.Error("customAlertProvider should've been equal to customAlertProvider") diff --git a/alerting/provider/mattermost/mattermost.go b/alerting/provider/mattermost/mattermost.go index 7f4f470c..11f4e827 100644 --- a/alerting/provider/mattermost/mattermost.go +++ b/alerting/provider/mattermost/mattermost.go @@ -38,7 +38,7 @@ func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, aler } else { prefix = ":x:" } - results += fmt.Sprintf("%s - `%s`\n", prefix, conditionResult.Condition) + results += fmt.Sprintf("%s - `%s`\\n", prefix, conditionResult.Condition) } return &custom.AlertProvider{ URL: provider.WebhookURL, diff --git a/alerting/provider/mattermost/mattermost_test.go b/alerting/provider/mattermost/mattermost_test.go index d9bbb557..c040df94 100644 --- a/alerting/provider/mattermost/mattermost_test.go +++ b/alerting/provider/mattermost/mattermost_test.go @@ -1,6 +1,8 @@ package mattermost import ( + "encoding/json" + "net/http" "strings" "testing" @@ -22,20 +24,42 @@ func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) { provider := AlertProvider{WebhookURL: "http://example.com"} customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "SUCCESSFUL_CONDITION", Success: true}}}, true) if customAlertProvider == nil { - t.Error("customAlertProvider shouldn't have been nil") + t.Fatal("customAlertProvider shouldn't have been nil") } if !strings.Contains(customAlertProvider.Body, "resolved") { t.Error("customAlertProvider.Body should've contained the substring resolved") } + if customAlertProvider.URL != "http://example.com" { + t.Errorf("expected URL to be %s, got %s", "http://example.com", customAlertProvider.URL) + } + if customAlertProvider.Method != http.MethodPost { + t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method) + } + body := make(map[string]interface{}) + err := json.Unmarshal([]byte(customAlertProvider.Body), &body) + if err != nil { + t.Error("expected body to be valid JSON, got error:", err.Error()) + } } func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) { provider := AlertProvider{WebhookURL: "http://example.com"} customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "UNSUCCESSFUL_CONDITION", Success: false}}}, false) if customAlertProvider == nil { - t.Error("customAlertProvider shouldn't have been nil") + t.Fatal("customAlertProvider shouldn't have been nil") } if !strings.Contains(customAlertProvider.Body, "triggered") { t.Error("customAlertProvider.Body should've contained the substring triggered") } + if customAlertProvider.URL != "http://example.com" { + t.Errorf("expected URL to be %s, got %s", "http://example.com", customAlertProvider.URL) + } + if customAlertProvider.Method != http.MethodPost { + t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method) + } + body := make(map[string]interface{}) + err := json.Unmarshal([]byte(customAlertProvider.Body), &body) + if err != nil { + t.Error("expected body to be valid JSON, got error:", err.Error()) + } } diff --git a/alerting/provider/messagebird/messagebird_test.go b/alerting/provider/messagebird/messagebird_test.go index 9a9d4b10..85188941 100644 --- a/alerting/provider/messagebird/messagebird_test.go +++ b/alerting/provider/messagebird/messagebird_test.go @@ -1,6 +1,8 @@ package messagebird import ( + "encoding/json" + "net/http" "strings" "testing" @@ -30,11 +32,22 @@ func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) { } customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true) if customAlertProvider == nil { - t.Error("customAlertProvider shouldn't have been nil") + t.Fatal("customAlertProvider shouldn't have been nil") } if !strings.Contains(customAlertProvider.Body, "RESOLVED") { t.Error("customAlertProvider.Body should've contained the substring RESOLVED") } + if customAlertProvider.URL != "https://rest.messagebird.com/messages" { + t.Errorf("expected URL to be %s, got %s", "https://rest.messagebird.com/messages", customAlertProvider.URL) + } + if customAlertProvider.Method != http.MethodPost { + t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method) + } + body := make(map[string]interface{}) + err := json.Unmarshal([]byte(customAlertProvider.Body), &body) + if err != nil { + t.Error("expected body to be valid JSON, got error:", err.Error()) + } } func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) { @@ -45,9 +58,20 @@ func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) { } customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, false) if customAlertProvider == nil { - t.Error("customAlertProvider shouldn't have been nil") + t.Fatal("customAlertProvider shouldn't have been nil") } if !strings.Contains(customAlertProvider.Body, "TRIGGERED") { t.Error("customAlertProvider.Body should've contained the substring TRIGGERED") } + if customAlertProvider.URL != "https://rest.messagebird.com/messages" { + t.Errorf("expected URL to be %s, got %s", "https://rest.messagebird.com/messages", customAlertProvider.URL) + } + if customAlertProvider.Method != http.MethodPost { + t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method) + } + body := make(map[string]interface{}) + err := json.Unmarshal([]byte(customAlertProvider.Body), &body) + if err != nil { + t.Error("expected body to be valid JSON, got error:", err.Error()) + } } diff --git a/alerting/provider/pagerduty/pagerduty_test.go b/alerting/provider/pagerduty/pagerduty_test.go index 0b246d50..4a0f60ed 100644 --- a/alerting/provider/pagerduty/pagerduty_test.go +++ b/alerting/provider/pagerduty/pagerduty_test.go @@ -1,6 +1,8 @@ package pagerduty import ( + "encoding/json" + "net/http" "strings" "testing" @@ -22,20 +24,42 @@ func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) { provider := AlertProvider{IntegrationKey: "00000000000000000000000000000000"} customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true) if customAlertProvider == nil { - t.Error("customAlertProvider shouldn't have been nil") + t.Fatal("customAlertProvider shouldn't have been nil") } if !strings.Contains(customAlertProvider.Body, "RESOLVED") { t.Error("customAlertProvider.Body should've contained the substring RESOLVED") } + if customAlertProvider.URL != "https://events.pagerduty.com/v2/enqueue" { + t.Errorf("expected URL to be %s, got %s", "https://events.pagerduty.com/v2/enqueue", customAlertProvider.URL) + } + if customAlertProvider.Method != http.MethodPost { + t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method) + } + body := make(map[string]interface{}) + err := json.Unmarshal([]byte(customAlertProvider.Body), &body) + if err != nil { + t.Error("expected body to be valid JSON, got error:", err.Error()) + } } func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) { provider := AlertProvider{IntegrationKey: "00000000000000000000000000000000"} customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, false) if customAlertProvider == nil { - t.Error("customAlertProvider shouldn't have been nil") + t.Fatal("customAlertProvider shouldn't have been nil") } if !strings.Contains(customAlertProvider.Body, "TRIGGERED") { t.Error("customAlertProvider.Body should've contained the substring TRIGGERED") } + if customAlertProvider.URL != "https://events.pagerduty.com/v2/enqueue" { + t.Errorf("expected URL to be %s, got %s", "https://events.pagerduty.com/v2/enqueue", customAlertProvider.URL) + } + if customAlertProvider.Method != http.MethodPost { + t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method) + } + body := make(map[string]interface{}) + err := json.Unmarshal([]byte(customAlertProvider.Body), &body) + if err != nil { + t.Error("expected body to be valid JSON, got error:", err.Error()) + } } diff --git a/alerting/provider/slack/slack.go b/alerting/provider/slack/slack.go index 655f9f70..3a81a0ad 100644 --- a/alerting/provider/slack/slack.go +++ b/alerting/provider/slack/slack.go @@ -35,7 +35,7 @@ func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, aler } else { prefix = ":x:" } - results += fmt.Sprintf("%s - `%s`\n", prefix, conditionResult.Condition) + results += fmt.Sprintf("%s - `%s`\\n", prefix, conditionResult.Condition) } return &custom.AlertProvider{ URL: provider.WebhookURL, diff --git a/alerting/provider/slack/slack_test.go b/alerting/provider/slack/slack_test.go index 4f1c8d2d..ed74dd5b 100644 --- a/alerting/provider/slack/slack_test.go +++ b/alerting/provider/slack/slack_test.go @@ -1,6 +1,8 @@ package slack import ( + "encoding/json" + "net/http" "strings" "testing" @@ -22,20 +24,42 @@ func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) { provider := AlertProvider{WebhookURL: "http://example.com"} customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "SUCCESSFUL_CONDITION", Success: true}}}, true) if customAlertProvider == nil { - t.Error("customAlertProvider shouldn't have been nil") + t.Fatal("customAlertProvider shouldn't have been nil") } if !strings.Contains(customAlertProvider.Body, "resolved") { t.Error("customAlertProvider.Body should've contained the substring resolved") } + if customAlertProvider.URL != "http://example.com" { + t.Errorf("expected URL to be %s, got %s", "http://example.com", customAlertProvider.URL) + } + if customAlertProvider.Method != http.MethodPost { + t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method) + } + body := make(map[string]interface{}) + err := json.Unmarshal([]byte(customAlertProvider.Body), &body) + if err != nil { + t.Error("expected body to be valid JSON, got error:", err.Error()) + } } func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) { provider := AlertProvider{WebhookURL: "http://example.com"} customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "UNSUCCESSFUL_CONDITION", Success: false}}}, false) if customAlertProvider == nil { - t.Error("customAlertProvider shouldn't have been nil") + t.Fatal("customAlertProvider shouldn't have been nil") } if !strings.Contains(customAlertProvider.Body, "triggered") { t.Error("customAlertProvider.Body should've contained the substring triggered") } + if customAlertProvider.URL != "http://example.com" { + t.Errorf("expected URL to be %s, got %s", "http://example.com", customAlertProvider.URL) + } + if customAlertProvider.Method != http.MethodPost { + t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method) + } + body := make(map[string]interface{}) + err := json.Unmarshal([]byte(customAlertProvider.Body), &body) + if err != nil { + t.Error("expected body to be valid JSON, got error:", err.Error()) + } } diff --git a/alerting/provider/twilio/twilio_test.go b/alerting/provider/twilio/twilio_test.go index 661948fa..46dcfc49 100644 --- a/alerting/provider/twilio/twilio_test.go +++ b/alerting/provider/twilio/twilio_test.go @@ -1,6 +1,7 @@ package twilio import ( + "net/http" "strings" "testing" @@ -26,31 +27,49 @@ func TestTwilioAlertProvider_IsValid(t *testing.T) { func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) { provider := AlertProvider{ SID: "1", - Token: "1", - From: "1", - To: "1", + Token: "2", + From: "3", + To: "4", } - customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true) + customAlertProvider := provider.ToCustomAlertProvider(&core.Service{Name: "service-name"}, &core.Alert{Description: "alert-description"}, &core.Result{}, true) if customAlertProvider == nil { - t.Error("customAlertProvider shouldn't have been nil") + t.Fatal("customAlertProvider shouldn't have been nil") } if !strings.Contains(customAlertProvider.Body, "RESOLVED") { t.Error("customAlertProvider.Body should've contained the substring RESOLVED") } + if customAlertProvider.URL != "https://api.twilio.com/2010-04-01/Accounts/1/Messages.json" { + t.Errorf("expected URL to be %s, got %s", "https://api.twilio.com/2010-04-01/Accounts/1/Messages.json", customAlertProvider.URL) + } + if customAlertProvider.Method != http.MethodPost { + t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method) + } + if customAlertProvider.Body != "Body=RESOLVED%3A+service-name+-+alert-description&From=3&To=4" { + t.Errorf("expected body to be %s, got %s", "Body=RESOLVED%3A+service-name+-+alert-description&From=3&To=4", customAlertProvider.Body) + } } func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) { provider := AlertProvider{ - SID: "1", - Token: "1", - From: "1", + SID: "4", + Token: "3", + From: "2", To: "1", } - customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, false) + customAlertProvider := provider.ToCustomAlertProvider(&core.Service{Name: "service-name"}, &core.Alert{Description: "alert-description"}, &core.Result{}, false) if customAlertProvider == nil { - t.Error("customAlertProvider shouldn't have been nil") + t.Fatal("customAlertProvider shouldn't have been nil") } if !strings.Contains(customAlertProvider.Body, "TRIGGERED") { t.Error("customAlertProvider.Body should've contained the substring TRIGGERED") } + if customAlertProvider.URL != "https://api.twilio.com/2010-04-01/Accounts/4/Messages.json" { + t.Errorf("expected URL to be %s, got %s", "https://api.twilio.com/2010-04-01/Accounts/4/Messages.json", customAlertProvider.URL) + } + if customAlertProvider.Method != http.MethodPost { + t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method) + } + if customAlertProvider.Body != "Body=TRIGGERED%3A+service-name+-+alert-description&From=2&To=1" { + t.Errorf("expected body to be %s, got %s", "Body=TRIGGERED%3A+service-name+-+alert-description&From=2&To=1", customAlertProvider.Body) + } }