mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Fix flaky TestNotificationChannels take 2 (#34880)
* 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>
This commit is contained in:
parent
b73d168753
commit
8278c7bf77
@ -1,9 +1,13 @@
|
||||
package alerting
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
@ -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"
|
||||
@ -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())
|
||||
}
|
||||
|
||||
@ -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
|
||||
@ -279,11 +280,11 @@ 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])
|
||||
@ -291,24 +292,43 @@ func (nc *mockNotificationChannel) matchesExpNotifications(exp map[string][]stri
|
||||
}
|
||||
|
||||
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])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
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)
|
||||
|
||||
require.Equal(t, expMap, actMap)
|
||||
}
|
||||
|
||||
func (nc *mockNotificationChannel) Close() error {
|
||||
|
Loading…
Reference in New Issue
Block a user