mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Remove flakiness of google chat tests (#40592)
This commit is contained in:
parent
38709df33b
commit
36e12e2b26
@ -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),
|
||||
},
|
||||
})
|
||||
|
||||
|
@ -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...)
|
||||
|
23
pkg/services/ngalert/notifier/channels/testing.go
Normal file
23
pkg/services/ngalert/notifier/channels/testing.go
Normal 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
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user