alertmanager/notify/notify_test.go

289 lines
6.5 KiB
Go
Raw Normal View History

2015-10-11 15:24:49 +00:00
// Copyright 2015 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.
2015-09-29 13:12:31 +00:00
package notify
2015-09-28 12:13:18 +00:00
import (
2015-09-28 19:43:28 +00:00
"fmt"
2015-09-28 12:13:18 +00:00
"reflect"
"testing"
2015-09-28 16:28:13 +00:00
"time"
2015-09-28 12:13:18 +00:00
"github.com/prometheus/common/model"
"golang.org/x/net/context"
2015-09-28 19:43:28 +00:00
"github.com/prometheus/alertmanager/provider"
2015-09-28 12:13:18 +00:00
"github.com/prometheus/alertmanager/types"
)
type recordNotifier struct {
2015-09-28 16:28:13 +00:00
ctx context.Context
2015-09-28 12:13:18 +00:00
alerts []*types.Alert
}
func (n *recordNotifier) Notify(ctx context.Context, as ...*types.Alert) error {
2015-09-28 16:28:13 +00:00
n.ctx = ctx
2015-09-28 12:13:18 +00:00
n.alerts = append(n.alerts, as...)
return nil
}
2015-09-28 19:43:28 +00:00
type failNotifier struct{}
func (n *failNotifier) Notify(ctx context.Context, as ...*types.Alert) error {
return fmt.Errorf("some error")
2015-09-28 19:43:28 +00:00
}
func TestDedupingNotifier(t *testing.T) {
var (
record = &recordNotifier{}
notifies = provider.NewMemNotifies(provider.NewMemData())
2015-10-11 14:54:31 +00:00
deduper = Dedup(notifies, record)
2015-09-28 19:43:28 +00:00
ctx = context.Background()
)
now := time.Now()
2015-11-10 12:47:04 +00:00
ctx = WithReceiver(ctx, "name")
ctx = WithRepeatInterval(ctx, time.Duration(100*time.Minute))
ctx = WithSendResolved(ctx, true)
ctx = WithNow(ctx, now)
2015-09-28 19:43:28 +00:00
alerts := []*types.Alert{
2015-10-01 12:55:55 +00:00
{
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "0"},
},
}, {
2015-10-01 12:55:55 +00:00
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "1"},
EndsAt: now.Add(-5 * time.Minute),
2015-10-01 12:55:55 +00:00
},
}, {
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "2"},
EndsAt: now.Add(-9 * time.Minute),
2015-10-01 12:55:55 +00:00
},
}, {
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "3"},
EndsAt: now.Add(-10 * time.Minute),
2015-10-01 12:55:55 +00:00
},
}, {
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "4"},
},
}, {
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "5"},
},
},
2015-09-28 19:43:28 +00:00
}
var fps []model.Fingerprint
for _, a := range alerts {
fps = append(fps, a.Fingerprint())
}
nsBefore := []*types.NotifyInfo{
// The first a new alert starting now.
nil,
// The second alert was not previously notified about and
// is already resolved.
2015-09-28 19:43:28 +00:00
nil,
// The third alert is an attempt to resolve a previously
// firing alert.
2015-09-28 19:43:28 +00:00
{
Alert: fps[2],
2015-11-10 12:47:04 +00:00
Receiver: "name",
2015-09-28 19:43:28 +00:00
Resolved: false,
Timestamp: now.Add(-10 * time.Minute),
},
// The fourth alert is an attempt to resolve an alert again
2015-09-28 19:43:28 +00:00
// even though the previous notification succeeded.
{
Alert: fps[3],
2015-11-10 12:47:04 +00:00
Receiver: "name",
2015-09-28 19:43:28 +00:00
Resolved: true,
Timestamp: now.Add(-10 * time.Minute),
},
// The fifth alert resends a previously successful notification
2015-09-28 19:43:28 +00:00
// that was longer than ago than the repeat interval.
{
Alert: fps[4],
2015-11-10 12:47:04 +00:00
Receiver: "name",
2015-09-28 19:43:28 +00:00
Resolved: false,
Timestamp: now.Add(-110 * time.Minute),
},
// The sixth alert is a firing again after being resolved before.
{
Alert: fps[5],
2015-11-10 12:47:04 +00:00
Receiver: "name",
Resolved: true,
Timestamp: now.Add(3 * time.Minute),
},
2015-09-28 19:43:28 +00:00
}
if err := notifies.Set(nsBefore...); err != nil {
2015-09-28 19:43:28 +00:00
t.Fatalf("Setting notifies failed: %s", err)
}
deduper.notifier = &failNotifier{}
if err := deduper.Notify(ctx, alerts...); err == nil {
t.Fatalf("Fail notifier did not fail")
}
// After a failing notify the notifies data must be unchanged.
nsCur, err := notifies.Get("name", fps...)
if err != nil {
t.Fatalf("Error getting notify info: %s", err)
2015-09-28 19:43:28 +00:00
}
if !reflect.DeepEqual(nsBefore, nsCur) {
t.Fatalf("Notify info data has changed unexpectedly")
2015-09-28 19:43:28 +00:00
}
deduper.notifier = record
if err := deduper.Notify(ctx, alerts...); err != nil {
t.Fatalf("Notify failed: %s", err)
}
alertsExp := []*types.Alert{
alerts[0],
alerts[2],
alerts[4],
2015-09-28 19:43:28 +00:00
alerts[5],
}
nsAfter := []*types.NotifyInfo{
2015-09-28 19:43:28 +00:00
{
Alert: fps[0],
2015-11-10 12:47:04 +00:00
Receiver: "name",
Resolved: false,
2015-09-28 19:43:28 +00:00
},
nil,
2015-09-28 19:43:28 +00:00
{
Alert: fps[2],
2015-11-10 12:47:04 +00:00
Receiver: "name",
Resolved: true,
2015-09-28 19:43:28 +00:00
},
nsBefore[3],
2015-09-28 19:43:28 +00:00
{
Alert: fps[4],
2015-11-10 12:47:04 +00:00
Receiver: "name",
Resolved: false,
2015-09-28 19:43:28 +00:00
},
{
Alert: fps[5],
2015-11-10 12:47:04 +00:00
Receiver: "name",
Resolved: false,
2015-09-28 19:43:28 +00:00
},
}
if !reflect.DeepEqual(record.alerts, alertsExp) {
t.Fatalf("Expected alerts %v, got %v", alertsExp, record.alerts)
}
nsCur, err = notifies.Get("name", fps...)
if err != nil {
2015-11-05 09:25:25 +00:00
t.Fatalf("Error getting notifies: %s", err)
}
for i, after := range nsAfter {
cur := nsCur[i]
// Hack correct timestamps back in if they are sane.
if cur != nil && after.Timestamp.IsZero() {
if cur.Timestamp.Before(now) {
t.Fatalf("Wrong timestamp for notify %v", cur)
}
after.Timestamp = cur.Timestamp
}
if !reflect.DeepEqual(after, cur) {
t.Errorf("Unexpected notifies, expected: %v, got: %v", after, cur)
}
}
2015-09-28 19:43:28 +00:00
}
2015-09-28 16:28:13 +00:00
func TestRoutedNotifier(t *testing.T) {
2015-10-11 14:54:31 +00:00
router := Router{
2015-09-28 16:28:13 +00:00
"1": &recordNotifier{},
"2": &recordNotifier{},
"3": &recordNotifier{},
}
for _, route := range []string{"3", "2", "1"} {
var (
2015-11-10 12:47:04 +00:00
ctx = WithReceiver(context.Background(), route)
2015-09-28 16:28:13 +00:00
alert = &types.Alert{
2015-10-01 12:53:49 +00:00
Alert: model.Alert{
Labels: model.LabelSet{"route": model.LabelValue(route)},
},
2015-09-28 16:28:13 +00:00
}
)
2015-10-11 14:54:31 +00:00
err := router.Notify(ctx, alert)
2015-09-28 16:28:13 +00:00
if err != nil {
t.Fatal(err)
}
2015-10-11 14:54:31 +00:00
rn := router[route].(*recordNotifier)
2015-09-28 16:28:13 +00:00
if len(rn.alerts) != 1 && alert != rn.alerts[0] {
t.Fatalf("Expeceted alert %v, got %v", alert, rn.alerts)
}
}
}
2015-09-28 12:13:18 +00:00
func TestMutingNotifier(t *testing.T) {
// Mute all label sets that have a "mute" key.
muter := types.MuteFunc(func(lset model.LabelSet) bool {
2015-09-28 16:28:13 +00:00
_, ok := lset["mute"]
return ok
2015-09-28 12:13:18 +00:00
})
record := &recordNotifier{}
2015-10-11 14:54:31 +00:00
muteNotifer := Mute(muter, record)
2015-09-28 12:13:18 +00:00
in := []model.LabelSet{
{},
{"test": "set"},
{"mute": "me"},
{"foo": "bar", "test": "set"},
{"foo": "bar", "mute": "me"},
{},
{"not": "muted"},
}
out := []model.LabelSet{
{},
{"test": "set"},
{"foo": "bar", "test": "set"},
{},
{"not": "muted"},
}
var inAlerts []*types.Alert
for _, lset := range in {
2015-10-01 12:53:49 +00:00
inAlerts = append(inAlerts, &types.Alert{
Alert: model.Alert{Labels: lset},
})
2015-09-28 12:13:18 +00:00
}
if err := muteNotifer.Notify(nil, inAlerts...); err != nil {
t.Fatalf("Notifying failed: %s", err)
}
var got []model.LabelSet
for _, a := range record.alerts {
got = append(got, a.Labels)
}
if !reflect.DeepEqual(got, out) {
t.Fatalf("Muting failed, expected: %v\ngot %v", out, got)
}
}