github.com/minio/minio-go/v6@v6.0.57/bucket-notification_test.go (about) 1 /* 2 * MinIO Go Library for Amazon S3 Compatible Cloud Storage 3 * Copyright 2015-2020 MinIO, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package minio 19 20 import ( 21 "encoding/xml" 22 "testing" 23 ) 24 25 func TestEqualNotificationEventTypeList(t *testing.T) { 26 type args struct { 27 a []NotificationEventType 28 b []NotificationEventType 29 } 30 tests := []struct { 31 name string 32 args args 33 want bool 34 }{ 35 { 36 name: "same order", 37 args: args{ 38 a: []NotificationEventType{ObjectCreatedAll, ObjectAccessedAll}, 39 b: []NotificationEventType{ObjectCreatedAll, ObjectAccessedAll}, 40 }, 41 want: true, 42 }, 43 { 44 name: "different order", 45 args: args{ 46 a: []NotificationEventType{ObjectCreatedAll, ObjectAccessedAll}, 47 b: []NotificationEventType{ObjectAccessedAll, ObjectCreatedAll}, 48 }, 49 want: true, 50 }, 51 { 52 name: "not equal", 53 args: args{ 54 a: []NotificationEventType{ObjectCreatedAll, ObjectAccessedAll}, 55 b: []NotificationEventType{ObjectRemovedAll, ObjectAccessedAll}, 56 }, 57 }, 58 } 59 for _, tt := range tests { 60 t.Run(tt.name, func(t *testing.T) { 61 if got := EqualNotificationEventTypeList(tt.args.a, tt.args.b); got != tt.want { 62 t.Errorf("EqualNotificationEventTypeList() = %v, want %v", got, tt.want) 63 } 64 }) 65 } 66 } 67 68 func TestEqualFilterRuleList(t *testing.T) { 69 type args struct { 70 a []FilterRule 71 b []FilterRule 72 } 73 tests := []struct { 74 name string 75 args args 76 want bool 77 }{ 78 { 79 name: "same order", 80 args: args{ 81 a: []FilterRule{{Name: "prefix", Value: "prefix1"}, {Name: "suffix", Value: "suffix1"}}, 82 b: []FilterRule{{Name: "prefix", Value: "prefix1"}, {Name: "suffix", Value: "suffix1"}}, 83 }, 84 want: true, 85 }, 86 { 87 name: "different order", 88 args: args{ 89 a: []FilterRule{{Name: "prefix", Value: "prefix1"}, {Name: "suffix", Value: "suffix1"}}, 90 b: []FilterRule{{Name: "suffix", Value: "suffix1"}, {Name: "prefix", Value: "prefix1"}}, 91 }, 92 want: true, 93 }, 94 { 95 name: "not equal", 96 args: args{ 97 a: []FilterRule{{Name: "prefix", Value: "prefix1"}, {Name: "suffix", Value: "suffix1"}}, 98 b: []FilterRule{{Name: "prefix", Value: "prefix2"}, {Name: "suffix", Value: "suffix1"}}, 99 }, 100 }, 101 } 102 for _, tt := range tests { 103 t.Run(tt.name, func(t *testing.T) { 104 if got := EqualFilterRuleList(tt.args.a, tt.args.b); got != tt.want { 105 t.Errorf("EqualFilterRuleList() = %v, want %v", got, tt.want) 106 } 107 }) 108 } 109 } 110 111 func TestNotificationConfig_Equal(t *testing.T) { 112 type fields struct { 113 ID string 114 Arn Arn 115 Events []NotificationEventType 116 Filter *Filter 117 } 118 type args struct { 119 events []NotificationEventType 120 prefix string 121 suffix string 122 } 123 tests := []struct { 124 name string 125 fields fields 126 args args 127 want bool 128 }{ 129 { 130 name: "same order", 131 fields: fields{ 132 Arn: NewArn("minio", "sqs", "", "1", "postgresql"), 133 Events: []NotificationEventType{ObjectCreatedAll, ObjectAccessedAll}, 134 Filter: &Filter{ 135 S3Key: S3Key{ 136 FilterRules: []FilterRule{{Name: "prefix", Value: "prefix1"}, {Name: "suffix", Value: "suffix1"}}, 137 }, 138 }, 139 }, 140 args: args{ 141 events: []NotificationEventType{ObjectCreatedAll, ObjectAccessedAll}, 142 prefix: "prefix1", 143 suffix: "suffix1", 144 }, 145 want: true, 146 }, 147 { 148 name: "different order", 149 fields: fields{ 150 Arn: NewArn("minio", "sqs", "", "1", "postgresql"), 151 Events: []NotificationEventType{ObjectAccessedAll, ObjectCreatedAll}, 152 Filter: &Filter{ 153 S3Key: S3Key{ 154 FilterRules: []FilterRule{{Name: "suffix", Value: "suffix1"}, {Name: "prefix", Value: "prefix1"}}, 155 }, 156 }, 157 }, 158 args: args{ 159 events: []NotificationEventType{ObjectCreatedAll, ObjectAccessedAll}, 160 prefix: "prefix1", 161 suffix: "suffix1", 162 }, 163 want: true, 164 }, 165 { 166 name: "not equal", 167 fields: fields{ 168 Arn: NewArn("minio", "sqs", "", "1", "postgresql"), 169 Events: []NotificationEventType{ObjectAccessedAll}, 170 Filter: &Filter{ 171 S3Key: S3Key{ 172 FilterRules: []FilterRule{{Name: "suffix", Value: "suffix1"}, {Name: "prefix", Value: "prefix1"}}, 173 }, 174 }, 175 }, 176 args: args{ 177 events: []NotificationEventType{ObjectCreatedAll, ObjectAccessedAll}, 178 prefix: "prefix1", 179 suffix: "suffix1", 180 }, 181 want: false, 182 }, 183 { 184 name: "different arn", 185 fields: fields{ 186 Events: []NotificationEventType{ObjectAccessedAll}, 187 Filter: &Filter{ 188 S3Key: S3Key{ 189 FilterRules: []FilterRule{{Name: "suffix", Value: "suffix1"}, {Name: "prefix", Value: "prefix1"}}, 190 }, 191 }, 192 }, 193 args: args{ 194 events: []NotificationEventType{ObjectCreatedAll, ObjectAccessedAll}, 195 prefix: "prefix1", 196 suffix: "suffix1", 197 }, 198 want: false, 199 }, 200 } 201 for _, tt := range tests { 202 t.Run(tt.name, func(t *testing.T) { 203 nc := &NotificationConfig{ 204 ID: tt.fields.ID, 205 Arn: tt.fields.Arn, 206 Events: tt.fields.Events, 207 Filter: tt.fields.Filter, 208 } 209 if got := nc.Equal(tt.args.events, tt.args.prefix, tt.args.suffix); got != tt.want { 210 t.Errorf("Equal() = %v, want %v", got, tt.want) 211 } 212 }) 213 } 214 } 215 216 func TestBucketNotification_RemoveQueueByArnEventsPrefixSuffix(t *testing.T) { 217 type fields struct { 218 XMLName xml.Name 219 LambdaConfigs []LambdaConfig 220 TopicConfigs []TopicConfig 221 QueueConfigs []QueueConfig 222 } 223 type args struct { 224 arn Arn 225 events []NotificationEventType 226 prefix string 227 suffix string 228 } 229 tests := []struct { 230 name string 231 fields fields 232 args args 233 wantErr bool 234 }{ 235 { 236 name: "Queue Configuration Removed with events, prefix", 237 fields: fields{ 238 XMLName: xml.Name{}, 239 LambdaConfigs: nil, 240 TopicConfigs: nil, 241 QueueConfigs: []QueueConfig{ 242 { 243 NotificationConfig: NotificationConfig{ 244 ID: "", 245 Arn: Arn{ 246 Partition: "minio", 247 Service: "sqs", 248 Region: "", 249 AccountID: "1", 250 Resource: "postgresql", 251 }, 252 Events: []NotificationEventType{ 253 ObjectAccessedAll, 254 }, 255 Filter: &Filter{ 256 S3Key: S3Key{ 257 FilterRules: []FilterRule{ 258 { 259 Name: "prefix", 260 Value: "x", 261 }, 262 }, 263 }, 264 }, 265 }, 266 Queue: "arn:minio:sqs::1:postgresql", 267 }, 268 }, 269 }, 270 args: args{ 271 arn: Arn{ 272 Partition: "minio", 273 Service: "sqs", 274 Region: "", 275 AccountID: "1", 276 Resource: "postgresql", 277 }, 278 events: []NotificationEventType{ 279 ObjectAccessedAll, 280 }, 281 prefix: "x", 282 suffix: "", 283 }, 284 wantErr: false, 285 }, 286 { 287 name: "Queue Configuration Removed with events, prefix, suffix", 288 fields: fields{ 289 XMLName: xml.Name{}, 290 LambdaConfigs: nil, 291 TopicConfigs: nil, 292 QueueConfigs: []QueueConfig{ 293 { 294 NotificationConfig: NotificationConfig{ 295 ID: "", 296 Arn: Arn{ 297 Partition: "minio", 298 Service: "sqs", 299 Region: "", 300 AccountID: "1", 301 Resource: "postgresql", 302 }, 303 Events: []NotificationEventType{ 304 ObjectAccessedAll, 305 }, 306 Filter: &Filter{ 307 S3Key: S3Key{ 308 FilterRules: []FilterRule{ 309 { 310 Name: "prefix", 311 Value: "x", 312 }, 313 { 314 Name: "suffix", 315 Value: "y", 316 }, 317 }, 318 }, 319 }, 320 }, 321 Queue: "arn:minio:sqs::1:postgresql", 322 }, 323 }, 324 }, 325 args: args{ 326 arn: Arn{ 327 Partition: "minio", 328 Service: "sqs", 329 Region: "", 330 AccountID: "1", 331 Resource: "postgresql", 332 }, 333 events: []NotificationEventType{ 334 ObjectAccessedAll, 335 }, 336 prefix: "x", 337 suffix: "y", 338 }, 339 wantErr: false, 340 }, 341 { 342 name: "Error Returned Queue Configuration Not Removed", 343 fields: fields{ 344 XMLName: xml.Name{}, 345 LambdaConfigs: nil, 346 TopicConfigs: nil, 347 QueueConfigs: []QueueConfig{ 348 { 349 NotificationConfig: NotificationConfig{ 350 ID: "", 351 Arn: Arn{ 352 Partition: "minio", 353 Service: "sqs", 354 Region: "", 355 AccountID: "1", 356 Resource: "postgresql", 357 }, 358 Events: []NotificationEventType{ 359 ObjectAccessedAll, 360 }, 361 Filter: &Filter{ 362 S3Key: S3Key{ 363 FilterRules: []FilterRule{ 364 { 365 Name: "prefix", 366 Value: "x", 367 }, 368 }, 369 }, 370 }, 371 }, 372 Queue: "arn:minio:sqs::1:postgresql", 373 }, 374 }, 375 }, 376 args: args{ 377 arn: Arn{ 378 Partition: "minio", 379 Service: "sqs", 380 Region: "", 381 AccountID: "1", 382 Resource: "postgresql", 383 }, 384 events: []NotificationEventType{ 385 ObjectAccessedAll, 386 }, 387 prefix: "", 388 suffix: "", 389 }, 390 wantErr: true, 391 }, 392 } 393 for _, tt := range tests { 394 t.Run(tt.name, func(t *testing.T) { 395 b := &BucketNotification{ 396 XMLName: tt.fields.XMLName, 397 LambdaConfigs: tt.fields.LambdaConfigs, 398 TopicConfigs: tt.fields.TopicConfigs, 399 QueueConfigs: tt.fields.QueueConfigs, 400 } 401 if err := b.RemoveQueueByArnEventsPrefixSuffix(tt.args.arn, tt.args.events, tt.args.prefix, tt.args.suffix); (err != nil) != tt.wantErr { 402 t.Errorf("RemoveQueueByArnEventsPrefixSuffix() error = %v, wantErr %v", err, tt.wantErr) 403 } 404 }) 405 } 406 } 407 408 func TestBucketNotification_RemoveLambdaByArnEventsPrefixSuffix(t *testing.T) { 409 type fields struct { 410 XMLName xml.Name 411 LambdaConfigs []LambdaConfig 412 TopicConfigs []TopicConfig 413 QueueConfigs []QueueConfig 414 } 415 type args struct { 416 arn Arn 417 events []NotificationEventType 418 prefix string 419 suffix string 420 } 421 tests := []struct { 422 name string 423 fields fields 424 args args 425 wantErr bool 426 }{ 427 { 428 name: "Lambda Configuration Removed with events, prefix", 429 fields: fields{ 430 XMLName: xml.Name{}, 431 QueueConfigs: nil, 432 TopicConfigs: nil, 433 LambdaConfigs: []LambdaConfig{ 434 { 435 NotificationConfig: NotificationConfig{ 436 ID: "", 437 Arn: Arn{ 438 Partition: "minio", 439 Service: "lambda", 440 Region: "", 441 AccountID: "1", 442 Resource: "provider", 443 }, 444 Events: []NotificationEventType{ 445 ObjectAccessedAll, 446 }, 447 Filter: &Filter{ 448 S3Key: S3Key{ 449 FilterRules: []FilterRule{ 450 { 451 Name: "prefix", 452 Value: "x", 453 }, 454 }, 455 }, 456 }, 457 }, 458 Lambda: "arn:minio:lambda::1:provider", 459 }, 460 }, 461 }, 462 args: args{ 463 arn: Arn{ 464 Partition: "minio", 465 Service: "lambda", 466 Region: "", 467 AccountID: "1", 468 Resource: "provider", 469 }, 470 events: []NotificationEventType{ 471 ObjectAccessedAll, 472 }, 473 prefix: "x", 474 suffix: "", 475 }, 476 wantErr: false, 477 }, 478 { 479 name: "Lambda Configuration Removed with events, prefix, suffix", 480 fields: fields{ 481 XMLName: xml.Name{}, 482 QueueConfigs: nil, 483 TopicConfigs: nil, 484 LambdaConfigs: []LambdaConfig{ 485 { 486 NotificationConfig: NotificationConfig{ 487 ID: "", 488 Arn: Arn{ 489 Partition: "minio", 490 Service: "lambda", 491 Region: "", 492 AccountID: "1", 493 Resource: "provider", 494 }, 495 Events: []NotificationEventType{ 496 ObjectAccessedAll, 497 }, 498 Filter: &Filter{ 499 S3Key: S3Key{ 500 FilterRules: []FilterRule{ 501 { 502 Name: "prefix", 503 Value: "x", 504 }, 505 { 506 Name: "suffix", 507 Value: "y", 508 }, 509 }, 510 }, 511 }, 512 }, 513 Lambda: "arn:minio:lambda::1:provider", 514 }, 515 }, 516 }, 517 args: args{ 518 arn: Arn{ 519 Partition: "minio", 520 Service: "lambda", 521 Region: "", 522 AccountID: "1", 523 Resource: "provider", 524 }, 525 events: []NotificationEventType{ 526 ObjectAccessedAll, 527 }, 528 prefix: "x", 529 suffix: "y", 530 }, 531 wantErr: false, 532 }, 533 { 534 name: "Error Returned Lambda Configuration Not Removed", 535 fields: fields{ 536 XMLName: xml.Name{}, 537 QueueConfigs: nil, 538 TopicConfigs: nil, 539 LambdaConfigs: []LambdaConfig{ 540 { 541 NotificationConfig: NotificationConfig{ 542 ID: "", 543 Arn: Arn{ 544 Partition: "minio", 545 Service: "lambda", 546 Region: "", 547 AccountID: "1", 548 Resource: "provider", 549 }, 550 Events: []NotificationEventType{ 551 ObjectAccessedAll, 552 }, 553 Filter: &Filter{ 554 S3Key: S3Key{ 555 FilterRules: []FilterRule{ 556 { 557 Name: "prefix", 558 Value: "x", 559 }, 560 }, 561 }, 562 }, 563 }, 564 Lambda: "arn:minio:lambda::1:provider", 565 }, 566 }, 567 }, 568 args: args{ 569 arn: Arn{ 570 Partition: "minio", 571 Service: "lambda", 572 Region: "", 573 AccountID: "1", 574 Resource: "provider", 575 }, 576 events: []NotificationEventType{ 577 ObjectAccessedAll, 578 }, 579 prefix: "", 580 suffix: "", 581 }, 582 wantErr: true, 583 }, 584 { 585 name: "Error Returned Invalid ARN", 586 fields: fields{ 587 XMLName: xml.Name{}, 588 QueueConfigs: nil, 589 TopicConfigs: nil, 590 LambdaConfigs: []LambdaConfig{ 591 { 592 NotificationConfig: NotificationConfig{ 593 ID: "", 594 Arn: Arn{ 595 Partition: "minio", 596 Service: "lambda", 597 Region: "", 598 AccountID: "1", 599 Resource: "provider", 600 }, 601 Events: []NotificationEventType{ 602 ObjectAccessedAll, 603 }, 604 Filter: &Filter{ 605 S3Key: S3Key{ 606 FilterRules: []FilterRule{ 607 { 608 Name: "prefix", 609 Value: "x", 610 }, 611 }, 612 }, 613 }, 614 }, 615 Lambda: "arn:minio:lambda::1:provider", 616 }, 617 }, 618 }, 619 args: args{ 620 arn: Arn{ 621 Partition: "minio", 622 623 Service: "lambda", 624 Region: "", 625 AccountID: "2", 626 Resource: "provider", 627 }, 628 events: []NotificationEventType{ 629 ObjectAccessedAll, 630 }, 631 prefix: "", 632 suffix: "", 633 }, 634 wantErr: true, 635 }, 636 } 637 for _, tt := range tests { 638 t.Run(tt.name, func(t *testing.T) { 639 b := &BucketNotification{ 640 XMLName: tt.fields.XMLName, 641 LambdaConfigs: tt.fields.LambdaConfigs, 642 TopicConfigs: tt.fields.TopicConfigs, 643 QueueConfigs: tt.fields.QueueConfigs, 644 } 645 if err := b.RemoveLambdaByArnEventsPrefixSuffix(tt.args.arn, tt.args.events, tt.args.prefix, tt.args.suffix); (err != nil) != tt.wantErr { 646 t.Errorf("RemoveLambdaByArnEventsPrefixSuffix() error = %v, wantErr %v", err, tt.wantErr) 647 } 648 }) 649 } 650 } 651 652 func TestBucketNotification_RemoveTopicByArnEventsPrefixSuffix(t *testing.T) { 653 type fields struct { 654 XMLName xml.Name 655 LambdaConfigs []LambdaConfig 656 TopicConfigs []TopicConfig 657 QueueConfigs []QueueConfig 658 } 659 type args struct { 660 arn Arn 661 events []NotificationEventType 662 prefix string 663 suffix string 664 } 665 tests := []struct { 666 name string 667 fields fields 668 args args 669 wantErr bool 670 }{ 671 { 672 name: "Topic Configuration Removed with events, prefix", 673 fields: fields{ 674 XMLName: xml.Name{}, 675 QueueConfigs: nil, 676 LambdaConfigs: nil, 677 TopicConfigs: []TopicConfig{ 678 { 679 NotificationConfig: NotificationConfig{ 680 ID: "", 681 Arn: Arn{ 682 Partition: "minio", 683 Service: "sns", 684 Region: "", 685 AccountID: "1", 686 Resource: "kafka", 687 }, 688 Events: []NotificationEventType{ 689 ObjectAccessedAll, 690 }, 691 Filter: &Filter{ 692 S3Key: S3Key{ 693 FilterRules: []FilterRule{ 694 { 695 Name: "prefix", 696 Value: "x", 697 }, 698 }, 699 }, 700 }, 701 }, 702 Topic: "arn:minio:sns::1:kafka", 703 }, 704 }, 705 }, 706 args: args{ 707 arn: Arn{ 708 Partition: "minio", 709 Service: "sns", 710 Region: "", 711 AccountID: "1", 712 Resource: "kafka", 713 }, 714 events: []NotificationEventType{ 715 ObjectAccessedAll, 716 }, 717 prefix: "x", 718 suffix: "", 719 }, 720 wantErr: false, 721 }, 722 { 723 name: "Topic Configuration Removed with events, prefix, suffix", 724 fields: fields{ 725 XMLName: xml.Name{}, 726 QueueConfigs: nil, 727 LambdaConfigs: nil, 728 TopicConfigs: []TopicConfig{ 729 { 730 NotificationConfig: NotificationConfig{ 731 ID: "", 732 Arn: Arn{ 733 Partition: "minio", 734 Service: "sns", 735 Region: "", 736 AccountID: "1", 737 Resource: "kafka", 738 }, 739 Events: []NotificationEventType{ 740 ObjectAccessedAll, 741 }, 742 Filter: &Filter{ 743 S3Key: S3Key{ 744 FilterRules: []FilterRule{ 745 { 746 Name: "prefix", 747 Value: "x", 748 }, 749 { 750 Name: "suffix", 751 Value: "y", 752 }, 753 }, 754 }, 755 }, 756 }, 757 Topic: "arn:minio:sns::1:kafka", 758 }, 759 }, 760 }, 761 args: args{ 762 arn: Arn{ 763 Partition: "minio", 764 Service: "sns", 765 Region: "", 766 AccountID: "1", 767 Resource: "kafka", 768 }, 769 events: []NotificationEventType{ 770 ObjectAccessedAll, 771 }, 772 prefix: "x", 773 suffix: "y", 774 }, 775 wantErr: false, 776 }, 777 { 778 name: "Error Returned Topic Configuration Not Removed", 779 fields: fields{ 780 XMLName: xml.Name{}, 781 QueueConfigs: nil, 782 LambdaConfigs: nil, 783 TopicConfigs: []TopicConfig{ 784 { 785 NotificationConfig: NotificationConfig{ 786 ID: "", 787 Arn: Arn{ 788 Partition: "minio", 789 Service: "sns", 790 Region: "", 791 AccountID: "1", 792 Resource: "kafka", 793 }, 794 Events: []NotificationEventType{ 795 ObjectAccessedAll, 796 }, 797 Filter: &Filter{ 798 S3Key: S3Key{ 799 FilterRules: []FilterRule{ 800 { 801 Name: "prefix", 802 Value: "x", 803 }, 804 }, 805 }, 806 }, 807 }, 808 Topic: "arn:minio:sns::1:kafka", 809 }, 810 }, 811 }, 812 args: args{ 813 arn: Arn{ 814 Partition: "minio", 815 Service: "sns", 816 Region: "", 817 AccountID: "1", 818 Resource: "kafka", 819 }, 820 events: []NotificationEventType{ 821 ObjectAccessedAll, 822 }, 823 prefix: "", 824 suffix: "", 825 }, 826 wantErr: true, 827 }, 828 } 829 for _, tt := range tests { 830 t.Run(tt.name, func(t *testing.T) { 831 b := &BucketNotification{ 832 XMLName: tt.fields.XMLName, 833 LambdaConfigs: tt.fields.LambdaConfigs, 834 TopicConfigs: tt.fields.TopicConfigs, 835 QueueConfigs: tt.fields.QueueConfigs, 836 } 837 if err := b.RemoveTopicByArnEventsPrefixSuffix(tt.args.arn, tt.args.events, tt.args.prefix, tt.args.suffix); (err != nil) != tt.wantErr { 838 t.Errorf("RemoveTopicByArnEventsPrefixSuffix() error = %v, wantErr %v", err, tt.wantErr) 839 } 840 }) 841 } 842 }