Fix #146: Alerting causes panic with some providers

This commit is contained in:
TwinProduction 2021-07-29 18:13:37 -04:00
parent 07b1a2eafb
commit cdbc075439
5 changed files with 22 additions and 9 deletions

View File

@ -9,6 +9,10 @@ import (
"github.com/TwinProduction/gatus/core" "github.com/TwinProduction/gatus/core"
) )
const (
restAPIURL = "https://events.pagerduty.com/v2/enqueue"
)
// AlertProvider is the configuration necessary for sending an alert using PagerDuty // AlertProvider is the configuration necessary for sending an alert using PagerDuty
type AlertProvider struct { type AlertProvider struct {
IntegrationKey string `yaml:"integration-key"` IntegrationKey string `yaml:"integration-key"`
@ -37,7 +41,7 @@ func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, aler
resolveKey = "" resolveKey = ""
} }
return &custom.AlertProvider{ return &custom.AlertProvider{
URL: "https://events.pagerduty.com/v2/enqueue", URL: restAPIURL,
Method: http.MethodPost, Method: http.MethodPost,
Body: fmt.Sprintf(`{ Body: fmt.Sprintf(`{
"routing_key": "%s", "routing_key": "%s",

View File

@ -16,7 +16,10 @@ import (
// GetHTTPClient returns the shared HTTP client // GetHTTPClient returns the shared HTTP client
func GetHTTPClient(config *Config) *http.Client { func GetHTTPClient(config *Config) *http.Client {
return config.GetHTTPClient() if config == nil {
return defaultConfig.getHTTPClient()
}
return config.getHTTPClient()
} }
// CanCreateTCPConnection checks whether a connection can be established with a TCP service // CanCreateTCPConnection checks whether a connection can be established with a TCP service

View File

@ -6,12 +6,18 @@ import (
) )
func TestGetHTTPClient(t *testing.T) { func TestGetHTTPClient(t *testing.T) {
GetHTTPClient(&Config{ cfg := &Config{
Insecure: false, Insecure: false,
IgnoreRedirect: false, IgnoreRedirect: false,
Timeout: 0, Timeout: 0,
httpClient: nil, }
}) cfg.ValidateAndSetDefaults()
if GetHTTPClient(cfg) == nil {
t.Error("expected client to not be nil")
}
if GetHTTPClient(nil) == nil {
t.Error("expected client to not be nil")
}
} }
func TestPing(t *testing.T) { func TestPing(t *testing.T) {

View File

@ -47,7 +47,7 @@ func (c *Config) ValidateAndSetDefaults() {
} }
// GetHTTPClient return a HTTP client matching the Config's parameters. // GetHTTPClient return a HTTP client matching the Config's parameters.
func (c *Config) GetHTTPClient() *http.Client { func (c *Config) getHTTPClient() *http.Client {
if c.httpClient == nil { if c.httpClient == nil {
c.httpClient = &http.Client{ c.httpClient = &http.Client{
Timeout: c.Timeout, Timeout: c.Timeout,

View File

@ -6,10 +6,10 @@ import (
"time" "time"
) )
func TestConfig_GetHTTPClient(t *testing.T) { func TestConfig_getHTTPClient(t *testing.T) {
insecureConfig := &Config{Insecure: true} insecureConfig := &Config{Insecure: true}
insecureConfig.ValidateAndSetDefaults() insecureConfig.ValidateAndSetDefaults()
insecureClient := insecureConfig.GetHTTPClient() insecureClient := insecureConfig.getHTTPClient()
if !(insecureClient.Transport).(*http.Transport).TLSClientConfig.InsecureSkipVerify { if !(insecureClient.Transport).(*http.Transport).TLSClientConfig.InsecureSkipVerify {
t.Error("expected Config.Insecure set to true to cause the HTTP client to skip certificate verification") t.Error("expected Config.Insecure set to true to cause the HTTP client to skip certificate verification")
} }
@ -23,7 +23,7 @@ func TestConfig_GetHTTPClient(t *testing.T) {
secureConfig := &Config{IgnoreRedirect: true, Timeout: 5 * time.Second} secureConfig := &Config{IgnoreRedirect: true, Timeout: 5 * time.Second}
secureConfig.ValidateAndSetDefaults() secureConfig.ValidateAndSetDefaults()
secureClient := secureConfig.GetHTTPClient() secureClient := secureConfig.getHTTPClient()
if (secureClient.Transport).(*http.Transport).TLSClientConfig.InsecureSkipVerify { if (secureClient.Transport).(*http.Transport).TLSClientConfig.InsecureSkipVerify {
t.Error("expected Config.Insecure set to false to cause the HTTP client to not skip certificate verification") t.Error("expected Config.Insecure set to false to cause the HTTP client to not skip certificate verification")
} }