mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Fix test fails in some environments (#58251)
This commit is contained in:
@@ -27,7 +27,7 @@ func (i *Image) ExtendDuration(d time.Duration) {
|
|||||||
|
|
||||||
// HasExpired returns true if the image has expired.
|
// HasExpired returns true if the image has expired.
|
||||||
func (i *Image) HasExpired() bool {
|
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.
|
// HasPath returns true if the image has a path on disk.
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/benbjohnson/clock"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,12 +21,18 @@ func TestImage_ExtendDuration(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestImage_HasExpired(t *testing.T) {
|
func TestImage_HasExpired(t *testing.T) {
|
||||||
|
oldTimeNow := timeNow
|
||||||
|
timeNow = clock.NewMock().Now
|
||||||
|
t.Cleanup(func() {
|
||||||
|
timeNow = oldTimeNow
|
||||||
|
})
|
||||||
|
|
||||||
var i Image
|
var i Image
|
||||||
i.ExpiresAt = time.Now().Add(time.Minute)
|
i.ExpiresAt = timeNow().Add(time.Minute)
|
||||||
assert.False(t, i.HasExpired())
|
assert.False(t, i.HasExpired())
|
||||||
i.ExpiresAt = time.Now()
|
i.ExpiresAt = timeNow()
|
||||||
assert.True(t, i.HasExpired())
|
assert.False(t, i.HasExpired())
|
||||||
i.ExpiresAt = time.Now().Add(-time.Minute)
|
i.ExpiresAt = timeNow().Add(-time.Minute)
|
||||||
assert.True(t, i.HasExpired())
|
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
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user