webhook_queue_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. package webhook
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. "google.golang.org/protobuf/proto"
  10. )
  11. func TestConfigValidation(t *testing.T) {
  12. tests := []struct {
  13. name string
  14. config *config
  15. wantErr bool
  16. errMsg string
  17. }{
  18. {
  19. name: "valid config",
  20. config: &config{
  21. endpoint: "https://example.com/webhook",
  22. authBearerToken: "test-token",
  23. timeoutSeconds: 30,
  24. maxRetries: 3,
  25. backoffSeconds: 5,
  26. maxBackoffSeconds: 30,
  27. nWorkers: 5,
  28. bufferSize: 10000,
  29. },
  30. wantErr: false,
  31. },
  32. {
  33. name: "empty endpoint",
  34. config: &config{
  35. endpoint: "",
  36. timeoutSeconds: 30,
  37. maxRetries: 3,
  38. backoffSeconds: 5,
  39. maxBackoffSeconds: 30,
  40. nWorkers: 5,
  41. bufferSize: 10000,
  42. },
  43. wantErr: true,
  44. errMsg: "endpoint is required",
  45. },
  46. {
  47. name: "invalid URL",
  48. config: &config{
  49. endpoint: "://invalid-url",
  50. timeoutSeconds: 30,
  51. maxRetries: 3,
  52. backoffSeconds: 5,
  53. maxBackoffSeconds: 30,
  54. nWorkers: 5,
  55. bufferSize: 10000,
  56. },
  57. wantErr: true,
  58. errMsg: "invalid webhook endpoint",
  59. },
  60. {
  61. name: "timeout too large",
  62. config: &config{
  63. endpoint: "https://example.com/webhook",
  64. timeoutSeconds: 301,
  65. maxRetries: 3,
  66. backoffSeconds: 5,
  67. maxBackoffSeconds: 30,
  68. nWorkers: 5,
  69. bufferSize: 10000,
  70. },
  71. wantErr: true,
  72. errMsg: "timeout must be between",
  73. },
  74. {
  75. name: "too many retries",
  76. config: &config{
  77. endpoint: "https://example.com/webhook",
  78. timeoutSeconds: 30,
  79. maxRetries: 11,
  80. backoffSeconds: 5,
  81. maxBackoffSeconds: 30,
  82. nWorkers: 5,
  83. bufferSize: 10000,
  84. },
  85. wantErr: true,
  86. errMsg: "max retries must be between",
  87. },
  88. {
  89. name: "too many workers",
  90. config: &config{
  91. endpoint: "https://example.com/webhook",
  92. timeoutSeconds: 30,
  93. maxRetries: 3,
  94. backoffSeconds: 5,
  95. maxBackoffSeconds: 30,
  96. nWorkers: 101,
  97. bufferSize: 10000,
  98. },
  99. wantErr: true,
  100. errMsg: "workers must be between",
  101. },
  102. {
  103. name: "buffer too large",
  104. config: &config{
  105. endpoint: "https://example.com/webhook",
  106. timeoutSeconds: 30,
  107. maxRetries: 3,
  108. backoffSeconds: 5,
  109. maxBackoffSeconds: 30,
  110. nWorkers: 5,
  111. bufferSize: 1000001,
  112. },
  113. wantErr: true,
  114. errMsg: "buffer size must be between",
  115. },
  116. }
  117. for _, tt := range tests {
  118. t.Run(tt.name, func(t *testing.T) {
  119. err := tt.config.validate()
  120. if (err != nil) != tt.wantErr {
  121. t.Errorf("validate() error = %v, wantErr %v", err, tt.wantErr)
  122. }
  123. if err != nil && tt.errMsg != "" {
  124. if err.Error() == "" || !strings.Contains(err.Error(), tt.errMsg) {
  125. t.Errorf("validate() error message = %v, want to contain %v", err.Error(), tt.errMsg)
  126. }
  127. }
  128. })
  129. }
  130. }
  131. func TestWebhookMessageSerialization(t *testing.T) {
  132. msg := &filer_pb.EventNotification{
  133. OldEntry: nil,
  134. NewEntry: &filer_pb.Entry{
  135. Name: "test.txt",
  136. IsDirectory: false,
  137. },
  138. }
  139. webhookMsg := newWebhookMessage("/test/path", msg)
  140. wmMsg, err := webhookMsg.toWaterMillMessage()
  141. if err != nil {
  142. t.Fatalf("Failed to convert to watermill message: %v", err)
  143. }
  144. // Unmarshal the protobuf payload directly
  145. var eventNotification filer_pb.EventNotification
  146. err = proto.Unmarshal(wmMsg.Payload, &eventNotification)
  147. if err != nil {
  148. t.Fatalf("Failed to unmarshal protobuf message: %v", err)
  149. }
  150. // Check metadata
  151. if wmMsg.Metadata.Get("key") != "/test/path" {
  152. t.Errorf("Expected key '/test/path', got %v", wmMsg.Metadata.Get("key"))
  153. }
  154. if wmMsg.Metadata.Get("event_type") != "create" {
  155. t.Errorf("Expected event type 'create', got %v", wmMsg.Metadata.Get("event_type"))
  156. }
  157. if eventNotification.NewEntry.Name != "test.txt" {
  158. t.Errorf("Expected file name 'test.txt', got %v", eventNotification.NewEntry.Name)
  159. }
  160. }
  161. func TestQueueInitialize(t *testing.T) {
  162. cfg := &config{
  163. endpoint: "https://example.com/webhook",
  164. authBearerToken: "test-token",
  165. timeoutSeconds: 10,
  166. maxRetries: 3,
  167. backoffSeconds: 3,
  168. maxBackoffSeconds: 60,
  169. nWorkers: 1,
  170. bufferSize: 100,
  171. }
  172. q := &Queue{}
  173. err := q.initialize(cfg)
  174. if err != nil {
  175. t.Errorf("Initialize() error = %v", err)
  176. }
  177. defer func() {
  178. if q.cancel != nil {
  179. q.cancel()
  180. }
  181. time.Sleep(100 * time.Millisecond)
  182. if q.router != nil {
  183. q.router.Close()
  184. }
  185. }()
  186. if q.router == nil {
  187. t.Error("Expected router to be initialized")
  188. }
  189. if q.queueChannel == nil {
  190. t.Error("Expected queueChannel to be initialized")
  191. }
  192. if q.client == nil {
  193. t.Error("Expected client to be initialized")
  194. }
  195. if q.config == nil {
  196. t.Error("Expected config to be initialized")
  197. }
  198. }
  199. // TestQueueSendMessage test sending messages to the queue
  200. func TestQueueSendMessage(t *testing.T) {
  201. cfg := &config{
  202. endpoint: "https://example.com/webhook",
  203. authBearerToken: "test-token",
  204. timeoutSeconds: 1,
  205. maxRetries: 1,
  206. backoffSeconds: 1,
  207. maxBackoffSeconds: 1,
  208. nWorkers: 1,
  209. bufferSize: 10,
  210. }
  211. q := &Queue{}
  212. err := q.initialize(cfg)
  213. if err != nil {
  214. t.Fatalf("Failed to initialize queue: %v", err)
  215. }
  216. defer func() {
  217. if q.cancel != nil {
  218. q.cancel()
  219. }
  220. time.Sleep(100 * time.Millisecond)
  221. if q.router != nil {
  222. q.router.Close()
  223. }
  224. }()
  225. msg := &filer_pb.EventNotification{
  226. NewEntry: &filer_pb.Entry{
  227. Name: "test.txt",
  228. },
  229. }
  230. err = q.SendMessage("/test/path", msg)
  231. if err != nil {
  232. t.Errorf("SendMessage() error = %v", err)
  233. }
  234. }
  235. func TestQueueHandleWebhook(t *testing.T) {
  236. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  237. w.WriteHeader(http.StatusOK)
  238. }))
  239. defer server.Close()
  240. cfg := &config{
  241. endpoint: server.URL,
  242. authBearerToken: "test-token",
  243. timeoutSeconds: 1,
  244. maxRetries: 0,
  245. backoffSeconds: 1,
  246. maxBackoffSeconds: 1,
  247. nWorkers: 1,
  248. bufferSize: 10,
  249. }
  250. client, _ := newHTTPClient(cfg)
  251. q := &Queue{
  252. client: client,
  253. }
  254. message := newWebhookMessage("/test/path", &filer_pb.EventNotification{
  255. NewEntry: &filer_pb.Entry{
  256. Name: "test.txt",
  257. },
  258. })
  259. wmMsg, err := message.toWaterMillMessage()
  260. if err != nil {
  261. t.Fatalf("Failed to create watermill message: %v", err)
  262. }
  263. err = q.handleWebhook(wmMsg)
  264. if err != nil {
  265. t.Errorf("handleWebhook() error = %v", err)
  266. }
  267. }
  268. func TestQueueEndToEnd(t *testing.T) {
  269. // Simplified test - just verify the queue can be created and message can be sent
  270. // without needing full end-to-end processing
  271. cfg := &config{
  272. endpoint: "https://example.com/webhook",
  273. authBearerToken: "test-token",
  274. timeoutSeconds: 1,
  275. maxRetries: 0,
  276. backoffSeconds: 1,
  277. maxBackoffSeconds: 1,
  278. nWorkers: 1,
  279. bufferSize: 10,
  280. }
  281. q := &Queue{}
  282. err := q.initialize(cfg)
  283. if err != nil {
  284. t.Fatalf("Failed to initialize queue: %v", err)
  285. }
  286. defer func() {
  287. if q.cancel != nil {
  288. q.cancel()
  289. }
  290. time.Sleep(100 * time.Millisecond)
  291. if q.router != nil {
  292. q.router.Close()
  293. }
  294. }()
  295. msg := &filer_pb.EventNotification{
  296. NewEntry: &filer_pb.Entry{
  297. Name: "test.txt",
  298. },
  299. }
  300. err = q.SendMessage("/test/path", msg)
  301. if err != nil {
  302. t.Errorf("SendMessage() error = %v", err)
  303. }
  304. }
  305. func TestQueueRetryMechanism(t *testing.T) {
  306. cfg := &config{
  307. endpoint: "https://example.com/webhook",
  308. authBearerToken: "test-token",
  309. timeoutSeconds: 1,
  310. maxRetries: 3, // Test that this config is used
  311. backoffSeconds: 2,
  312. maxBackoffSeconds: 10,
  313. nWorkers: 1,
  314. bufferSize: 10,
  315. }
  316. q := &Queue{}
  317. err := q.initialize(cfg)
  318. if err != nil {
  319. t.Fatalf("Failed to initialize queue: %v", err)
  320. }
  321. defer func() {
  322. if q.cancel != nil {
  323. q.cancel()
  324. }
  325. time.Sleep(100 * time.Millisecond)
  326. if q.router != nil {
  327. q.router.Close()
  328. }
  329. }()
  330. // Verify that the queue is properly configured for retries
  331. if q.config.maxRetries != 3 {
  332. t.Errorf("Expected maxRetries=3, got %d", q.config.maxRetries)
  333. }
  334. if q.config.backoffSeconds != 2 {
  335. t.Errorf("Expected backoffSeconds=2, got %d", q.config.backoffSeconds)
  336. }
  337. if q.config.maxBackoffSeconds != 10 {
  338. t.Errorf("Expected maxBackoffSeconds=10, got %d", q.config.maxBackoffSeconds)
  339. }
  340. // Test that we can send a message (retry behavior is handled by Watermill middleware)
  341. msg := &filer_pb.EventNotification{
  342. NewEntry: &filer_pb.Entry{Name: "test.txt"},
  343. }
  344. err = q.SendMessage("/test/retry", msg)
  345. if err != nil {
  346. t.Errorf("SendMessage() error = %v", err)
  347. }
  348. }
  349. func TestQueueSendMessageWithFilter(t *testing.T) {
  350. tests := []struct {
  351. name string
  352. cfg *config
  353. key string
  354. notification *filer_pb.EventNotification
  355. shouldPublish bool
  356. }{
  357. {
  358. name: "allowed event type",
  359. cfg: &config{
  360. endpoint: "https://example.com/webhook",
  361. timeoutSeconds: 10,
  362. maxRetries: 1,
  363. backoffSeconds: 1,
  364. maxBackoffSeconds: 1,
  365. nWorkers: 1,
  366. bufferSize: 10,
  367. eventTypes: []string{"create"},
  368. },
  369. key: "/data/file.txt",
  370. notification: &filer_pb.EventNotification{
  371. NewEntry: &filer_pb.Entry{Name: "file.txt"},
  372. },
  373. shouldPublish: true,
  374. },
  375. {
  376. name: "filtered event type",
  377. cfg: &config{
  378. endpoint: "https://example.com/webhook",
  379. timeoutSeconds: 10,
  380. maxRetries: 1,
  381. backoffSeconds: 1,
  382. maxBackoffSeconds: 1,
  383. nWorkers: 1,
  384. bufferSize: 10,
  385. eventTypes: []string{"update", "rename"},
  386. },
  387. key: "/data/file.txt",
  388. notification: &filer_pb.EventNotification{
  389. NewEntry: &filer_pb.Entry{Name: "file.txt"},
  390. },
  391. shouldPublish: false,
  392. },
  393. {
  394. name: "allowed path prefix",
  395. cfg: &config{
  396. endpoint: "https://example.com/webhook",
  397. timeoutSeconds: 10,
  398. maxRetries: 1,
  399. backoffSeconds: 1,
  400. maxBackoffSeconds: 1,
  401. nWorkers: 1,
  402. bufferSize: 10,
  403. pathPrefixes: []string{"/data/"},
  404. },
  405. key: "/data/file.txt",
  406. notification: &filer_pb.EventNotification{
  407. NewEntry: &filer_pb.Entry{Name: "file.txt"},
  408. },
  409. shouldPublish: true,
  410. },
  411. {
  412. name: "filtered path prefix",
  413. cfg: &config{
  414. endpoint: "https://example.com/webhook",
  415. timeoutSeconds: 10,
  416. maxRetries: 1,
  417. backoffSeconds: 1,
  418. maxBackoffSeconds: 1,
  419. nWorkers: 1,
  420. bufferSize: 10,
  421. pathPrefixes: []string{"/logs/"},
  422. },
  423. key: "/data/file.txt",
  424. notification: &filer_pb.EventNotification{
  425. NewEntry: &filer_pb.Entry{Name: "file.txt"},
  426. },
  427. shouldPublish: false,
  428. },
  429. {
  430. name: "combined filters - both pass",
  431. cfg: &config{
  432. endpoint: "https://example.com/webhook",
  433. timeoutSeconds: 10,
  434. maxRetries: 1,
  435. backoffSeconds: 1,
  436. maxBackoffSeconds: 1,
  437. nWorkers: 1,
  438. bufferSize: 10,
  439. eventTypes: []string{"create", "delete"},
  440. pathPrefixes: []string{"/data/", "/logs/"},
  441. },
  442. key: "/data/file.txt",
  443. notification: &filer_pb.EventNotification{
  444. NewEntry: &filer_pb.Entry{Name: "file.txt"},
  445. },
  446. shouldPublish: true,
  447. },
  448. {
  449. name: "combined filters - event fails",
  450. cfg: &config{
  451. endpoint: "https://example.com/webhook",
  452. timeoutSeconds: 10,
  453. maxRetries: 1,
  454. backoffSeconds: 1,
  455. maxBackoffSeconds: 1,
  456. nWorkers: 1,
  457. bufferSize: 10,
  458. eventTypes: []string{"update", "delete"},
  459. pathPrefixes: []string{"/data/", "/logs/"},
  460. },
  461. key: "/data/file.txt",
  462. notification: &filer_pb.EventNotification{
  463. NewEntry: &filer_pb.Entry{Name: "file.txt"},
  464. },
  465. shouldPublish: false,
  466. },
  467. {
  468. name: "combined filters - path fails",
  469. cfg: &config{
  470. endpoint: "https://example.com/webhook",
  471. timeoutSeconds: 10,
  472. maxRetries: 1,
  473. backoffSeconds: 1,
  474. maxBackoffSeconds: 1,
  475. nWorkers: 1,
  476. bufferSize: 10,
  477. eventTypes: []string{"create", "delete"},
  478. pathPrefixes: []string{"/logs/"},
  479. },
  480. key: "/data/file.txt",
  481. notification: &filer_pb.EventNotification{
  482. NewEntry: &filer_pb.Entry{Name: "file.txt"},
  483. },
  484. shouldPublish: false,
  485. },
  486. }
  487. for _, tt := range tests {
  488. t.Run(tt.name, func(t *testing.T) {
  489. shouldPublish := newFilter(tt.cfg).shouldPublish(tt.key, tt.notification)
  490. if shouldPublish != tt.shouldPublish {
  491. t.Errorf("Expected shouldPublish=%v, got %v", tt.shouldPublish, shouldPublish)
  492. }
  493. })
  494. }
  495. }