2021-04-23 08:29:28 -05:00
|
|
|
package channels
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-04 11:47:21 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
"github.com/grafana/grafana/pkg/services/secrets/fakes"
|
|
|
|
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
|
2021-10-07 09:33:50 -05:00
|
|
|
|
2021-04-23 08:29:28 -05:00
|
|
|
"github.com/prometheus/alertmanager/notify"
|
|
|
|
"github.com/prometheus/alertmanager/template"
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWebhookNotifier(t *testing.T) {
|
2021-05-12 04:43:43 -05:00
|
|
|
tmpl := templateForTests(t)
|
2021-04-23 08:29:28 -05:00
|
|
|
|
|
|
|
externalURL, err := url.Parse("http://localhost")
|
|
|
|
require.NoError(t, err)
|
|
|
|
tmpl.ExternalURL = externalURL
|
|
|
|
|
2021-10-08 07:52:44 -05:00
|
|
|
orgID := int64(1)
|
|
|
|
|
2021-04-23 08:29:28 -05:00
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
settings string
|
|
|
|
alerts []*types.Alert
|
|
|
|
expMsg *webhookMessage
|
|
|
|
expUrl string
|
|
|
|
expUsername string
|
|
|
|
expPassword string
|
|
|
|
expHttpMethod string
|
2021-07-19 03:58:35 -05:00
|
|
|
expInitError string
|
2021-04-23 08:29:28 -05:00
|
|
|
expMsgError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Default config with one alert",
|
|
|
|
settings: `{"url": "http://localhost/test"}`,
|
|
|
|
alerts: []*types.Alert{
|
|
|
|
{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"},
|
2021-05-26 09:49:39 -05:00
|
|
|
Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"},
|
2021-04-23 08:29:28 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expUrl: "http://localhost/test",
|
|
|
|
expHttpMethod: "POST",
|
|
|
|
expMsg: &webhookMessage{
|
2021-05-26 09:49:39 -05:00
|
|
|
ExtendedData: &ExtendedData{
|
2021-04-23 08:29:28 -05:00
|
|
|
Receiver: "my_receiver",
|
|
|
|
Status: "firing",
|
2021-05-26 09:49:39 -05:00
|
|
|
Alerts: ExtendedAlerts{
|
2021-04-23 08:29:28 -05:00
|
|
|
{
|
|
|
|
Status: "firing",
|
|
|
|
Labels: template.KV{
|
|
|
|
"alertname": "alert1",
|
|
|
|
"lbl1": "val1",
|
|
|
|
},
|
|
|
|
Annotations: template.KV{
|
|
|
|
"ann1": "annv1",
|
|
|
|
},
|
2021-05-26 09:49:39 -05:00
|
|
|
Fingerprint: "fac0861a85de433a",
|
|
|
|
DashboardURL: "http://localhost/d/abcd",
|
|
|
|
PanelURL: "http://localhost/d/abcd?viewPanel=efgh",
|
2022-03-02 06:06:35 -06:00
|
|
|
SilenceURL: "http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1",
|
2021-04-23 08:29:28 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
GroupLabels: template.KV{
|
|
|
|
"alertname": "",
|
|
|
|
},
|
|
|
|
CommonLabels: template.KV{
|
|
|
|
"alertname": "alert1",
|
|
|
|
"lbl1": "val1",
|
|
|
|
},
|
|
|
|
CommonAnnotations: template.KV{
|
|
|
|
"ann1": "annv1",
|
|
|
|
},
|
|
|
|
ExternalURL: "http://localhost",
|
|
|
|
},
|
|
|
|
Version: "1",
|
|
|
|
GroupKey: "alertname",
|
|
|
|
Title: "[FIRING:1] (val1)",
|
|
|
|
State: "alerting",
|
2022-03-02 06:06:35 -06:00
|
|
|
Message: "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n",
|
2021-10-08 07:52:44 -05:00
|
|
|
OrgID: orgID,
|
2021-04-23 08:29:28 -05:00
|
|
|
},
|
2021-07-19 03:58:35 -05:00
|
|
|
expMsgError: nil,
|
2021-04-23 08:29:28 -05:00
|
|
|
}, {
|
|
|
|
name: "Custom config with multiple alerts",
|
|
|
|
settings: `{
|
|
|
|
"url": "http://localhost/test1",
|
|
|
|
"username": "user1",
|
|
|
|
"password": "mysecret",
|
|
|
|
"httpMethod": "PUT",
|
|
|
|
"maxAlerts": 2
|
|
|
|
}`,
|
|
|
|
alerts: []*types.Alert{
|
|
|
|
{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"},
|
|
|
|
Annotations: model.LabelSet{"ann1": "annv1"},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val2"},
|
|
|
|
Annotations: model.LabelSet{"ann1": "annv2"},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val3"},
|
|
|
|
Annotations: model.LabelSet{"ann1": "annv3"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expUrl: "http://localhost/test1",
|
|
|
|
expHttpMethod: "PUT",
|
|
|
|
expUsername: "user1",
|
|
|
|
expPassword: "mysecret",
|
|
|
|
expMsg: &webhookMessage{
|
2021-05-26 09:49:39 -05:00
|
|
|
ExtendedData: &ExtendedData{
|
2021-04-23 08:29:28 -05:00
|
|
|
Receiver: "my_receiver",
|
|
|
|
Status: "firing",
|
2021-05-26 09:49:39 -05:00
|
|
|
Alerts: ExtendedAlerts{
|
2021-04-23 08:29:28 -05:00
|
|
|
{
|
|
|
|
Status: "firing",
|
|
|
|
Labels: template.KV{
|
|
|
|
"alertname": "alert1",
|
|
|
|
"lbl1": "val1",
|
|
|
|
},
|
|
|
|
Annotations: template.KV{
|
|
|
|
"ann1": "annv1",
|
|
|
|
},
|
|
|
|
Fingerprint: "fac0861a85de433a",
|
2022-03-02 06:06:35 -06:00
|
|
|
SilenceURL: "http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1",
|
2021-04-23 08:29:28 -05:00
|
|
|
}, {
|
|
|
|
Status: "firing",
|
|
|
|
Labels: template.KV{
|
|
|
|
"alertname": "alert1",
|
|
|
|
"lbl1": "val2",
|
|
|
|
},
|
|
|
|
Annotations: template.KV{
|
|
|
|
"ann1": "annv2",
|
|
|
|
},
|
|
|
|
Fingerprint: "fab6861a85d5eeb5",
|
2022-03-02 06:06:35 -06:00
|
|
|
SilenceURL: "http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval2",
|
2021-04-23 08:29:28 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
GroupLabels: template.KV{
|
|
|
|
"alertname": "",
|
|
|
|
},
|
|
|
|
CommonLabels: template.KV{
|
|
|
|
"alertname": "alert1",
|
|
|
|
},
|
|
|
|
CommonAnnotations: template.KV{},
|
|
|
|
ExternalURL: "http://localhost",
|
|
|
|
},
|
|
|
|
Version: "1",
|
|
|
|
GroupKey: "alertname",
|
|
|
|
TruncatedAlerts: 1,
|
|
|
|
Title: "[FIRING:2] ",
|
|
|
|
State: "alerting",
|
2022-03-02 06:06:35 -06:00
|
|
|
Message: "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval2\n",
|
2021-10-08 07:52:44 -05:00
|
|
|
OrgID: orgID,
|
2021-04-23 08:29:28 -05:00
|
|
|
},
|
2021-07-19 03:58:35 -05:00
|
|
|
expMsgError: nil,
|
2021-04-23 08:29:28 -05:00
|
|
|
}, {
|
|
|
|
name: "Error in initing",
|
|
|
|
settings: `{}`,
|
2022-03-14 18:27:10 -05:00
|
|
|
expInitError: `could not find url property in settings`,
|
2021-04-23 08:29:28 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
settingsJSON, err := simplejson.NewJson([]byte(c.settings))
|
|
|
|
require.NoError(t, err)
|
2021-11-22 05:56:18 -06:00
|
|
|
secureSettings := make(map[string][]byte)
|
2021-04-23 08:29:28 -05:00
|
|
|
|
2021-05-18 03:04:47 -05:00
|
|
|
m := &NotificationChannelConfig{
|
2021-11-22 05:56:18 -06:00
|
|
|
OrgID: orgID,
|
|
|
|
Name: "webhook_testing",
|
|
|
|
Type: "webhook",
|
|
|
|
Settings: settingsJSON,
|
|
|
|
SecureSettings: secureSettings,
|
2021-04-23 08:29:28 -05:00
|
|
|
}
|
|
|
|
|
2022-01-26 09:42:40 -06:00
|
|
|
webhookSender := mockNotificationService()
|
2021-11-04 11:47:21 -05:00
|
|
|
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())
|
|
|
|
decryptFn := secretsService.GetDecryptedValue
|
2022-03-14 18:27:10 -05:00
|
|
|
cfg, err := NewWebHookConfig(m, decryptFn)
|
2021-07-19 03:58:35 -05:00
|
|
|
if c.expInitError != "" {
|
2021-04-23 08:29:28 -05:00
|
|
|
require.Error(t, err)
|
2021-07-19 03:58:35 -05:00
|
|
|
require.Equal(t, c.expInitError, err.Error())
|
2021-04-23 08:29:28 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx := notify.WithGroupKey(context.Background(), "alertname")
|
|
|
|
ctx = notify.WithGroupLabels(ctx, model.LabelSet{"alertname": ""})
|
|
|
|
ctx = notify.WithReceiverName(ctx, "my_receiver")
|
2022-05-23 03:44:19 -05:00
|
|
|
pn := NewWebHookNotifier(cfg, webhookSender, &UnavailableImageStore{}, tmpl)
|
2021-04-23 08:29:28 -05:00
|
|
|
ok, err := pn.Notify(ctx, c.alerts...)
|
|
|
|
if c.expMsgError != nil {
|
|
|
|
require.False(t, ok)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, c.expMsgError.Error(), err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
expBody, err := json.Marshal(c.expMsg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-26 09:42:40 -06:00
|
|
|
require.JSONEq(t, string(expBody), webhookSender.Webhook.Body)
|
|
|
|
require.Equal(t, c.expUrl, webhookSender.Webhook.Url)
|
|
|
|
require.Equal(t, c.expUsername, webhookSender.Webhook.User)
|
|
|
|
require.Equal(t, c.expPassword, webhookSender.Webhook.Password)
|
|
|
|
require.Equal(t, c.expHttpMethod, webhookSender.Webhook.HttpMethod)
|
2021-04-23 08:29:28 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|