2024-06-13 14:08:02 -07:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-07-16 10:15:28 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2024-06-13 14:08:02 -07:00
|
|
|
"github.com/prometheus/common/promslog"
|
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAlertStore(t *testing.T) {
|
|
|
|
alertStore := NewFileStore(promslog.NewNopLogger(), "alertstoretest")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Remove("alertstoretest")
|
|
|
|
})
|
|
|
|
|
|
|
|
alertsByRule := make(map[uint64][]*Alert)
|
|
|
|
baseTime := time.Now()
|
|
|
|
al1 := &Alert{State: StateFiring, Labels: labels.FromStrings("a1", "1"), Annotations: labels.FromStrings("annotation1", "a1"), ActiveAt: baseTime, KeepFiringSince: baseTime}
|
|
|
|
al2 := &Alert{State: StateFiring, Labels: labels.FromStrings("a2", "2"), Annotations: labels.FromStrings("annotation2", "a2"), ActiveAt: baseTime, KeepFiringSince: baseTime}
|
|
|
|
|
|
|
|
alertsByRule[1] = []*Alert{al1, al2}
|
|
|
|
alertsByRule[2] = []*Alert{al2}
|
|
|
|
alertsByRule[3] = []*Alert{al1}
|
|
|
|
alertsByRule[4] = []*Alert{}
|
|
|
|
|
|
|
|
for key, alerts := range alertsByRule {
|
2024-07-16 10:15:28 -07:00
|
|
|
sortAlerts(alerts)
|
2024-06-13 14:08:02 -07:00
|
|
|
err := alertStore.SetAlerts(key, alerts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
got, err := alertStore.GetAlerts(key)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(alerts), len(got))
|
2024-07-16 10:15:28 -07:00
|
|
|
|
|
|
|
result := make([]*Alert, 0, len(got))
|
|
|
|
for _, value := range got {
|
|
|
|
result = append(result, value)
|
|
|
|
}
|
|
|
|
sortAlerts(result)
|
|
|
|
|
2024-06-13 14:08:02 -07:00
|
|
|
j := 0
|
2024-07-16 10:15:28 -07:00
|
|
|
for _, al := range result {
|
2024-06-13 14:08:02 -07:00
|
|
|
require.Equal(t, alerts[j].State, al.State)
|
|
|
|
require.Equal(t, alerts[j].Labels, al.Labels)
|
|
|
|
require.Equal(t, alerts[j].Annotations, al.Annotations)
|
|
|
|
require.Equal(t, alerts[j].ActiveAt, al.ActiveAt)
|
|
|
|
require.Equal(t, alerts[j].KeepFiringSince, al.KeepFiringSince)
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|