|
14 | 14 | package telegram
|
15 | 15 |
|
16 | 16 | import (
|
| 17 | + "context" |
| 18 | + "encoding/json" |
17 | 19 | "fmt"
|
| 20 | + "io" |
| 21 | + "net/http" |
| 22 | + "net/http/httptest" |
18 | 23 | "net/url"
|
19 | 24 | "testing"
|
| 25 | + "time" |
20 | 26 |
|
21 | 27 | "github.com/go-kit/log"
|
22 | 28 | commoncfg "github.com/prometheus/common/config"
|
| 29 | + "github.com/prometheus/common/model" |
23 | 30 | "github.com/stretchr/testify/require"
|
24 | 31 | "gopkg.in/yaml.v2"
|
25 | 32 |
|
26 | 33 | "github.com/prometheus/alertmanager/config"
|
| 34 | + "github.com/prometheus/alertmanager/notify" |
27 | 35 | "github.com/prometheus/alertmanager/notify/test"
|
| 36 | + "github.com/prometheus/alertmanager/types" |
28 | 37 | )
|
29 | 38 |
|
30 | 39 | func TestTelegramUnmarshal(t *testing.T) {
|
@@ -73,3 +82,71 @@ func TestTelegramRetry(t *testing.T) {
|
73 | 82 | require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode))
|
74 | 83 | }
|
75 | 84 | }
|
| 85 | + |
| 86 | +func TestTelegramNotify(t *testing.T) { |
| 87 | + for _, tc := range []struct { |
| 88 | + name string |
| 89 | + cfg config.TelegramConfig |
| 90 | + expText string |
| 91 | + }{ |
| 92 | + { |
| 93 | + name: "No escaping by default", |
| 94 | + cfg: config.TelegramConfig{ |
| 95 | + Message: "<code>x < y</code>", |
| 96 | + HTTPConfig: &commoncfg.HTTPClientConfig{}, |
| 97 | + }, |
| 98 | + expText: "<code>x < y</code>", |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "Characters escaped in HTML mode", |
| 102 | + cfg: config.TelegramConfig{ |
| 103 | + ParseMode: "HTML", |
| 104 | + Message: "<code>x < y</code>", |
| 105 | + HTTPConfig: &commoncfg.HTTPClientConfig{}, |
| 106 | + }, |
| 107 | + expText: "<code>x < y</code>", |
| 108 | + }, |
| 109 | + } { |
| 110 | + t.Run(tc.name, func(t *testing.T) { |
| 111 | + var out []byte |
| 112 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 113 | + var err error |
| 114 | + out, err = io.ReadAll(r.Body) |
| 115 | + require.NoError(t, err) |
| 116 | + w.Write([]byte(`{"ok":true,"result":{"chat":{}}}`)) |
| 117 | + })) |
| 118 | + defer srv.Close() |
| 119 | + u, _ := url.Parse(srv.URL) |
| 120 | + |
| 121 | + tc.cfg.APIUrl = &config.URL{URL: u} |
| 122 | + |
| 123 | + notifier, err := New(&tc.cfg, test.CreateTmpl(t), log.NewNopLogger()) |
| 124 | + require.NoError(t, err) |
| 125 | + |
| 126 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 127 | + defer cancel() |
| 128 | + ctx = notify.WithGroupKey(ctx, "1") |
| 129 | + |
| 130 | + retry, err := notifier.Notify(ctx, []*types.Alert{ |
| 131 | + { |
| 132 | + Alert: model.Alert{ |
| 133 | + Labels: model.LabelSet{ |
| 134 | + "lbl1": "val1", |
| 135 | + "lbl3": "val3", |
| 136 | + }, |
| 137 | + StartsAt: time.Now(), |
| 138 | + EndsAt: time.Now().Add(time.Hour), |
| 139 | + }, |
| 140 | + }, |
| 141 | + }...) |
| 142 | + |
| 143 | + require.False(t, retry) |
| 144 | + require.NoError(t, err) |
| 145 | + |
| 146 | + req := map[string]string{} |
| 147 | + err = json.Unmarshal(out, &req) |
| 148 | + require.NoError(t, err) |
| 149 | + require.Equal(t, tc.expText, req["text"]) |
| 150 | + }) |
| 151 | + } |
| 152 | +} |
0 commit comments