Skip to content

Commit

Permalink
Add tests for matchFilterLabels in v2 api
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Araujo <vear91@gmail.com>
  • Loading branch information
vears91 committed Oct 30, 2020
1 parent 9c9d09b commit 1027f78
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions api/v2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
open_api_models "github.com/prometheus/alertmanager/api/v2/models"
general_ops "github.com/prometheus/alertmanager/api/v2/restapi/operations/general"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/pkg/labels"
"github.com/prometheus/alertmanager/types"
)

Expand Down Expand Up @@ -171,3 +172,40 @@ func TestAlertToOpenAPIAlert(t *testing.T) {
},
}, openAPIAlert)
}

func TestMatchFilterLabels(t *testing.T) {
sms := map[string]string{
"foo": "bar",
}

testCases := []struct {
matcher labels.MatchType
name string
val string
expected bool
}{
{labels.MatchEqual, "foo", "bar", true},
{labels.MatchEqual, "baz", "", true},
{labels.MatchEqual, "baz", "qux", false},
{labels.MatchEqual, "baz", "qux|", false},
{labels.MatchRegexp, "foo", "bar", true},
{labels.MatchRegexp, "baz", "", true},
{labels.MatchRegexp, "baz", "qux", false},
{labels.MatchRegexp, "baz", "qux|", true},
{labels.MatchNotEqual, "foo", "bar", false},
{labels.MatchNotEqual, "baz", "", false},
{labels.MatchNotEqual, "baz", "qux", true},
{labels.MatchNotEqual, "baz", "qux|", true},
{labels.MatchNotRegexp, "foo", "bar", false},
{labels.MatchNotRegexp, "baz", "", false},
{labels.MatchNotRegexp, "baz", "qux", true},
{labels.MatchNotRegexp, "baz", "qux|", false},
}

for _, tc := range testCases {
m, err := labels.NewMatcher(tc.matcher, tc.name, tc.val)
require.NoError(t, err)
ms := []*labels.Matcher{m}
require.Equal(t, tc.expected, matchFilterLabels(ms, sms))
}
}

0 comments on commit 1027f78

Please sign in to comment.