feat(alerting): Add new providers for Datadog, IFTTT, Line, NewRelic, Plivo, RocketChat, SendGrid, Signal, SIGNL4, Splunk, Squadcast, Vonage, Webex and Zapier (#1224)
* feat(alerting): Add new providers for Datadog, IFTTT, Line, NewRelic, Plivo, RocketChat, SendGrid, Signal, SIGNL4, Splunk, Squadcast, Vonage, Webex and Zapier Relevant: https://github.com/TwiN/gatus/discussions/1223 Fixes #1073 Fixes #1074 * chore: Clean up code * docs: Fix table formatting * Update alerting/provider/datadog/datadog.go * Update alerting/provider/signal/signal.go * Update alerting/provider/ifttt/ifttt.go * Update alerting/provider/newrelic/newrelic.go * Update alerting/provider/squadcast/squadcast.go * Update alerting/provider/squadcast/squadcast.go
This commit is contained in:
212
alerting/provider/vonage/vonage.go
Normal file
212
alerting/provider/vonage/vonage.go
Normal file
@@ -0,0 +1,212 @@
|
||||
package vonage
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/TwiN/gatus/v5/alerting/alert"
|
||||
"github.com/TwiN/gatus/v5/client"
|
||||
"github.com/TwiN/gatus/v5/config/endpoint"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const ApiURL = "https://rest.nexmo.com/sms/json"
|
||||
|
||||
var (
|
||||
ErrAPIKeyNotSet = errors.New("api-key not set")
|
||||
ErrAPISecretNotSet = errors.New("api-secret not set")
|
||||
ErrFromNotSet = errors.New("from not set")
|
||||
ErrToNotSet = errors.New("to not set")
|
||||
ErrDuplicateGroupOverride = errors.New("duplicate group override")
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
APIKey string `yaml:"api-key"`
|
||||
APISecret string `yaml:"api-secret"`
|
||||
From string `yaml:"from"`
|
||||
To []string `yaml:"to"`
|
||||
}
|
||||
|
||||
func (cfg *Config) Validate() error {
|
||||
if len(cfg.APIKey) == 0 {
|
||||
return ErrAPIKeyNotSet
|
||||
}
|
||||
if len(cfg.APISecret) == 0 {
|
||||
return ErrAPISecretNotSet
|
||||
}
|
||||
if len(cfg.From) == 0 {
|
||||
return ErrFromNotSet
|
||||
}
|
||||
if len(cfg.To) == 0 {
|
||||
return ErrToNotSet
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cfg *Config) Merge(override *Config) {
|
||||
if len(override.APIKey) > 0 {
|
||||
cfg.APIKey = override.APIKey
|
||||
}
|
||||
if len(override.APISecret) > 0 {
|
||||
cfg.APISecret = override.APISecret
|
||||
}
|
||||
if len(override.From) > 0 {
|
||||
cfg.From = override.From
|
||||
}
|
||||
if len(override.To) > 0 {
|
||||
cfg.To = override.To
|
||||
}
|
||||
}
|
||||
|
||||
// AlertProvider is the configuration necessary for sending an alert using Vonage
|
||||
type AlertProvider struct {
|
||||
DefaultConfig Config `yaml:",inline"`
|
||||
|
||||
// DefaultAlert is the default alert configuration to use for endpoints with an alert of the appropriate type
|
||||
DefaultAlert *alert.Alert `yaml:"default-alert,omitempty"`
|
||||
|
||||
// Overrides is a list of Override that may be prioritized over the default configuration
|
||||
Overrides []Override `yaml:"overrides,omitempty"`
|
||||
}
|
||||
|
||||
// Override is a case under which the default integration is overridden
|
||||
type Override struct {
|
||||
Group string `yaml:"group"`
|
||||
Config `yaml:",inline"`
|
||||
}
|
||||
|
||||
// Validate the provider's configuration
|
||||
func (provider *AlertProvider) Validate() error {
|
||||
registeredGroups := make(map[string]bool)
|
||||
if provider.Overrides != nil {
|
||||
for _, override := range provider.Overrides {
|
||||
if isAlreadyRegistered := registeredGroups[override.Group]; isAlreadyRegistered || override.Group == "" {
|
||||
return ErrDuplicateGroupOverride
|
||||
}
|
||||
registeredGroups[override.Group] = true
|
||||
}
|
||||
}
|
||||
return provider.DefaultConfig.Validate()
|
||||
}
|
||||
|
||||
// Send an alert using the provider
|
||||
func (provider *AlertProvider) Send(ep *endpoint.Endpoint, alert *alert.Alert, result *endpoint.Result, resolved bool) error {
|
||||
cfg, err := provider.GetConfig(ep.Group, alert)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
message := provider.buildMessage(cfg, ep, alert, result, resolved)
|
||||
|
||||
// Send SMS to each recipient
|
||||
for _, recipient := range cfg.To {
|
||||
if err := provider.sendSMS(cfg, recipient, message); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendSMS sends an individual SMS message
|
||||
func (provider *AlertProvider) sendSMS(cfg *Config, to, message string) error {
|
||||
data := url.Values{}
|
||||
data.Set("api_key", cfg.APIKey)
|
||||
data.Set("api_secret", cfg.APISecret)
|
||||
data.Set("from", cfg.From)
|
||||
data.Set("to", to)
|
||||
data.Set("text", message)
|
||||
request, err := http.NewRequest(http.MethodPost, ApiURL, strings.NewReader(data.Encode()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
response, err := client.GetHTTPClient(nil).Do(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
// Read response body once and use it for both error handling and JSON processing
|
||||
body, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.StatusCode >= 400 {
|
||||
return fmt.Errorf("call to vonage alert returned status code %d: %s", response.StatusCode, string(body))
|
||||
}
|
||||
// Check response for errors in messages array
|
||||
var vonageResponse Response
|
||||
if err := json.Unmarshal(body, &vonageResponse); err != nil {
|
||||
return err
|
||||
}
|
||||
// Check if any message failed
|
||||
for _, msg := range vonageResponse.Messages {
|
||||
if msg.Status != "0" {
|
||||
return fmt.Errorf("vonage SMS failed with status %s: %s", msg.Status, msg.ErrorText)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
MessageCount string `json:"message-count"`
|
||||
Messages []Message `json:"messages"`
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
To string `json:"to"`
|
||||
MessageID string `json:"message-id"`
|
||||
Status string `json:"status"`
|
||||
ErrorText string `json:"error-text"`
|
||||
RemainingBalance string `json:"remaining-balance"`
|
||||
MessagePrice string `json:"message-price"`
|
||||
Network string `json:"network"`
|
||||
}
|
||||
|
||||
// buildMessage builds the SMS message content
|
||||
func (provider *AlertProvider) buildMessage(cfg *Config, ep *endpoint.Endpoint, alert *alert.Alert, result *endpoint.Result, resolved bool) string {
|
||||
if resolved {
|
||||
return fmt.Sprintf("RESOLVED: %s - %s", ep.DisplayName(), alert.GetDescription())
|
||||
} else {
|
||||
return fmt.Sprintf("TRIGGERED: %s - %s", ep.DisplayName(), alert.GetDescription())
|
||||
}
|
||||
}
|
||||
|
||||
// GetDefaultAlert returns the provider's default alert configuration
|
||||
func (provider *AlertProvider) GetDefaultAlert() *alert.Alert {
|
||||
return provider.DefaultAlert
|
||||
}
|
||||
|
||||
// GetConfig returns the configuration for the provider with the overrides applied
|
||||
func (provider *AlertProvider) GetConfig(group string, alert *alert.Alert) (*Config, error) {
|
||||
cfg := provider.DefaultConfig
|
||||
// Handle group overrides
|
||||
if provider.Overrides != nil {
|
||||
for _, override := range provider.Overrides {
|
||||
if group == override.Group {
|
||||
cfg.Merge(&override.Config)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// Handle alert overrides
|
||||
if len(alert.ProviderOverride) != 0 {
|
||||
overrideConfig := Config{}
|
||||
if err := yaml.Unmarshal(alert.ProviderOverrideAsBytes(), &overrideConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.Merge(&overrideConfig)
|
||||
}
|
||||
// Validate the configuration
|
||||
err := cfg.Validate()
|
||||
return &cfg, err
|
||||
}
|
||||
|
||||
// ValidateOverrides validates the alert's provider override and, if present, the group override
|
||||
func (provider *AlertProvider) ValidateOverrides(group string, alert *alert.Alert) error {
|
||||
_, err := provider.GetConfig(group, alert)
|
||||
return err
|
||||
}
|
||||
546
alerting/provider/vonage/vonage_test.go
Normal file
546
alerting/provider/vonage/vonage_test.go
Normal file
@@ -0,0 +1,546 @@
|
||||
package vonage
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/TwiN/gatus/v5/alerting/alert"
|
||||
"github.com/TwiN/gatus/v5/client"
|
||||
"github.com/TwiN/gatus/v5/config/endpoint"
|
||||
"github.com/TwiN/gatus/v5/test"
|
||||
)
|
||||
|
||||
func TestVonageAlertProvider_IsValid(t *testing.T) {
|
||||
invalidProvider := AlertProvider{}
|
||||
if err := invalidProvider.Validate(); err == nil {
|
||||
t.Error("provider shouldn't have been valid")
|
||||
}
|
||||
validProvider := AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
}
|
||||
if err := validProvider.Validate(); err != nil {
|
||||
t.Error("provider should've been valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVonageAlertProvider_IsValidWithOverride(t *testing.T) {
|
||||
validProvider := AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
Overrides: []Override{
|
||||
{
|
||||
Group: "test-group",
|
||||
Config: Config{
|
||||
APIKey: "override-key",
|
||||
APISecret: "override-secret",
|
||||
From: "Override",
|
||||
To: []string{"+9876543210"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
if err := validProvider.Validate(); err != nil {
|
||||
t.Error("provider should've been valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVonageAlertProvider_IsNotValidWithInvalidOverrideGroup(t *testing.T) {
|
||||
invalidProvider := AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
Overrides: []Override{
|
||||
{
|
||||
Group: "",
|
||||
Config: Config{
|
||||
APIKey: "override-key",
|
||||
APISecret: "override-secret",
|
||||
From: "Override",
|
||||
To: []string{"+9876543210"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
if err := invalidProvider.Validate(); err == nil {
|
||||
t.Error("provider shouldn't have been valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVonageAlertProvider_IsNotValidWithDuplicateOverrideGroup(t *testing.T) {
|
||||
invalidProvider := AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
Overrides: []Override{
|
||||
{
|
||||
Group: "test-group",
|
||||
Config: Config{
|
||||
APIKey: "override-key1",
|
||||
APISecret: "override-secret1",
|
||||
From: "Override1",
|
||||
To: []string{"+9876543210"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Group: "test-group",
|
||||
Config: Config{
|
||||
APIKey: "override-key2",
|
||||
APISecret: "override-secret2",
|
||||
From: "Override2",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
if err := invalidProvider.Validate(); err == nil {
|
||||
t.Error("provider shouldn't have been valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVonageAlertProvider_IsValidWithInvalidFrom(t *testing.T) {
|
||||
invalidProvider := AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
}
|
||||
if err := invalidProvider.Validate(); err == nil {
|
||||
t.Error("provider shouldn't have been valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVonageAlertProvider_IsValidWithInvalidTo(t *testing.T) {
|
||||
invalidProvider := AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{},
|
||||
},
|
||||
}
|
||||
if err := invalidProvider.Validate(); err == nil {
|
||||
t.Error("provider shouldn't have been valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlertProvider_Send(t *testing.T) {
|
||||
defer client.InjectHTTPClient(nil)
|
||||
firstDescription := "description-1"
|
||||
secondDescription := "description-2"
|
||||
scenarios := []struct {
|
||||
Name string
|
||||
Provider AlertProvider
|
||||
Alert alert.Alert
|
||||
Resolved bool
|
||||
MockRoundTripper test.MockRoundTripper
|
||||
ExpectedError bool
|
||||
}{
|
||||
{
|
||||
Name: "triggered",
|
||||
Provider: AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
},
|
||||
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
||||
Resolved: false,
|
||||
MockRoundTripper: test.MockRoundTripper(func(r *http.Request) *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(strings.NewReader(`{"message-count":"1","messages":[{"to":"+1234567890","message-id":"test-id","status":"0","remaining-balance":"10.50","message-price":"0.10","network":"12345"}]}`)),
|
||||
}
|
||||
}),
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "triggered-error-status-code",
|
||||
Provider: AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
},
|
||||
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
||||
Resolved: false,
|
||||
MockRoundTripper: test.MockRoundTripper(func(r *http.Request) *http.Response {
|
||||
return &http.Response{StatusCode: http.StatusInternalServerError, Body: http.NoBody}
|
||||
}),
|
||||
ExpectedError: true,
|
||||
},
|
||||
{
|
||||
Name: "triggered-error-vonage-response",
|
||||
Provider: AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
},
|
||||
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
||||
Resolved: false,
|
||||
MockRoundTripper: test.MockRoundTripper(func(r *http.Request) *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(strings.NewReader(`{"message-count":"1","messages":[{"to":"+1234567890","message-id":"","status":"2","error-text":"Missing from param"}]}`)),
|
||||
}
|
||||
}),
|
||||
ExpectedError: true,
|
||||
},
|
||||
{
|
||||
Name: "resolved",
|
||||
Provider: AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
},
|
||||
Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
||||
Resolved: true,
|
||||
MockRoundTripper: test.MockRoundTripper(func(r *http.Request) *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(strings.NewReader(`{"message-count":"1","messages":[{"to":"+1234567890","message-id":"test-id","status":"0","remaining-balance":"10.40","message-price":"0.10","network":"12345"}]}`)),
|
||||
}
|
||||
}),
|
||||
ExpectedError: false,
|
||||
},
|
||||
{
|
||||
Name: "multiple-recipients",
|
||||
Provider: AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890", "+0987654321"},
|
||||
},
|
||||
},
|
||||
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
||||
Resolved: false,
|
||||
MockRoundTripper: test.MockRoundTripper(func(r *http.Request) *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(strings.NewReader(`{"message-count":"1","messages":[{"to":"+1234567890","message-id":"test-id","status":"0","remaining-balance":"10.30","message-price":"0.10","network":"12345"}]}`)),
|
||||
}
|
||||
}),
|
||||
ExpectedError: false,
|
||||
},
|
||||
}
|
||||
for _, scenario := range scenarios {
|
||||
t.Run(scenario.Name, func(t *testing.T) {
|
||||
client.InjectHTTPClient(&http.Client{Transport: scenario.MockRoundTripper})
|
||||
err := scenario.Provider.Send(
|
||||
&endpoint.Endpoint{Name: "endpoint-name"},
|
||||
&scenario.Alert,
|
||||
&endpoint.Result{
|
||||
ConditionResults: []*endpoint.ConditionResult{
|
||||
{Condition: "[CONNECTED] == true", Success: scenario.Resolved},
|
||||
{Condition: "[STATUS] == 200", Success: scenario.Resolved},
|
||||
},
|
||||
},
|
||||
scenario.Resolved,
|
||||
)
|
||||
if scenario.ExpectedError && err == nil {
|
||||
t.Error("expected error, got none")
|
||||
}
|
||||
if !scenario.ExpectedError && err != nil {
|
||||
t.Error("expected no error, got", err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlertProvider_buildMessage(t *testing.T) {
|
||||
firstDescription := "description-1"
|
||||
secondDescription := "description-2"
|
||||
scenarios := []struct {
|
||||
Name string
|
||||
Provider AlertProvider
|
||||
Alert alert.Alert
|
||||
Resolved bool
|
||||
ExpectedMessage string
|
||||
}{
|
||||
{
|
||||
Name: "triggered",
|
||||
Provider: AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
},
|
||||
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
||||
Resolved: false,
|
||||
ExpectedMessage: "TRIGGERED: endpoint-name - description-1",
|
||||
},
|
||||
{
|
||||
Name: "resolved",
|
||||
Provider: AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
},
|
||||
Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3},
|
||||
Resolved: true,
|
||||
ExpectedMessage: "RESOLVED: endpoint-name - description-2",
|
||||
},
|
||||
}
|
||||
for _, scenario := range scenarios {
|
||||
t.Run(scenario.Name, func(t *testing.T) {
|
||||
message := scenario.Provider.buildMessage(
|
||||
&scenario.Provider.DefaultConfig,
|
||||
&endpoint.Endpoint{Name: "endpoint-name"},
|
||||
&scenario.Alert,
|
||||
&endpoint.Result{
|
||||
ConditionResults: []*endpoint.ConditionResult{
|
||||
{Condition: "[CONNECTED] == true", Success: scenario.Resolved},
|
||||
{Condition: "[STATUS] == 200", Success: scenario.Resolved},
|
||||
},
|
||||
},
|
||||
scenario.Resolved,
|
||||
)
|
||||
if message != scenario.ExpectedMessage {
|
||||
t.Errorf("expected %s, got %s", scenario.ExpectedMessage, message)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlertProvider_GetDefaultAlert(t *testing.T) {
|
||||
if (&AlertProvider{DefaultAlert: &alert.Alert{}}).GetDefaultAlert() == nil {
|
||||
t.Error("expected default alert to be not nil")
|
||||
}
|
||||
if (&AlertProvider{DefaultAlert: nil}).GetDefaultAlert() != nil {
|
||||
t.Error("expected default alert to be nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlertProvider_GetConfig(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
Name string
|
||||
Provider AlertProvider
|
||||
InputGroup string
|
||||
InputAlert alert.Alert
|
||||
ExpectedOutput Config
|
||||
}{
|
||||
{
|
||||
Name: "provider-no-override-should-default",
|
||||
Provider: AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
},
|
||||
InputGroup: "",
|
||||
InputAlert: alert.Alert{},
|
||||
ExpectedOutput: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "provider-with-group-override",
|
||||
Provider: AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
Overrides: []Override{
|
||||
{
|
||||
Group: "test-group",
|
||||
Config: Config{
|
||||
APIKey: "group-override-key",
|
||||
APISecret: "group-override-secret",
|
||||
From: "GroupOverride",
|
||||
To: []string{"+9876543210"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
InputGroup: "test-group",
|
||||
InputAlert: alert.Alert{},
|
||||
ExpectedOutput: Config{
|
||||
APIKey: "group-override-key",
|
||||
APISecret: "group-override-secret",
|
||||
From: "GroupOverride",
|
||||
To: []string{"+9876543210"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "provider-with-group-override-partial",
|
||||
Provider: AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
Overrides: []Override{
|
||||
{
|
||||
Group: "test-group",
|
||||
Config: Config{
|
||||
To: []string{"+9876543210"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
InputGroup: "test-group",
|
||||
InputAlert: alert.Alert{},
|
||||
ExpectedOutput: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+9876543210"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "provider-with-alert-override",
|
||||
Provider: AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
},
|
||||
InputGroup: "",
|
||||
InputAlert: alert.Alert{ProviderOverride: map[string]any{
|
||||
"api-key": "override-key",
|
||||
"api-secret": "override-secret",
|
||||
"from": "Override",
|
||||
"to": []string{"+9876543210"},
|
||||
}},
|
||||
ExpectedOutput: Config{
|
||||
APIKey: "override-key",
|
||||
APISecret: "override-secret",
|
||||
From: "Override",
|
||||
To: []string{"+9876543210"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "provider-with-both-group-and-alert-override",
|
||||
Provider: AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
Overrides: []Override{
|
||||
{
|
||||
Group: "test-group",
|
||||
Config: Config{
|
||||
APIKey: "group-override-key",
|
||||
From: "GroupOverride",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
InputGroup: "test-group",
|
||||
InputAlert: alert.Alert{ProviderOverride: map[string]any{
|
||||
"api-secret": "alert-override-secret",
|
||||
"to": []string{"+9876543210"},
|
||||
}},
|
||||
ExpectedOutput: Config{
|
||||
APIKey: "group-override-key",
|
||||
APISecret: "alert-override-secret",
|
||||
From: "GroupOverride",
|
||||
To: []string{"+9876543210"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "provider-with-group-override-no-match",
|
||||
Provider: AlertProvider{
|
||||
DefaultConfig: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
Overrides: []Override{
|
||||
{
|
||||
Group: "different-group",
|
||||
Config: Config{
|
||||
APIKey: "group-override-key",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
InputGroup: "test-group",
|
||||
InputAlert: alert.Alert{},
|
||||
ExpectedOutput: Config{
|
||||
APIKey: "test-key",
|
||||
APISecret: "test-secret",
|
||||
From: "Gatus",
|
||||
To: []string{"+1234567890"},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, scenario := range scenarios {
|
||||
t.Run(scenario.Name, func(t *testing.T) {
|
||||
got, err := scenario.Provider.GetConfig(scenario.InputGroup, &scenario.InputAlert)
|
||||
if err != nil {
|
||||
t.Error("expected no error, got:", err.Error())
|
||||
}
|
||||
if got.APIKey != scenario.ExpectedOutput.APIKey {
|
||||
t.Errorf("expected APIKey to be %s, got %s", scenario.ExpectedOutput.APIKey, got.APIKey)
|
||||
}
|
||||
if got.APISecret != scenario.ExpectedOutput.APISecret {
|
||||
t.Errorf("expected APISecret to be %s, got %s", scenario.ExpectedOutput.APISecret, got.APISecret)
|
||||
}
|
||||
if got.From != scenario.ExpectedOutput.From {
|
||||
t.Errorf("expected From to be %s, got %s", scenario.ExpectedOutput.From, got.From)
|
||||
}
|
||||
if len(got.To) != len(scenario.ExpectedOutput.To) {
|
||||
t.Errorf("expected To to have length %d, got %d", len(scenario.ExpectedOutput.To), len(got.To))
|
||||
} else {
|
||||
for i, to := range got.To {
|
||||
if to != scenario.ExpectedOutput.To[i] {
|
||||
t.Errorf("expected To[%d] to be %s, got %s", i, scenario.ExpectedOutput.To[i], to)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Test ValidateOverrides as well, since it really just calls GetConfig
|
||||
if err = scenario.Provider.ValidateOverrides(scenario.InputGroup, &scenario.InputAlert); err != nil {
|
||||
t.Errorf("unexpected error: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user