api: Add bucket notification util tests. (#2289)

This commit is contained in:
Harshavardhana 2016-07-26 00:01:35 -07:00 committed by GitHub
parent 530ed67b59
commit 1f9e38e3cd
8 changed files with 337 additions and 75 deletions

View File

@ -19,7 +19,6 @@ package main
import ( import (
"bytes" "bytes"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"testing" "testing"
) )
@ -46,24 +45,12 @@ func mustNewSignedRequest(method string, urlStr string, contentLength int64, bod
// Tests is requested authenticated function, tests replies for s3 errors. // Tests is requested authenticated function, tests replies for s3 errors.
func TestIsReqAuthenticated(t *testing.T) { func TestIsReqAuthenticated(t *testing.T) {
savedServerConfig := serverConfig path, err := newTestConfig("us-east-1")
defer func() {
serverConfig = savedServerConfig
}()
serverConfig = nil
// Test initialized config file.
path, err := ioutil.TempDir("", "minio-")
if err != nil { if err != nil {
t.Fatalf("Unable to create a temporary directory, %s", err) t.Fatalf("unable initialize config file, %s", err)
} }
defer removeAll(path) 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"}) serverConfig.SetCredential(credential{"myuser", "mypassword"})
// List of test cases for validating http request authentication. // List of test cases for validating http request authentication.

View File

@ -15,3 +15,189 @@
*/ */
package main 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)
}
}
}

View File

