Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correcting the condition for updating a silence. Earlier was checking… #2816

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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