Skip to content

Commit

Permalink
Alerting: Fix flaky TestNotificationChannels take 2 (grafana#34880)
Browse files Browse the repository at this point in the history
* Alerting: Fix flaky TestNotificationChannels take 2

Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>

* Fix nits

Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>

* Fix lint

Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>
  • Loading branch information
codesome authored May 28, 2021
1 parent b73d168 commit 8278c7b
Showing 1 changed file with 46 additions and 26 deletions.
72 changes: 46 additions & 26 deletions pkg/tests/api/alerting/api_notification_channel_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package alerting

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"regexp"
"strings"
Expand All @@ -12,7 +16,6 @@ import (
"time"

"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/grafana/grafana/pkg/bus"
Expand Down Expand Up @@ -104,10 +107,11 @@ func TestNotificationChannels(t *testing.T) {
// Eventually, we'll get all the desired alerts.
// nolint:gosec
require.Eventually(t, func() bool {
return mockChannel.totalNotifications() == len(alertNames) &&
mockChannel.matchesExpNotifications(expNotifications)
return mockChannel.totalNotifications() == len(alertNames)
}, 30*time.Second, 1*time.Second)

mockChannel.matchesExpNotifications(t, expNotifications)

require.NoError(t, mockChannel.Close())
}

Expand Down Expand Up @@ -234,20 +238,17 @@ func (nc *mockNotificationChannel) totalNotifications() int {
return total
}

func (nc *mockNotificationChannel) matchesExpNotifications(exp map[string][]string) bool {
nc.t.Helper()
func (nc *mockNotificationChannel) matchesExpNotifications(t *testing.T, exp map[string][]string) {
t.Helper()
nc.receivedNotificationsMtx.Lock()
defer nc.receivedNotificationsMtx.Unlock()

if len(nc.receivedNotifications) != len(exp) {
return false
}
require.Len(t, nc.receivedNotifications, len(exp))

for expKey, expVals := range exp {
actVals, ok := nc.receivedNotifications[expKey]
if !ok || len(actVals) != len(expVals) {
return false
}
require.True(t, ok)
require.Len(t, actVals, len(expVals))
for i := range expVals {
expVal := expVals[i]
var r1, r2 *regexp.Regexp
Expand Down Expand Up @@ -279,36 +280,55 @@ func (nc *mockNotificationChannel) matchesExpNotifications(exp map[string][]stri
}
if r1 != nil {
parts := r1.FindStringSubmatch(actVals[i])
require.Equal(nc.t, 2, len(parts))
require.Len(t, parts, 2)
if expKey == "v1/alerts" {
// 2 fields for Prometheus Alertmanager.
parts2 := r2.FindStringSubmatch(actVals[i])
require.Equal(nc.t, 2, len(parts2))
require.Len(t, parts2, 2)
expVal = fmt.Sprintf(expVal, parts[1], parts2[1])
} else {
expVal = fmt.Sprintf(expVal, parts[1])
}
}

switch expKey {
case "pushover_recv/pushover_test", "telegram_recv/bot6sh027hs034h",
"line_recv/line_test", "threema_recv/threema_test":
// Multipart data or POST parameters.
if expVal != actVals[i] {
return false
}
case "line_recv/line_test", "threema_recv/threema_test":
// POST parameters.
require.Equal(t, expVal, actVals[i])
case "pushover_recv/pushover_test", "telegram_recv/bot6sh027hs034h":
// Multipart data.
multipartEqual(t, expVal, actVals[i])
default:
var expJson, actJson interface{}
require.NoError(nc.t, json.Unmarshal([]byte(expVal), &expJson))
require.NoError(nc.t, json.Unmarshal([]byte(actVals[i]), &actJson))
if !assert.ObjectsAreEqual(expJson, actJson) {
return false
}
require.JSONEq(t, expVal, actVals[i])
}
}
}
}

func multipartEqual(t *testing.T, exp, act string) {
t.Helper()

fillMap := func(r *multipart.Reader, m map[string]string) {
for {
part, err := r.NextPart()
if part == nil || errors.Is(err, io.EOF) {
break
}
require.NoError(t, err)
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(part)
require.NoError(t, err)
m[part.FormName()] = buf.String()
}
}

expReader := multipart.NewReader(strings.NewReader(exp), channels.GetBoundary())
actReader := multipart.NewReader(strings.NewReader(act), channels.GetBoundary())
expMap, actMap := make(map[string]string), make(map[string]string)
fillMap(expReader, expMap)
fillMap(actReader, actMap)

return true
require.Equal(t, expMap, actMap)
}

func (nc *mockNotificationChannel) Close() error {
Expand Down

0 comments on commit 8278c7b

Please sign in to comment.