1
0
Fork 0
mirror of https://github.com/TwiN/gatus.git synced 2024-12-14 11:58:04 +00:00

Improve test coverage

This commit is contained in:
TwinProduction 2021-01-10 01:22:27 -05:00
parent 6f8a728c5f
commit 20b4c86023
2 changed files with 72 additions and 0 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"strings" "strings"
"github.com/TwinProduction/gatus/client" "github.com/TwinProduction/gatus/client"
@ -74,6 +75,9 @@ func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription st
// Send a request to the alert provider and return the body // Send a request to the alert provider and return the body
func (provider *AlertProvider) Send(serviceName, alertDescription string, resolved bool) ([]byte, error) { func (provider *AlertProvider) Send(serviceName, alertDescription string, resolved bool) ([]byte, error) {
if os.Getenv("MOCK_ALERT_PROVIDER") == "true" {
return []byte("{}"), nil
}
request := provider.buildHTTPRequest(serviceName, alertDescription, resolved) request := provider.buildHTTPRequest(serviceName, alertDescription, resolved)
response, err := client.GetHTTPClient(provider.Insecure).Do(request) response, err := client.GetHTTPClient(provider.Insecure).Do(request)
if err != nil { if err != nil {

View file

@ -1,15 +1,20 @@
package watchdog package watchdog
import ( import (
"os"
"testing" "testing"
"github.com/TwinProduction/gatus/alerting" "github.com/TwinProduction/gatus/alerting"
"github.com/TwinProduction/gatus/alerting/provider/custom" "github.com/TwinProduction/gatus/alerting/provider/custom"
"github.com/TwinProduction/gatus/alerting/provider/pagerduty"
"github.com/TwinProduction/gatus/config" "github.com/TwinProduction/gatus/config"
"github.com/TwinProduction/gatus/core" "github.com/TwinProduction/gatus/core"
) )
func TestHandleAlerting(t *testing.T) { func TestHandleAlerting(t *testing.T) {
_ = os.Setenv("MOCK_ALERT_PROVIDER", "true")
defer os.Clearenv()
cfg := &config.Config{ cfg := &config.Config{
Debug: true, Debug: true,
Alerting: &alerting.Config{ Alerting: &alerting.Config{
@ -134,6 +139,9 @@ func TestHandleAlerting(t *testing.T) {
} }
func TestHandleAlertingWhenAlertingConfigIsNil(t *testing.T) { func TestHandleAlertingWhenAlertingConfigIsNil(t *testing.T) {
_ = os.Setenv("MOCK_ALERT_PROVIDER", "true")
defer os.Clearenv()
cfg := &config.Config{ cfg := &config.Config{
Debug: true, Debug: true,
Alerting: nil, Alerting: nil,
@ -143,6 +151,9 @@ func TestHandleAlertingWhenAlertingConfigIsNil(t *testing.T) {
} }
func TestHandleAlertingWithBadAlertProvider(t *testing.T) { func TestHandleAlertingWithBadAlertProvider(t *testing.T) {
_ = os.Setenv("MOCK_ALERT_PROVIDER", "true")
defer os.Clearenv()
cfg := &config.Config{ cfg := &config.Config{
Alerting: &alerting.Config{}, Alerting: &alerting.Config{},
} }
@ -195,6 +206,9 @@ func TestHandleAlertingWithBadAlertProvider(t *testing.T) {
} }
func TestHandleAlertingWithoutSendingAlertOnResolve(t *testing.T) { func TestHandleAlertingWithoutSendingAlertOnResolve(t *testing.T) {
_ = os.Setenv("MOCK_ALERT_PROVIDER", "true")
defer os.Clearenv()
cfg := &config.Config{ cfg := &config.Config{
Alerting: &alerting.Config{}, Alerting: &alerting.Config{},
} }
@ -247,6 +261,9 @@ func TestHandleAlertingWithoutSendingAlertOnResolve(t *testing.T) {
} }
func TestHandleAlertingWhenTriggeredAlertIsAlmostResolvedButServiceStartFailingAgain(t *testing.T) { func TestHandleAlertingWhenTriggeredAlertIsAlmostResolvedButServiceStartFailingAgain(t *testing.T) {
_ = os.Setenv("MOCK_ALERT_PROVIDER", "true")
defer os.Clearenv()
cfg := &config.Config{ cfg := &config.Config{
Debug: true, Debug: true,
Alerting: &alerting.Config{ Alerting: &alerting.Config{
@ -284,3 +301,54 @@ func TestHandleAlertingWhenTriggeredAlertIsAlmostResolvedButServiceStartFailingA
t.Fatal("The alert was already triggered at the beginning of this test") t.Fatal("The alert was already triggered at the beginning of this test")
} }
} }
func TestHandleAlertingWhenTriggeredAlertIsResolvedPagerDuty(t *testing.T) {
_ = os.Setenv("MOCK_ALERT_PROVIDER", "true")
defer os.Clearenv()
cfg := &config.Config{
Debug: true,
Alerting: &alerting.Config{
PagerDuty: &pagerduty.AlertProvider{
IntegrationKey: "00000000000000000000000000000000",
},
},
}
config.Set(cfg)
service := &core.Service{
URL: "http://example.com",
Alerts: []*core.Alert{
{
Type: core.PagerDutyAlert,
Enabled: true,
FailureThreshold: 1,
SuccessThreshold: 1,
SendOnResolved: true,
Triggered: false,
},
},
NumberOfFailuresInARow: 0,
}
HandleAlerting(service, &core.Result{Success: false})
if service.NumberOfFailuresInARow != 1 {
t.Fatal("service.NumberOfFailuresInARow should've increased from 0 to 1, got", service.NumberOfFailuresInARow)
}
if service.NumberOfSuccessesInARow != 0 {
t.Fatal("service.NumberOfSuccessesInARow should've stayed at 0, got", service.NumberOfSuccessesInARow)
}
if !service.Alerts[0].Triggered {
t.Fatal("The alert should've been triggered")
}
HandleAlerting(service, &core.Result{Success: true})
if service.NumberOfFailuresInARow != 0 {
t.Fatal("service.NumberOfFailuresInARow should've decreased from 1 to 0, got", service.NumberOfFailuresInARow)
}
if service.NumberOfSuccessesInARow != 1 {
t.Fatal("service.NumberOfSuccessesInARow should've increased from 0 to 1, got", service.NumberOfSuccessesInARow)
}
if service.Alerts[0].Triggered {
t.Fatal("The alert shouldn't be triggered anymore")
}
}