From 36e12e2b26547cff3eb977b61d10de5e63efab73 Mon Sep 17 00:00:00 2001 From: Yuriy Tseretyan Date: Tue, 19 Oct 2021 16:18:44 -0400 Subject: [PATCH] Remove flakiness of google chat tests (#40592) --- .../ngalert/notifier/channels/googlechat.go | 2 +- .../notifier/channels/googlechat_test.go | 13 ++++------- .../ngalert/notifier/channels/testing.go | 23 +++++++++++++++++++ .../ngalert/notifier/channels/utils.go | 8 ++++++- 4 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 pkg/services/ngalert/notifier/channels/testing.go diff --git a/pkg/services/ngalert/notifier/channels/googlechat.go b/pkg/services/ngalert/notifier/channels/googlechat.go index 56b30caf295..2f0448ac723 100644 --- a/pkg/services/ngalert/notifier/channels/googlechat.go +++ b/pkg/services/ngalert/notifier/channels/googlechat.go @@ -84,7 +84,7 @@ func (gcn *GoogleChatNotifier) Notify(ctx context.Context, as ...*types.Alert) ( // Add text paragraph widget for the build version and timestamp. widgets = append(widgets, textParagraphWidget{ Text: text{ - Text: "Grafana v" + setting.BuildVersion + " | " + (time.Now()).Format(time.RFC822), + Text: "Grafana v" + setting.BuildVersion + " | " + (timeNow()).Format(time.RFC822), }, }) diff --git a/pkg/services/ngalert/notifier/channels/googlechat_test.go b/pkg/services/ngalert/notifier/channels/googlechat_test.go index 941882e96ad..9d57958e84f 100644 --- a/pkg/services/ngalert/notifier/channels/googlechat_test.go +++ b/pkg/services/ngalert/notifier/channels/googlechat_test.go @@ -19,6 +19,9 @@ import ( ) func TestGoogleChatNotifier(t *testing.T) { + constNow := time.Now() + defer mockTimeNow(constNow)() + tmpl := templateForTests(t) externalURL, err := url.Parse("http://localhost") @@ -77,7 +80,7 @@ func TestGoogleChatNotifier(t *testing.T) { textParagraphWidget{ Text: text{ // RFC822 only has the minute, hence it works in most cases. - Text: "Grafana v" + setting.BuildVersion + " | " + (time.Now()).Format(time.RFC822), + Text: "Grafana v" + setting.BuildVersion + " | " + constNow.Format(time.RFC822), }, }, }, @@ -135,7 +138,7 @@ func TestGoogleChatNotifier(t *testing.T) { }, textParagraphWidget{ Text: text{ - Text: "Grafana v" + setting.BuildVersion + " | " + (time.Now()).Format(time.RFC822), + Text: "Grafana v" + setting.BuildVersion + " | " + constNow.Format(time.RFC822), }, }, }, @@ -177,12 +180,6 @@ func TestGoogleChatNotifier(t *testing.T) { return nil }) - if time.Now().Second() == 59 { - // The notification payload has a time component with a precision - // of minute. So if we are at the edge of a minute, we delay for 1 second - // to avoid any flakiness. - time.Sleep(1 * time.Second) - } ctx := notify.WithGroupKey(context.Background(), "alertname") ctx = notify.WithGroupLabels(ctx, model.LabelSet{"alertname": ""}) ok, err := pn.Notify(ctx, c.alerts...) diff --git a/pkg/services/ngalert/notifier/channels/testing.go b/pkg/services/ngalert/notifier/channels/testing.go new file mode 100644 index 00000000000..0f22f81ebbb --- /dev/null +++ b/pkg/services/ngalert/notifier/channels/testing.go @@ -0,0 +1,23 @@ +package channels + +import "time" + +// mockTimeNow replaces function timeNow to return constant time. +// It returns a function that resets the variable back to its original value. +// This allows usage of this function with defer: +// func Test (t *testing.T) { +// now := time.Now() +// defer mockTimeNow(now)() +// ... +// } +func mockTimeNow(constTime time.Time) func() { + timeNow = func() time.Time { + return constTime + } + return resetTimeNow +} + +// resetTimeNow resets the global variable timeNow to the default value, which is time.Now +func resetTimeNow() { + timeNow = time.Now +} diff --git a/pkg/services/ngalert/notifier/channels/utils.go b/pkg/services/ngalert/notifier/channels/utils.go index 6308553b8e3..aebdc360eb6 100644 --- a/pkg/services/ngalert/notifier/channels/utils.go +++ b/pkg/services/ngalert/notifier/channels/utils.go @@ -12,9 +12,10 @@ import ( "path" "time" + "github.com/prometheus/common/model" + "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/util" - "github.com/prometheus/common/model" "github.com/grafana/grafana/pkg/components/simplejson" ) @@ -25,6 +26,11 @@ const ( ColorAlertResolved = "#36a64f" ) +var ( + // Provides current time. Can be overwritten in tests. + timeNow = time.Now +) + type receiverInitError struct { Reason string Err error