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:
Ganesh Vernekar 2021-05-28 15:24:18 +05:30 committed by GitHub
parent b73d168753
commit 8278c7bf77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,13 @@
package alerting package alerting
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"mime/multipart"
"net/http" "net/http"
"regexp" "regexp"
"strings" "strings"
@ -12,7 +16,6 @@ import (
"time" "time"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
@ -104,10 +107,11 @@ func TestNotificationChannels(t *testing.T) {
// Eventually, we'll get all the desired alerts. // Eventually, we'll get all the desired alerts.
// nolint:gosec // nolint:gosec
require.Eventually(t, func() bool { require.Eventually(t, func() bool {
return mockChannel.totalNotifications() == len(alertNames) && return mockChannel.totalNotifications() == len(alertNames)
mockChannel.matchesExpNotifications(expNotifications)
}, 30*time.Second, 1*time.Second) }, 30*time.Second, 1*time.Second)
mockChannel.matchesExpNotifications(t, expNotifications)
require.NoError(t, mockChannel.Close()) require.NoError(t, mockChannel.Close())
} }
@ -234,20 +238,17 @@ func (nc *mockNotificationChannel) totalNotifications() int {
return total return total
} }
func (nc *mockNotificationChannel) matchesExpNotifications(exp map[string][]string) bool { func (nc *mockNotificationChannel) matchesExpNotifications(t *testing.T, exp map[string][]string) {
nc.t.Helper() t.Helper()
nc.receivedNotificationsMtx.Lock() nc.receivedNotificationsMtx.Lock()
defer nc.receivedNotificationsMtx.Unlock() defer nc.receivedNotificationsMtx.Unlock()
if len(nc.receivedNotifications) != len(exp) { require.Len(t, nc.receivedNotifications, len(exp))
return false
}
for expKey, expVals := range exp { for expKey, expVals := range exp {
actVals, ok := nc.receivedNotifications[expKey] actVals, ok := nc.receivedNotifications[expKey]
if !ok || len(actVals) != len(expVals) { require.True(t, ok)
return false require.Len(t, actVals, len(expVals))
}
for i := range expVals { for i := range expVals {
expVal := expVals[i] expVal := expVals[i]
var r1, r2 *regexp.Regexp var r1, r2 *regexp.Regexp
@ -279,11 +280,11 @@ func (nc *mockNotificationChannel) matchesExpNotifications(exp map[string][]stri
} }
if r1 != nil { if r1 != nil {
parts := r1.FindStringSubmatch(actVals[i]) parts := r1.FindStringSubmatch(actVals[i])
require.Equal(nc.t, 2, len(parts)) require.Len(t, parts, 2)
if expKey == "v1/alerts" { if expKey == "v1/alerts" {
// 2 fields for Prometheus Alertmanager. // 2 fields for Prometheus Alertmanager.
parts2 := r2.FindStringSubmatch(actVals[i]) 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]) expVal = fmt.Sprintf(expVal, parts[1], parts2[1])
} else { } else {
expVal = fmt.Sprintf(expVal, parts[1]) expVal = fmt.Sprintf(expVal, parts[1])
@ -291,24 +292,43 @@ func (nc *mockNotificationChannel) matchesExpNotifications(exp map[string][]stri
} }
switch expKey { switch expKey {
case "pushover_recv/pushover_test", "telegram_recv/bot6sh027hs034h", case "line_recv/line_test", "threema_recv/threema_test":
"line_recv/line_test", "threema_recv/threema_test": // POST parameters.
// Multipart data or POST parameters. require.Equal(t, expVal, actVals[i])
if expVal != actVals[i] { case "pushover_recv/pushover_test", "telegram_recv/bot6sh027hs034h":
return false // Multipart data.
} multipartEqual(t, expVal, actVals[i])
default: default:
var expJson, actJson interface{} require.JSONEq(t, expVal, actVals[i])
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
}
} }
} }
} }
}
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 { func (nc *mockNotificationChannel) Close() error {