alertmanager/pkg/parse/parse_test.go
stuart nelson 1e34f29532 Filter alerts (#633)
* Vendor dependencies.

This updates several old dependencies, removes
some that are no longer needed, and adds
`pkg/labels` from prometheus `dev-2.0` branch.

* Add metrics selector parsing code

This is a temporary simplified re-implementation
of promQL's metric selector parsing.

* Add alerts filtering

Filter alerts through `?filter=` query string.

* Add silences filtering

Filter silences through `?filter=` query string.

* Move `parse` to `pkg/parse`
2017-03-16 11:16:10 +01:00

117 lines
3.2 KiB
Go

package parse
import (
"reflect"
"testing"
"github.com/prometheus/prometheus/pkg/labels"
)
func TestMatchers(t *testing.T) {
testCases := []struct {
input string
want []*labels.Matcher
}{
{
input: `{foo="bar"}`,
want: func() []*labels.Matcher {
ms := []*labels.Matcher{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
return append(ms, m)
}(),
},
{
input: `{foo=~"bar.*"}`,
want: func() []*labels.Matcher {
ms := []*labels.Matcher{}
m, _ := labels.NewMatcher(labels.MatchRegexp, "foo", "bar.*")
return append(ms, m)
}(),
},
{
input: `{foo!="bar"}`,
want: func() []*labels.Matcher {
ms := []*labels.Matcher{}
m, _ := labels.NewMatcher(labels.MatchNotEqual, "foo", "bar")
return append(ms, m)
}(),
},
{
input: `{foo!~"bar.*"}`,
want: func() []*labels.Matcher {
ms := []*labels.Matcher{}
m, _ := labels.NewMatcher(labels.MatchNotRegexp, "foo", "bar.*")
return append(ms, m)
}(),
},
{
input: `{foo="bar", baz!="quux"}`,
want: func() []*labels.Matcher {
ms := []*labels.Matcher{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
m2, _ := labels.NewMatcher(labels.MatchNotEqual, "baz", "quux")
return append(ms, m, m2)
}(),
},
{
input: `{foo="bar", baz!~"quux.*"}`,
want: func() []*labels.Matcher {
ms := []*labels.Matcher{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
m2, _ := labels.NewMatcher(labels.MatchNotRegexp, "baz", "quux.*")
return append(ms, m, m2)
}(),
},
{
input: `{foo="bar",baz!~".*quux", derp="wat"}`,
want: func() []*labels.Matcher {
ms := []*labels.Matcher{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
m2, _ := labels.NewMatcher(labels.MatchNotRegexp, "baz", ".*quux")
m3, _ := labels.NewMatcher(labels.MatchEqual, "derp", "wat")
return append(ms, m, m2, m3)
}(),
},
{
input: `{foo="bar", baz!="quux", derp="wat"}`,
want: func() []*labels.Matcher {
ms := []*labels.Matcher{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
m2, _ := labels.NewMatcher(labels.MatchNotEqual, "baz", "quux")
m3, _ := labels.NewMatcher(labels.MatchEqual, "derp", "wat")
return append(ms, m, m2, m3)
}(),
},
{
input: `{foo="bar", baz!~".*quux.*", derp="wat"}`,
want: func() []*labels.Matcher {
ms := []*labels.Matcher{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
m2, _ := labels.NewMatcher(labels.MatchNotRegexp, "baz", ".*quux.*")
m3, _ := labels.NewMatcher(labels.MatchEqual, "derp", "wat")
return append(ms, m, m2, m3)
}(),
},
{
input: `{foo="bar", instance=~"some-api.*"}`,
want: func() []*labels.Matcher {
ms := []*labels.Matcher{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
m2, _ := labels.NewMatcher(labels.MatchRegexp, "instance", "some-api.*")
return append(ms, m, m2)
}(),
},
}
for i, tc := range testCases {
got, err := Matchers(tc.input)
if err != nil {
t.Fatalf("error (i=%d): %v", i, err)
}
if !reflect.DeepEqual(got, tc.want) {
t.Fatalf("error not equal (i=%d):\ngot %v\nwant %v", i, got, tc.want)
}
}
}