-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathemail_test.go
101 lines (90 loc) · 3.28 KB
/
email_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package email
import (
"context"
"net/url"
"testing"
"github.com/prometheus/alertmanager/types"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
"github.com/grafana/alerting/images"
"github.com/grafana/alerting/logging"
"github.com/grafana/alerting/receivers"
"github.com/grafana/alerting/templates"
)
func TestNotify(t *testing.T) {
tmpl := templates.ForTests(t)
externalURL, err := url.Parse("http://localhost/base")
require.NoError(t, err)
tmpl.ExternalURL = externalURL
t.Run("with the correct settings it should not fail and produce the expected command", func(t *testing.T) {
settings := Config{
SingleEmail: false,
Addresses: []string{
"someops@example.com",
"somedev@example.com",
},
Message: "{{ template \"default.title\" . }}",
Subject: templates.DefaultMessageTitleEmbed,
}
emailSender := receivers.MockNotificationService()
emailNotifier := &Notifier{
Base: &receivers.Base{
Name: "",
Type: "",
UID: "",
DisableResolveMessage: false,
},
log: &logging.FakeLogger{},
ns: emailSender,
tmpl: tmpl,
settings: settings,
images: &images.UnavailableImageStore{},
}
alerts := []*types.Alert{
{
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "AlwaysFiring", "severity": "warning"},
Annotations: model.LabelSet{"runbook_url": "http://fix.me", "__dashboardUid__": "abc", "__panelId__": "5"},
},
},
}
ok, err := emailNotifier.Notify(context.Background(), alerts...)
require.NoError(t, err)
require.True(t, ok)
expected := map[string]interface{}{
"subject": emailSender.EmailSync.Subject,
"to": emailSender.EmailSync.To,
"single_email": emailSender.EmailSync.SingleEmail,
"template": emailSender.EmailSync.Template,
"data": emailSender.EmailSync.Data,
}
require.Equal(t, map[string]interface{}{
"subject": "[FIRING:1] (AlwaysFiring warning)",
"to": []string{"someops@example.com", "somedev@example.com"},
"single_email": false,
"template": "ng_alert_notification",
"data": map[string]interface{}{
"Title": "[FIRING:1] (AlwaysFiring warning)",
"Message": "[FIRING:1] (AlwaysFiring warning)",
"Status": "firing",
"Alerts": templates.ExtendedAlerts{
templates.ExtendedAlert{
Status: "firing",
Labels: templates.KV{"alertname": "AlwaysFiring", "severity": "warning"},
Annotations: templates.KV{"runbook_url": "http://fix.me"},
Fingerprint: "15a37193dce72bab",
SilenceURL: "http://localhost/base/alerting/silence/new?alertmanager=grafana&matcher=alertname%3DAlwaysFiring&matcher=severity%3Dwarning",
DashboardURL: "http://localhost/base/d/abc",
PanelURL: "http://localhost/base/d/abc?viewPanel=5",
},
},
"GroupLabels": templates.KV{},
"CommonLabels": templates.KV{"alertname": "AlwaysFiring", "severity": "warning"},
"CommonAnnotations": templates.KV{"runbook_url": "http://fix.me"},
"ExternalURL": "http://localhost/base",
"RuleUrl": "http://localhost/base/alerting/list",
"AlertPageUrl": "http://localhost/base/alerting/list?alertState=firing&view=state",
},
}, expected)
})
}