Remove flakiness of google chat tests (#40592)

This commit is contained in:
Yuriy Tseretyan 2021-10-19 16:18:44 -04:00 committed by GitHub
parent 38709df33b
commit 36e12e2b26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 10 deletions

View File

@ -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),
},
})

View File

@ -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...)

View File

@ -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
}

View File

@ -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