1
2
3
4
5
6
7
8
9
10
11
12
13
14 package discord
15
16 import (
17 "context"
18 "encoding/json"
19 "fmt"
20 "net/http"
21 "net/http/httptest"
22 "net/url"
23 "testing"
24 "time"
25
26 "github.com/go-kit/log"
27 commoncfg "github.com/prometheus/common/config"
28 "github.com/prometheus/common/model"
29 "github.com/stretchr/testify/require"
30
31 "github.com/prometheus/alertmanager/config"
32 "github.com/prometheus/alertmanager/notify"
33 "github.com/prometheus/alertmanager/notify/test"
34 "github.com/prometheus/alertmanager/types"
35 )
36
37
38 var testWebhookURL, _ = url.Parse("https://discord.com/api/webhooks/971139602272503183/78ZWZ4V3xwZUBKRFF-G9m1nRtDtNTChl_WzW6Q4kxShjSB02oLSiPTPa8TS2tTGO9EYf")
39
40 func TestDiscordRetry(t *testing.T) {
41 notifier, err := New(
42 &config.DiscordConfig{
43 WebhookURL: &config.SecretURL{URL: testWebhookURL},
44 HTTPConfig: &commoncfg.HTTPClientConfig{},
45 },
46 test.CreateTmpl(t),
47 log.NewNopLogger(),
48 )
49 require.NoError(t, err)
50
51 for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) {
52 actual, _ := notifier.retrier.Check(statusCode, nil)
53 require.Equal(t, expected, actual, fmt.Sprintf("retry - error on status %d", statusCode))
54 }
55 }
56
57 func TestDiscordTemplating(t *testing.T) {
58 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
59 dec := json.NewDecoder(r.Body)
60 out := make(map[string]interface{})
61 err := dec.Decode(&out)
62 if err != nil {
63 panic(err)
64 }
65 }))
66 defer srv.Close()
67 u, _ := url.Parse(srv.URL)
68
69 for _, tc := range []struct {
70 title string
71 cfg *config.DiscordConfig
72
73 retry bool
74 errMsg string
75 }{
76 {
77 title: "full-blown message",
78 cfg: &config.DiscordConfig{
79 Title: `{{ template "discord.default.title" . }}`,
80 Message: `{{ template "discord.default.message" . }}`,
81 },
82 retry: false,
83 },
84 {
85 title: "title with templating errors",
86 cfg: &config.DiscordConfig{
87 Title: "{{ ",
88 },
89 errMsg: "template: :1: unclosed action",
90 },
91 {
92 title: "message with templating errors",
93 cfg: &config.DiscordConfig{
94 Title: `{{ template "discord.default.title" . }}`,
95 Message: "{{ ",
96 },
97 errMsg: "template: :1: unclosed action",
98 },
99 } {
100 t.Run(tc.title, func(t *testing.T) {
101 tc.cfg.WebhookURL = &config.SecretURL{URL: u}
102 tc.cfg.HTTPConfig = &commoncfg.HTTPClientConfig{}
103 pd, err := New(tc.cfg, test.CreateTmpl(t), log.NewNopLogger())
104 require.NoError(t, err)
105
106 ctx := context.Background()
107 ctx = notify.WithGroupKey(ctx, "1")
108
109 ok, err := pd.Notify(ctx, []*types.Alert{
110 {
111 Alert: model.Alert{
112 Labels: model.LabelSet{
113 "lbl1": "val1",
114 },
115 StartsAt: time.Now(),
116 EndsAt: time.Now().Add(time.Hour),
117 },
118 },
119 }...)
120 if tc.errMsg == "" {
121 require.NoError(t, err)
122 } else {
123 require.Error(t, err)
124 require.Contains(t, err.Error(), tc.errMsg)
125 }
126 require.Equal(t, tc.retry, ok)
127 })
128 }
129 }
130
View as plain text