From 4dc1ebbb66e8d78889c8828399d5c55321a96571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Philippe=20Qu=C3=A9m=C3=A9ner?= Date: Thu, 8 Feb 2024 13:36:09 +0100 Subject: [PATCH] fix(alerting): add a proper compare func for location in mute timings (#82153) --- pkg/services/ngalert/api/api_alertmanager_guards.go | 10 +++++++++- .../ngalert/api/api_alertmanager_guards_test.go | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/services/ngalert/api/api_alertmanager_guards.go b/pkg/services/ngalert/api/api_alertmanager_guards.go index 1b1f7b438d6..90b6dd92254 100644 --- a/pkg/services/ngalert/api/api_alertmanager_guards.go +++ b/pkg/services/ngalert/api/api_alertmanager_guards.go @@ -3,6 +3,7 @@ package api import ( "encoding/json" "fmt" + "time" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" @@ -137,7 +138,14 @@ func checkMuteTimes(currentConfig apimodels.GettableUserConfig, newConfig apimod return fmt.Errorf("cannot delete provisioned mute time '%s'", muteTime.Name) } reporter := cmputil.DiffReporter{} - options := []cmp.Option{cmp.Reporter(&reporter), cmpopts.EquateEmpty()} + options := []cmp.Option{ + cmp.Reporter(&reporter), + cmp.Comparer(func(a, b *time.Location) bool { + // Check if both are nil or both have the same string representation + return (a == nil && b == nil) || (a != nil && b != nil && a.String() == b.String()) + }), + cmpopts.EquateEmpty(), + } timesEqual := cmp.Equal(muteTime.TimeIntervals, postedMT.TimeIntervals, options...) if !timesEqual { return fmt.Errorf("cannot save provisioned mute time '%s'", muteTime.Name) diff --git a/pkg/services/ngalert/api/api_alertmanager_guards_test.go b/pkg/services/ngalert/api/api_alertmanager_guards_test.go index d0219ed02f0..93f0d8dcf63 100644 --- a/pkg/services/ngalert/api/api_alertmanager_guards_test.go +++ b/pkg/services/ngalert/api/api_alertmanager_guards_test.go @@ -2,6 +2,7 @@ package api import ( "testing" + "time" amConfig "github.com/prometheus/alertmanager/config" "github.com/prometheus/alertmanager/pkg/labels" @@ -563,6 +564,7 @@ func defaultInterval(t *testing.T) []timeinterval.TimeInterval { t.Helper() return []timeinterval.TimeInterval{ { + Location: &timeinterval.Location{Location: time.Local}, Years: []timeinterval.YearRange{ { InclusiveRange: timeinterval.InclusiveRange{