@ -329,13 +329,15 @@ func testPutBucketPolicyHandler(obj ObjectLayer, instanceType string, t TestErrH
apiRouter := initTestAPIEndPoints(obj, []string{"PutBucketPolicy"}) apiRouter := initTestAPIEndPoints(obj, []string{"PutBucketPolicy"})
// initialize the server and obtain the credentials and root. // initialize the server and obtain the credentials and root.
// credentials are necessary to sign the HTTP request. // credentials are necessary to sign the HTTP request.
credentials, rootPath, err := initTestConfig("us-east-1") rootPath, err := newTestConfig("us-east-1")
if err != nil { if err != nil {
t.Fatalf("Init Test config failed") t.Fatalf("Init Test config failed")
} }
// remove the root folder after the test ends. // remove the root folder after the test ends.
defer removeAll(rootPath) defer removeAll(rootPath)
credentials := serverConfig.GetCredential()
// template for constructing HTTP request body for PUT bucket policy. // template for constructing HTTP request body for PUT bucket policy.
bucketPolicyTemplate := `{ bucketPolicyTemplate := `{
"Version": "2012-10-17", "Version": "2012-10-17",
@ -425,13 +427,15 @@ func testGetBucketPolicyHandler(obj ObjectLayer, instanceType string, t TestErrH
apiRouter := initTestAPIEndPoints(obj, []string{"PutBucketPolicy", "GetBucketPolicy"}) apiRouter := initTestAPIEndPoints(obj, []string{"PutBucketPolicy", "GetBucketPolicy"})
// initialize the server and obtain the credentials and root. // initialize the server and obtain the credentials and root.
// credentials are necessary to sign the HTTP request. // credentials are necessary to sign the HTTP request.
credentials, rootPath, err := initTestConfig("us-east-1") rootPath, err := newTestConfig("us-east-1")
if err != nil { if err != nil {
t.Fatalf("Init Test config failed") t.Fatalf("Init Test config failed")
} }
// remove the root folder after the test ends. // remove the root folder after the test ends.
defer removeAll(rootPath) defer removeAll(rootPath)
credentials := serverConfig.GetCredential()
// template for constructing HTTP request body for PUT bucket policy. // template for constructing HTTP request body for PUT bucket policy.
bucketPolicyTemplate := `{ bucketPolicyTemplate := `{
"Version": "2012-10-17", "Version": "2012-10-17",
@ -559,18 +563,22 @@ func testDeleteBucketPolicyHandler(obj ObjectLayer, instanceType string, t TestE
// failed to create newbucket, abort. // failed to create newbucket, abort.
t.Fatalf("%s : %s", instanceType, err) t.Fatalf("%s : %s", instanceType, err)
} }
// Register the API end points with XL/FS object layer. // Register the API end points with XL/FS object layer.
// Registering PutBucketPolicy and DeleteBucketPolicy handlers. // Registering PutBucketPolicy and DeleteBucketPolicy handlers.
apiRouter := initTestAPIEndPoints(obj, []string{"PutBucketPolicy", "DeleteBucketPolicy"}) apiRouter := initTestAPIEndPoints(obj, []string{"PutBucketPolicy", "DeleteBucketPolicy"})
// initialize the server and obtain the credentials and root. // initialize the server and obtain the credentials and root.
// credentials are necessary to sign the HTTP request. // credentials are necessary to sign the HTTP request.
credentials, rootPath, err := initTestConfig("us-east-1") rootPath, err := newTestConfig("us-east-1")
if err != nil { if err != nil {
t.Fatalf("Init Test config failed") t.Fatalf("Init Test config failed")
} }
// remove the root folder after the test ends. // remove the root folder after the test ends.
defer removeAll(rootPath) defer removeAll(rootPath)
credentials := serverConfig.GetCredential()
// template for constructing HTTP request body for PUT bucket policy. // template for constructing HTTP request body for PUT bucket policy.
bucketPolicyTemplate := `{ bucketPolicyTemplate := `{
"Version": "2012-10-17", "Version": "2012-10-17",

View File

@ -27,23 +27,11 @@ import (
// Tests validate bucket LocationConstraint. // Tests validate bucket LocationConstraint.
func TestIsValidLocationContraint(t *testing.T) { func TestIsValidLocationContraint(t *testing.T) {
savedServerConfig := serverConfig path, err := newTestConfig("us-east-1")
defer func() {
serverConfig = savedServerConfig
}()
serverConfig = nil
// Test initialized config file.
path, err := ioutil.TempDir("", "minio-")
if err != nil { 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) t.Fatalf("unable initialize config file, %s", err)
} }
defer removeAll(path)
// generates the input request with XML bucket configuration set to the request body. // generates the input request with XML bucket configuration set to the request body.
createExpectedRequest := func(req *http.Request, location string) (*http.Request, error) { createExpectedRequest := func(req *http.Request, location string) (*http.Request, error) {

View File

@ -15,7 +15,7 @@ import (
) )
const ( 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 leakDetectDeadline = 5
// pause time (in milliseconds) between each snapshot at the end of the go routine leak detection. // pause time (in milliseconds) between each snapshot at the end of the go routine leak detection.
leakDetectPauseTimeMs = 50 leakDetectPauseTimeMs = 50

107
queues_test.go Normal file
View File

@ -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)
}
}
}

View File

@ -126,23 +126,11 @@ func TestNewJWT(t *testing.T) {
// Tests JWT.GenerateToken() // Tests JWT.GenerateToken()
func TestGenerateToken(t *testing.T) { func TestGenerateToken(t *testing.T) {
savedServerConfig := serverConfig testPath, err := newTestConfig("us-east-1")
defer func() {
serverConfig = savedServerConfig
}()
serverConfig = nil
// Setup.
testPath, err := ioutil.TempDir("", "minio-")
if err != nil { 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) t.Fatalf("unable initialize config file, %s", err)
} }
defer removeAll(testPath)
jwt, err := newJWT() jwt, err := newJWT()
if err != nil { if err != nil {
@ -185,23 +173,11 @@ func TestGenerateToken(t *testing.T) {
// Tests JWT.Authenticate() // Tests JWT.Authenticate()
func TestAuthenticate(t *testing.T) { func TestAuthenticate(t *testing.T) {
savedServerConfig := serverConfig testPath, err := newTestConfig("us-east-1")
defer func() {
serverConfig = savedServerConfig
}()
serverConfig = nil
// Setup.
testPath, err := ioutil.TempDir("", "minio-")
if err != nil { 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) t.Fatalf("unable initialize config file, %s", err)
} }
defer removeAll(testPath)
jwt, err := newJWT() jwt, err := newJWT()
if err != nil { if err != nil {

View File

@ -120,10 +120,14 @@ func StartTestServer(t TestErrHandler, instanceType string) TestServer {
t.Fatalf("Failed obtaining Temp Backend: <ERROR> %s", err) t.Fatalf("Failed obtaining Temp Backend: <ERROR> %s", err)
} }
credentials, root, err := initTestConfig("us-east-1") root, err := newTestConfig("us-east-1")
if err != nil { if err != nil {
t.Fatalf("%s", err) t.Fatalf("%s", err)
} }
// Get credential.
credentials := serverConfig.GetCredential()
testServer.Root = root testServer.Root = root
testServer.Disks = erasureDisks testServer.Disks = erasureDisks
testServer.AccessKey = credentials.AccessKeyID testServer.AccessKey = credentials.AccessKeyID
@ -135,25 +139,31 @@ func StartTestServer(t TestErrHandler, instanceType string) TestServer {
} }
// Configure the server for the test run. // Configure the server for the test run.
func initTestConfig(bucketLocation string) (credential, string, error) { func newTestConfig(bucketLocation string) (rootPath string, err error) {
// Initialize server config. // Get test root.
initConfig() rootPath, err = getTestRoot()
// Get credential.
credentials := serverConfig.GetCredential()
// Set a default region.
serverConfig.SetRegion(bucketLocation)
rootPath, err := getTestRoot()
if err != nil { if err != nil {
return credential{}, "", err return "", err
} }
// Do this only once here. // Do this only once here.
setGlobalConfigPath(rootPath) setGlobalConfigPath(rootPath)
err = serverConfig.Save() // Initialize server config.
if err != nil { if err = initConfig(); err != nil {
return credential{}, "", err 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. // Deleting the temporary backend and stopping the server.