Remove flakiness of google chat tests (#40592)
This commit is contained in:
@@ -84,7 +84,7 @@ func (gcn *GoogleChatNotifier) Notify(ctx context.Context, as ...*types.Alert) (
|
|||||||
// Add text paragraph widget for the build version and timestamp.
|
// Add text paragraph widget for the build version and timestamp.
|
||||||
widgets = append(widgets, textParagraphWidget{
|
widgets = append(widgets, textParagraphWidget{
|
||||||
Text: text{
|
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) {
|
func TestGoogleChatNotifier(t *testing.T) {
|
||||||
|
constNow := time.Now()
|
||||||
|
defer mockTimeNow(constNow)()
|
||||||
|
|
||||||
tmpl := templateForTests(t)
|
tmpl := templateForTests(t)
|
||||||
|
|
||||||
externalURL, err := url.Parse("http://localhost")
|
externalURL, err := url.Parse("http://localhost")
|
||||||
@@ -77,7 +80,7 @@ func TestGoogleChatNotifier(t *testing.T) {
|
|||||||
textParagraphWidget{
|
textParagraphWidget{
|
||||||
Text: text{
|
Text: text{
|
||||||
// RFC822 only has the minute, hence it works in most cases.
|
// 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{
|
textParagraphWidget{
|
||||||
Text: text{
|
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
|
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.WithGroupKey(context.Background(), "alertname")
|
||||||
ctx = notify.WithGroupLabels(ctx, model.LabelSet{"alertname": ""})
|
ctx = notify.WithGroupLabels(ctx, model.LabelSet{"alertname": ""})
|
||||||
ok, err := pn.Notify(ctx, c.alerts...)
|
ok, err := pn.Notify(ctx, c.alerts...)
|
||||||
|
|||||||
@@ -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"
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/prometheus/common/model"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/util"
|
"github.com/grafana/grafana/pkg/util"
|
||||||
"github.com/prometheus/common/model"
|
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
)
|
)
|
||||||
@@ -25,6 +26,11 @@ const (
|
|||||||
ColorAlertResolved = "#36a64f"
|
ColorAlertResolved = "#36a64f"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Provides current time. Can be overwritten in tests.
|
||||||
|
timeNow = time.Now
|
||||||
|
)
|
||||||
|
|
||||||
type receiverInitError struct {
|
type receiverInitError struct {
|
||||||
Reason string
|
Reason string
|
||||||
Err error
|
Err error
|
||||||
|
|||||||
Reference in New Issue
Block a user