Merge pull request #2816 from prashbnair/update_check

Correcting the condition for updating a silence. Earlier was checking…
This commit is contained in:
Simon Pasquier 2022-03-04 15:17:12 +01:00 committed by GitHub
commit 3f42c5e813
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 2 deletions

View File

@ -586,7 +586,7 @@ func canUpdate(a, b *pb.Silence, now time.Time) bool {
// Allowed timestamp modifications depend on the current time.
switch st := getState(a, now); st {
case types.SilenceStateActive:
if !b.StartsAt.Equal(a.StartsAt) {
if b.StartsAt.Unix() != a.StartsAt.Unix() {
return false
}
if b.EndsAt.Before(now) {

View File

@ -362,7 +362,6 @@ func TestSilenceSet(t *testing.T) {
},
}
require.Equal(t, want, s.st, "unexpected state after silence creation")
// Update silence 2 with new matcher expires it and creates a new one.
now = now.Add(time.Minute)
now4 := now
@ -429,6 +428,55 @@ func TestSilenceSet(t *testing.T) {
require.Equal(t, want, s.st, "unexpected state after silence creation")
}
func TestSetActiveSilence(t *testing.T) {
s, err := New(Options{
Retention: time.Hour,
})
require.NoError(t, err)
now := utcNow()
s.now = func() time.Time { return now }
startsAt := now.Add(-1 * time.Minute)
endsAt := now.Add(5 * time.Minute)
// Insert silence with fixed start time.
sil1 := &pb.Silence{
Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}},
StartsAt: startsAt,
EndsAt: endsAt,
}
id1, _ := s.Set(sil1)
// Update silence with 2 extra nanoseconds so the "seconds" part should not change
newStartsAt := now.Add(2 * time.Nanosecond)
newEndsAt := endsAt.Add(2 * time.Minute)
sil2 := cloneSilence(sil1)
sil2.Id = id1
sil2.StartsAt = newStartsAt
sil2.EndsAt = newEndsAt
now = now.Add(time.Minute)
id2, err := s.Set(sil2)
require.NoError(t, err)
require.Equal(t, id1, id2)
want := state{
id2: &pb.MeshSilence{
Silence: &pb.Silence{
Id: id1,
Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}},
StartsAt: newStartsAt,
EndsAt: newEndsAt,
UpdatedAt: now,
},
ExpiresAt: newEndsAt.Add(s.retention),
},
}
require.Equal(t, want, s.st, "unexpected state after silence creation")
}
func TestSilencesSetFail(t *testing.T) {
s, err := New(Options{})
require.NoError(t, err)