From 5195460c95a1162bae565968661644533e8a07ce Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Mon, 13 Sep 2021 16:12:48 +0200 Subject: [PATCH] Correctly call default silence maintenance function (#2701) https://github.com/prometheus/alertmanager/pull/2689 introduced a regression where the default maintenance function would no longer be called even if no override was specified. The Alertmanager now crashes on any silence maintenance run without this fix. Signed-off-by: Julius Volz --- silence/silence.go | 2 +- silence/silence_test.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/silence/silence.go b/silence/silence.go index c61dac50..11adc9bc 100644 --- a/silence/silence.go +++ b/silence/silence.go @@ -378,7 +378,7 @@ func (s *Silences) Maintenance(interval time.Duration, snapf string, stopc <-cha return size, f.Close() } - if doMaintenance != nil { + if override != nil { doMaintenance = override } diff --git a/silence/silence_test.go b/silence/silence_test.go index 1e86a9b8..3674da27 100644 --- a/silence/silence_test.go +++ b/silence/silence_test.go @@ -180,6 +180,25 @@ func TestSilencesSnapshot(t *testing.T) { } } +// This tests a regression introduced by https://github.com/prometheus/alertmanager/pull/2689. +func TestSilences_Maintenance_DefaultMaintenanceFuncDoesntCrash(t *testing.T) { + f, err := ioutil.TempFile("", "snapshot") + require.NoError(t, err, "creating temp file failed") + s := &Silences{st: state{}, logger: log.NewNopLogger(), now: utcNow, metrics: newMetrics(nil, nil)} + stopc := make(chan struct{}) + + done := make(chan struct{}) + go func() { + s.Maintenance(100*time.Millisecond, f.Name(), stopc, nil) + close(done) + }() + + time.Sleep(200 * time.Millisecond) + close(stopc) + + <-done +} + func TestSilences_Maintenance_SupportsCustomCallback(t *testing.T) { f, err := ioutil.TempFile("", "snapshot") require.NoError(t, err, "creating temp file failed")