| 
									
										
										
										
											2016-08-05 13:01:58 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  |  * Minio Cloud Storage, (C) 2016, 2017 Minio, Inc. | 
					
						
							| 
									
										
										
										
											2016-08-05 13:01:58 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-08-05 13:01:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-10 17:23:28 +08:00
										 |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-01-17 09:05:00 +08:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2016-09-10 17:23:28 +08:00
										 |  |  | 	"reflect" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2016-09-10 17:23:28 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | // Test InitEventNotifier with faulty disks
 | 
					
						
							|  |  |  | func TestInitEventNotifierFaultyDisks(t *testing.T) { | 
					
						
							|  |  |  | 	// Prepare for tests
 | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | 	rootPath, err := newTestConfig(globalMinioDefaultRegion) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Init Test config failed") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-10 15:37:12 +08:00
										 |  |  | 	// remove the root directory after the test ends.
 | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	defer removeAll(rootPath) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 09:05:00 +08:00
										 |  |  | 	disks, err := getRandomDisks(4) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unable to create directories for FS backend. ", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-17 09:05:00 +08:00
										 |  |  | 	defer removeRoots(disks) | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	endpoints, err := parseStorageEndpoints(disks) | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-12 08:36:07 +08:00
										 |  |  | 	obj, _, err := initObjectLayer(endpoints) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unable to initialize FS backend.", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bucketName := "bucket" | 
					
						
							|  |  |  | 	if err := obj.MakeBucket(bucketName); err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unexpected error:", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 09:05:00 +08:00
										 |  |  | 	xl := obj.(*xlObjects) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	listenARN := "arn:minio:sns:us-east-1:1:listen" | 
					
						
							|  |  |  | 	queueARN := "arn:minio:sqs:us-east-1:1:redis" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Write a notification.xml in the disk
 | 
					
						
							|  |  |  | 	notificationXML := "<NotificationConfiguration>" | 
					
						
							|  |  |  | 	notificationXML += "<TopicConfiguration><Event>s3:ObjectRemoved:*</Event><Event>s3:ObjectRemoved:*</Event><Topic>" + listenARN + "</Topic></TopicConfiguration>" | 
					
						
							|  |  |  | 	notificationXML += "<QueueConfiguration><Event>s3:ObjectRemoved:*</Event><Event>s3:ObjectRemoved:*</Event><Queue>" + queueARN + "</Queue></QueueConfiguration>" | 
					
						
							|  |  |  | 	notificationXML += "</NotificationConfiguration>" | 
					
						
							| 
									
										
										
										
											2017-01-17 09:05:00 +08:00
										 |  |  | 	size := int64(len([]byte(notificationXML))) | 
					
						
							|  |  |  | 	reader := bytes.NewReader([]byte(notificationXML)) | 
					
						
							|  |  |  | 	if _, err := xl.PutObject(minioMetaBucket, bucketConfigPrefix+"/"+bucketName+"/"+bucketNotificationConfig, size, reader, nil, ""); err != nil { | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 		t.Fatal("Unexpected error:", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 09:05:00 +08:00
										 |  |  | 	for i, d := range xl.storageDisks { | 
					
						
							|  |  |  | 		xl.storageDisks[i] = newNaughtyDisk(d.(*retryStorage), nil, errFaultyDisk) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	// Test initEventNotifier() with faulty disks
 | 
					
						
							| 
									
										
										
										
											2016-12-22 03:29:32 +08:00
										 |  |  | 	for i := 1; i <= 3; i++ { | 
					
						
							| 
									
										
										
										
											2017-01-17 09:05:00 +08:00
										 |  |  | 		if err := initEventNotifier(xl); errorCause(err) != errFaultyDisk { | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 			t.Fatal("Unexpected error:", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 06:22:10 +08:00
										 |  |  | // InitEventNotifierWithPostgreSQL - tests InitEventNotifier when PostgreSQL is not prepared
 | 
					
						
							|  |  |  | func TestInitEventNotifierWithPostgreSQL(t *testing.T) { | 
					
						
							|  |  |  | 	// initialize the server and obtain the credentials and root.
 | 
					
						
							|  |  |  | 	// credentials are necessary to sign the HTTP request.
 | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | 	rootPath, err := newTestConfig(globalMinioDefaultRegion) | 
					
						
							| 
									
										
										
										
											2017-01-10 06:22:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Init Test config failed") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// remove the root directory after the test ends.
 | 
					
						
							|  |  |  | 	defer removeAll(rootPath) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	disks, err := getRandomDisks(1) | 
					
						
							|  |  |  | 	defer removeAll(disks[0]) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unable to create directories for FS backend. ", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	endpoints, err := parseStorageEndpoints(disks) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	fs, _, err := initObjectLayer(endpoints) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unable to initialize FS backend.", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	serverConfig.SetPostgreSQLNotifyByID("1", postgreSQLNotify{Enable: true}) | 
					
						
							|  |  |  | 	if err := initEventNotifier(fs); err == nil { | 
					
						
							|  |  |  | 		t.Fatal("PostgreSQL config didn't fail.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // InitEventNotifierWithNATS - tests InitEventNotifier when NATS is not prepared
 | 
					
						
							|  |  |  | func TestInitEventNotifierWithNATS(t *testing.T) { | 
					
						
							|  |  |  | 	// initialize the server and obtain the credentials and root.
 | 
					
						
							|  |  |  | 	// credentials are necessary to sign the HTTP request.
 | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | 	rootPath, err := newTestConfig(globalMinioDefaultRegion) | 
					
						
							| 
									
										
										
										
											2017-01-10 06:22:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Init Test config failed") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// remove the root directory after the test ends.
 | 
					
						
							|  |  |  | 	defer removeAll(rootPath) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	disks, err := getRandomDisks(1) | 
					
						
							|  |  |  | 	defer removeAll(disks[0]) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unable to create directories for FS backend. ", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	endpoints, err := parseStorageEndpoints(disks) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	fs, _, err := initObjectLayer(endpoints) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unable to initialize FS backend.", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	serverConfig.SetNATSNotifyByID("1", natsNotify{Enable: true}) | 
					
						
							|  |  |  | 	if err := initEventNotifier(fs); err == nil { | 
					
						
							|  |  |  | 		t.Fatal("NATS config didn't fail.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // InitEventNotifierWithWebHook - tests InitEventNotifier when WebHook is not prepared
 | 
					
						
							|  |  |  | func TestInitEventNotifierWithWebHook(t *testing.T) { | 
					
						
							|  |  |  | 	// initialize the server and obtain the credentials and root.
 | 
					
						
							|  |  |  | 	// credentials are necessary to sign the HTTP request.
 | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | 	rootPath, err := newTestConfig(globalMinioDefaultRegion) | 
					
						
							| 
									
										
										
										
											2017-01-10 06:22:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Init Test config failed") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// remove the root directory after the test ends.
 | 
					
						
							|  |  |  | 	defer removeAll(rootPath) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	disks, err := getRandomDisks(1) | 
					
						
							|  |  |  | 	defer removeAll(disks[0]) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unable to create directories for FS backend. ", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	endpoints, err := parseStorageEndpoints(disks) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	fs, _, err := initObjectLayer(endpoints) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unable to initialize FS backend.", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	serverConfig.SetWebhookNotifyByID("1", webhookNotify{Enable: true}) | 
					
						
							|  |  |  | 	if err := initEventNotifier(fs); err == nil { | 
					
						
							|  |  |  | 		t.Fatal("WebHook config didn't fail.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | // InitEventNotifierWithAMQP - tests InitEventNotifier when AMQP is not prepared
 | 
					
						
							|  |  |  | func TestInitEventNotifierWithAMQP(t *testing.T) { | 
					
						
							|  |  |  | 	// initialize the server and obtain the credentials and root.
 | 
					
						
							|  |  |  | 	// credentials are necessary to sign the HTTP request.
 | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | 	rootPath, err := newTestConfig(globalMinioDefaultRegion) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Init Test config failed") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-10 15:37:12 +08:00
										 |  |  | 	// remove the root directory after the test ends.
 | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	defer removeAll(rootPath) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	disks, err := getRandomDisks(1) | 
					
						
							|  |  |  | 	defer removeAll(disks[0]) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unable to create directories for FS backend. ", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	endpoints, err := parseStorageEndpoints(disks) | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-12 08:36:07 +08:00
										 |  |  | 	fs, _, err := initObjectLayer(endpoints) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unable to initialize FS backend.", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	serverConfig.SetAMQPNotifyByID("1", amqpNotify{Enable: true}) | 
					
						
							|  |  |  | 	if err := initEventNotifier(fs); err == nil { | 
					
						
							|  |  |  | 		t.Fatal("AMQP config didn't fail.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // InitEventNotifierWithElasticSearch - test InitEventNotifier when ElasticSearch is not ready
 | 
					
						
							|  |  |  | func TestInitEventNotifierWithElasticSearch(t *testing.T) { | 
					
						
							|  |  |  | 	// initialize the server and obtain the credentials and root.
 | 
					
						
							|  |  |  | 	// credentials are necessary to sign the HTTP request.
 | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | 	rootPath, err := newTestConfig(globalMinioDefaultRegion) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Init Test config failed") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-10 15:37:12 +08:00
										 |  |  | 	// remove the root directory after the test ends.
 | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	defer removeAll(rootPath) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	disks, err := getRandomDisks(1) | 
					
						
							|  |  |  | 	defer removeAll(disks[0]) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unable to create directories for FS backend. ", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	endpoints, err := parseStorageEndpoints(disks) | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-12 08:36:07 +08:00
										 |  |  | 	fs, _, err := initObjectLayer(endpoints) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unable to initialize FS backend.", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	serverConfig.SetElasticSearchNotifyByID("1", elasticSearchNotify{Enable: true}) | 
					
						
							|  |  |  | 	if err := initEventNotifier(fs); err == nil { | 
					
						
							|  |  |  | 		t.Fatal("ElasticSearch config didn't fail.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // InitEventNotifierWithRedis - test InitEventNotifier when Redis is not ready
 | 
					
						
							|  |  |  | func TestInitEventNotifierWithRedis(t *testing.T) { | 
					
						
							|  |  |  | 	// initialize the server and obtain the credentials and root.
 | 
					
						
							|  |  |  | 	// credentials are necessary to sign the HTTP request.
 | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | 	rootPath, err := newTestConfig(globalMinioDefaultRegion) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Init Test config failed") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-10 15:37:12 +08:00
										 |  |  | 	// remove the root directory after the test ends.
 | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	defer removeAll(rootPath) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	disks, err := getRandomDisks(1) | 
					
						
							|  |  |  | 	defer removeAll(disks[0]) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unable to create directories for FS backend. ", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	endpoints, err := parseStorageEndpoints(disks) | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-12 08:36:07 +08:00
										 |  |  | 	fs, _, err := initObjectLayer(endpoints) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unable to initialize FS backend.", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	serverConfig.SetRedisNotifyByID("1", redisNotify{Enable: true}) | 
					
						
							|  |  |  | 	if err := initEventNotifier(fs); err == nil { | 
					
						
							|  |  |  | 		t.Fatal("Redis config didn't fail.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | type TestPeerRPCServerData struct { | 
					
						
							|  |  |  | 	serverType string | 
					
						
							|  |  |  | 	testServer TestServer | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | func (s *TestPeerRPCServerData) Setup(t *testing.T) { | 
					
						
							|  |  |  | 	s.testServer = StartTestPeersRPCServer(t, s.serverType) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	// setup port and minio addr
 | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	host, port, err := net.SplitHostPort(s.testServer.Server.Listener.Addr().String()) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 		t.Fatalf("Initialisation error: %v", err) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	globalMinioHost = host | 
					
						
							|  |  |  | 	globalMinioPort = port | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	globalMinioAddr = getLocalAddress( | 
					
						
							|  |  |  | 		s.testServer.SrvCmdCfg, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// initialize the peer client(s)
 | 
					
						
							|  |  |  | 	initGlobalS3Peers(s.testServer.Disks) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *TestPeerRPCServerData) TearDown() { | 
					
						
							|  |  |  | 	s.testServer.Stop() | 
					
						
							|  |  |  | 	_ = removeAll(s.testServer.Root) | 
					
						
							|  |  |  | 	for _, d := range s.testServer.Disks { | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 		_ = removeAll(d.Path) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | func TestSetNGetBucketNotification(t *testing.T) { | 
					
						
							|  |  |  | 	s := TestPeerRPCServerData{serverType: "XL"} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// setup and teardown
 | 
					
						
							|  |  |  | 	s.Setup(t) | 
					
						
							|  |  |  | 	defer s.TearDown() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bucketName := getRandomBucketName() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	obj := s.testServer.Obj | 
					
						
							|  |  |  | 	if err := initEventNotifier(obj); err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unexpected error:", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	globalEventNotifier.SetBucketNotificationConfig(bucketName, ¬ificationConfig{}) | 
					
						
							|  |  |  | 	nConfig := globalEventNotifier.GetBucketNotificationConfig(bucketName) | 
					
						
							|  |  |  | 	if nConfig == nil { | 
					
						
							|  |  |  | 		t.Errorf("Notification expected to be set, but notification not set.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !reflect.DeepEqual(nConfig, ¬ificationConfig{}) { | 
					
						
							|  |  |  | 		t.Errorf("Mismatching notification configs.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestInitEventNotifier(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-12-23 23:12:19 +08:00
										 |  |  | 	currentIsDistXL := globalIsDistXL | 
					
						
							|  |  |  | 	defer func() { | 
					
						
							|  |  |  | 		globalIsDistXL = currentIsDistXL | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	s := TestPeerRPCServerData{serverType: "XL"} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// setup and teardown
 | 
					
						
							|  |  |  | 	s.Setup(t) | 
					
						
							|  |  |  | 	defer s.TearDown() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// test if empty object layer arg. returns expected error.
 | 
					
						
							|  |  |  | 	if err := initEventNotifier(nil); err == nil || err != errInvalidArgument { | 
					
						
							|  |  |  | 		t.Fatalf("initEventNotifier returned unexpected error value - %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	obj := s.testServer.Obj | 
					
						
							|  |  |  | 	bucketName := getRandomBucketName() | 
					
						
							|  |  |  | 	// declare sample configs
 | 
					
						
							|  |  |  | 	filterRules := []filterRule{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Name:  "prefix", | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | 			Value: globalMinioDefaultOwnerID, | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Name:  "suffix", | 
					
						
							|  |  |  | 			Value: "*.jpg", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	sampleSvcCfg := ServiceConfig{ | 
					
						
							|  |  |  | 		[]string{"s3:ObjectRemoved:*", "s3:ObjectCreated:*"}, | 
					
						
							|  |  |  | 		filterStruct{ | 
					
						
							|  |  |  | 			keyFilter{filterRules}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		"1", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	sampleNotifCfg := notificationConfig{ | 
					
						
							|  |  |  | 		QueueConfigs: []queueConfig{ | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				ServiceConfig: sampleSvcCfg, | 
					
						
							|  |  |  | 				QueueARN:      "testqARN", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	sampleListenCfg := []listenerConfig{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			TopicConfig: topicConfig{ServiceConfig: sampleSvcCfg, | 
					
						
							|  |  |  | 				TopicARN: "testlARN"}, | 
					
						
							|  |  |  | 			TargetServer: globalMinioAddr, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create bucket
 | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	if err := obj.MakeBucket(bucketName); err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unexpected error:", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	// bucket is created, now writing should not give errors.
 | 
					
						
							|  |  |  | 	if err := persistNotificationConfig(bucketName, &sampleNotifCfg, obj); err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unexpected error:", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	if err := persistListenerConfig(bucketName, sampleListenCfg, obj); err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unexpected error:", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 11:13:00 +08:00
										 |  |  | 	// needed to load listener config from disk for testing (in
 | 
					
						
							| 
									
										
										
										
											2017-01-17 09:05:00 +08:00
										 |  |  | 	// single peer mode, the listener config is ignored, but here
 | 
					
						
							| 
									
										
										
										
											2016-10-27 11:13:00 +08:00
										 |  |  | 	// we want to test the loading from disk too.)
 | 
					
						
							| 
									
										
										
										
											2016-11-08 04:09:24 +08:00
										 |  |  | 	globalIsDistXL = true | 
					
						
							| 
									
										
										
										
											2016-10-27 11:13:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	// test event notifier init
 | 
					
						
							|  |  |  | 	if err := initEventNotifier(obj); err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unexpected error:", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// fetch bucket configs and verify
 | 
					
						
							|  |  |  | 	ncfg := globalEventNotifier.GetBucketNotificationConfig(bucketName) | 
					
						
							|  |  |  | 	if ncfg == nil { | 
					
						
							|  |  |  | 		t.Error("Bucket notification was not present for ", bucketName) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(ncfg.QueueConfigs) != 1 || ncfg.QueueConfigs[0].QueueARN != "testqARN" { | 
					
						
							|  |  |  | 		t.Error("Unexpected bucket notification found - ", *ncfg) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if globalEventNotifier.GetExternalTarget("testqARN") != nil { | 
					
						
							|  |  |  | 		t.Error("A logger was not expected to be found as it was not enabled in the config.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	lcfg := globalEventNotifier.GetBucketListenerConfig(bucketName) | 
					
						
							|  |  |  | 	if lcfg == nil { | 
					
						
							|  |  |  | 		t.Error("Bucket listener was not present for ", bucketName) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(lcfg) != 1 || lcfg[0].TargetServer != globalMinioAddr || lcfg[0].TopicConfig.TopicARN != "testlARN" { | 
					
						
							|  |  |  | 		t.Error("Unexpected listener config found - ", lcfg[0]) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if globalEventNotifier.GetInternalTarget("testlARN") == nil { | 
					
						
							|  |  |  | 		t.Error("A listen logger was not found.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestListenBucketNotification(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-12-23 23:12:19 +08:00
										 |  |  | 	currentIsDistXL := globalIsDistXL | 
					
						
							|  |  |  | 	defer func() { | 
					
						
							|  |  |  | 		globalIsDistXL = currentIsDistXL | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	s := TestPeerRPCServerData{serverType: "XL"} | 
					
						
							|  |  |  | 	// setup and teardown
 | 
					
						
							|  |  |  | 	s.Setup(t) | 
					
						
							|  |  |  | 	defer s.TearDown() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// test initialisation
 | 
					
						
							|  |  |  | 	obj := s.testServer.Obj | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bucketName := "bucket" | 
					
						
							|  |  |  | 	objectName := "object" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Create the bucket to listen on
 | 
					
						
							|  |  |  | 	if err := obj.MakeBucket(bucketName); err != nil { | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 		t.Fatal("Unexpected error:", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-21 16:25:17 +08:00
										 |  |  | 	listenARN := fmt.Sprintf("%s:%s:1:%s-%s", | 
					
						
							|  |  |  | 		minioTopic, | 
					
						
							|  |  |  | 		serverConfig.GetRegion(), | 
					
						
							|  |  |  | 		snsTypeMinio, | 
					
						
							|  |  |  | 		s.testServer.Server.Listener.Addr(), | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	lcfg := listenerConfig{ | 
					
						
							| 
									
										
										
										
											2016-10-21 16:25:17 +08:00
										 |  |  | 		TopicConfig: topicConfig{ | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 			ServiceConfig{ | 
					
						
							|  |  |  | 				[]string{"s3:ObjectRemoved:*", "s3:ObjectCreated:*"}, | 
					
						
							|  |  |  | 				filterStruct{}, | 
					
						
							|  |  |  | 				"0", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			listenARN, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-21 16:25:17 +08:00
										 |  |  | 		TargetServer: globalMinioAddr, | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// write listener config to storage layer
 | 
					
						
							|  |  |  | 	lcfgs := []listenerConfig{lcfg} | 
					
						
							|  |  |  | 	if err := persistListenerConfig(bucketName, lcfgs, obj); err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Test Setup error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 11:13:00 +08:00
										 |  |  | 	// needed to load listener config from disk for testing (in
 | 
					
						
							|  |  |  | 	// single peer mode, the listener config is ingored, but here
 | 
					
						
							|  |  |  | 	// we want to test the loading from disk too.)
 | 
					
						
							| 
									
										
										
										
											2016-11-08 04:09:24 +08:00
										 |  |  | 	globalIsDistXL = true | 
					
						
							| 
									
										
										
										
											2016-10-27 11:13:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	// Init event notifier
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	if err := initEventNotifier(obj); err != nil { | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 		t.Fatal("Unexpected error:", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Check if the config is loaded
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	listenerCfg := globalEventNotifier.GetBucketListenerConfig(bucketName) | 
					
						
							|  |  |  | 	if listenerCfg == nil { | 
					
						
							|  |  |  | 		t.Fatal("Cannot load bucket listener config") | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	if len(listenerCfg) != 1 { | 
					
						
							|  |  |  | 		t.Fatal("Listener config is not correctly loaded. Exactly one listener config is expected") | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	// Check if topic ARN is correct
 | 
					
						
							|  |  |  | 	if listenerCfg[0].TopicConfig.TopicARN != listenARN { | 
					
						
							|  |  |  | 		t.Fatal("Configured topic ARN is incorrect.") | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Create a new notification event channel.
 | 
					
						
							|  |  |  | 	nEventCh := make(chan []NotificationEvent) | 
					
						
							|  |  |  | 	// Close the listener channel.
 | 
					
						
							|  |  |  | 	defer close(nEventCh) | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	// Add events channel for listener.
 | 
					
						
							|  |  |  | 	if err := globalEventNotifier.AddListenerChan(listenARN, nEventCh); err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Test Setup error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Remove listen channel after the writer has closed or the
 | 
					
						
							|  |  |  | 	// client disconnected.
 | 
					
						
							|  |  |  | 	defer globalEventNotifier.RemoveListenerChan(listenARN) | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Fire an event notification
 | 
					
						
							|  |  |  | 	go eventNotify(eventData{ | 
					
						
							|  |  |  | 		Type:   ObjectRemovedDelete, | 
					
						
							|  |  |  | 		Bucket: bucketName, | 
					
						
							|  |  |  | 		ObjInfo: ObjectInfo{ | 
					
						
							|  |  |  | 			Bucket: bucketName, | 
					
						
							|  |  |  | 			Name:   objectName, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		ReqParams: map[string]string{ | 
					
						
							|  |  |  | 			"sourceIPAddress": "localhost:1337", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Wait for the event notification here, if nothing is received within 30 seconds,
 | 
					
						
							|  |  |  | 	// test error will be fired
 | 
					
						
							|  |  |  | 	select { | 
					
						
							|  |  |  | 	case n := <-nEventCh: | 
					
						
							|  |  |  | 		// Check that received event
 | 
					
						
							|  |  |  | 		if len(n) == 0 { | 
					
						
							| 
									
										
										
										
											2016-10-04 15:09:21 +08:00
										 |  |  | 			t.Fatal("Unexpected error occurred") | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if n[0].S3.Object.Key != objectName { | 
					
						
							|  |  |  | 			t.Fatalf("Received wrong object name in notification, expected %s, received %s", n[0].S3.Object.Key, objectName) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		break | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	case <-time.After(3 * time.Second): | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | 		break | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-17 05:26:05 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | func TestAddRemoveBucketListenerConfig(t *testing.T) { | 
					
						
							|  |  |  | 	s := TestPeerRPCServerData{serverType: "XL"} | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	// setup and teardown
 | 
					
						
							|  |  |  | 	s.Setup(t) | 
					
						
							|  |  |  | 	defer s.TearDown() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// test code
 | 
					
						
							|  |  |  | 	obj := s.testServer.Obj | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | 	if err := initEventNotifier(obj); err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 		t.Fatalf("Failed to initialize event notifier: %v", err) | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Make a bucket to store topicConfigs.
 | 
					
						
							|  |  |  | 	randBucket := getRandomBucketName() | 
					
						
							|  |  |  | 	if err := obj.MakeBucket(randBucket); err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 		t.Fatalf("Failed to make bucket %s", randBucket) | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Add a topicConfig to an empty notificationConfig.
 | 
					
						
							|  |  |  | 	accountID := fmt.Sprintf("%d", time.Now().UTC().UnixNano()) | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	accountARN := fmt.Sprintf( | 
					
						
							|  |  |  | 		"arn:minio:sqs:%s:%s:listen-%s", | 
					
						
							|  |  |  | 		serverConfig.GetRegion(), | 
					
						
							|  |  |  | 		accountID, | 
					
						
							|  |  |  | 		globalMinioAddr, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Make topic configuration
 | 
					
						
							|  |  |  | 	filterRules := []filterRule{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Name:  "prefix", | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | 			Value: globalMinioDefaultOwnerID, | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Name:  "suffix", | 
					
						
							|  |  |  | 			Value: "*.jpg", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	sampleTopicCfg := topicConfig{ | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | 		TopicARN: accountARN, | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 		ServiceConfig: ServiceConfig{ | 
					
						
							|  |  |  | 			[]string{"s3:ObjectRemoved:*", "s3:ObjectCreated:*"}, | 
					
						
							|  |  |  | 			filterStruct{ | 
					
						
							|  |  |  | 				keyFilter{filterRules}, | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 			"sns-" + accountID, | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	sampleListenerCfg := &listenerConfig{ | 
					
						
							|  |  |  | 		TopicConfig:  sampleTopicCfg, | 
					
						
							|  |  |  | 		TargetServer: globalMinioAddr, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 		lCfg        *listenerConfig | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | 		expectedErr error | 
					
						
							|  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 		{sampleListenerCfg, nil}, | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | 		{nil, errInvalidArgument}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range testCases { | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 		err := AddBucketListenerConfig(randBucket, test.lCfg, obj) | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | 		if err != test.expectedErr { | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 			t.Errorf( | 
					
						
							|  |  |  | 				"Test %d: Failed with error %v, expected to fail with %v", | 
					
						
							|  |  |  | 				i+1, err, test.expectedErr, | 
					
						
							|  |  |  | 			) | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 	// test remove listener actually removes a listener
 | 
					
						
							|  |  |  | 	RemoveBucketListenerConfig(randBucket, sampleListenerCfg, obj) | 
					
						
							|  |  |  | 	// since it does not return errors we fetch the config and
 | 
					
						
							|  |  |  | 	// check
 | 
					
						
							|  |  |  | 	lcSlice := globalEventNotifier.GetBucketListenerConfig(randBucket) | 
					
						
							|  |  |  | 	if len(lcSlice) != 0 { | 
					
						
							|  |  |  | 		t.Errorf("Remove Listener Config Test: did not remove listener config - %v", | 
					
						
							|  |  |  | 			lcSlice) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-29 13:46:19 +08:00
										 |  |  | } |