Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ func matchFilterLabels(matchers []*labels.Matcher, sms map[string]string) bool {
if string(m.Value) == "" && !prs {
continue
}
if !prs || !m.Matches(string(v)) {
if !m.Matches(string(v)) {
return false
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func matchFilterLabels(matchers []*labels.Matcher, sms map[string]string) bool {
if m.Value == "" && !prs {
continue
}
if !prs || !m.Matches(v) {
if !m.Matches(v) {
return false
}
}
Expand Down
39 changes: 39 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,41 @@ 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))
}
}