provider/mesh: always store UTC

This commit is contained in:
Fabian Reinartz 2016-06-20 16:05:13 +02:00
parent 2a7047cdc1
commit ddd3189bfe
3 changed files with 50 additions and 49 deletions

View File

@ -17,8 +17,7 @@ import (
func TestNotificationInfosOnGossip(t *testing.T) { func TestNotificationInfosOnGossip(t *testing.T) {
var ( var (
t0 = time.Now() now = utcNow()
t1 = t0.Add(time.Minute)
) )
cases := []struct { cases := []struct {
initial map[string]notificationEntry initial map[string]notificationEntry
@ -29,37 +28,37 @@ func TestNotificationInfosOnGossip(t *testing.T) {
{ {
initial: map[string]notificationEntry{}, initial: map[string]notificationEntry{},
msg: map[string]notificationEntry{ msg: map[string]notificationEntry{
"123:recv1": {true, t0}, "123:recv1": {true, now},
}, },
delta: map[string]notificationEntry{ delta: map[string]notificationEntry{
"123:recv1": {true, t0}, "123:recv1": {true, now},
}, },
final: map[string]notificationEntry{ final: map[string]notificationEntry{
"123:recv1": {true, t0}, "123:recv1": {true, now},
}, },
}, { }, {
initial: map[string]notificationEntry{ initial: map[string]notificationEntry{
"123:recv1": {true, t0}, "123:recv1": {true, now},
}, },
msg: map[string]notificationEntry{ msg: map[string]notificationEntry{
"123:recv1": {false, t1}, "123:recv1": {false, now.Add(time.Minute)},
}, },
delta: map[string]notificationEntry{ delta: map[string]notificationEntry{
"123:recv1": {false, t1}, "123:recv1": {false, now.Add(time.Minute)},
}, },
final: map[string]notificationEntry{ final: map[string]notificationEntry{
"123:recv1": {false, t1}, "123:recv1": {false, now.Add(time.Minute)},
}, },
}, { }, {
initial: map[string]notificationEntry{ initial: map[string]notificationEntry{
"123:recv1": {true, t1}, "123:recv1": {true, now.Add(time.Minute)},
}, },
msg: map[string]notificationEntry{ msg: map[string]notificationEntry{
"123:recv1": {false, t0}, "123:recv1": {false, now},
}, },
delta: map[string]notificationEntry{}, delta: map[string]notificationEntry{},
final: map[string]notificationEntry{ final: map[string]notificationEntry{
"123:recv1": {true, t1}, "123:recv1": {true, now.Add(time.Minute)},
}, },
}, },
} }
@ -146,10 +145,7 @@ func TestNotificationInfosOnGossip(t *testing.T) {
func TestNotificationInfosSet(t *testing.T) { func TestNotificationInfosSet(t *testing.T) {
var ( var (
t0 = time.Now() now = utcNow()
t1 = t0.Add(10 * time.Minute)
// t2 = t0.Add(20 * time.Minute)
// t3 = t0.Add(30 * time.Minute)
) )
cases := []struct { cases := []struct {
initial map[string]notificationEntry initial map[string]notificationEntry
@ -164,14 +160,14 @@ func TestNotificationInfosSet(t *testing.T) {
Alert: 0x10, Alert: 0x10,
Receiver: "recv1", Receiver: "recv1",
Resolved: false, Resolved: false,
Timestamp: t0, Timestamp: now,
}, },
}, },
update: map[string]notificationEntry{ update: map[string]notificationEntry{
"0000000000000010:recv1": {false, t0}, "0000000000000010:recv1": {false, now},
}, },
final: map[string]notificationEntry{ final: map[string]notificationEntry{
"0000000000000010:recv1": {false, t0}, "0000000000000010:recv1": {false, now},
}, },
}, },
{ {
@ -181,38 +177,38 @@ func TestNotificationInfosSet(t *testing.T) {
// The update is okay to propagate but the final state must correctly // The update is okay to propagate but the final state must correctly
// drop it. // drop it.
initial: map[string]notificationEntry{ initial: map[string]notificationEntry{
"0000000000000010:recv1": {false, t0}, "0000000000000010:recv1": {false, now},
"0000000000000010:recv2": {false, t1}, "0000000000000010:recv2": {false, now.Add(10 * time.Minute)},
}, },
input: []*types.NotifyInfo{ input: []*types.NotifyInfo{
{ {
Alert: 0x10, Alert: 0x10,
Receiver: "recv1", Receiver: "recv1",
Resolved: true, Resolved: true,
Timestamp: t1, Timestamp: now.Add(10 * time.Minute),
}, },
{ {
Alert: 0x10, Alert: 0x10,
Receiver: "recv2", Receiver: "recv2",
Resolved: true, Resolved: true,
Timestamp: t0, Timestamp: now,
}, },
{ {
Alert: 0x20, Alert: 0x20,
Receiver: "recv2", Receiver: "recv2",
Resolved: false, Resolved: false,
Timestamp: t0, Timestamp: now,
}, },
}, },
update: map[string]notificationEntry{ update: map[string]notificationEntry{
"0000000000000010:recv1": {true, t1}, "0000000000000010:recv1": {true, now.Add(10 * time.Minute)},
"0000000000000010:recv2": {true, t0}, "0000000000000010:recv2": {true, now},
"0000000000000020:recv2": {false, t0}, "0000000000000020:recv2": {false, now},
}, },
final: map[string]notificationEntry{ final: map[string]notificationEntry{
"0000000000000010:recv1": {true, t1}, "0000000000000010:recv1": {true, now.Add(10 * time.Minute)},
"0000000000000010:recv2": {false, t1}, "0000000000000010:recv2": {false, now.Add(10 * time.Minute)},
"0000000000000020:recv2": {false, t0}, "0000000000000020:recv2": {false, now},
}, },
}, },
} }
@ -243,8 +239,7 @@ func TestNotificationInfosSet(t *testing.T) {
func TestNotificationInfosGet(t *testing.T) { func TestNotificationInfosGet(t *testing.T) {
var ( var (
t0 = time.Now() now = utcNow()
t1 = t0.Add(time.Minute)
) )
type query struct { type query struct {
recv string recv string
@ -257,10 +252,10 @@ func TestNotificationInfosGet(t *testing.T) {
}{ }{
{ {
state: map[string]notificationEntry{ state: map[string]notificationEntry{
"0000000000000010:recv1": {true, t1}, "0000000000000010:recv1": {true, now.Add(time.Minute)},
"0000000000000030:recv1": {true, t1}, "0000000000000030:recv1": {true, now.Add(time.Minute)},
"0000000000000010:recv2": {false, t1}, "0000000000000010:recv2": {false, now.Add(time.Minute)},
"0000000000000020:recv2": {false, t0}, "0000000000000020:recv2": {false, now},
}, },
queries: []query{ queries: []query{
{ {
@ -272,7 +267,7 @@ func TestNotificationInfosGet(t *testing.T) {
Alert: 0x10, Alert: 0x10,
Receiver: "recv1", Receiver: "recv1",
Resolved: true, Resolved: true,
Timestamp: t1, Timestamp: now.Add(time.Minute),
}, },
nil, nil,
}, },
@ -303,7 +298,7 @@ func TestNotificationInfosGet(t *testing.T) {
func TestSilencesSet(t *testing.T) { func TestSilencesSet(t *testing.T) {
var ( var (
now = time.Now() now = utcNow()
id1 = uuid.NewV4() id1 = uuid.NewV4()
matchers = types.NewMatchers(types.NewMatcher("a", "b")) matchers = types.NewMatchers(types.NewMatcher("a", "b"))
) )

View File

@ -13,6 +13,8 @@ import (
"github.com/weaveworks/mesh" "github.com/weaveworks/mesh"
) )
func utcNow() time.Time { return time.Now().UTC() }
type notificationEntry struct { type notificationEntry struct {
Resolved bool Resolved bool
Timestamp time.Time Timestamp time.Time
@ -31,7 +33,7 @@ func newNotificationState() *notificationState {
return &notificationState{ return &notificationState{
set: map[string]notificationEntry{}, set: map[string]notificationEntry{},
stopc: make(chan struct{}), stopc: make(chan struct{}),
now: time.Now, now: utcNow,
} }
} }
@ -146,7 +148,7 @@ func newSilenceState() *silenceState {
return &silenceState{ return &silenceState{
m: map[uuid.UUID]*types.Silence{}, m: map[uuid.UUID]*types.Silence{},
stopc: make(chan struct{}), stopc: make(chan struct{}),
now: time.Now, now: utcNow,
} }
} }
@ -235,6 +237,9 @@ func (st *silenceState) set(s *types.Silence) error {
st.mtx.Lock() st.mtx.Lock()
defer st.mtx.Unlock() defer st.mtx.Unlock()
s.StartsAt = s.StartsAt.UTC()
s.EndsAt = s.EndsAt.UTC()
now := st.now() now := st.now()
s.UpdatedAt = now s.UpdatedAt = now

View File

@ -13,7 +13,7 @@ import (
) )
func TestNotificationStateGC(t *testing.T) { func TestNotificationStateGC(t *testing.T) {
now := time.Now() now := utcNow()
initial := map[string]notificationEntry{ initial := map[string]notificationEntry{
"1": {true, now}, "1": {true, now},
@ -43,7 +43,7 @@ func TestNotificationStateGC(t *testing.T) {
func TestSilenceStateGC(t *testing.T) { func TestSilenceStateGC(t *testing.T) {
var ( var (
now = time.Now() now = utcNow()
id1 = uuid.NewV4() id1 = uuid.NewV4()
id2 = uuid.NewV4() id2 = uuid.NewV4()
@ -90,7 +90,7 @@ func TestSilenceStateGC(t *testing.T) {
func TestSilenceStateSet(t *testing.T) { func TestSilenceStateSet(t *testing.T) {
var ( var (
now = time.Now() now = utcNow()
id1 = uuid.NewV4() id1 = uuid.NewV4()
matchers = types.NewMatchers(types.NewMatcher("a", "b")) matchers = types.NewMatchers(types.NewMatcher("a", "b"))
) )
@ -126,10 +126,11 @@ func TestSilenceStateSet(t *testing.T) {
}, },
}, },
input: &types.Silence{ input: &types.Silence{
ID: id1, ID: id1,
Matchers: matchers, Matchers: matchers,
StartsAt: now.Add(time.Minute), // Different input timezones must be normalized to UTC.
EndsAt: now.Add(time.Hour), StartsAt: now.Add(time.Minute).In(time.FixedZone("test", 100000)),
EndsAt: now.Add(time.Hour).In(time.FixedZone("test", 10000000)),
CreatedBy: "x", CreatedBy: "x",
Comment: "x", Comment: "x",
}, },
@ -251,7 +252,7 @@ func TestSilenceStateSet(t *testing.T) {
func TestSilenceStateDel(t *testing.T) { func TestSilenceStateDel(t *testing.T) {
var ( var (
now = time.Now() now = utcNow()
id1 = uuid.NewV4() id1 = uuid.NewV4()
matchers = types.NewMatchers(types.NewMatcher("a", "b")) matchers = types.NewMatchers(types.NewMatcher("a", "b"))
) )
@ -352,7 +353,7 @@ func TestSilenceStateDel(t *testing.T) {
func TestSilenceModAllowed(t *testing.T) { func TestSilenceModAllowed(t *testing.T) {
var ( var (
now = time.Now() now = utcNow()
id1 = uuid.NewV4() id1 = uuid.NewV4()
matchers = types.NewMatchers(types.NewMatcher("a", "b")) matchers = types.NewMatchers(types.NewMatcher("a", "b"))
) )