2021-05-18 09:31:51 -05:00
|
|
|
package channels
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
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-05-18 09:31:51 -05:00
|
|
|
"github.com/prometheus/alertmanager/notify"
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSensuGoNotifier(t *testing.T) {
|
2021-12-03 06:34:31 -06:00
|
|
|
constNow := time.Now()
|
|
|
|
defer mockTimeNow(constNow)()
|
|
|
|
|
2021-05-18 09:31:51 -05:00
|
|
|
tmpl := templateForTests(t)
|
|
|
|
|
|
|
|
externalURL, err := url.Parse("http://localhost")
|
|
|
|
require.NoError(t, err)
|
|
|
|
tmpl.ExternalURL = externalURL
|
|
|
|
|
2022-06-20 08:34:53 -05:00
|
|
|
images := newFakeImageStore(2)
|
2022-06-15 04:15:16 -05:00
|
|
|
|
2021-05-18 09:31:51 -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-05-18 09:31:51 -05:00
|
|
|
expMsgError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Default config with one alert",
|
|
|
|
settings: `{"url": "http://sensu-api.local:8080", "apikey": "<apikey>"}`,
|
|
|
|
alerts: []*types.Alert{
|
|
|
|
{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"__alert_rule_uid__": "rule uid", "alertname": "alert1", "lbl1": "val1"},
|
2022-07-04 05:05:36 -05:00
|
|
|
Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh", "__alertImageToken__": "test-image-1"},
|
2021-05-18 09:31:51 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expMsg: map[string]interface{}{
|
|
|
|
"entity": map[string]interface{}{
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "default",
|
|
|
|
"namespace": "default",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"check": map[string]interface{}{
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "default",
|
|
|
|
"labels": map[string]string{
|
2022-06-20 08:34:53 -05:00
|
|
|
"imageURL": "https://www.example.com/test-image-1.jpg",
|
2022-06-15 04:15:16 -05:00
|
|
|
"ruleURL": "http://localhost/alerting/list",
|
2021-05-18 09:31:51 -05:00
|
|
|
},
|
|
|
|
},
|
2022-03-02 06:06:35 -06:00
|
|
|
"output": "**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-12-03 06:34:31 -06:00
|
|
|
"issued": timeNow().Unix(),
|
2021-05-18 09:31:51 -05:00
|
|
|
"interval": 86400,
|
|
|
|
"status": 2,
|
|
|
|
"handlers": nil,
|
|
|
|
},
|
|
|
|
"ruleUrl": "http://localhost/alerting/list",
|
|
|
|
},
|
2021-07-19 03:58:35 -05:00
|
|
|
expMsgError: nil,
|
2021-05-18 09:31:51 -05:00
|
|
|
}, {
|
|
|
|
name: "Custom config with multiple alerts",
|
|
|
|
settings: `{
|
|
|
|
"url": "http://sensu-api.local:8080",
|
|
|
|
"entity": "grafana_instance_01",
|
|
|
|
"check": "grafana_rule_0",
|
|
|
|
"namespace": "namespace",
|
|
|
|
"handler": "myhandler",
|
|
|
|
"apikey": "<apikey>",
|
|
|
|
"message": "{{ len .Alerts.Firing }} alerts are firing, {{ len .Alerts.Resolved }} are resolved"
|
|
|
|
}`,
|
|
|
|
alerts: []*types.Alert{
|
|
|
|
{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"__alert_rule_uid__": "rule uid", "alertname": "alert1", "lbl1": "val1"},
|
2022-07-04 05:05:36 -05:00
|
|
|
Annotations: model.LabelSet{"ann1": "annv1", "__alertImageToken__": "test-image-1"},
|
2021-05-18 09:31:51 -05:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val2"},
|
2022-07-04 05:05:36 -05:00
|
|
|
Annotations: model.LabelSet{"ann1": "annv2", "__alertImageToken__": "test-image-2"},
|
2021-05-18 09:31:51 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expMsg: map[string]interface{}{
|
|
|
|
"entity": map[string]interface{}{
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "grafana_instance_01",
|
|
|
|
"namespace": "namespace",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"check": map[string]interface{}{
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "grafana_rule_0",
|
|
|
|
"labels": map[string]string{
|
2022-06-20 08:34:53 -05:00
|
|
|
"imageURL": "https://www.example.com/test-image-1.jpg",
|
2022-06-15 04:15:16 -05:00
|
|
|
"ruleURL": "http://localhost/alerting/list",
|
2021-05-18 09:31:51 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"output": "2 alerts are firing, 0 are resolved",
|
2021-12-03 06:34:31 -06:00
|
|
|
"issued": timeNow().Unix(),
|
2021-05-18 09:31:51 -05:00
|
|
|
"interval": 86400,
|
|
|
|
"status": 2,
|
|
|
|
"handlers": []string{"myhandler"},
|
|
|
|
},
|
|
|
|
"ruleUrl": "http://localhost/alerting/list",
|
|
|
|
},
|
2021-07-19 03:58:35 -05:00
|
|
|
expMsgError: nil,
|
2021-05-18 09:31:51 -05:00
|
|
|
}, {
|
|
|
|
name: "Error in initing: missing URL",
|
|
|
|
settings: `{
|
|
|
|
"apikey": "<apikey>"
|
|
|
|
}`,
|
2022-03-14 18:27:10 -05:00
|
|
|
expInitError: `could not find URL property in settings`,
|
2021-05-18 09:31:51 -05:00
|
|
|
}, {
|
|
|
|
name: "Error in initing: missing API key",
|
|
|
|
settings: `{
|
|
|
|
"url": "http://sensu-api.local:8080"
|
|
|
|
}`,
|
2022-03-14 18:27:10 -05:00
|
|
|
expInitError: `could not find the API key property in settings`,
|
2021-05-18 09:31:51 -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-05-18 09:31:51 -05:00
|
|
|
|
|
|
|
m := &NotificationChannelConfig{
|
2021-11-22 05:56:18 -06:00
|
|
|
Name: "Sensu Go",
|
|
|
|
Type: "sensugo",
|
|
|
|
Settings: settingsJSON,
|
|
|
|
SecureSettings: secureSettings,
|
2021-05-18 09:31:51 -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-11-18 08:24:12 -06:00
|
|
|
|
|
|
|
fc := FactoryConfig{
|
|
|
|
Config: m,
|
|
|
|
ImageStore: images,
|
|
|
|
NotificationService: webhookSender,
|
|
|
|
Template: tmpl,
|
|
|
|
DecryptFunc: decryptFn,
|
|
|
|
}
|
|
|
|
|
|
|
|
sn, err := NewSensuGoNotifier(fc)
|
2021-07-19 03:58:35 -05:00
|
|
|
if c.expInitError != "" {
|
2021-05-18 09:31:51 -05:00
|
|
|
require.Error(t, err)
|
2021-07-19 03:58:35 -05:00
|
|
|
require.Equal(t, c.expInitError, err.Error())
|
2021-05-18 09:31:51 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx := notify.WithGroupKey(context.Background(), "alertname")
|
|
|
|
ctx = notify.WithGroupLabels(ctx, model.LabelSet{"alertname": ""})
|
|
|
|
ok, err := sn.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)
|
2021-05-18 09:31:51 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|