2016-06-29 06:13:44 -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.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-06-29 06:13:44 -04:00
|
|
|
|
|
|
|
import (
|
2016-09-13 14:01:10 -04:00
|
|
|
"bytes"
|
2016-06-29 06:13:44 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestNewFS - tests initialization of all input disks
|
|
|
|
// and constructs a valid `FS` object layer.
|
|
|
|
func TestNewFS(t *testing.T) {
|
|
|
|
// Do not attempt to create this path, the test validates
|
|
|
|
// so that newFSObjects initializes non existing paths
|
|
|
|
// and successfully returns initialized object layer.
|
|
|
|
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
|
|
|
|
defer removeAll(disk)
|
|
|
|
|
2016-07-03 23:01:40 -04:00
|
|
|
// Setup to test errFSDiskFormat.
|
|
|
|
disks := []string{}
|
|
|
|
for i := 0; i < 6; i++ {
|
|
|
|
xlDisk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
|
|
|
|
defer removeAll(xlDisk)
|
|
|
|
disks = append(disks, xlDisk)
|
|
|
|
}
|
|
|
|
|
2016-10-27 06:30:52 -04:00
|
|
|
endpoints, err := parseStorageEndpoints([]string{disk})
|
2016-10-05 15:48:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Uexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:36:07 -05:00
|
|
|
fsStorageDisks, err := initStorageDisks(endpoints)
|
2016-10-18 15:49:24 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Uexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2016-10-27 06:30:52 -04:00
|
|
|
endpoints, err = parseStorageEndpoints(disks)
|
2016-10-18 15:49:24 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Uexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:36:07 -05:00
|
|
|
xlStorageDisks, err := initStorageDisks(endpoints)
|
2016-10-05 15:48:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Uexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2016-07-03 23:01:40 -04:00
|
|
|
// Initializes all disks with XL
|
2016-11-02 11:51:06 -04:00
|
|
|
err = waitForFormatDisks(true, endpoints, xlStorageDisks)
|
2016-08-30 22:22:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to format XL %s", err)
|
|
|
|
}
|
2016-10-05 15:48:07 -04:00
|
|
|
_, err = newXLObjects(xlStorageDisks)
|
2016-06-29 06:13:44 -04:00
|
|
|
if err != nil {
|
2016-07-03 23:01:40 -04:00
|
|
|
t.Fatalf("Unable to initialize XL object, %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2016-10-05 15:48:07 -04:00
|
|
|
disk StorageAPI
|
2016-07-03 23:01:40 -04:00
|
|
|
expectedErr error
|
|
|
|
}{
|
2016-10-05 15:48:07 -04:00
|
|
|
{fsStorageDisks[0], nil},
|
|
|
|
{xlStorageDisks[0], errFSDiskFormat},
|
2016-07-03 23:01:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
2016-11-02 11:51:06 -04:00
|
|
|
if err = waitForFormatDisks(true, endpoints, []StorageAPI{testCase.disk}); err != testCase.expectedErr {
|
2016-10-05 15:48:07 -04:00
|
|
|
t.Errorf("expected: %s, got :%s", testCase.expectedErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = newFSObjects(nil)
|
|
|
|
if err != errInvalidArgument {
|
|
|
|
t.Errorf("Expecting error invalid argument, got %s", err)
|
|
|
|
}
|
|
|
|
_, err = newFSObjects(xlStorageDisks[0])
|
|
|
|
if err != nil {
|
|
|
|
errMsg := "Unable to recognize backend format, Disk is not in FS format."
|
|
|
|
if err.Error() == errMsg {
|
|
|
|
t.Errorf("Expecting %s, got %s", errMsg, err)
|
2016-07-03 23:01:40 -04:00
|
|
|
}
|
2016-06-29 06:13:44 -04:00
|
|
|
}
|
|
|
|
}
|
2016-09-13 14:01:10 -04:00
|
|
|
|
2016-11-11 19:36:07 -05:00
|
|
|
// TestFSShutdown - initialize a new FS object layer then calls
|
|
|
|
// Shutdown to check returned results
|
2016-09-13 14:01:10 -04:00
|
|
|
func TestFSShutdown(t *testing.T) {
|
2016-11-11 19:36:07 -05:00
|
|
|
rootPath, err := newTestConfig("us-east-1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer removeAll(rootPath)
|
2016-09-13 14:01:10 -04:00
|
|
|
|
2016-11-11 19:36:07 -05:00
|
|
|
bucketName := "testbucket"
|
|
|
|
objectName := "object"
|
2016-11-09 13:10:14 -05:00
|
|
|
// Create and return an fsObject with its path in the disk
|
|
|
|
prepareTest := func() (fsObjects, string) {
|
|
|
|
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
|
|
|
|
obj := initFSObjects(disk, t)
|
|
|
|
fs := obj.(fsObjects)
|
|
|
|
objectContent := "12345"
|
|
|
|
obj.MakeBucket(bucketName)
|
|
|
|
sha256sum := ""
|
|
|
|
obj.PutObject(bucketName, objectName, int64(len(objectContent)), bytes.NewReader([]byte(objectContent)), nil, sha256sum)
|
|
|
|
return fs, disk
|
|
|
|
}
|
2016-09-13 14:01:10 -04:00
|
|
|
|
2016-09-16 16:06:49 -04:00
|
|
|
// Test Shutdown with regular conditions
|
2016-11-09 13:10:14 -05:00
|
|
|
fs, disk := prepareTest()
|
2016-09-13 14:01:10 -04:00
|
|
|
if err := fs.Shutdown(); err != nil {
|
|
|
|
t.Fatal("Cannot shutdown the FS object: ", err)
|
|
|
|
}
|
2016-11-09 13:10:14 -05:00
|
|
|
removeAll(disk)
|
2016-09-13 14:01:10 -04:00
|
|
|
|
2016-11-09 13:10:14 -05:00
|
|
|
// Test Shutdown with faulty disk
|
2016-09-13 14:01:10 -04:00
|
|
|
for i := 1; i <= 5; i++ {
|
2016-11-09 13:10:14 -05:00
|
|
|
fs, disk := prepareTest()
|
2016-11-11 19:36:07 -05:00
|
|
|
fs.DeleteObject(bucketName, objectName)
|
2016-11-09 13:10:14 -05:00
|
|
|
fsStorage := fs.storage.(*posix)
|
2016-09-16 16:06:49 -04:00
|
|
|
fs.storage = newNaughtyDisk(fsStorage, map[int]error{i: errFaultyDisk}, nil)
|
2016-09-13 22:00:01 -04:00
|
|
|
if err := fs.Shutdown(); errorCause(err) != errFaultyDisk {
|
2016-09-13 14:01:10 -04:00
|
|
|
t.Fatal(i, ", Got unexpected fs shutdown error: ", err)
|
|
|
|
}
|
2016-11-09 13:10:14 -05:00
|
|
|
removeAll(disk)
|
2016-09-13 14:01:10 -04:00
|
|
|
}
|
2016-09-16 16:06:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestFSLoadFormatFS - test loadFormatFS with healty and faulty disks
|
|
|
|
func TestFSLoadFormatFS(t *testing.T) {
|
|
|
|
// Prepare for testing
|
|
|
|
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
|
|
|
|
defer removeAll(disk)
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
obj := initFSObjects(disk, t)
|
2016-09-16 16:06:49 -04:00
|
|
|
fs := obj.(fsObjects)
|
|
|
|
|
|
|
|
// Regular format loading
|
2016-10-05 15:48:07 -04:00
|
|
|
_, err := loadFormatFS(fs.storage)
|
2016-09-16 16:06:49 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Should not fail here", err)
|
|
|
|
}
|
|
|
|
// Loading corrupted format file
|
|
|
|
fs.storage.AppendFile(minioMetaBucket, fsFormatJSONFile, []byte{'b'})
|
|
|
|
_, err = loadFormatFS(fs.storage)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Should return an error here")
|
|
|
|
}
|
|
|
|
// Loading format file from faulty disk
|
|
|
|
fsStorage := fs.storage.(*posix)
|
|
|
|
fs.storage = newNaughtyDisk(fsStorage, nil, errFaultyDisk)
|
|
|
|
_, err = loadFormatFS(fs.storage)
|
|
|
|
if err != errFaultyDisk {
|
|
|
|
t.Fatal("Should return faulty disk error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFSGetBucketInfo - test GetBucketInfo with healty and faulty disks
|
|
|
|
func TestFSGetBucketInfo(t *testing.T) {
|
|
|
|
// Prepare for testing
|
|
|
|
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
|
|
|
|
defer removeAll(disk)
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
obj := initFSObjects(disk, t)
|
2016-09-16 16:06:49 -04:00
|
|
|
fs := obj.(fsObjects)
|
|
|
|
bucketName := "bucket"
|
|
|
|
|
|
|
|
obj.MakeBucket(bucketName)
|
|
|
|
|
|
|
|
// Test with valid parameters
|
|
|
|
info, err := fs.GetBucketInfo(bucketName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if info.Name != bucketName {
|
|
|
|
t.Fatalf("wrong bucket name, expected: %s, found: %s", bucketName, info.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with inexistant bucket
|
|
|
|
_, err = fs.GetBucketInfo("a")
|
|
|
|
if !isSameType(errorCause(err), BucketNameInvalid{}) {
|
|
|
|
t.Fatal("BucketNameInvalid error not returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loading format file from faulty disk
|
|
|
|
fsStorage := fs.storage.(*posix)
|
|
|
|
fs.storage = newNaughtyDisk(fsStorage, nil, errFaultyDisk)
|
|
|
|
_, err = fs.GetBucketInfo(bucketName)
|
|
|
|
if errorCause(err) != errFaultyDisk {
|
|
|
|
t.Fatal("errFaultyDisk error not returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFSDeleteObject - test fs.DeleteObject() with healthy and corrupted disks
|
|
|
|
func TestFSDeleteObject(t *testing.T) {
|
|
|
|
// Prepare for tests
|
|
|
|
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
|
|
|
|
defer removeAll(disk)
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
obj := initFSObjects(disk, t)
|
2016-09-16 16:06:49 -04:00
|
|
|
fs := obj.(fsObjects)
|
|
|
|
bucketName := "bucket"
|
|
|
|
objectName := "object"
|
|
|
|
|
|
|
|
obj.MakeBucket(bucketName)
|
2016-10-02 18:51:49 -04:00
|
|
|
sha256sum := ""
|
|
|
|
obj.PutObject(bucketName, objectName, int64(len("abcd")), bytes.NewReader([]byte("abcd")), nil, sha256sum)
|
2016-09-16 16:06:49 -04:00
|
|
|
|
|
|
|
// Test with invalid bucket name
|
|
|
|
if err := fs.DeleteObject("fo", objectName); !isSameType(errorCause(err), BucketNameInvalid{}) {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
// Test with invalid object name
|
2016-09-30 19:56:36 -04:00
|
|
|
if err := fs.DeleteObject(bucketName, "\\"); !isSameType(errorCause(err), ObjectNameInvalid{}) {
|
2016-09-16 16:06:49 -04:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
// Test with inexist bucket/object
|
|
|
|
if err := fs.DeleteObject("foobucket", "fooobject"); !isSameType(errorCause(err), BucketNotFound{}) {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
// Test with valid condition
|
|
|
|
if err := fs.DeleteObject(bucketName, objectName); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loading format file from faulty disk
|
|
|
|
fsStorage := fs.storage.(*posix)
|
|
|
|
fs.storage = newNaughtyDisk(fsStorage, nil, errFaultyDisk)
|
|
|
|
if err := fs.DeleteObject(bucketName, objectName); errorCause(err) != errFaultyDisk {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFSDeleteBucket - tests for fs DeleteBucket
|
|
|
|
func TestFSDeleteBucket(t *testing.T) {
|
|
|
|
// Prepare for testing
|
|
|
|
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
|
|
|
|
defer removeAll(disk)
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
obj := initFSObjects(disk, t)
|
2016-09-16 16:06:49 -04:00
|
|
|
fs := obj.(fsObjects)
|
|
|
|
bucketName := "bucket"
|
|
|
|
|
|
|
|
err := obj.MakeBucket(bucketName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with an invalid bucket name
|
|
|
|
if err := fs.DeleteBucket("fo"); !isSameType(errorCause(err), BucketNameInvalid{}) {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
// Test with an inexistant bucket
|
|
|
|
if err := fs.DeleteBucket("foobucket"); !isSameType(errorCause(err), BucketNotFound{}) {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
// Test with a valid case
|
|
|
|
if err := fs.DeleteBucket(bucketName); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.MakeBucket(bucketName)
|
|
|
|
|
|
|
|
// Loading format file from faulty disk
|
|
|
|
fsStorage := fs.storage.(*posix)
|
|
|
|
for i := 1; i <= 2; i++ {
|
|
|
|
fs.storage = newNaughtyDisk(fsStorage, map[int]error{i: errFaultyDisk}, nil)
|
|
|
|
if err := fs.DeleteBucket(bucketName); errorCause(err) != errFaultyDisk {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFSListBuckets - tests for fs ListBuckets
|
|
|
|
func TestFSListBuckets(t *testing.T) {
|
|
|
|
// Prepare for tests
|
|
|
|
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
|
|
|
|
defer removeAll(disk)
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
obj := initFSObjects(disk, t)
|
2016-09-16 16:06:49 -04:00
|
|
|
fs := obj.(fsObjects)
|
|
|
|
|
|
|
|
bucketName := "bucket"
|
|
|
|
if err := obj.MakeBucket(bucketName); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a bucket with invalid name
|
|
|
|
if err := fs.storage.MakeVol("vo^"); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
buckets, err := fs.ListBuckets()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
if len(buckets) != 1 {
|
|
|
|
t.Fatal("ListBuckets not working properly")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test ListBuckets with faulty disks
|
|
|
|
fsStorage := fs.storage.(*posix)
|
|
|
|
for i := 1; i <= 2; i++ {
|
|
|
|
fs.storage = newNaughtyDisk(fsStorage, nil, errFaultyDisk)
|
|
|
|
if _, err := fs.ListBuckets(); errorCause(err) != errFaultyDisk {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFSHealObject - tests for fs HealObject
|
|
|
|
func TestFSHealObject(t *testing.T) {
|
|
|
|
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
|
|
|
|
defer removeAll(disk)
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
obj := initFSObjects(disk, t)
|
|
|
|
err := obj.HealObject("bucket", "object")
|
2016-09-16 16:06:49 -04:00
|
|
|
if err == nil || !isSameType(errorCause(err), NotImplemented{}) {
|
|
|
|
t.Fatalf("Heal Object should return NotImplemented error ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFSListObjectHeal - tests for fs ListObjectHeals
|
|
|
|
func TestFSListObjectsHeal(t *testing.T) {
|
|
|
|
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
|
|
|
|
defer removeAll(disk)
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
obj := initFSObjects(disk, t)
|
|
|
|
_, err := obj.ListObjectsHeal("bucket", "prefix", "marker", "delimiter", 1000)
|
2016-09-16 16:06:49 -04:00
|
|
|
if err == nil || !isSameType(errorCause(err), NotImplemented{}) {
|
|
|
|
t.Fatalf("Heal Object should return NotImplemented error ")
|
|
|
|
}
|
2016-09-13 14:01:10 -04:00
|
|
|
}
|