Skip to content

Commit e542c68

Browse files
ultrotterGuido Trotter
andauthored
MockWebhook: track shutdown status more gracefully (#4761)
* MockWebhook: track shutdown status more gracefully When we close the server sometimes the messages are interrupted and can't be decoded by the webhook. This change accepts any decoding failure after server shutdown. Signed-off-by: Guido Trotter <[email protected]> * Call NewWebhook passing 't', and protect against nil cmd in Terminate Signed-off-by: Guido Trotter <[email protected]> --------- Signed-off-by: Guido Trotter <[email protected]> Co-authored-by: Guido Trotter <[email protected]>
1 parent 6e4e228 commit e542c68

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

test/cli/acceptance/cli_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ receivers:
422422
Tolerance: 1 * time.Second,
423423
})
424424
co := at.Collector("webhook")
425-
wh := NewWebhook(co)
425+
wh := NewWebhook(t, co)
426426

427427
amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
428428
require.NoError(t, amc.Start())
@@ -475,7 +475,7 @@ receivers:
475475
Tolerance: 1 * time.Second,
476476
})
477477
co := at.Collector("webhook")
478-
wh := NewWebhook(co)
478+
wh := NewWebhook(t, co)
479479

480480
amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
481481
require.NoError(t, amc.Start())

test/testutils/acceptance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func (amc *AlertmanagerCluster) Terminate() {
383383
// data.
384384
func (am *Alertmanager) Terminate() {
385385
am.T.Helper()
386-
if am.cmd.Process != nil {
386+
if am.cmd != nil && am.cmd.Process != nil {
387387
if err := syscall.Kill(am.cmd.Process.Pid, syscall.SIGTERM); err != nil {
388388
am.T.Logf("Error sending SIGTERM to Alertmanager process: %v", err)
389389
}

test/testutils/mock.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ package testutils
1616
import (
1717
"encoding/json"
1818
"fmt"
19+
"io"
1920
"maps"
2021
"net/http"
2122
"net/http/httptest"
2223
"reflect"
24+
"sync/atomic"
2325
"testing"
2426
"time"
2527

@@ -182,6 +184,7 @@ type MockWebhook struct {
182184
opts *AcceptanceOpts
183185
collector *Collector
184186
addr string
187+
closing atomic.Bool
185188

186189
// Func is called early on when retrieving a notification by an
187190
// Alertmanager. If Func returns true, the given notification is dropped.
@@ -202,6 +205,7 @@ func NewWebhook(t *testing.T, c *Collector) *MockWebhook {
202205
wh.addr = server.Listener.Addr().String()
203206

204207
t.Cleanup(func() {
208+
wh.closing.Store(true)
205209
server.Close()
206210
})
207211

@@ -222,6 +226,10 @@ func (ws *MockWebhook) ServeHTTP(w http.ResponseWriter, req *http.Request) {
222226

223227
var v webhook.Message
224228
if err := dec.Decode(&v); err != nil {
229+
// During shutdown, ignore EOF errors from interrupted connections
230+
if ws.closing.Load() && (err == io.EOF || err.Error() == "EOF") {
231+
return
232+
}
225233
panic(err)
226234
}
227235

0 commit comments

Comments
 (0)