Skip to content

Commit

Permalink
Merge pull request #1087 from prymitive/silences-api
Browse files Browse the repository at this point in the history
fix(api): better handling for silence search terms
  • Loading branch information
prymitive authored Oct 27, 2019
2 parents d5e951d + 30ce99b commit 6aec574
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 23 deletions.
6 changes: 1 addition & 5 deletions cmd/karma/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
138 changes: 120 additions & 18 deletions cmd/karma/views_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"sort"
"testing"
"time"

Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
}
}

0 comments on commit 6aec574

Please sign in to comment.