Add tests to receiver filtering (#1098)

This commit is contained in:
Jose Donizetti 2017-11-12 14:35:49 -02:00 committed by stuart nelson
parent 7fbe63b94f
commit 95e80d1aa8
2 changed files with 24 additions and 7 deletions

View File

@ -263,8 +263,8 @@ func (api *API) alertGroups(w http.ResponseWriter, r *http.Request) {
func (api *API) listAlerts(w http.ResponseWriter, r *http.Request) {
var (
err error
re *regexp.Regexp
err error
receiverFilter *regexp.Regexp
// Initialize result slice to prevent api returning `null` when there
// are no alerts present
res = []*dispatch.APIAlert{}
@ -315,7 +315,7 @@ func (api *API) listAlerts(w http.ResponseWriter, r *http.Request) {
}
if receiverParam := r.FormValue("receiver"); receiverParam != "" {
re, err = regexp.Compile("^(?:" + receiverParam + ")$")
receiverFilter, err = regexp.Compile("^(?:" + receiverParam + ")$")
if err != nil {
api.respondError(w, apiError{
typ: errorBadData,
@ -343,7 +343,7 @@ func (api *API) listAlerts(w http.ResponseWriter, r *http.Request) {
receivers = append(receivers, r.RouteOpts.Receiver)
}
if re != nil && !regexpAny(re, receivers) {
if receiverFilter != nil && !receiversMatchFilter(receivers, receiverFilter) {
continue
}
@ -389,9 +389,9 @@ func (api *API) listAlerts(w http.ResponseWriter, r *http.Request) {
api.respond(w, res)
}
func regexpAny(re *regexp.Regexp, ss []string) bool {
for _, s := range ss {
if re.MatchString(s) {
func receiversMatchFilter(receivers []string, filter *regexp.Regexp) bool {
for _, r := range receivers {
if filter.MatchString(r) {
return true
}
}

View File

@ -2,6 +2,7 @@ package api
import (
"fmt"
"regexp"
"testing"
"github.com/prometheus/common/model"
@ -88,3 +89,19 @@ func TestAlertFiltering(t *testing.T) {
require.Equal(t, test.expected, actual, msg)
}
}
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))
}