Add tests to memory provider (#1104)

This commit is contained in:
Jose Donizetti 2018-01-21 12:27:21 -02:00 committed by stuart nelson
parent 63598904dc
commit 2fe013bcaa

View File

@ -18,12 +18,55 @@ import (
"testing"
"time"
"sync"
"github.com/kylelemons/godebug/pretty"
"github.com/prometheus/alertmanager/provider"
"github.com/prometheus/alertmanager/types"
"github.com/prometheus/common/model"
)
var (
t0 = time.Now()
t1 = t0.Add(100 * time.Millisecond)
alert1 = &types.Alert{
Alert: model.Alert{
Labels: model.LabelSet{"bar": "foo"},
Annotations: model.LabelSet{"foo": "bar"},
StartsAt: t0,
EndsAt: t1,
GeneratorURL: "http://example.com/prometheus",
},
UpdatedAt: t0,
Timeout: false,
}
alert2 = &types.Alert{
Alert: model.Alert{
Labels: model.LabelSet{"bar": "foo2"},
Annotations: model.LabelSet{"foo": "bar2"},
StartsAt: t0,
EndsAt: t1,
GeneratorURL: "http://example.com/prometheus",
},
UpdatedAt: t0,
Timeout: false,
}
alert3 = &types.Alert{
Alert: model.Alert{
Labels: model.LabelSet{"bar": "foo3"},
Annotations: model.LabelSet{"foo": "bar3"},
StartsAt: t0,
EndsAt: t1,
GeneratorURL: "http://example.com/prometheus",
},
UpdatedAt: t0,
Timeout: false,
}
)
func init() {
pretty.CompareConfig.IncludeUnexported = true
}
@ -35,44 +78,7 @@ func TestAlertsPut(t *testing.T) {
t.Fatal(err)
}
var (
t0 = time.Now()
t1 = t0.Add(10 * time.Minute)
)
insert := []*types.Alert{
{
Alert: model.Alert{
Labels: model.LabelSet{"bar": "foo"},
Annotations: model.LabelSet{"foo": "bar"},
StartsAt: t0,
EndsAt: t1,
GeneratorURL: "http://example.com/prometheus",
},
UpdatedAt: t0,
Timeout: false,
}, {
Alert: model.Alert{
Labels: model.LabelSet{"bar": "foo2"},
Annotations: model.LabelSet{"foo": "bar2"},
StartsAt: t0,
EndsAt: t1,
GeneratorURL: "http://example.com/prometheus",
},
UpdatedAt: t0,
Timeout: false,
}, {
Alert: model.Alert{
Labels: model.LabelSet{"bar": "foo3"},
Annotations: model.LabelSet{"foo": "bar3"},
StartsAt: t0,
EndsAt: t1,
GeneratorURL: "http://example.com/prometheus",
},
UpdatedAt: t0,
Timeout: false,
},
}
insert := []*types.Alert{alert1, alert2, alert3}
if err := alerts.Put(insert...); err != nil {
t.Fatalf("Insert failed: %s", err)
@ -90,6 +96,139 @@ func TestAlertsPut(t *testing.T) {
}
}
func TestAlertsSubscribe(t *testing.T) {
dir, err := ioutil.TempDir("", "alerts_test")
if err != nil {
t.Fatal(err)
}
marker := types.NewMarker()
alerts, err := NewAlerts(marker, 30*time.Minute, dir)
if err != nil {
t.Fatal(err)
}
// add alert1 to validate if pending alerts will be send
if err := alerts.Put(alert1); err != nil {
t.Fatalf("Insert failed: %s", err)
}
var wg sync.WaitGroup
wg.Add(2)
iterator1 := alerts.Subscribe()
iterator2 := alerts.Subscribe()
go func() {
defer wg.Done()
expectedAlerts := map[model.Fingerprint]*types.Alert{
alert1.Fingerprint(): alert1,
alert2.Fingerprint(): alert2,
alert3.Fingerprint(): alert3,
}
for i := 0; i < 3; i++ {
actual := <-iterator1.Next()
expected := expectedAlerts[actual.Fingerprint()]
if !alertsEqual(actual, expected) {
t.Errorf("Unexpected alert")
t.Fatalf(pretty.Compare(actual, expected))
}
delete(expectedAlerts, actual.Fingerprint())
}
if len(expectedAlerts) != 0 {
t.Fatalf("Unexpected number of alerts: %s", len(expectedAlerts))
}
}()
go func() {
defer wg.Done()
expectedAlerts := map[model.Fingerprint]*types.Alert{
alert1.Fingerprint(): alert1,
alert2.Fingerprint(): alert2,
alert3.Fingerprint(): alert3,
}
for i := 0; i < 3; i++ {
actual := <-iterator2.Next()
expected := expectedAlerts[actual.Fingerprint()]
if !alertsEqual(actual, expected) {
t.Errorf("Unexpected alert")
t.Fatalf(pretty.Compare(actual, expected))
}
delete(expectedAlerts, actual.Fingerprint())
}
if len(expectedAlerts) != 0 {
t.Fatalf("Unexpected number of alerts: %s", len(expectedAlerts))
}
}()
if err := alerts.Put(alert2); err != nil {
t.Fatalf("Insert failed: %s", err)
}
if err := alerts.Put(alert3); err != nil {
t.Fatalf("Insert failed: %s", err)
}
wg.Wait()
iterator1.Close()
iterator2.Close()
}
func TestAlertsGetPending(t *testing.T) {
dir, err := ioutil.TempDir("", "alerts_test")
if err != nil {
t.Fatal(err)
}
marker := types.NewMarker()
alerts, err := NewAlerts(marker, 30*time.Minute, dir)
if err != nil {
t.Fatal(err)
}
if err := alerts.Put(alert1, alert2); err != nil {
t.Fatalf("Insert failed: %s", err)
}
expectedAlerts := map[model.Fingerprint]*types.Alert{
alert1.Fingerprint(): alert1,
alert2.Fingerprint(): alert2,
}
iterator := alerts.GetPending()
for actual := range iterator.Next() {
expected := expectedAlerts[actual.Fingerprint()]
if !alertsEqual(actual, expected) {
t.Errorf("Unexpected alert")
t.Fatalf(pretty.Compare(actual, expected))
}
}
if err := alerts.Put(alert3); err != nil {
t.Fatalf("Insert failed: %s", err)
}
expectedAlerts = map[model.Fingerprint]*types.Alert{
alert1.Fingerprint(): alert1,
alert2.Fingerprint(): alert2,
alert3.Fingerprint(): alert3,
}
iterator = alerts.GetPending()
for actual := range iterator.Next() {
expected := expectedAlerts[actual.Fingerprint()]
if !alertsEqual(actual, expected) {
t.Errorf("Unexpected alert")
t.Fatalf(pretty.Compare(actual, expected))
}
}
}
func TestAlertsGC(t *testing.T) {
marker := types.NewMarker()
alerts, err := NewAlerts(marker, 200*time.Millisecond)
@ -97,44 +236,7 @@ func TestAlertsGC(t *testing.T) {
t.Fatal(err)
}
var (
t0 = time.Now()
t1 = t0.Add(100 * time.Millisecond)
)
insert := []*types.Alert{
{
Alert: model.Alert{
Labels: model.LabelSet{"bar": "foo"},
Annotations: model.LabelSet{"foo": "bar"},
StartsAt: t0,
EndsAt: t1,
GeneratorURL: "http://example.com/prometheus",
},
UpdatedAt: t0,
Timeout: false,
}, {
Alert: model.Alert{
Labels: model.LabelSet{"bar": "foo2"},
Annotations: model.LabelSet{"foo": "bar2"},
StartsAt: t0,
EndsAt: t1,
GeneratorURL: "http://example.com/prometheus",
},
UpdatedAt: t0,
Timeout: false,
}, {
Alert: model.Alert{
Labels: model.LabelSet{"bar": "foo3"},
Annotations: model.LabelSet{"foo": "bar3"},
StartsAt: t0,
EndsAt: t1,
GeneratorURL: "http://example.com/prometheus",
},
UpdatedAt: t0,
Timeout: false,
},
}
insert := []*types.Alert{alert1, alert2, alert3}
if err := alerts.Put(insert...); err != nil {
t.Fatalf("Insert failed: %s", err)