tests: Implemented more tests for fs-v1*.go (#2686)
parent
7d37dea449
commit
b89a1cd482
@ -0,0 +1,165 @@ |
||||
/* |
||||
* 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 cmd |
||||
|
||||
import ( |
||||
"os" |
||||
"path/filepath" |
||||
"testing" |
||||
"time" |
||||
) |
||||
|
||||
// TestFSIsBucketExist - complete test of isBucketExist
|
||||
func TestFSIsBucketExist(t *testing.T) { |
||||
// Prepare for testing
|
||||
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix()) |
||||
defer removeAll(disk) |
||||
obj, err := newFSObjects(disk) |
||||
if err != nil { |
||||
t.Fatal("Cannot create a new FS object: ", err) |
||||
} |
||||
|
||||
fs := obj.(fsObjects) |
||||
bucketName := "bucket" |
||||
|
||||
if err := obj.MakeBucket(bucketName); err != nil { |
||||
t.Fatal("Cannot create bucket, err: ", err) |
||||
} |
||||
|
||||
// Test with a valid bucket
|
||||
if found := fs.isBucketExist(bucketName); !found { |
||||
t.Fatal("isBucketExist should true") |
||||
} |
||||
|
||||
// Test with a inexistant bucket
|
||||
if found := fs.isBucketExist("foo"); found { |
||||
t.Fatal("isBucketExist should false") |
||||
} |
||||
|
||||
// Using a faulty disk
|
||||
fsStorage := fs.storage.(*posix) |
||||
naughty := newNaughtyDisk(fsStorage, nil, errFaultyDisk) |
||||
fs.storage = naughty |
||||
if found := fs.isBucketExist(bucketName); found { |
||||
t.Fatal("isBucketExist should return false because it is wired to a corrupted disk") |
||||
} |
||||
} |
||||
|
||||
// TestFSIsUploadExists - complete test with valid and invalid cases
|
||||
func TestFSIsUploadExists(t *testing.T) { |
||||
// Prepare for testing
|
||||
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix()) |
||||
defer removeAll(disk) |
||||
obj, err := newFSObjects(disk) |
||||
if err != nil { |
||||
t.Fatal("Cannot create a new FS object: ", err) |
||||
} |
||||
|
||||
fs := obj.(fsObjects) |
||||
|
||||
bucketName := "bucket" |
||||
objectName := "object" |
||||
|
||||
obj.MakeBucket(bucketName) |
||||
uploadID, err := obj.NewMultipartUpload(bucketName, objectName, nil) |
||||
|
||||
// Test with valid upload id
|
||||
if exists := fs.isUploadIDExists(bucketName, objectName, uploadID); !exists { |
||||
t.Fatal("Wrong result, expected: ", exists) |
||||
} |
||||
|
||||
// Test with inexistant bucket/object names
|
||||
if exists := fs.isUploadIDExists("bucketfoo", "objectfoo", uploadID); exists { |
||||
t.Fatal("Wrong result, expected: ", !exists) |
||||
} |
||||
|
||||
// Test with inexistant upload ID
|
||||
if exists := fs.isUploadIDExists(bucketName, objectName, uploadID+"-ff"); exists { |
||||
t.Fatal("Wrong result, expected: ", !exists) |
||||
} |
||||
|
||||
// isUploadIdExists with a faulty disk should return false
|
||||
fsStorage := fs.storage.(*posix) |
||||
naughty := newNaughtyDisk(fsStorage, nil, errFaultyDisk) |
||||
fs.storage = naughty |
||||
if exists := fs.isUploadIDExists(bucketName, objectName, uploadID); exists { |
||||
t.Fatal("Wrong result, expected: ", !exists) |
||||
} |
||||
} |
||||
|
||||
// TestFSWriteUploadJSON - tests for writeUploadJSON for FS
|
||||
func TestFSWriteUploadJSON(t *testing.T) { |
||||
// Prepare for tests
|
||||
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix()) |
||||
defer removeAll(disk) |
||||
obj, err := newFSObjects(disk) |
||||
if err != nil { |
||||
t.Fatal("Unexpected err: ", err) |
||||
} |
||||
fs := obj.(fsObjects) |
||||
|
||||
bucketName := "bucket" |
||||
objectName := "object" |
||||
|
||||
obj.MakeBucket(bucketName) |
||||
uploadID, err := obj.NewMultipartUpload(bucketName, objectName, nil) |
||||
|
||||
if err := fs.writeUploadJSON(bucketName, objectName, uploadID, time.Now()); err != nil { |
||||
t.Fatal("Unexpected err: ", err) |
||||
} |
||||
|
||||
// isUploadIdExists with a faulty disk should return false
|
||||
fsStorage := fs.storage.(*posix) |
||||
for i := 1; i <= 3; i++ { |
||||
naughty := newNaughtyDisk(fsStorage, map[int]error{i: errFaultyDisk}, nil) |
||||
fs.storage = naughty |
||||
if err := fs.writeUploadJSON(bucketName, objectName, uploadID, time.Now()); errorCause(err) != errFaultyDisk { |
||||
t.Fatal("Unexpected err: ", err) |
||||
} |
||||
} |
||||
} |
||||
|
||||
// TestFSUpdateUploadsJSON - tests for updateUploadsJSON for FS
|
||||
func TestFSUpdateUploadsJSON(t *testing.T) { |
||||
// Prepare for tests
|
||||
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix()) |
||||
defer removeAll(disk) |
||||
obj, err := newFSObjects(disk) |
||||
if err != nil { |
||||
t.Fatal("Unexpected err: ", err) |
||||
} |
||||
fs := obj.(fsObjects) |
||||
|
||||
bucketName := "bucket" |
||||
objectName := "object" |
||||
|
||||
obj.MakeBucket(bucketName) |
||||
|
||||
if err := fs.updateUploadsJSON(bucketName, objectName, uploadsV1{}); err != nil { |
||||
t.Fatal("Unexpected err: ", err) |
||||
} |
||||
|
||||
// isUploadIdExists with a faulty disk should return false
|
||||
fsStorage := fs.storage.(*posix) |
||||
for i := 1; i <= 2; i++ { |
||||
naughty := newNaughtyDisk(fsStorage, map[int]error{i: errFaultyDisk}, nil) |
||||
fs.storage = naughty |
||||
if err := fs.updateUploadsJSON(bucketName, objectName, uploadsV1{}); errorCause(err) != errFaultyDisk { |
||||
t.Fatal("Unexpected err: ", err) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,226 @@ |
||||
/* |
||||
* 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 cmd |
||||
|
||||
import ( |
||||
"bytes" |
||||
"crypto/md5" |
||||
"encoding/hex" |
||||
"os" |
||||
"path/filepath" |
||||
"reflect" |
||||
"testing" |
||||
) |
||||
|
||||
// TestNewMultipartUploadFaultyDisk - test NewMultipartUpload with faulty disks
|
||||
func TestNewMultipartUploadFaultyDisk(t *testing.T) { |
||||
// Prepare for tests
|
||||
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix()) |
||||
defer removeAll(disk) |
||||
obj, err := newFSObjects(disk) |
||||
if err != nil { |
||||
t.Fatal("Cannot create a new FS object: ", err) |
||||
} |
||||
|
||||
fs := obj.(fsObjects) |
||||
bucketName := "bucket" |
||||
objectName := "object" |
||||
|
||||
if err := obj.MakeBucket(bucketName); err != nil { |
||||
t.Fatal("Cannot create bucket, err: ", err) |
||||
} |
||||
|
||||
// Test with faulty disk
|
||||
fsStorage := fs.storage.(*posix) |
||||
for i := 1; i <= 5; i++ { |
||||
// Faulty disk generates errFaultyDisk at 'i' storage api call number
|
||||
fs.storage = newNaughtyDisk(fsStorage, map[int]error{i: errFaultyDisk}, nil) |
||||
if _, err := fs.NewMultipartUpload(bucketName, objectName, map[string]string{"X-Amz-Meta-xid": "3f"}); errorCause(err) != errFaultyDisk { |
||||
switch i { |
||||
case 1: |
||||
if !isSameType(errorCause(err), BucketNotFound{}) { |
||||
t.Fatal("Unexpected error ", err) |
||||
} |
||||
default: |
||||
t.Fatal("Unexpected error ", err) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
// TestPutObjectPartFaultyDisk - test PutObjectPart with faulty disks
|
||||
func TestPutObjectPartFaultyDisk(t *testing.T) { |
||||
// Prepare for tests
|
||||
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix()) |
||||
defer removeAll(disk) |
||||
obj, err := newFSObjects(disk) |
||||
if err != nil { |
||||
t.Fatal("Cannot create a new FS object: ", err) |
||||
} |
||||
|
||||
fs := obj.(fsObjects) |
||||
bucketName := "bucket" |
||||
objectName := "object" |
||||
data := []byte("12345") |
||||
dataLen := int64(len(data)) |
||||
|
||||
if err = obj.MakeBucket(bucketName); err != nil { |
||||
t.Fatal("Cannot create bucket, err: ", err) |
||||
} |
||||
|
||||
uploadID, err := fs.NewMultipartUpload(bucketName, objectName, map[string]string{"X-Amz-Meta-xid": "3f"}) |
||||
if err != nil { |
||||
t.Fatal("Unexpected error ", err) |
||||
} |
||||
|
||||
md5Writer := md5.New() |
||||
md5Writer.Write(data) |
||||
md5Hex := hex.EncodeToString(md5Writer.Sum(nil)) |
||||
|
||||
// Test with faulty disk
|
||||
fsStorage := fs.storage.(*posix) |
||||
for i := 1; i <= 7; i++ { |
||||
// Faulty disk generates errFaultyDisk at 'i' storage api call number
|
||||
fs.storage = newNaughtyDisk(fsStorage, map[int]error{i: errFaultyDisk}, nil) |
||||
if _, err := fs.PutObjectPart(bucketName, objectName, uploadID, 1, dataLen, bytes.NewReader(data), md5Hex); errorCause(err) != errFaultyDisk { |
||||
switch i { |
||||
case 1: |
||||
if !isSameType(errorCause(err), BucketNotFound{}) { |
||||
t.Fatal("Unexpected error ", err) |
||||
} |
||||
case 2, 4: |
||||
if !isSameType(errorCause(err), InvalidUploadID{}) { |
||||
t.Fatal("Unexpected error ", err) |
||||
} |
||||
default: |
||||
t.Fatal("Unexpected error ", i, err, reflect.TypeOf(errorCause(err)), reflect.TypeOf(errFaultyDisk)) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
// TestCompleteMultipartUploadFaultyDisk - test CompleteMultipartUpload with faulty disks
|
||||
func TestCompleteMultipartUploadFaultyDisk(t *testing.T) { |
||||
// Prepare for tests
|
||||
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix()) |
||||
defer removeAll(disk) |
||||
obj, err := newFSObjects(disk) |
||||
if err != nil { |
||||
t.Fatal("Cannot create a new FS object: ", err) |
||||
} |
||||
|
||||
fs := obj.(fsObjects) |
||||
bucketName := "bucket" |
||||
objectName := "object" |
||||
data := []byte("12345") |
||||
|
||||
if err = obj.MakeBucket(bucketName); err != nil { |
||||
t.Fatal("Cannot create bucket, err: ", err) |
||||
} |
||||
|
||||
uploadID, err := fs.NewMultipartUpload(bucketName, objectName, map[string]string{"X-Amz-Meta-xid": "3f"}) |
||||
if err != nil { |
||||
t.Fatal("Unexpected error ", err) |
||||
} |
||||
|
||||
md5Writer := md5.New() |
||||
md5Writer.Write(data) |
||||
md5Hex := hex.EncodeToString(md5Writer.Sum(nil)) |
||||
|
||||
if _, err := fs.PutObjectPart(bucketName, objectName, uploadID, 1, 5, bytes.NewReader(data), md5Hex); err != nil { |
||||
t.Fatal("Unexpected error ", err) |
||||
} |
||||
|
||||
parts := []completePart{{PartNumber: 1, ETag: md5Hex}} |
||||
|
||||
fsStorage := fs.storage.(*posix) |
||||
for i := 1; i <= 3; i++ { |
||||
// Faulty disk generates errFaultyDisk at 'i' storage api call number
|
||||
fs.storage = newNaughtyDisk(fsStorage, map[int]error{i: errFaultyDisk}, nil) |
||||
if _, err := fs.CompleteMultipartUpload(bucketName, objectName, uploadID, parts); errorCause(err) != errFaultyDisk { |
||||
switch i { |
||||
case 1: |
||||
if !isSameType(errorCause(err), BucketNotFound{}) { |
||||
t.Fatal("Unexpected error ", err) |
||||
} |
||||
case 2: |
||||
if !isSameType(errorCause(err), InvalidUploadID{}) { |
||||
t.Fatal("Unexpected error ", err) |
||||
} |
||||
default: |
||||
t.Fatal("Unexpected error ", i, err, reflect.TypeOf(errorCause(err)), reflect.TypeOf(errFaultyDisk)) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
// TestListMultipartUploadsFaultyDisk - test ListMultipartUploads with faulty disks
|
||||
func TestListMultipartUploadsFaultyDisk(t *testing.T) { |
||||
// Prepare for tests
|
||||
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix()) |
||||
defer removeAll(disk) |
||||
obj, err := newFSObjects(disk) |
||||
if err != nil { |
||||
t.Fatal("Cannot create a new FS object: ", err) |
||||
} |
||||
|
||||
fs := obj.(fsObjects) |
||||
bucketName := "bucket" |
||||
objectName := "object" |
||||
data := []byte("12345") |
||||
|
||||
if err = obj.MakeBucket(bucketName); err != nil { |
||||
t.Fatal("Cannot create bucket, err: ", err) |
||||
} |
||||
|
||||
uploadID, err := fs.NewMultipartUpload(bucketName, objectName, map[string]string{"X-Amz-Meta-xid": "3f"}) |
||||
if err != nil { |
||||
t.Fatal("Unexpected error ", err) |
||||
} |
||||
|
||||
md5Writer := md5.New() |
||||
md5Writer.Write(data) |
||||
md5Hex := hex.EncodeToString(md5Writer.Sum(nil)) |
||||
|
||||
if _, err := fs.PutObjectPart(bucketName, objectName, uploadID, 1, 5, bytes.NewReader(data), md5Hex); err != nil { |
||||
t.Fatal("Unexpected error ", err) |
||||
} |
||||
|
||||
fsStorage := fs.storage.(*posix) |
||||
for i := 1; i <= 4; i++ { |
||||
// Faulty disk generates errFaultyDisk at 'i' storage api call number
|
||||
fs.storage = newNaughtyDisk(fsStorage, map[int]error{i: errFaultyDisk}, nil) |
||||
if _, err := fs.ListMultipartUploads(bucketName, objectName, "", "", "", 1000); errorCause(err) != errFaultyDisk { |
||||
switch i { |
||||
case 1: |
||||
if !isSameType(errorCause(err), BucketNotFound{}) { |
||||
t.Fatal("Unexpected error ", err) |
||||
} |
||||
case 2: |
||||
if !isSameType(errorCause(err), InvalidUploadID{}) { |
||||
t.Fatal("Unexpected error ", err) |
||||
} |
||||
case 3: |
||||
if errorCause(err) != errFileNotFound { |
||||
t.Fatal("Unexpected error ", err) |
||||
} |
||||
default: |
||||
t.Fatal("Unexpected error ", i, err, reflect.TypeOf(errorCause(err)), reflect.TypeOf(errFaultyDisk)) |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue