2019-03-01 15:43:33 +00:00
|
|
|
// Copyright 2018 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-09-03 12:52:53 +00:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-03-25 16:59:51 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
2018-09-03 12:52:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetGet(t *testing.T) {
|
2019-09-18 07:29:34 +00:00
|
|
|
a := NewAlerts()
|
2018-09-03 12:52:53 +00:00
|
|
|
alert := &types.Alert{
|
|
|
|
UpdatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
require.NoError(t, a.Set(alert))
|
|
|
|
want := alert.Fingerprint()
|
|
|
|
got, err := a.Get(want)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, want, got.Fingerprint())
|
|
|
|
}
|
|
|
|
|
2024-05-07 09:34:03 +00:00
|
|
|
func TestDeleteIfNotModified(t *testing.T) {
|
|
|
|
t.Run("unmodified alert should be deleted", func(t *testing.T) {
|
|
|
|
a := NewAlerts()
|
|
|
|
a1 := &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
UpdatedAt: time.Now().Add(-time.Second),
|
|
|
|
}
|
|
|
|
require.NoError(t, a.Set(a1))
|
2018-09-03 12:52:53 +00:00
|
|
|
|
2024-05-07 09:34:03 +00:00
|
|
|
// a1 should be deleted as it has not been modified.
|
|
|
|
a.DeleteIfNotModified(types.AlertSlice{a1})
|
|
|
|
got, err := a.Get(a1.Fingerprint())
|
|
|
|
require.Equal(t, ErrNotFound, err)
|
|
|
|
require.Nil(t, got)
|
|
|
|
})
|
2018-09-03 12:52:53 +00:00
|
|
|
|
2024-05-07 09:34:03 +00:00
|
|
|
t.Run("modified alert should not be deleted", func(t *testing.T) {
|
|
|
|
a := NewAlerts()
|
|
|
|
a1 := &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
UpdatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
require.NoError(t, a.Set(a1))
|
|
|
|
|
|
|
|
// Make a copy of a1 that is older, but do not put it.
|
|
|
|
// We want to make sure a1 is not deleted.
|
|
|
|
a2 := &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
UpdatedAt: time.Now().Add(-time.Second),
|
|
|
|
}
|
|
|
|
require.True(t, a2.UpdatedAt.Before(a1.UpdatedAt))
|
|
|
|
a.DeleteIfNotModified(types.AlertSlice{a2})
|
|
|
|
// a1 should not be deleted.
|
|
|
|
got, err := a.Get(a1.Fingerprint())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, a1, got)
|
|
|
|
|
|
|
|
// Make another copy of a1 that is older, but do not put it.
|
|
|
|
// We want to make sure a2 is not deleted here either.
|
|
|
|
a3 := &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
UpdatedAt: time.Now().Add(time.Second),
|
|
|
|
}
|
|
|
|
require.True(t, a3.UpdatedAt.After(a1.UpdatedAt))
|
|
|
|
a.DeleteIfNotModified(types.AlertSlice{a3})
|
|
|
|
// a1 should not be deleted.
|
|
|
|
got, err = a.Get(a1.Fingerprint())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, a1, got)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should not delete other alerts", func(t *testing.T) {
|
|
|
|
a := NewAlerts()
|
|
|
|
a1 := &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
UpdatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
a2 := &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
UpdatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
require.NoError(t, a.Set(a1))
|
|
|
|
require.NoError(t, a.Set(a2))
|
2018-09-03 12:52:53 +00:00
|
|
|
|
2024-05-07 09:34:03 +00:00
|
|
|
// Deleting a1 should not delete a2.
|
|
|
|
require.NoError(t, a.DeleteIfNotModified(types.AlertSlice{a1}))
|
|
|
|
// a1 should be deleted.
|
|
|
|
got, err := a.Get(a1.Fingerprint())
|
|
|
|
require.Equal(t, ErrNotFound, err)
|
|
|
|
require.Nil(t, got)
|
|
|
|
// a2 should not be deleted.
|
|
|
|
got, err = a.Get(a2.Fingerprint())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, a2, got)
|
|
|
|
})
|
2018-09-03 12:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGC(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
newAlert := func(key string, start, end time.Duration) *types.Alert {
|
|
|
|
return &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{model.LabelName(key): "b"},
|
|
|
|
StartsAt: now.Add(start * time.Minute),
|
|
|
|
EndsAt: now.Add(end * time.Minute),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
active := []*types.Alert{
|
|
|
|
newAlert("b", 10, 20),
|
|
|
|
newAlert("c", -10, 10),
|
|
|
|
}
|
|
|
|
resolved := []*types.Alert{
|
|
|
|
newAlert("a", -10, -5),
|
|
|
|
newAlert("d", -10, -1),
|
|
|
|
}
|
2019-09-18 07:29:34 +00:00
|
|
|
s := NewAlerts()
|
|
|
|
var (
|
2019-10-22 07:25:31 +00:00
|
|
|
n int
|
|
|
|
done = make(chan struct{})
|
|
|
|
ctx, cancel = context.WithCancel(context.Background())
|
2019-09-18 07:29:34 +00:00
|
|
|
)
|
2024-03-10 17:40:58 +00:00
|
|
|
s.SetGCCallback(func(a []types.Alert) {
|
2019-09-18 07:29:34 +00:00
|
|
|
n += len(a)
|
|
|
|
if n >= len(resolved) {
|
2019-10-22 07:25:31 +00:00
|
|
|
cancel()
|
2018-09-03 12:52:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
for _, alert := range append(active, resolved...) {
|
|
|
|
require.NoError(t, s.Set(alert))
|
|
|
|
}
|
2019-10-22 07:25:31 +00:00
|
|
|
go func() {
|
|
|
|
s.Run(ctx, 10*time.Millisecond)
|
|
|
|
close(done)
|
|
|
|
}()
|
2019-09-18 07:29:34 +00:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
break
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("garbage collection didn't complete in time")
|
|
|
|
}
|
2018-09-03 12:52:53 +00:00
|
|
|
|
|
|
|
for _, alert := range active {
|
|
|
|
if _, err := s.Get(alert.Fingerprint()); err != nil {
|
|
|
|
t.Errorf("alert %v should not have been gc'd", alert)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, alert := range resolved {
|
|
|
|
if _, err := s.Get(alert.Fingerprint()); err == nil {
|
|
|
|
t.Errorf("alert %v should have been gc'd", alert)
|
|
|
|
}
|
|
|
|
}
|
2023-12-10 08:33:13 +00:00
|
|
|
require.Len(t, resolved, n)
|
2018-09-03 12:52:53 +00:00
|
|
|
}
|