Files
seaweedfs/weed/filer/filer_notification_webhooks_test.go
T
Chris LuandClaude Opus 5.5 95f46c8fff admin: manage filer webhook targets from the UI with live reload (#12690)
* webhook: share the default target settings

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* webhook: managed webhook targets with a drainable queue

Targets kept as JSON (name, endpoint, token, filters, retry and worker
settings) build their own queue; Close waits for queued and in-flight
deliveries before stopping it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: run webhook targets stored in /etc/seaweedfs when notification.webhook.managed is set

The targets in /etc/seaweedfs/notification_webhooks.json start with the
filer and are rebuilt from the store whenever the file changes on any
filer, alongside notification.toml. The previous queue drains before it
stops. Events for the file itself, which holds bearer tokens, are not
sent to notification queues.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* admin: manage filer webhook targets

A Webhooks page under Management lists, adds, edits and deletes the
targets kept in /etc/seaweedfs/notification_webhooks.json. Bearer
tokens are write-only: the API reports whether one is set, and an empty
token on edit keeps the stored one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: keep the webhook queue when the stored targets did not change

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: keep a replaced webhook queue open until its senders hand over

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* admin: read chunk-backed webhook targets before editing them

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: keep the stored webhook targets out of fs.meta.notify

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* webhook: reject negative managed target settings

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* webhook: no signal handler on queues that reloads replace

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: swap the webhook queue without waiting behind blocked senders

The read lock held across SendMessage made a reload wait for any sender
blocked on a saturated target, and the waiting writer stalled every
other filer write with it, which is when an operator disables that
target. Close now refuses new sends and waits for the senders already
inside SendMessage, and a send that hits the closing queue retries on
its replacement.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* webhook: release blocked test handlers before the server shuts down

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: always run the webhook targets managed in the admin UI

Drop the notification.webhook.managed switch: every filer loads and live
reloads the stored targets. Without stored targets no queue is created.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* scaffold: place the admin-managed webhook note above enabled

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* scaffold: separate the admin-managed webhook note

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* scaffold: note that admin-managed webhooks follow the notification queue

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: retry loading webhook targets after a failed read

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: retire the admin-managed webhook queue on shutdown

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:54:54 -08:00

177 lines
5.4 KiB
Go

package filer
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
func TestManagedNotificationWebhooks(t *testing.T) {
var mu sync.Mutex
var keys []string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body struct{ Key string }
_ = json.NewDecoder(r.Body).Decode(&body)
mu.Lock()
keys = append(keys, body.Key)
mu.Unlock()
}))
defer server.Close()
received := func() []string {
mu.Lock()
defer mu.Unlock()
return append([]string(nil), keys...)
}
store := newStubFilerStore()
f := newTestFiler(t, store, nil)
t.Cleanup(func() {
if q := f.webhooks.queue.Load(); q != nil {
q.Close(time.Second)
}
})
saveConf := func(content string) {
store.entries[NotificationWebhooksConfPath] = &Entry{FullPath: util.FullPath(NotificationWebhooksConfPath), Content: []byte(content)}
}
confEvent := &filer_pb.SubscribeMetadataResponse{
Directory: DirectoryEtcSeaweedFS,
EventNotification: &filer_pb.EventNotification{NewEntry: &filer_pb.Entry{Name: NotificationWebhooksConfName}},
}
f.LoadNotificationWebhooks()
if f.webhooks.queue.Load() != nil {
t.Fatal("expected no webhook queue without stored targets")
}
saveConf(fmt.Sprintf(`{"targets":[{"name":"data","enabled":true,"endpoint":%q,"bearer_token":"tok","path_prefixes":["/"]}]}`, server.URL))
f.maybeReloadNotificationWebhooks(confEvent)
if f.webhooks.queue.Load() == nil {
t.Fatal("expected a webhook queue after the targets were stored")
}
ctx := context.Background()
f.NotifyUpdateEvent(ctx, nil, store.entries[NotificationWebhooksConfPath], false, false, nil)
f.NotifyUpdateEvent(ctx, nil, &Entry{FullPath: "/data/a.txt"}, false, false, nil)
waitForWebhook(t, func() bool { return len(received()) == 1 })
time.Sleep(200 * time.Millisecond)
if got := received(); len(got) != 1 || got[0] != "/data/a.txt" {
t.Fatalf("expected only /data/a.txt to be delivered, got %v", got)
}
running := f.webhooks.queue.Load()
f.maybeReloadNotificationWebhooks(confEvent)
if f.webhooks.queue.Load() != running {
t.Fatal("unchanged targets must keep the running queue")
}
saveConf(`{"targets":[{"name":"bad name","enabled":true,"endpoint":"https://example.com"}]}`)
f.maybeReloadNotificationWebhooks(confEvent)
if f.webhooks.queue.Load() != running {
t.Fatal("invalid targets must keep the running queue")
}
delete(store.entries, NotificationWebhooksConfPath)
f.maybeReloadNotificationWebhooks(&filer_pb.SubscribeMetadataResponse{
Directory: DirectoryEtcSeaweedFS,
EventNotification: &filer_pb.EventNotification{OldEntry: &filer_pb.Entry{Name: NotificationWebhooksConfName}},
})
if f.webhooks.queue.Load() != nil {
t.Fatal("expected deleting the stored targets to stop the queue")
}
}
func waitForWebhook(t *testing.T, cond func() bool) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for !cond() {
if time.Now().After(deadline) {
t.Fatal("timed out")
}
time.Sleep(20 * time.Millisecond)
}
}
func TestManagedNotificationWebhooksRetryAfterReadError(t *testing.T) {
retry := notificationWebhooksRetry
t.Cleanup(func() { notificationWebhooksRetry = retry })
notificationWebhooksRetry = 10 * time.Millisecond
store := newStubFilerStore()
f := newTestFiler(t, store, nil)
t.Cleanup(func() {
f.webhooks.reloadMu.Lock()
if f.webhooks.retry != nil {
f.webhooks.retry.Stop()
}
f.webhooks.reloadMu.Unlock()
if q := f.webhooks.queue.Load(); q != nil {
q.Close(time.Second)
}
})
store.mu.Lock()
store.entries[NotificationWebhooksConfPath] = &Entry{
FullPath: util.FullPath(NotificationWebhooksConfPath),
Content: []byte(`{"targets":[{"name":"data","enabled":true,"endpoint":"http://127.0.0.1:1/hook"}]}`),
}
store.findErr = errors.New("store unavailable")
store.mu.Unlock()
f.LoadNotificationWebhooks()
if f.webhooks.queue.Load() != nil {
t.Fatal("expected no webhook queue while the stored targets cannot be read")
}
store.mu.Lock()
store.findErr = nil
store.mu.Unlock()
deadline := time.Now().Add(5 * time.Second)
for f.webhooks.queue.Load() == nil {
if time.Now().After(deadline) {
t.Fatal("stored webhook targets were not applied after the read recovered")
}
time.Sleep(10 * time.Millisecond)
}
}
func TestManagedNotificationWebhooksStopOnShutdown(t *testing.T) {
store := newStubFilerStore()
f := newTestFiler(t, store, nil)
store.entries[NotificationWebhooksConfPath] = &Entry{
FullPath: util.FullPath(NotificationWebhooksConfPath),
Content: []byte(`{"targets":[{"name":"data","enabled":true,"endpoint":"http://127.0.0.1:1/hook"}]}`),
}
f.LoadNotificationWebhooks()
if f.webhooks.queue.Load() == nil {
t.Fatal("expected a webhook queue for the stored targets")
}
close(f.deletionQuit)
deadline := time.Now().Add(5 * time.Second)
for f.webhooks.queue.Load() != nil {
if time.Now().After(deadline) {
t.Fatal("webhook queue still installed after shutdown")
}
time.Sleep(10 * time.Millisecond)
}
store.entries[NotificationWebhooksConfPath] = &Entry{
FullPath: util.FullPath(NotificationWebhooksConfPath),
Content: []byte(`{"targets":[{"name":"late","enabled":true,"endpoint":"http://127.0.0.1:1/late"}]}`),
}
f.reloadNotificationWebhooks()
if f.webhooks.queue.Load() != nil {
t.Fatal("a reload after shutdown installed a webhook queue")
}
}