2017-11-04 13:38:16 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-11-12 16:35:49 +00:00
|
|
|
"regexp"
|
2017-11-04 13:38:16 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-11-15 19:29:06 +00:00
|
|
|
"github.com/prometheus/alertmanager/types"
|
2017-11-04 13:38:16 +00:00
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAlertFiltering(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
alert *model.Alert
|
|
|
|
msg string
|
|
|
|
expected bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal
|
|
|
|
equal, err := labels.NewMatcher(labels.MatchEqual, "label1", "test1")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []test{
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test1"}}, "label1=test1", true},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test2"}}, "label1=test2", false},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label2": "test2"}}, "label2=test2", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := alertMatchesFilterLabels(test.alert, []*labels.Matcher{equal})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not Equal
|
|
|
|
notEqual, err := labels.NewMatcher(labels.MatchNotEqual, "label1", "test1")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests = []test{
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test1"}}, "label1!=test1", false},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test2"}}, "label1!=test2", true},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label2": "test2"}}, "label2!=test2", true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := alertMatchesFilterLabels(test.alert, []*labels.Matcher{notEqual})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regexp Equal
|
|
|
|
regexpEqual, err := labels.NewMatcher(labels.MatchRegexp, "label1", "tes.*")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests = []test{
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test1"}}, "label1=~test1", true},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test2"}}, "label1=~test2", true},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label2": "test2"}}, "label2=~test2", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := alertMatchesFilterLabels(test.alert, []*labels.Matcher{regexpEqual})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regexp Not Equal
|
|
|
|
regexpNotEqual, err := labels.NewMatcher(labels.MatchNotRegexp, "label1", "tes.*")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests = []test{
|
2017-11-15 19:25:46 +00:00
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test1"}}, "label1!~test1", false},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test2"}}, "label1!~test2", false},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label2": "test2"}}, "label2!~test2", true},
|
2017-11-04 13:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := alertMatchesFilterLabels(test.alert, []*labels.Matcher{regexpNotEqual})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
}
|
2017-11-12 16:35:49 +00:00
|
|
|
|
2017-11-15 19:29:06 +00:00
|
|
|
func TestSilenceFiltering(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
silence *types.Silence
|
|
|
|
msg string
|
|
|
|
expected bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal
|
|
|
|
equal, err := labels.NewMatcher(labels.MatchEqual, "label1", "test1")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []test{
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test1"})},
|
|
|
|
"label1=test1",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test2"})},
|
|
|
|
"label1=test2",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label2": "test2"})},
|
|
|
|
"label2=test2",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := silenceMatchesFilterLabels(test.silence, []*labels.Matcher{equal})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not Equal
|
|
|
|
notEqual, err := labels.NewMatcher(labels.MatchNotEqual, "label1", "test1")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests = []test{
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test1"})},
|
|
|
|
"label1!=test1",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test2"})},
|
|
|
|
"label1!=test2",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label2": "test2"})},
|
|
|
|
"label2!=test2",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := silenceMatchesFilterLabels(test.silence, []*labels.Matcher{notEqual})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regexp Equal
|
|
|
|
regexpEqual, err := labels.NewMatcher(labels.MatchRegexp, "label1", "tes.*")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests = []test{
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test1"})},
|
|
|
|
"label1=~test1",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test2"})},
|
|
|
|
"label1=~test2",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label2": "test2"})},
|
|
|
|
"label2=~test2",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := silenceMatchesFilterLabels(test.silence, []*labels.Matcher{regexpEqual})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regexp Not Equal
|
|
|
|
regexpNotEqual, err := labels.NewMatcher(labels.MatchNotRegexp, "label1", "tes.*")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests = []test{
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test1"})},
|
|
|
|
"label1!~test1",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test2"})},
|
|
|
|
"label1!~test2",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label2": "test2"})},
|
|
|
|
"label2!~test2",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := silenceMatchesFilterLabels(test.silence, []*labels.Matcher{regexpNotEqual})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 16:35:49 +00:00
|
|
|
func TestReceiversMatchFilter(t *testing.T) {
|
|
|
|
receivers := []string{"pagerduty", "slack", "hipchat"}
|
|
|
|
|
|
|
|
filter, err := regexp.Compile(fmt.Sprintf("^(?:%s)$", "hip.*"))
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
require.True(t, receiversMatchFilter(receivers, filter))
|
|
|
|
|
|
|
|
filter, err = regexp.Compile(fmt.Sprintf("^(?:%s)$", "hip"))
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
require.False(t, receiversMatchFilter(receivers, filter))
|
|
|
|
}
|
2017-11-15 19:29:06 +00:00
|
|
|
|
|
|
|
func newMatcher(labelSet model.LabelSet) types.Matchers {
|
|
|
|
matchers := make([]*types.Matcher, 0, len(labelSet))
|
|
|
|
for key, val := range labelSet {
|
|
|
|
matchers = append(matchers, types.NewMatcher(key, string(val)))
|
|
|
|
}
|
|
|
|
return matchers
|
|
|
|
}
|