Skip to content

Commit

Permalink
Merge pull request #2816 from prashbnair/update_check
Browse files Browse the repository at this point in the history
Correcting the condition for updating a silence. Earlier was checking…
  • Loading branch information
simonpasquier authored Mar 4, 2022
2 parents 9f3a25d + 6618217 commit 3f42c5e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion silence/silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
50 changes: 49 additions & 1 deletion silence/silence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3f42c5e

Please sign in to comment.