From 1f9e38e3cd759764d2308c598674255eb576c19e Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 26 Jul 2016 00:01:35 -0700 Subject: [PATCH] api: Add bucket notification util tests. (#2289) --- auth-handler_test.go | 17 +-- bucket-notification-utils_test.go | 186 ++++++++++++++++++++++++++++++ bucket-policy-handlers_test.go | 14 ++- handler-utils_test.go | 16 +-- leak-detect_test.go | 2 +- queues_test.go | 107 +++++++++++++++++ signature-jwt_test.go | 32 +---- test-utils_test.go | 38 +++--- 8 files changed, 337 insertions(+), 75 deletions(-) create mode 100644 queues_test.go diff --git a/auth-handler_test.go b/auth-handler_test.go index 5484168c6..5f0d8032c 100644 --- a/auth-handler_test.go +++ b/auth-handler_test.go @@ -19,7 +19,6 @@ package main import ( "bytes" "io" - "io/ioutil" "net/http" "testing" ) @@ -46,24 +45,12 @@ func mustNewSignedRequest(method string, urlStr string, contentLength int64, bod // Tests is requested authenticated function, tests replies for s3 errors. func TestIsReqAuthenticated(t *testing.T) { - savedServerConfig := serverConfig - defer func() { - serverConfig = savedServerConfig - }() - serverConfig = nil - - // Test initialized config file. - path, err := ioutil.TempDir("", "minio-") + path, err := newTestConfig("us-east-1") if err != nil { - t.Fatalf("Unable to create a temporary directory, %s", err) + t.Fatalf("unable initialize config file, %s", err) } defer removeAll(path) - // Inititalize a new config. - setGlobalConfigPath(path) - if err := initConfig(); err != nil { - t.Fatalf("unable initialize config file, %s", err) - } serverConfig.SetCredential(credential{"myuser", "mypassword"}) // List of test cases for validating http request authentication. diff --git a/bucket-notification-utils_test.go b/bucket-notification-utils_test.go index 4e7aad742..783f03a5f 100644 --- a/bucket-notification-utils_test.go +++ b/bucket-notification-utils_test.go @@ -15,3 +15,189 @@ */ package main + +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) + } + } +} + +// Tests queue arn validation. +func TestQueueArn(t *testing.T) { + rootPath, err := newTestConfig("us-east-1") + if err != nil { + t.Fatalf("unable initialize config file, %s", err) + } + defer removeAll(rootPath) + + testCases := []struct { + queueArn string + errCode APIErrorCode + }{ + // Valid redis queue arn. + { + queueArn: "arn:minio:sqs:us-east-1:1:redis", + errCode: ErrNone, + }, + // Valid elasticsearch queue arn. + { + queueArn: "arn:minio:sqs:us-east-1:1:elasticsearch", + errCode: ErrNone, + }, + // Valid amqp queue arn. + { + queueArn: "arn:minio:sqs:us-east-1:1:amqp", + errCode: ErrNone, + }, + // Invalid empty queue arn. + { + queueArn: "", + errCode: ErrARNNotification, + }, + // Invalid region 'us-west-1' in queue arn. + { + queueArn: "arn:minio:sqs:us-west-1:1:redis", + errCode: ErrRegionNotification, + }, + } + + for i, testCase := range testCases { + errCode := checkQueueArn(testCase.queueArn) + if testCase.errCode != errCode { + t.Errorf("Test %d: Expected \"%d\", got \"%d\"", i+1, testCase.errCode, errCode) + } + } +} + +// Test unmarshal queue arn. +func TestUnmarshalSqsArn(t *testing.T) { + rootPath, err := newTestConfig("us-east-1") + if err != nil { + t.Fatalf("unable initialize config file, %s", err) + } + defer removeAll(rootPath) + + testCases := []struct { + queueArn string + sqsType string + }{ + // Valid redis queue arn. + { + queueArn: "arn:minio:sqs:us-east-1:1:redis", + sqsType: "1:redis", + }, + // Valid elasticsearch queue arn. + { + queueArn: "arn:minio:sqs:us-east-1:1:elasticsearch", + sqsType: "1:elasticsearch", + }, + // Valid amqp queue arn. + { + queueArn: "arn:minio:sqs:us-east-1:1:amqp", + sqsType: "1:amqp", + }, + // Invalid empty queue arn. + { + queueArn: "", + sqsType: "", + }, + // Invalid region 'us-west-1' in queue arn. + { + queueArn: "arn:minio:sqs:us-west-1:1:redis", + sqsType: "", + }, + // Partial queue arn. + { + queueArn: "arn:minio:sqs:", + sqsType: "", + }, + // Invalid queue service value. + { + queueArn: "arn:minio:sqs:us-east-1:1:*", + sqsType: "", + }, + } + + for i, testCase := range testCases { + mSqs := unmarshalSqsArn(testCase.queueArn) + if testCase.sqsType != mSqs.sqsType { + t.Errorf("Test %d: Expected \"%s\", got \"%s\"", i+1, testCase.sqsType, mSqs.sqsType) + } + } + +} diff --git a/bucket-policy-handlers_test.go b/bucket-policy-handlers_test.go index 7dd94110d..b8fc0d71c 100644 --- a/bucket-policy-handlers_test.go +++ b/bucket-policy-handlers_test.go @@ -329,13 +329,15 @@ func testPutBucketPolicyHandler(obj ObjectLayer, instanceType string, t TestErrH apiRouter := initTestAPIEndPoints(obj, []string{"PutBucketPolicy"}) // initialize the server and obtain the credentials and root. // credentials are necessary to sign the HTTP request. - credentials, rootPath, err := initTestConfig("us-east-1") + rootPath, err := newTestConfig("us-east-1") if err != nil { t.Fatalf("Init Test config failed") } // remove the root folder after the test ends. defer removeAll(rootPath) + credentials := serverConfig.GetCredential() + // template for constructing HTTP request body for PUT bucket policy. bucketPolicyTemplate := `{ "Version": "2012-10-17", @@ -425,13 +427,15 @@ func testGetBucketPolicyHandler(obj ObjectLayer, instanceType string, t TestErrH apiRouter := initTestAPIEndPoints(obj, []string{"PutBucketPolicy", "GetBucketPolicy"}) // initialize the server and obtain the credentials and root. // credentials are necessary to sign the HTTP request. - credentials, rootPath, err := initTestConfig("us-east-1") + rootPath, err := newTestConfig("us-east-1") if err != nil { t.Fatalf("Init Test config failed") } // remove the root folder after the test ends. defer removeAll(rootPath) + credentials := serverConfig.GetCredential() + // template for constructing HTTP request body for PUT bucket policy. bucketPolicyTemplate := `{ "Version": "2012-10-17", @@ -559,18 +563,22 @@ func testDeleteBucketPolicyHandler(obj ObjectLayer, instanceType string, t TestE // failed to create newbucket, abort. t.Fatalf("%s : %s", instanceType, err) } + // Register the API end points with XL/FS object layer. // Registering PutBucketPolicy and DeleteBucketPolicy handlers. apiRouter := initTestAPIEndPoints(obj, []string{"PutBucketPolicy", "DeleteBucketPolicy"}) + // initialize the server and obtain the credentials and root. // credentials are necessary to sign the HTTP request. - credentials, rootPath, err := initTestConfig("us-east-1") + rootPath, err := newTestConfig("us-east-1") if err != nil { t.Fatalf("Init Test config failed") } // remove the root folder after the test ends. defer removeAll(rootPath) + credentials := serverConfig.GetCredential() + // template for constructing HTTP request body for PUT bucket policy. bucketPolicyTemplate := `{ "Version": "2012-10-17", diff --git a/handler-utils_test.go b/handler-utils_test.go index a52cdbf33..767f0a7eb 100644 --- a/handler-utils_test.go +++ b/handler-utils_test.go @@ -27,23 +27,11 @@ import ( // Tests validate bucket LocationConstraint. func TestIsValidLocationContraint(t *testing.T) { - savedServerConfig := serverConfig - defer func() { - serverConfig = savedServerConfig - }() - serverConfig = nil - - // Test initialized config file. - path, err := ioutil.TempDir("", "minio-") + path, err := newTestConfig("us-east-1") if err != nil { - t.Fatalf("Unable to create a temporary directory, %s", err) - } - defer removeAll(path) - - setGlobalConfigPath(path) - if err := initConfig(); err != nil { t.Fatalf("unable initialize config file, %s", err) } + defer removeAll(path) // generates the input request with XML bucket configuration set to the request body. createExpectedRequest := func(req *http.Request, location string) (*http.Request, error) { diff --git a/leak-detect_test.go b/leak-detect_test.go index f73df1687..617f19b0d 100644 --- a/leak-detect_test.go +++ b/leak-detect_test.go @@ -15,7 +15,7 @@ import ( ) const ( - // deadline (in seconds) upto which the go routine leak detection has to be retried. + // deadline (in seconds) up to which the go routine leak detection has to be retried. leakDetectDeadline = 5 // pause time (in milliseconds) between each snapshot at the end of the go routine leak detection. leakDetectPauseTimeMs = 50 diff --git a/queues_test.go b/queues_test.go new file mode 100644 index 000000000..ba0702434 --- /dev/null +++ b/queues_test.go @@ -0,0 +1,107 @@ +/* + * 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 + +import "testing" + +// Tests all event match. +func TestEventMatch(t *testing.T) { + testCases := []struct { + eventName EventName + events []string + match bool + }{ + // Valid object created PUT event. + { + eventName: ObjectCreatedPut, + events: []string{ + "s3:ObjectCreated:Put", + }, + match: true, + }, + // Valid object removed DELETE event. + { + eventName: ObjectRemovedDelete, + events: []string{ + "s3:ObjectRemoved:Delete", + }, + match: true, + }, + // Invalid events fails to match with empty events. + { + eventName: ObjectRemovedDelete, + events: []string{""}, + match: false, + }, + // Invalid events fails to match with valid events. + { + eventName: ObjectCreatedCompleteMultipartUpload, + events: []string{ + "s3:ObjectRemoved:*", + }, + match: false, + }, + // Valid events wild card match. + { + eventName: ObjectCreatedPut, + events: []string{ + "s3:ObjectCreated:*", + }, + match: true, + }, + // Valid events wild card match. + { + eventName: ObjectCreatedPost, + events: []string{ + "s3:ObjectCreated:*", + }, + match: true, + }, + // Valid events wild card match. + { + eventName: ObjectCreatedCopy, + events: []string{ + "s3:ObjectCreated:*", + }, + match: true, + }, + // Valid events wild card match. + { + eventName: ObjectCreatedCompleteMultipartUpload, + events: []string{ + "s3:ObjectCreated:*", + }, + match: true, + }, + // Valid events wild card match. + { + eventName: ObjectCreatedPut, + events: []string{ + "s3:ObjectCreated:*", + "s3:ObjectRemoved:*", + }, + match: true, + }, + } + + for i, testCase := range testCases { + ok := eventMatch(testCase.eventName, testCase.events) + if testCase.match != ok { + t.Errorf("Test %d: Expected \"%t\", got \"%t\"", i+1, testCase.match, ok) + } + } +} diff --git a/signature-jwt_test.go b/signature-jwt_test.go index 14a3b7413..9402a3ebf 100644 --- a/signature-jwt_test.go +++ b/signature-jwt_test.go @@ -126,23 +126,11 @@ func TestNewJWT(t *testing.T) { // Tests JWT.GenerateToken() func TestGenerateToken(t *testing.T) { - savedServerConfig := serverConfig - defer func() { - serverConfig = savedServerConfig - }() - serverConfig = nil - - // Setup. - testPath, err := ioutil.TempDir("", "minio-") + testPath, err := newTestConfig("us-east-1") if err != nil { - t.Fatalf("Unable to create a temporary directory, %s", err) - } - defer removeAll(testPath) - - setGlobalConfigPath(testPath) - if err = initConfig(); err != nil { t.Fatalf("unable initialize config file, %s", err) } + defer removeAll(testPath) jwt, err := newJWT() if err != nil { @@ -185,23 +173,11 @@ func TestGenerateToken(t *testing.T) { // Tests JWT.Authenticate() func TestAuthenticate(t *testing.T) { - savedServerConfig := serverConfig - defer func() { - serverConfig = savedServerConfig - }() - serverConfig = nil - - // Setup. - testPath, err := ioutil.TempDir("", "minio-") + testPath, err := newTestConfig("us-east-1") if err != nil { - t.Fatalf("Unable to create a temporary directory, %s", err) - } - defer removeAll(testPath) - - setGlobalConfigPath(testPath) - if err = initConfig(); err != nil { t.Fatalf("unable initialize config file, %s", err) } + defer removeAll(testPath) jwt, err := newJWT() if err != nil { diff --git a/test-utils_test.go b/test-utils_test.go index 956dfa3fb..83dbc65bf 100644 --- a/test-utils_test.go +++ b/test-utils_test.go @@ -120,10 +120,14 @@ func StartTestServer(t TestErrHandler, instanceType string) TestServer { t.Fatalf("Failed obtaining Temp Backend: %s", err) } - credentials, root, err := initTestConfig("us-east-1") + root, err := newTestConfig("us-east-1") if err != nil { t.Fatalf("%s", err) } + + // Get credential. + credentials := serverConfig.GetCredential() + testServer.Root = root testServer.Disks = erasureDisks testServer.AccessKey = credentials.AccessKeyID @@ -135,25 +139,31 @@ func StartTestServer(t TestErrHandler, instanceType string) TestServer { } // Configure the server for the test run. -func initTestConfig(bucketLocation string) (credential, string, error) { - // Initialize server config. - initConfig() - // Get credential. - credentials := serverConfig.GetCredential() - // Set a default region. - serverConfig.SetRegion(bucketLocation) - rootPath, err := getTestRoot() +func newTestConfig(bucketLocation string) (rootPath string, err error) { + // Get test root. + rootPath, err = getTestRoot() if err != nil { - return credential{}, "", err + return "", err } + // Do this only once here. setGlobalConfigPath(rootPath) - err = serverConfig.Save() - if err != nil { - return credential{}, "", err + // Initialize server config. + if err = initConfig(); err != nil { + return "", err } - return credentials, rootPath, nil + + // Set a default region. + serverConfig.SetRegion(bucketLocation) + + // Save config. + if err = serverConfig.Save(); err != nil { + return "", err + } + + // Return root path. + return rootPath, nil } // Deleting the temporary backend and stopping the server.