NGAlert: Add integration tests for remaining notification channels (#34662) (#34728)

Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>
(cherry picked from commit b168223029)

Co-authored-by: Ganesh Vernekar <15064823+codesome@users.noreply.github.com>
This commit is contained in:
Grot (@grafanabot)
2021-05-26 19:59:34 +05:30
committed by GitHub
co-authored by Ganesh Vernekar
parent ebe20d1998
commit 81f2f68535
8 changed files with 748 additions and 30 deletions
@@ -432,8 +432,6 @@ func (am *Alertmanager) buildReceiverIntegrations(receiver *apimodels.PostableAp
n, err = channels.NewSensuGoNotifier(cfg, tmpl)
case "discord":
n, err = channels.NewDiscordNotifier(cfg, tmpl)
case "alertmanager":
n, err = channels.NewAlertmanagerNotifier(cfg, tmpl)
case "googlechat":
n, err = channels.NewGoogleChatNotifier(cfg, tmpl)
case "line":
@@ -3,6 +3,7 @@ package channels
import (
"context"
"fmt"
"strings"
gokit_log "github.com/go-kit/kit/log"
"github.com/prometheus/alertmanager/notify"
@@ -103,7 +104,7 @@ func (kn *KafkaNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool,
return false, err
}
topicURL := kn.Endpoint + "/topics/" + kn.Topic
topicURL := strings.TrimRight(kn.Endpoint, "/") + "/topics/" + kn.Topic
cmd := &models.SendWebhookSync{
Url: topicURL,
@@ -18,7 +18,7 @@ import (
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
)
const (
var (
LineNotifyURL string = "https://notify-api.line.me/api/notify"
)
@@ -20,16 +20,10 @@ import (
"github.com/prometheus/common/model"
)
const (
PUSHOVERENDPOINT = "https://api.pushover.net/1/messages.json"
var (
PushoverEndpoint = "https://api.pushover.net/1/messages.json"
)
// getBoundary is used for overriding the behaviour for tests
// and set a boundary
var getBoundary = func() string {
return ""
}
// PushoverNotifier is responsible for sending
// alert notifications to Pushover
type PushoverNotifier struct {
@@ -51,6 +45,10 @@ type PushoverNotifier struct {
// NewSlackNotifier is the constructor for the Slack notifier
func NewPushoverNotifier(model *NotificationChannelConfig, t *template.Template) (*PushoverNotifier, error) {
if model.Settings == nil {
return nil, alerting.ValidationError{Reason: "No settings supplied"}
}
userKey := model.DecryptedValue("userKey", model.Settings.Get("userKey").MustString())
APIToken := model.DecryptedValue("apiToken", model.Settings.Get("apiToken").MustString())
device := model.Settings.Get("device").MustString()
@@ -108,7 +106,7 @@ func (pn *PushoverNotifier) Notify(ctx context.Context, as ...*types.Alert) (boo
}
cmd := &models.SendWebhookSync{
Url: PUSHOVERENDPOINT,
Url: PushoverEndpoint,
HttpMethod: "POST",
HttpHeader: headers,
Body: uploadBody.String(),
@@ -140,7 +138,7 @@ func (pn *PushoverNotifier) genPushoverBody(ctx context.Context, as ...*types.Al
tmpl := notify.TmplText(pn.tmpl, data, &tmplErr)
w := multipart.NewWriter(&b)
boundary := getBoundary()
boundary := GetBoundary()
if boundary != "" {
err = w.SetBoundary(boundary)
if err != nil {
@@ -133,13 +133,13 @@ func TestPushoverNotifier(t *testing.T) {
}
for _, c := range cases {
origGetBoundary := getBoundary
origGetBoundary := GetBoundary
boundary := "abcd"
getBoundary = func() string {
GetBoundary = func() string {
return boundary
}
t.Cleanup(func() {
getBoundary = origGetBoundary
GetBoundary = origGetBoundary
})
t.Run(c.name, func(t *testing.T) {
@@ -82,6 +82,13 @@ func (tn *TelegramNotifier) Notify(ctx context.Context, as ...*types.Alert) (boo
tn.log.Warn("Failed to close writer", "err", err)
}
}()
boundary := GetBoundary()
if boundary != "" {
err = w.SetBoundary(boundary)
if err != nil {
return false, err
}
}
for k, v := range msg {
if err := writeField(w, k, v); err != nil {
@@ -122,3 +122,9 @@ func joinUrlPath(base, additionalPath string) (string, error) {
return u.String(), nil
}
// GetBoundary is used for overriding the behaviour for tests
// and set a boundary for multipart body. DO NOT set this outside tests.
var GetBoundary = func() string {
return ""
}