Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test for Notify of the Discord integration #4082

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions notify/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
if n.conf.WebhookURL != nil {
url = n.conf.WebhookURL.String()
} else {
content, err := os.ReadFile(n.conf.WebhookURLFile)
b, err := os.ReadFile(n.conf.WebhookURLFile)
if err != nil {
return false, fmt.Errorf("read webhook_url_file: %w", err)
}
url = strings.TrimSpace(string(content))
url = strings.TrimSpace(string(b))
}

w := webhook{
Expand Down
61 changes: 61 additions & 0 deletions notify/discord/discord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -168,3 +169,63 @@ func TestDiscordReadingURLFromFile(t *testing.T) {

test.AssertNotifyLeaksNoSecret(ctx, t, notifier, u.String())
}

func TestDiscord_Notify(t *testing.T) {
// Create a fake HTTP server to simulate the Discord webhook
var resp string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Read the request as a string
body, err := io.ReadAll(r.Body)
require.NoError(t, err, "reading request body failed")
// Store the request body in the response
resp = string(body)

w.WriteHeader(http.StatusOK)
}))

// Create a temporary file to simulate the WebhookURLFile
tempFile, err := os.CreateTemp("", "webhook_url")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, os.Remove(tempFile.Name()))
})

// Write the fake webhook URL to the temp file
_, err = tempFile.WriteString(srv.URL)
require.NoError(t, err)

// Create a DiscordConfig with the WebhookURLFile set
cfg := &config.DiscordConfig{
WebhookURLFile: tempFile.Name(),
HTTPConfig: &commoncfg.HTTPClientConfig{},
Title: "Test Title",
Message: "Test Message",
Content: "Test Content",
}

// Create a new Discord notifier
notifier, err := New(cfg, test.CreateTmpl(t), log.NewNopLogger())
require.NoError(t, err)

// Create a context and alerts
ctx := context.Background()
ctx = notify.WithGroupKey(ctx, "1")
alerts := []*types.Alert{
{
Alert: model.Alert{
Labels: model.LabelSet{
"lbl1": "val1",
},
StartsAt: time.Now(),
EndsAt: time.Now().Add(time.Hour),
},
},
}

// Call the Notify method
ok, err := notifier.Notify(ctx, alerts...)
require.NoError(t, err)
require.False(t, ok)

require.Equal(t, "{\"content\":\"Test Content\",\"embeds\":[{\"title\":\"Test Title\",\"description\":\"Test Message\",\"color\":10038562}]}\n", resp)
}
Loading