2024-12-12 13:50:47 -08:00
|
|
|
// Copyright 2025 The Prometheus Authors
|
|
|
|
// 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.
|
|
|
|
|
2024-06-13 14:08:02 -07:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-07-30 10:18:03 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2024-12-12 13:50:47 -08:00
|
|
|
"github.com/prometheus/common/promslog"
|
2024-07-16 10:15:28 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2024-06-13 14:08:02 -07:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAlertStore(t *testing.T) {
|
2024-07-30 10:18:03 -07:00
|
|
|
alertStore := NewFileStore(promslog.NewNopLogger(), "alertstoretest", prometheus.NewRegistry())
|
2024-06-13 14:08:02 -07:00
|
|
|
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-07-30 10:18:03 -07:00
|
|
|
err := alertStore.SetAlerts(key, "test/test1", alerts)
|
2024-06-13 14:08:02 -07:00
|
|
|
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++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|