2021-04-22 11:16:26 -05:00
|
|
|
package channels
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/notify"
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTeamsNotifier(t *testing.T) {
|
2021-05-12 04:43:43 -05:00
|
|
|
tmpl := templateForTests(t)
|
2021-04-22 11:16:26 -05:00
|
|
|
|
2021-04-23 08:29:28 -05:00
|
|
|
externalURL, err := url.Parse("http://localhost")
|
|
|
|
require.NoError(t, err)
|
|
|
|
tmpl.ExternalURL = externalURL
|
|
|
|
|
2021-04-22 11:16:26 -05:00
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
settings string
|
|
|
|
alerts []*types.Alert
|
|
|
|
expMsg map[string]interface{}
|
2021-07-19 03:58:35 -05:00
|
|
|
expInitError string
|
2021-04-22 11:16:26 -05:00
|
|
|
expMsgError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Default config with one alert",
|
|
|
|
settings: `{"url": "http://localhost"}`,
|
|
|
|
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-22 11:16:26 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expMsg: map[string]interface{}{
|
|
|
|
"@type": "MessageCard",
|
|
|
|
"@context": "http://schema.org/extensions",
|
2021-05-17 05:35:09 -05:00
|
|
|
"summary": "[FIRING:1] (val1)",
|
|
|
|
"title": "[FIRING:1] (val1)",
|
2021-04-22 11:16:26 -05:00
|
|
|
"themeColor": "#D63232",
|
|
|
|
"sections": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"title": "Details",
|
2022-03-02 06:06:35 -06:00
|
|
|
"text": "**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-04-22 11:16:26 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"potentialAction": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"@context": "http://schema.org",
|
|
|
|
"@type": "OpenUri",
|
|
|
|
"name": "View Rule",
|
2021-05-20 03:12:08 -05:00
|
|
|
"targets": []map[string]interface{}{{"os": "default", "uri": "http://localhost/alerting/list"}},
|
2021-04-22 11:16:26 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-07-19 03:58:35 -05:00
|
|
|
expMsgError: nil,
|
2021-04-22 11:16:26 -05:00
|
|
|
}, {
|
|
|
|
name: "Custom config with multiple alerts",
|
|
|
|
settings: `{
|
|
|
|
"url": "http://localhost",
|
|
|
|
"message": "{{ len .Alerts.Firing }} alerts are firing, {{ len .Alerts.Resolved }} are resolved"
|
|
|
|
}`,
|
|
|
|
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"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expMsg: map[string]interface{}{
|
|
|
|
"@type": "MessageCard",
|
|
|
|
"@context": "http://schema.org/extensions",
|
2021-05-17 05:35:09 -05:00
|
|
|
"summary": "[FIRING:2] ",
|
|
|
|
"title": "[FIRING:2] ",
|
2021-04-22 11:16:26 -05:00
|
|
|
"themeColor": "#D63232",
|
|
|
|
"sections": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"title": "Details",
|
|
|
|
"text": "2 alerts are firing, 0 are resolved",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"potentialAction": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"@context": "http://schema.org",
|
|
|
|
"@type": "OpenUri",
|
|
|
|
"name": "View Rule",
|
2021-05-20 03:12:08 -05:00
|
|
|
"targets": []map[string]interface{}{{"os": "default", "uri": "http://localhost/alerting/list"}},
|
2021-04-22 11:16:26 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-07-19 03:58:35 -05:00
|
|
|
expMsgError: nil,
|
2022-03-31 13:57:48 -05:00
|
|
|
}, {
|
|
|
|
name: "Missing field in template",
|
|
|
|
settings: `{
|
|
|
|
"url": "http://localhost",
|
|
|
|
"message": "I'm a custom template {{ .NotAField }} bad template"
|
|
|
|
}`,
|
|
|
|
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"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expMsg: map[string]interface{}{
|
|
|
|
"@type": "MessageCard",
|
|
|
|
"@context": "http://schema.org/extensions",
|
|
|
|
"summary": "[FIRING:2] ",
|
|
|
|
"title": "[FIRING:2] ",
|
|
|
|
"themeColor": "#D63232",
|
|
|
|
"sections": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"title": "Details",
|
|
|
|
"text": "I'm a custom template ",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"potentialAction": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"@context": "http://schema.org",
|
|
|
|
"@type": "OpenUri",
|
|
|
|
"name": "View Rule",
|
|
|
|
"targets": []map[string]interface{}{{"os": "default", "uri": "http://localhost/alerting/list"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expMsgError: nil,
|
|
|
|
}, {
|
|
|
|
name: "Invalid template",
|
|
|
|
settings: `{
|
|
|
|
"url": "http://localhost",
|
|
|
|
"message": "I'm a custom template {{ {.NotAField }} bad template"
|
|
|
|
}`,
|
|
|
|
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"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expMsg: map[string]interface{}{
|
|
|
|
"@type": "MessageCard",
|
|
|
|
"@context": "http://schema.org/extensions",
|
|
|
|
"summary": "[FIRING:2] ",
|
|
|
|
"title": "[FIRING:2] ",
|
|
|
|
"themeColor": "#D63232",
|
|
|
|
"sections": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"title": "Details",
|
|
|
|
"text": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"potentialAction": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"@context": "http://schema.org",
|
|
|
|
"@type": "OpenUri",
|
|
|
|
"name": "View Rule",
|
|
|
|
"targets": []map[string]interface{}{{"os": "default", "uri": "http://localhost/alerting/list"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expMsgError: nil,
|
2021-04-22 11:16:26 -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-22 11:16:26 -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-05-18 03:04:47 -05:00
|
|
|
m := &NotificationChannelConfig{
|
2021-04-22 11:16:26 -05:00
|
|
|
Name: "teams_testing",
|
|
|
|
Type: "teams",
|
|
|
|
Settings: settingsJSON,
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:42:40 -06:00
|
|
|
webhookSender := mockNotificationService()
|
2022-03-14 18:27:10 -05:00
|
|
|
cfg, err := NewTeamsConfig(m)
|
2021-07-19 03:58:35 -05:00
|
|
|
if c.expInitError != "" {
|
2021-04-22 11:16:26 -05:00
|
|
|
require.Error(t, err)
|
2021-07-19 03:58:35 -05:00
|
|
|
require.Equal(t, c.expInitError, err.Error())
|
2021-04-22 11:16:26 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx := notify.WithGroupKey(context.Background(), "alertname")
|
|
|
|
ctx = notify.WithGroupLabels(ctx, model.LabelSet{"alertname": ""})
|
2022-03-14 18:27:10 -05:00
|
|
|
pn := NewTeamsNotifier(cfg, webhookSender, tmpl)
|
2021-04-22 11:16:26 -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.True(t, ok)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-31 13:57:48 -05:00
|
|
|
require.NotEmpty(t, webhookSender.Webhook.Url)
|
|
|
|
|
2021-04-22 11:16:26 -05:00
|
|
|
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)
|
2021-04-22 11:16:26 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|