Alerting: Fix dates stored in local time when pausing alerts (#19525)

* Make tests more sensitive to timezone.

* Time stamps should use UTC for consistency.

* Test: Assert state is paused after pauseAllAlerts call.
This commit is contained in:
Ben Patterson 2019-10-08 10:25:15 -04:00 committed by Sofia Papagiannaki
parent 82ce5a194d
commit 9904e14f8e
2 changed files with 11 additions and 4 deletions

View File

@ -320,10 +320,10 @@ func PauseAlert(cmd *m.PauseAlertCommand) error {
buffer.WriteString(`UPDATE alert SET state = ?, new_state_date = ?`)
if cmd.Paused {
params = append(params, string(m.AlertStatePaused))
params = append(params, timeNow())
params = append(params, timeNow().UTC())
} else {
params = append(params, string(m.AlertStateUnknown))
params = append(params, timeNow())
params = append(params, timeNow().UTC())
}
buffer.WriteString(` WHERE id IN (?` + strings.Repeat(",?", len(cmd.AlertIds)-1) + `)`)
@ -351,7 +351,7 @@ func PauseAllAlerts(cmd *m.PauseAllAlertCommand) error {
newState = string(m.AlertStateUnknown)
}
res, err := sess.Exec(`UPDATE alert SET state = ?, new_state_date = ?`, newState, timeNow())
res, err := sess.Exec(`UPDATE alert SET state = ?, new_state_date = ?`, newState, timeNow().UTC())
if err != nil {
return err
}

View File

@ -12,7 +12,8 @@ import (
func mockTimeNow() {
var timeSeed int64
timeNow = func() time.Time {
fakeNow := time.Unix(timeSeed, 0)
loc := time.FixedZone("MockZoneUTC-5", -5*60*60)
fakeNow := time.Unix(timeSeed, 0).In(loc)
timeSeed++
return fakeNow
}
@ -84,6 +85,12 @@ func TestAlertingDataAccess(t *testing.T) {
So(err, ShouldNotBeNil)
})
Convey("alert is paused", func() {
alert, _ = getAlertById(1)
currentState := alert.State
So(currentState, ShouldEqual, "paused")
})
Convey("pausing alerts should update their NewStateDate", func() {
alert, _ = getAlertById(1)
stateDateAfterPause := alert.NewStateDate