From 30ce99b3c5535bba247510ebae18d94696e23a07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sun, 27 Oct 2019 16:15:44 +0000 Subject: [PATCH] fix(api): better handling for silence search terms --- cmd/karma/views.go | 6 +- cmd/karma/views_test.go | 138 ++++++++++++++++++++++++++++++++++------ 2 files changed, 121 insertions(+), 23 deletions(-) diff --git a/cmd/karma/views.go b/cmd/karma/views.go index 9592e73f0..84b9a027c 100644 --- a/cmd/karma/views.go +++ b/cmd/karma/views.go @@ -445,11 +445,7 @@ func silences(c *gin.Context) { } if searchTerm == fmt.Sprintf("%s%s\"%s\"", strings.ToLower(match.Name), eq, strings.ToLower(match.Value)) { isMatch = true - } else if searchTerm == fmt.Sprintf("%s%s%s", match.Name, eq, match.Value) { - isMatch = true - } else if strings.Contains(strings.ToLower(match.Name), searchTerm) { - isMatch = true - } else if strings.Contains(strings.ToLower(match.Value), searchTerm) { + } else if strings.Contains(strings.ToLower(fmt.Sprintf("%s%s%s", match.Name, eq, match.Value)), searchTerm) { isMatch = true } } diff --git a/cmd/karma/views_test.go b/cmd/karma/views_test.go index ed0c4f22e..2cfe9afc7 100644 --- a/cmd/karma/views_test.go +++ b/cmd/karma/views_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "os" + "sort" "testing" "time" @@ -19,6 +20,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/gin-gonic/gin" + "github.com/google/go-cmp/cmp" "github.com/jarcoal/httpmock" ) @@ -586,25 +588,125 @@ func TestValidateAuthorFromHeaders(t *testing.T) { } func TestSilences(t *testing.T) { + type silenceTestCase struct { + searchTerm string + sortReverse string + showExpired string + results []string + } + + silenceServer7 := "Silenced server7" + silenceHostDown := "Silenced Host_Down alerts in the dev cluster" + silenceInstance := "Silenced instance" + + silenceTestCases := []silenceTestCase{ + { + searchTerm: "", + sortReverse: "", + showExpired: "", + results: []string{silenceHostDown, silenceInstance, silenceServer7}, + }, + { + searchTerm: "", + sortReverse: "1", + showExpired: "1", + results: []string{silenceHostDown, silenceInstance, silenceServer7}, + }, + { + searchTerm: "john", + sortReverse: "", + showExpired: "", + results: []string{silenceHostDown, silenceInstance, silenceServer7}, + }, + { + searchTerm: "john@example.com", + sortReverse: "", + showExpired: "", + results: []string{silenceHostDown, silenceInstance, silenceServer7}, + }, + { + searchTerm: "instance=web", + sortReverse: "0", + showExpired: "0", + results: []string{silenceInstance}, + }, + { + searchTerm: "instance=web1", + sortReverse: "1", + showExpired: "0", + results: []string{silenceInstance}, + }, + { + searchTerm: "instance=\"web1\"", + sortReverse: "0", + showExpired: "0", + results: []string{silenceInstance}, + }, + { + searchTerm: "instance=\"web", + sortReverse: "0", + showExpired: "0", + results: []string{}, + }, + { + searchTerm: "instance=web123", + sortReverse: "0", + showExpired: "1", + results: []string{}, + }, + { + searchTerm: "alertname=Host_Down", + sortReverse: "0", + showExpired: "1", + results: []string{silenceHostDown}, + }, + { + searchTerm: "instance", + sortReverse: "0", + showExpired: "1", + results: []string{silenceInstance, silenceServer7}, + }, + { + searchTerm: "nstance", + sortReverse: "0", + showExpired: "1", + results: []string{silenceInstance, silenceServer7}, + }, + { + searchTerm: "alertname=~Host_Down", + sortReverse: "0", + showExpired: "1", + results: []string{}, + }, + } + mockConfig() - for _, version := range mock.ListAllMocks() { - t.Logf("Validating silences.json response using mock files from Alertmanager %s", version) - mockAlerts(version) - r := ginTestEngine() - req := httptest.NewRequest("GET", "/silences.json?showExpired=1&sortReverse=1&searchTerm=a", nil) - resp := httptest.NewRecorder() - r.ServeHTTP(resp, req) - if resp.Code != http.StatusOK { - t.Errorf("GET /silences.json returned status %d", resp.Code) - } - ur := []models.ManagedSilence{} - body := resp.Body.Bytes() - err := json.Unmarshal(body, &ur) - if err != nil { - t.Errorf("Failed to unmarshal response: %s", err) - } - if len(ur) != 3 { - t.Errorf("Incorrect number of silences: got %d, wanted 3", len(ur)) + for _, testCase := range silenceTestCases { + for _, version := range mock.ListAllMocks() { + t.Logf("Validating silences.json response using mock files from Alertmanager %s", version) + mockAlerts(version) + r := ginTestEngine() + uri := fmt.Sprintf("/silences.json?showExpired=%s&sortReverse=%s&searchTerm=%s", testCase.showExpired, testCase.sortReverse, testCase.searchTerm) + req := httptest.NewRequest("GET", uri, nil) + resp := httptest.NewRecorder() + r.ServeHTTP(resp, req) + if resp.Code != http.StatusOK { + t.Errorf("GET /silences.json returned status %d", resp.Code) + } + ur := []models.ManagedSilence{} + body := resp.Body.Bytes() + err := json.Unmarshal(body, &ur) + if err != nil { + t.Errorf("Failed to unmarshal response: %s", err) + } + results := []string{} + for _, silence := range ur { + results = append(results, silence.Silence.Comment) + } + sort.Strings(results) // can't rely on API order since it's sorted based on timestamps, resort + if diff := cmp.Diff(testCase.results, results); diff != "" { + t.Errorf("Wrong silences returned (-want +got):\n%s", diff) + } } } }