Skip to content

Commit 615d5ff

Browse files
authored
Add a test for Notify of the Discord integration (prometheus#4082)
Just to ensure this works correclty as expected, I originally thought there was a bug with the shadowing of the `content` varible but there isn't - to avoid further confusion I have followed up on this document left by George: prometheus#3555 (comment) Signed-off-by: gotjosh <[email protected]>
1 parent 6b77acd commit 615d5ff

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

notify/discord/discord.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
137137
if n.conf.WebhookURL != nil {
138138
url = n.conf.WebhookURL.String()
139139
} else {
140-
content, err := os.ReadFile(n.conf.WebhookURLFile)
140+
b, err := os.ReadFile(n.conf.WebhookURLFile)
141141
if err != nil {
142142
return false, fmt.Errorf("read webhook_url_file: %w", err)
143143
}
144-
url = strings.TrimSpace(string(content))
144+
url = strings.TrimSpace(string(b))
145145
}
146146

147147
w := webhook{

notify/discord/discord_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"context"
1818
"encoding/json"
1919
"fmt"
20+
"io"
2021
"net/http"
2122
"net/http/httptest"
2223
"net/url"
@@ -168,3 +169,63 @@ func TestDiscordReadingURLFromFile(t *testing.T) {
168169

169170
test.AssertNotifyLeaksNoSecret(ctx, t, notifier, u.String())
170171
}
172+
173+
func TestDiscord_Notify(t *testing.T) {
174+
// Create a fake HTTP server to simulate the Discord webhook
175+
var resp string
176+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
177+
// Read the request as a string
178+
body, err := io.ReadAll(r.Body)
179+
require.NoError(t, err, "reading request body failed")
180+
// Store the request body in the response
181+
resp = string(body)
182+
183+
w.WriteHeader(http.StatusOK)
184+
}))
185+
186+
// Create a temporary file to simulate the WebhookURLFile
187+
tempFile, err := os.CreateTemp("", "webhook_url")
188+
require.NoError(t, err)
189+
t.Cleanup(func() {
190+
require.NoError(t, os.Remove(tempFile.Name()))
191+
})
192+
193+
// Write the fake webhook URL to the temp file
194+
_, err = tempFile.WriteString(srv.URL)
195+
require.NoError(t, err)
196+
197+
// Create a DiscordConfig with the WebhookURLFile set
198+
cfg := &config.DiscordConfig{
199+
WebhookURLFile: tempFile.Name(),
200+
HTTPConfig: &commoncfg.HTTPClientConfig{},
201+
Title: "Test Title",
202+
Message: "Test Message",
203+
Content: "Test Content",
204+
}
205+
206+
// Create a new Discord notifier
207+
notifier, err := New(cfg, test.CreateTmpl(t), log.NewNopLogger())
208+
require.NoError(t, err)
209+
210+
// Create a context and alerts
211+
ctx := context.Background()
212+
ctx = notify.WithGroupKey(ctx, "1")
213+
alerts := []*types.Alert{
214+
{
215+
Alert: model.Alert{
216+
Labels: model.LabelSet{
217+
"lbl1": "val1",
218+
},
219+
StartsAt: time.Now(),
220+
EndsAt: time.Now().Add(time.Hour),
221+
},
222+
},
223+
}
224+
225+
// Call the Notify method
226+
ok, err := notifier.Notify(ctx, alerts...)
227+
require.NoError(t, err)
228+
require.False(t, ok)
229+
230+
require.Equal(t, "{\"content\":\"Test Content\",\"embeds\":[{\"title\":\"Test Title\",\"description\":\"Test Message\",\"color\":10038562}]}\n", resp)
231+
}

0 commit comments

Comments
 (0)