2016-06-29 12:13:44 +02: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-19 01:23:42 +02:00
|
|
|
package cmd
|
2016-06-29 12:13:44 +02:00
|
|
|
|
|
|
|
import (
|
2016-09-13 20:01:10 +02:00
|
|
|
"bytes"
|
2016-06-29 12:13:44 +02: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-04 05:01:40 +02: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-18 21:49:24 +02:00
|
|
|
endpoints, err := parseStorageEndPoints([]string{disk}, 0)
|
2016-10-05 21:48:07 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Uexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2016-10-18 21:49:24 +02:00
|
|
|
fsStorageDisks, err := initStorageDisks(endpoints, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Uexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints, err = parseStorageEndPoints(disks, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Uexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
xlStorageDisks, err := initStorageDisks(endpoints, nil)
|
2016-10-05 21:48:07 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Uexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2016-07-04 05:01:40 +02:00
|
|
|
// Initializes all disks with XL
|
2016-10-05 21:48:07 +02:00
|
|
|
err = waitForFormatDisks(true, "", xlStorageDisks)
|
2016-08-31 04:22:27 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to format XL %s", err)
|
|
|
|
}
|
2016-10-05 21:48:07 +02:00
|
|
|
_, err = newXLObjects(xlStorageDisks)
|
2016-06-29 12:13:44 +02:00
|
|
|
if err != nil {
|
2016-07-04 05:01:40 +02:00
|
|
|
t.Fatalf("Unable to initialize XL object, %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2016-10-05 21:48:07 +02:00
|
|
|
disk StorageAPI
|
2016-07-04 05:01:40 +02:00
|
|
|
expectedErr error
|
|
|
|
}{
|
2016-10-05 21:48:07 +02:00
|
|
|
{fsStorageDisks[0], nil},
|
|
|
|
{xlStorageDisks[0], errFSDiskFormat},
|
2016-07-04 05:01:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
2016-10-05 21:48:07 +02:00
|
|
|
if err = waitForFormatDisks(true, "", []StorageAPI{testCase.disk}); err != testCase.expectedErr {
|
|
|
|
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-04 05:01:40 +02:00
|
|
|
}
|
2016-06-29 12:13:44 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-13 20:01:10 +02:00
|
|
|
|
|
|
|
// TestFSShutdown - initialize a new FS object layer then calls Shutdown
|
|
|
|
// to check returned results
|
|
|
|
func TestFSShutdown(t *testing.T) {
|
2016-09-16 22:06:49 +02:00
|
|
|
// Prepare for tests
|
2016-09-13 20:01:10 +02:00
|
|
|
disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
|
2016-09-16 22:06:49 +02:00
|
|
|
defer removeAll(disk)
|
2016-10-05 21:48:07 +02:00
|
|
|
obj := initFSObjects(disk, t)
|
2016-09-13 20:01:10 +02:00
|
|
|
|
|
|
|
fs := obj.(fsObjects)
|
|
|
|
fsStorage := fs.storage.(*posix)
|
|
|
|
|
|
|
|
bucketName := "testbucket"
|
|
|
|
objectName := "object"
|
|
|
|
objectContent := "12345"
|
|
|
|
|
|
|
|
obj.MakeBucket(bucketName)
|
2016-10-03 00:51:49 +02:00
|
|
|
sha256sum := ""
|
|
|
|
obj.PutObject(bucketName, objectName, int64(len(objectContent)), bytes.NewReader([]byte(objectContent)), nil, sha256sum)
|
2016-09-13 20:01:10 +02:00
|
|
|
|
2016-09-16 22:06:49 +02:00
|
|
|
// Test Shutdown with regular conditions
|
2016-09-13 20:01:10 +02:00
|
|
|
if err := fs.Shutdown(); err != nil {
|
|
|
|
t.Fatal("Cannot shutdown the FS object: ", err)
|
|
|
|
}
|
|
|
|
|
2016-09-16 22:06:49 +02:00
|
|
|
// Test Shutdown with faulty disks
|
2016-09-13 20:01:10 +02:00
|
|
|
for i := 1; i <= 5; i++ {
|
2016-09-16 22:06:49 +02:00
|
|
|
fs.storage = newNaughtyDisk(fsStorage, map[int]error{i: errFaultyDisk}, nil)
|
2016-09-14 04:00:01 +02:00
|
|
|
if err := fs.Shutdown(); errorCause(err) != errFaultyDisk {
|
2016-09-13 20:01:10 +02:00
|
|
|
t.Fatal(i, ", Got unexpected fs shutdown error: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 22:06:49 +02: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 21:48:07 +02:00
|
|
|
obj := initFSObjects(disk, t)
|
2016-09-16 22:06:49 +02:00
|
|
|
fs := obj.(fsObjects)
|
|
|
|
|
|
|
|
// Regular format loading
|
2016-10-05 21:48:07 +02:00
|
|
|
_, err := loadFormatFS(fs.storage)
|
2016-09-16 22:06:49 +02: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 21:48:07 +02:00
|
|
|
obj := initFSObjects(disk, t)
|
2016-09-16 22:06:49 +02: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 21:48:07 +02:00
|
|
|
obj := initFSObjects(disk, t)
|
2016-09-16 22:06:49 +02:00
|
|
|
fs := obj.(fsObjects)
|
|
|
|
bucketName := "bucket"
|
|
|
|
objectName := "object"
|
|
|
|
|
|
|
|
obj.MakeBucket(bucketName)
|
2016-10-03 00:51:49 +02:00
|
|
|
sha256sum := ""
|
|
|
|
obj.PutObject(bucketName, objectName, int64(len("abcd")), bytes.NewReader([]byte("abcd")), nil, sha256sum)
|
2016-09-16 22:06:49 +02: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-10-01 01:56:36 +02:00
|
|
|
if err := fs.DeleteObject(bucketName, "\\"); !isSameType(errorCause(err), ObjectNameInvalid{}) {
|
2016-09-16 22:06:49 +02: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 21:48:07 +02:00
|
|
|
obj := initFSObjects(disk, t)
|
2016-09-16 22:06:49 +02: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 21:48:07 +02:00
|
|
|
obj := initFSObjects(disk, t)
|
2016-09-16 22:06:49 +02: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 21:48:07 +02:00
|
|
|
obj := initFSObjects(disk, t)
|
|
|
|
err := obj.HealObject("bucket", "object")
|
2016-09-16 22:06:49 +02: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 21:48:07 +02:00
|
|
|
obj := initFSObjects(disk, t)
|
|
|
|
_, err := obj.ListObjectsHeal("bucket", "prefix", "marker", "delimiter", 1000)
|
2016-09-16 22:06:49 +02:00
|
|
|
if err == nil || !isSameType(errorCause(err), NotImplemented{}) {
|
|
|
|
t.Fatalf("Heal Object should return NotImplemented error ")
|
|
|
|
}
|
2016-09-13 20:01:10 +02:00
|
|
|
}
|