fix(maintenance): validate maintenance windows for external, DNS and SSH endpoints (#1804)
* fix(maintenance): validate maintenance windows for external, DNS and SSH endpoints ExternalEndpoint.ValidateAndSetDefaults never called maintenance.Config.ValidateAndSetDefaults, so the window's start time and timezone were never parsed. IsUnderMaintenance then treated every window as starting at midnight in the server's local timezone, so alerts were still sent during the configured window. Endpoint.ValidateAndSetDefaults had the same problem for DNS and SSH endpoints, which returned before reaching the maintenance window loop. The loop now runs before those early returns. Fixes #1551 Made by an AI agent (Claude Code, model Claude Opus 5). * test(api): assert external endpoint maintenance window suppresses alerting Pushes a failing result for an external endpoint whose maintenance window (Europe/Berlin) is currently active and checks that alerting is skipped, exercising the same maintenance check CreateExternalEndpointResult uses. --------- Co-authored-by: TwiN <twin@linux.com>
This commit is contained in:
1 parent
ae605f2919
commit
884f64d8e6
5 files changed
+152
-5
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/TwiN/gatus/v5/alerting"
|
||||
"github.com/TwiN/gatus/v5/alerting/alert"
|
||||
@@ -174,3 +175,53 @@ func TestCreateExternalEndpointResult(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCreateExternalEndpointResultUnderEndpointMaintenanceWindow(t *testing.T) {
|
||||
defer store.Get().Clear()
|
||||
defer cache.Clear()
|
||||
location, err := time.LoadLocation("Europe/Berlin")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
externalEndpoint := &endpoint.ExternalEndpoint{
|
||||
Name: "n",
|
||||
Group: "g",
|
||||
Token: "token",
|
||||
Alerts: []*alert.Alert{
|
||||
{
|
||||
Type: alert.TypeDiscord,
|
||||
FailureThreshold: 2,
|
||||
SuccessThreshold: 2,
|
||||
},
|
||||
},
|
||||
MaintenanceWindows: []*maintenance.Config{{
|
||||
Start: time.Now().In(location).Add(-time.Minute).Format("15:04"),
|
||||
Duration: time.Hour,
|
||||
Timezone: "Europe/Berlin",
|
||||
}},
|
||||
}
|
||||
if err := externalEndpoint.ValidateAndSetDefaults(); err != nil {
|
||||
t.Fatal("did not expect an error, got", err)
|
||||
}
|
||||
cfg := &config.Config{
|
||||
Alerting: &alerting.Config{
|
||||
Discord: &discord.AlertProvider{},
|
||||
},
|
||||
ExternalEndpoints: []*endpoint.ExternalEndpoint{externalEndpoint},
|
||||
Maintenance: &maintenance.Config{},
|
||||
}
|
||||
router := New(cfg).Router()
|
||||
request := httptest.NewRequest("POST", "/api/v1/endpoints/g_n/external?success=false", http.NoBody)
|
||||
request.Header.Set("Authorization", "Bearer token")
|
||||
response, err := router.Test(request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != 200 {
|
||||
t.Fatalf("expected 200, got %d", response.StatusCode)
|
||||
}
|
||||
if externalEndpoint.NumberOfFailuresInARow != 0 {
|
||||
t.Errorf("expected alerting to be skipped because the endpoint is under maintenance, but NumberOfFailuresInARow is %d", externalEndpoint.NumberOfFailuresInARow)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user