mirror of
https://github.com/grafana/grafana.git
synced 2025-01-27 16:57:14 -06:00
Alerting: Fix test fails in some environments (#58251)
This commit is contained in:
parent
480277f612
commit
8353f307aa
@ -27,7 +27,7 @@ func (i *Image) ExtendDuration(d time.Duration) {
|
||||
|
||||
// HasExpired returns true if the image has expired.
|
||||
func (i *Image) HasExpired() bool {
|
||||
return time.Now().After(i.ExpiresAt)
|
||||
return timeNow().After(i.ExpiresAt)
|
||||
}
|
||||
|
||||
// HasPath returns true if the image has a path on disk.
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/benbjohnson/clock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -20,12 +21,18 @@ func TestImage_ExtendDuration(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImage_HasExpired(t *testing.T) {
|
||||
oldTimeNow := timeNow
|
||||
timeNow = clock.NewMock().Now
|
||||
t.Cleanup(func() {
|
||||
timeNow = oldTimeNow
|
||||
})
|
||||
|
||||
var i Image
|
||||
i.ExpiresAt = time.Now().Add(time.Minute)
|
||||
i.ExpiresAt = timeNow().Add(time.Minute)
|
||||
assert.False(t, i.HasExpired())
|
||||
i.ExpiresAt = time.Now()
|
||||
assert.True(t, i.HasExpired())
|
||||
i.ExpiresAt = time.Now().Add(-time.Minute)
|
||||
i.ExpiresAt = timeNow()
|
||||
assert.False(t, i.HasExpired())
|
||||
i.ExpiresAt = timeNow().Add(-time.Minute)
|
||||
assert.True(t, i.HasExpired())
|
||||
}
|
||||
|
||||
|
8
pkg/services/ngalert/models/time.go
Normal file
8
pkg/services/ngalert/models/time.go
Normal file
@ -0,0 +1,8 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
var (
|
||||
// timeNow is an equivalent time.Now() that can be replaced in tests
|
||||
timeNow = time.Now
|
||||
)
|
Loading…
Reference in New Issue
Block a user