2016-06-29 06:13:44 -04:00
|
|
|
/*
|
2017-01-18 15:24:34 -05:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
2016-06-29 06:13:44 -04:00
|
|
|
*
|
|
|
|
* 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"
|
2017-01-16 20:05:00 -05:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-06-29 06:13:44 -04:00
|
|
|
"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
|
2017-01-16 20:05:00 -05:00
|
|
|
// so that newFSObjectLayer initializes non existing paths
|
2016-06-29 06:13:44 -04:00
|
|
|
// and successfully returns initialized object layer.
|
2016-12-16 01:25:05 -05:00
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
2016-06-29 06:13:44 -04:00
|
|
|
defer removeAll(disk)
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
_, err := newFSObjectLayer("")
|
2016-10-05 15:48:07 -04:00
|
|
|
if err != errInvalidArgument {
|
|
|
|
t.Errorf("Expecting error invalid argument, got %s", err)
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
_, err = newFSObjectLayer(disk)
|
2016-10-05 15:48:07 -04:00
|
|
|
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) {
|
2017-01-18 15:24:34 -05:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-11-11 19:36:07 -05:00
|
|
|
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
|
2017-01-16 20:05:00 -05:00
|
|
|
prepareTest := func() (*fsObjects, string) {
|
2016-12-16 01:25:05 -05:00
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
2016-11-09 13:10:14 -05:00
|
|
|
obj := initFSObjects(disk, t)
|
2017-01-16 20:05:00 -05:00
|
|
|
fs := obj.(*fsObjects)
|
2016-11-09 13:10:14 -05:00
|
|
|
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
|
2017-02-07 15:51:23 -05:00
|
|
|
fs, disk = prepareTest()
|
|
|
|
fs.DeleteObject(bucketName, objectName)
|
|
|
|
removeAll(disk)
|
|
|
|
if err := fs.Shutdown(); err != nil {
|
|
|
|
t.Fatal("Got unexpected fs shutdown error: ", err)
|
2016-12-01 16:59:06 -05: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
|
2016-12-16 01:25:05 -05:00
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
2016-09-16 16:06:49 -04:00
|
|
|
defer removeAll(disk)
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
// Assign a new UUID.
|
|
|
|
uuid := mustGetUUID()
|
|
|
|
|
|
|
|
// Initialize meta volume, if volume already exists ignores it.
|
|
|
|
if err := initMetaVolumeFS(disk, uuid); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-16 16:06:49 -04:00
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
fsFormatPath := pathJoin(disk, minioMetaBucket, fsFormatJSONFile)
|
|
|
|
if err := saveFormatFS(preparePath(fsFormatPath), newFSFormatV1()); err != nil {
|
|
|
|
t.Fatal("Should not fail here", err)
|
|
|
|
}
|
|
|
|
_, err := loadFormatFS(disk)
|
2016-09-16 16:06:49 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Should not fail here", err)
|
|
|
|
}
|
|
|
|
// Loading corrupted format file
|
2017-01-16 20:05:00 -05:00
|
|
|
file, err := os.OpenFile(preparePath(fsFormatPath), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Should not fail here", err)
|
|
|
|
}
|
|
|
|
file.Write([]byte{'b'})
|
|
|
|
file.Close()
|
|
|
|
_, err = loadFormatFS(disk)
|
2016-09-16 16:06:49 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Should return an error here")
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
// Loading format file from disk not found.
|
|
|
|
removeAll(disk)
|
|
|
|
_, err = loadFormatFS(disk)
|
|
|
|
if err != nil && err != errUnformattedDisk {
|
|
|
|
t.Fatal("Should return unformatted disk, but got", err)
|
2016-09-16 16:06:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFSGetBucketInfo - test GetBucketInfo with healty and faulty disks
|
|
|
|
func TestFSGetBucketInfo(t *testing.T) {
|
|
|
|
// Prepare for testing
|
2016-12-16 01:25:05 -05:00
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
2016-09-16 16:06:49 -04:00
|
|
|
defer removeAll(disk)
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
obj := initFSObjects(disk, t)
|
2017-01-16 20:05:00 -05:00
|
|
|
fs := obj.(*fsObjects)
|
2016-09-16 16:06:49 -04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
// Check for buckets and should get disk not found.
|
|
|
|
removeAll(disk)
|
2016-09-16 16:06:49 -04:00
|
|
|
_, err = fs.GetBucketInfo(bucketName)
|
2017-01-16 20:05:00 -05:00
|
|
|
if !isSameType(errorCause(err), BucketNotFound{}) {
|
|
|
|
t.Fatal("BucketNotFound error not returned")
|
2016-09-16 16:06:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFSDeleteObject - test fs.DeleteObject() with healthy and corrupted disks
|
|
|
|
func TestFSDeleteObject(t *testing.T) {
|
|
|
|
// Prepare for tests
|
2016-12-16 01:25:05 -05:00
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
2016-09-16 16:06:49 -04:00
|
|
|
defer removeAll(disk)
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
obj := initFSObjects(disk, t)
|
2017-01-16 20:05:00 -05:00
|
|
|
fs := obj.(*fsObjects)
|
2016-09-16 16:06:49 -04:00
|
|
|
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)
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
// Test with bucket does not exist
|
|
|
|
if err := fs.DeleteObject("foobucket", "fooobject"); !isSameType(errorCause(err), BucketNotFound{}) {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
2016-09-16 16:06:49 -04:00
|
|
|
// 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)
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
// Test with object does not exist.
|
|
|
|
if err := fs.DeleteObject(bucketName, "foooobject"); !isSameType(errorCause(err), ObjectNotFound{}) {
|
2016-09-16 16:06:49 -04:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
// Test with valid condition
|
|
|
|
if err := fs.DeleteObject(bucketName, objectName); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
// Delete object should err disk not found.
|
|
|
|
removeAll(disk)
|
|
|
|
if err := fs.DeleteObject(bucketName, objectName); err != nil {
|
|
|
|
if !isSameType(errorCause(err), BucketNotFound{}) {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
2016-09-16 16:06:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFSDeleteBucket - tests for fs DeleteBucket
|
|
|
|
func TestFSDeleteBucket(t *testing.T) {
|
|
|
|
// Prepare for testing
|
2016-12-16 01:25:05 -05:00
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
2016-09-16 16:06:49 -04:00
|
|
|
defer removeAll(disk)
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
obj := initFSObjects(disk, t)
|
2017-01-16 20:05:00 -05:00
|
|
|
fs := obj.(*fsObjects)
|
2016-09-16 16:06:49 -04:00
|
|
|
bucketName := "bucket"
|
|
|
|
|
|
|
|
err := obj.MakeBucket(bucketName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with an invalid bucket name
|
2017-01-16 20:05:00 -05:00
|
|
|
if err = fs.DeleteBucket("fo"); !isSameType(errorCause(err), BucketNameInvalid{}) {
|
2016-09-16 16:06:49 -04:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
// Test with an inexistant bucket
|
2017-01-16 20:05:00 -05:00
|
|
|
if err = fs.DeleteBucket("foobucket"); !isSameType(errorCause(err), BucketNotFound{}) {
|
2016-09-16 16:06:49 -04:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
// Test with a valid case
|
2017-01-16 20:05:00 -05:00
|
|
|
if err = fs.DeleteBucket(bucketName); err != nil {
|
2016-09-16 16:06:49 -04:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.MakeBucket(bucketName)
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
// Delete bucker should get error disk not found.
|
|
|
|
removeAll(disk)
|
|
|
|
if err = fs.DeleteBucket(bucketName); err != nil {
|
|
|
|
if !isSameType(errorCause(err), BucketNotFound{}) {
|
2016-09-16 16:06:49 -04:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFSListBuckets - tests for fs ListBuckets
|
|
|
|
func TestFSListBuckets(t *testing.T) {
|
|
|
|
// Prepare for tests
|
2016-12-16 01:25:05 -05:00
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
2016-09-16 16:06:49 -04:00
|
|
|
defer removeAll(disk)
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
obj := initFSObjects(disk, t)
|
2017-01-16 20:05:00 -05:00
|
|
|
fs := obj.(*fsObjects)
|
2016-09-16 16:06:49 -04:00
|
|
|
|
|
|
|
bucketName := "bucket"
|
|
|
|
if err := obj.MakeBucket(bucketName); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a bucket with invalid name
|
2017-01-16 20:05:00 -05:00
|
|
|
if err := mkdirAll(pathJoin(fs.fsPath, "vo^"), 0777); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
f, err := os.Create(pathJoin(fs.fsPath, "test"))
|
|
|
|
if err != nil {
|
2016-09-16 16:06:49 -04:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
f.Close()
|
2016-09-16 16:06:49 -04:00
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
// Test list buckets to have only one entry.
|
2016-09-16 16:06:49 -04:00
|
|
|
buckets, err := fs.ListBuckets()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
if len(buckets) != 1 {
|
2017-01-16 20:05:00 -05:00
|
|
|
t.Fatal("ListBuckets not working properly", buckets)
|
2016-09-16 16:06:49 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
// Test ListBuckets with disk not found.
|
|
|
|
removeAll(disk)
|
|
|
|
|
|
|
|
if _, err := fs.ListBuckets(); err != nil {
|
|
|
|
if errorCause(err) != errDiskNotFound {
|
2016-09-16 16:06:49 -04:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
longPath := fmt.Sprintf("%0256d", 1)
|
|
|
|
fs.fsPath = longPath
|
|
|
|
if _, err := fs.ListBuckets(); err != nil {
|
|
|
|
if errorCause(err) != errFileNameTooLong {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
}
|
2016-09-16 16:06:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestFSHealObject - tests for fs HealObject
|
|
|
|
func TestFSHealObject(t *testing.T) {
|
2016-12-16 01:25:05 -05:00
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
2016-09-16 16:06:49 -04:00
|
|
|
defer removeAll(disk)
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
obj := initFSObjects(disk, t)
|
2017-03-31 20:55:15 -04:00
|
|
|
_, _, 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) {
|
2016-12-16 01:25:05 -05:00
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
2016-09-16 16:06:49 -04:00
|
|
|
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
|
|
|
}
|