2016-07-25 20:53:55 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
2016-07-26 03:01:35 -04:00
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
// Tests filter name validation.
|
|
|
|
func TestIsValidFilterName(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
filterName string
|
|
|
|
status bool
|
|
|
|
}{
|
|
|
|
// Validate if 'prefix' is correct.
|
|
|
|
{
|
|
|
|
filterName: "prefix",
|
|
|
|
status: true,
|
|
|
|
},
|
|
|
|
// Validate if 'suffix' is correct.
|
|
|
|
{
|
|
|
|
filterName: "suffix",
|
|
|
|
status: true,
|
|
|
|
},
|
|
|
|
// Invalid filter name empty string should return false.
|
|
|
|
{
|
|
|
|
filterName: "",
|
|
|
|
status: false,
|
|
|
|
},
|
|
|
|
// Invalid filter name random character should return false.
|
|
|
|
{
|
|
|
|
filterName: "unknown",
|
|
|
|
status: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
status := isValidFilterName(testCase.filterName)
|
|
|
|
if testCase.status != status {
|
|
|
|
t.Errorf("Test %d: Expected \"%t\", got \"%t\"", i+1, testCase.status, status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests list of valid and invalid events.
|
|
|
|
func TestValidEvents(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
events []string
|
|
|
|
errCode APIErrorCode
|
|
|
|
}{
|
|
|
|
// Return error for unknown event element.
|
|
|
|
{
|
|
|
|
events: []string{
|
|
|
|
"s3:UnknownAPI",
|
|
|
|
},
|
|
|
|
errCode: ErrEventNotification,
|
|
|
|
},
|
|
|
|
// Return success for supported event.
|
|
|
|
{
|
|
|
|
events: []string{
|
|
|
|
"s3:ObjectCreated:Put",
|
|
|
|
},
|
|
|
|
errCode: ErrNone,
|
|
|
|
},
|
|
|
|
// Return success for supported events.
|
|
|
|
{
|
|
|
|
events: []string{
|
|
|
|
"s3:ObjectCreated:*",
|
|
|
|
"s3:ObjectRemoved:*",
|
|
|
|
},
|
|
|
|
errCode: ErrNone,
|
|
|
|
},
|
|
|
|
// Return error for empty event list.
|
|
|
|
{
|
|
|
|
events: []string{""},
|
|
|
|
errCode: ErrEventNotification,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
errCode := checkEvents(testCase.events)
|
|
|
|
if testCase.errCode != errCode {
|
|
|
|
t.Errorf("Test %d: Expected \"%d\", got \"%d\"", i+1, testCase.errCode, errCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 23:56:43 -04:00
|
|
|
// Tests topic arn validation.
|
|
|
|
func TestTopicARN(t *testing.T) {
|
2016-08-05 01:01:58 -04:00
|
|
|
rootPath, err := newTestConfig("us-east-1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable initialize config file, %s", err)
|
|
|
|
}
|
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
testCases := []struct {
|
2016-08-15 23:56:43 -04:00
|
|
|
topicARN string
|
|
|
|
errCode APIErrorCode
|
2016-08-05 01:01:58 -04:00
|
|
|
}{
|
2016-08-15 23:56:43 -04:00
|
|
|
// Valid minio topic with '1' account id.
|
2016-08-05 01:01:58 -04:00
|
|
|
{
|
2016-08-15 23:56:43 -04:00
|
|
|
topicARN: "arn:minio:sns:us-east-1:1:minio",
|
|
|
|
errCode: ErrNone,
|
2016-08-05 01:01:58 -04:00
|
|
|
},
|
2016-08-15 23:56:43 -04:00
|
|
|
// Valid minio topic with '10' account id.
|
2016-08-05 01:01:58 -04:00
|
|
|
{
|
2016-08-15 23:56:43 -04:00
|
|
|
topicARN: "arn:minio:sns:us-east-1:10:minio",
|
|
|
|
errCode: ErrNone,
|
2016-08-05 01:01:58 -04:00
|
|
|
},
|
|
|
|
// Invalid empty queue arn.
|
|
|
|
{
|
2016-08-15 23:56:43 -04:00
|
|
|
topicARN: "",
|
|
|
|
errCode: ErrARNNotification,
|
2016-08-05 01:01:58 -04:00
|
|
|
},
|
|
|
|
// Invalid region 'us-west-1' in queue arn.
|
|
|
|
{
|
2016-08-15 23:56:43 -04:00
|
|
|
topicARN: "arn:minio:sns:us-west-1:1:redis",
|
|
|
|
errCode: ErrRegionNotification,
|
2016-08-05 01:01:58 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
2016-08-15 23:56:43 -04:00
|
|
|
errCode := checkTopicARN(testCase.topicARN)
|
2016-08-05 01:01:58 -04:00
|
|
|
if testCase.errCode != errCode {
|
|
|
|
t.Errorf("Test %d: Expected \"%d\", got \"%d\"", i+1, testCase.errCode, errCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 03:01:35 -04:00
|
|
|
// Tests queue arn validation.
|
2016-08-05 01:01:58 -04:00
|
|
|
func TestQueueARN(t *testing.T) {
|
2016-07-26 03:01:35 -04:00
|
|
|
rootPath, err := newTestConfig("us-east-1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable initialize config file, %s", err)
|
|
|
|
}
|
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
testCases := []struct {
|
2016-08-05 01:01:58 -04:00
|
|
|
queueARN string
|
2016-07-26 03:01:35 -04:00
|
|
|
errCode APIErrorCode
|
|
|
|
}{
|
|
|
|
// Valid redis queue arn.
|
|
|
|
{
|
2016-08-05 01:01:58 -04:00
|
|
|
queueARN: "arn:minio:sqs:us-east-1:1:redis",
|
2016-07-26 03:01:35 -04:00
|
|
|
errCode: ErrNone,
|
|
|
|
},
|
|
|
|
// Valid elasticsearch queue arn.
|
|
|
|
{
|
2016-08-05 01:01:58 -04:00
|
|
|
queueARN: "arn:minio:sqs:us-east-1:1:elasticsearch",
|
2016-07-26 03:01:35 -04:00
|
|
|
errCode: ErrNone,
|
|
|
|
},
|
|
|
|
// Valid amqp queue arn.
|
|
|
|
{
|
2016-08-05 01:01:58 -04:00
|
|
|
queueARN: "arn:minio:sqs:us-east-1:1:amqp",
|
2016-07-26 03:01:35 -04:00
|
|
|
errCode: ErrNone,
|
|
|
|
},
|
|
|
|
// Invalid empty queue arn.
|
|
|
|
{
|
2016-08-05 01:01:58 -04:00
|
|
|
queueARN: "",
|
2016-07-26 03:01:35 -04:00
|
|
|
errCode: ErrARNNotification,
|
|
|
|
},
|
|
|
|
// Invalid region 'us-west-1' in queue arn.
|
|
|
|
{
|
2016-08-05 01:01:58 -04:00
|
|
|
queueARN: "arn:minio:sqs:us-west-1:1:redis",
|
2016-07-26 03:01:35 -04:00
|
|
|
errCode: ErrRegionNotification,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
2016-08-05 01:01:58 -04:00
|
|
|
errCode := checkQueueARN(testCase.queueARN)
|
2016-07-26 03:01:35 -04:00
|
|
|
if testCase.errCode != errCode {
|
|
|
|
t.Errorf("Test %d: Expected \"%d\", got \"%d\"", i+1, testCase.errCode, errCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 23:56:43 -04:00
|
|
|
// Test unmarshal topic arn.
|
|
|
|
func TestUnmarshalTopicARN(t *testing.T) {
|
2016-08-05 01:01:58 -04:00
|
|
|
rootPath, err := newTestConfig("us-east-1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable initialize config file, %s", err)
|
|
|
|
}
|
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
testCases := []struct {
|
2016-08-15 23:56:43 -04:00
|
|
|
topicARN string
|
|
|
|
Type string
|
2016-08-05 01:01:58 -04:00
|
|
|
}{
|
2016-08-15 23:56:43 -04:00
|
|
|
// Valid minio topic arn.
|
2016-08-05 01:01:58 -04:00
|
|
|
{
|
2016-08-15 23:56:43 -04:00
|
|
|
topicARN: "arn:minio:sns:us-east-1:1:listen",
|
|
|
|
Type: "listen",
|
2016-08-05 01:01:58 -04:00
|
|
|
},
|
2016-08-15 23:56:43 -04:00
|
|
|
// Invalid empty topic arn.
|
2016-08-05 01:01:58 -04:00
|
|
|
{
|
2016-08-15 23:56:43 -04:00
|
|
|
topicARN: "",
|
|
|
|
Type: "",
|
2016-08-05 01:01:58 -04:00
|
|
|
},
|
2016-08-15 23:56:43 -04:00
|
|
|
// Invalid region 'us-west-1' in topic arn.
|
2016-08-05 01:01:58 -04:00
|
|
|
{
|
2016-08-15 23:56:43 -04:00
|
|
|
topicARN: "arn:minio:sns:us-west-1:1:listen",
|
|
|
|
Type: "",
|
2016-08-05 01:01:58 -04:00
|
|
|
},
|
2016-08-15 23:56:43 -04:00
|
|
|
// Partial topic arn.
|
2016-08-05 01:01:58 -04:00
|
|
|
{
|
2016-08-15 23:56:43 -04:00
|
|
|
topicARN: "arn:minio:sns:",
|
|
|
|
Type: "",
|
2016-08-05 01:01:58 -04:00
|
|
|
},
|
2016-08-15 23:56:43 -04:00
|
|
|
// Invalid topic service value.
|
2016-08-05 01:01:58 -04:00
|
|
|
{
|
2016-08-15 23:56:43 -04:00
|
|
|
topicARN: "arn:minio:sns:us-east-1:1:*",
|
|
|
|
Type: "",
|
2016-08-05 01:01:58 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
2016-08-15 23:56:43 -04:00
|
|
|
topic := unmarshalTopicARN(testCase.topicARN)
|
|
|
|
if testCase.Type != topic.Type {
|
|
|
|
t.Errorf("Test %d: Expected \"%s\", got \"%s\"", i+1, testCase.Type, topic.Type)
|
2016-08-05 01:01:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test unmarshal queue arn.
|
|
|
|
func TestUnmarshalSqsARN(t *testing.T) {
|
2016-07-26 03:01:35 -04:00
|
|
|
rootPath, err := newTestConfig("us-east-1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable initialize config file, %s", err)
|
|
|
|
}
|
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
testCases := []struct {
|
2016-08-05 01:01:58 -04:00
|
|
|
queueARN string
|
|
|
|
Type string
|
2016-07-26 03:01:35 -04:00
|
|
|
}{
|
|
|
|
// Valid redis queue arn.
|
|
|
|
{
|
2016-08-05 01:01:58 -04:00
|
|
|
queueARN: "arn:minio:sqs:us-east-1:1:redis",
|
|
|
|
Type: "redis",
|
2016-07-26 03:01:35 -04:00
|
|
|
},
|
|
|
|
// Valid elasticsearch queue arn.
|
|
|
|
{
|
2016-08-05 01:01:58 -04:00
|
|
|
queueARN: "arn:minio:sqs:us-east-1:1:elasticsearch",
|
|
|
|
Type: "elasticsearch",
|
2016-07-26 03:01:35 -04:00
|
|
|
},
|
|
|
|
// Valid amqp queue arn.
|
|
|
|
{
|
2016-08-05 01:01:58 -04:00
|
|
|
queueARN: "arn:minio:sqs:us-east-1:1:amqp",
|
|
|
|
Type: "amqp",
|
2016-07-26 03:01:35 -04:00
|
|
|
},
|
|
|
|
// Invalid empty queue arn.
|
|
|
|
{
|
2016-08-05 01:01:58 -04:00
|
|
|
queueARN: "",
|
|
|
|
Type: "",
|
2016-07-26 03:01:35 -04:00
|
|
|
},
|
|
|
|
// Invalid region 'us-west-1' in queue arn.
|
|
|
|
{
|
2016-08-05 01:01:58 -04:00
|
|
|
queueARN: "arn:minio:sqs:us-west-1:1:redis",
|
|
|
|
Type: "",
|
2016-07-26 03:01:35 -04:00
|
|
|
},
|
|
|
|
// Partial queue arn.
|
|
|
|
{
|
2016-08-05 01:01:58 -04:00
|
|
|
queueARN: "arn:minio:sqs:",
|
|
|
|
Type: "",
|
2016-07-26 03:01:35 -04:00
|
|
|
},
|
|
|
|
// Invalid queue service value.
|
|
|
|
{
|
2016-08-05 01:01:58 -04:00
|
|
|
queueARN: "arn:minio:sqs:us-east-1:1:*",
|
|
|
|
Type: "",
|
2016-07-26 03:01:35 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
2016-08-05 01:01:58 -04:00
|
|
|
mSqs := unmarshalSqsARN(testCase.queueARN)
|
|
|
|
if testCase.Type != mSqs.Type {
|
|
|
|
t.Errorf("Test %d: Expected \"%s\", got \"%s\"", i+1, testCase.Type, mSqs.Type)
|
2016-07-26 03:01:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|