2016-06-21 18:48:27 -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-21 18:48:27 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-07-21 16:15:54 -04:00
|
|
|
"io/ioutil"
|
2016-10-08 20:08:17 -04:00
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"reflect"
|
2016-06-21 18:48:27 -04:00
|
|
|
"testing"
|
2016-10-08 20:08:17 -04:00
|
|
|
"time"
|
2016-11-22 21:18:22 -05:00
|
|
|
|
|
|
|
humanize "github.com/dustin/go-humanize"
|
2017-11-25 14:58:29 -05:00
|
|
|
"github.com/minio/minio/pkg/errors"
|
2016-06-21 18:48:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRepeatPutObjectPart(t *testing.T) {
|
|
|
|
var objLayer ObjectLayer
|
|
|
|
var disks []string
|
|
|
|
var err error
|
|
|
|
|
2016-08-30 22:22:27 -04:00
|
|
|
objLayer, disks, err = prepareXL()
|
2016-06-21 18:48:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleaning up of temporary test directories
|
|
|
|
defer removeRoots(disks)
|
|
|
|
|
2017-06-01 12:43:20 -04:00
|
|
|
err = objLayer.MakeBucketWithLocation("bucket1", "")
|
2016-06-21 18:48:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadID, err := objLayer.NewMultipartUpload("bucket1", "mpartObj1", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-11-22 21:18:22 -05:00
|
|
|
fiveMBBytes := bytes.Repeat([]byte("a"), 5*humanize.MiByte)
|
2016-11-21 16:51:05 -05:00
|
|
|
md5Hex := getMD5Hash(fiveMBBytes)
|
2017-10-22 01:30:34 -04:00
|
|
|
_, err = objLayer.PutObjectPart("bucket1", "mpartObj1", uploadID, 1, mustGetHashReader(t, bytes.NewReader(fiveMBBytes), 5*humanize.MiByte, md5Hex, ""))
|
2016-06-21 18:48:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// PutObjectPart should succeed even if part already exists. ref: https://github.com/minio/minio/issues/1930
|
2017-10-22 01:30:34 -04:00
|
|
|
_, err = objLayer.PutObjectPart("bucket1", "mpartObj1", uploadID, 1, mustGetHashReader(t, bytes.NewReader(fiveMBBytes), 5*humanize.MiByte, md5Hex, ""))
|
2016-06-21 18:48:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 16:15:54 -04:00
|
|
|
|
|
|
|
func TestXLDeleteObjectBasic(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
bucket string
|
|
|
|
object string
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{".test", "obj", BucketNameInvalid{Bucket: ".test"}},
|
|
|
|
{"----", "obj", BucketNameInvalid{Bucket: "----"}},
|
|
|
|
{"bucket", "", ObjectNameInvalid{Bucket: "bucket", Object: ""}},
|
|
|
|
{"bucket", "doesnotexist", ObjectNotFound{Bucket: "bucket", Object: "doesnotexist"}},
|
2017-05-09 17:32:24 -04:00
|
|
|
{"bucket", "obj/", ObjectNotFound{Bucket: "bucket", Object: "obj/"}},
|
|
|
|
{"bucket", "/obj", ObjectNotFound{Bucket: "bucket", Object: "/obj"}},
|
2016-07-21 16:15:54 -04:00
|
|
|
{"bucket", "obj", nil},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an instance of xl backend
|
2016-08-30 22:22:27 -04:00
|
|
|
xl, fsDirs, err := prepareXL()
|
2016-07-21 16:15:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make bucket for Test 7 to pass
|
2017-06-01 12:43:20 -04:00
|
|
|
err = xl.MakeBucketWithLocation("bucket", "")
|
2016-07-21 16:15:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create object "obj" under bucket "bucket" for Test 7 to pass
|
2017-10-22 01:30:34 -04:00
|
|
|
_, err = xl.PutObject("bucket", "obj", mustGetHashReader(t, bytes.NewReader([]byte("abcd")), int64(len("abcd")), "", ""), nil)
|
2016-08-12 03:26:30 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("XL Object upload failed: <ERROR> %s", err)
|
|
|
|
}
|
2016-07-21 16:15:54 -04:00
|
|
|
for i, test := range testCases {
|
|
|
|
actualErr := xl.DeleteObject(test.bucket, test.object)
|
2017-11-25 14:58:29 -05:00
|
|
|
actualErr = errors.Cause(actualErr)
|
2016-07-21 16:15:54 -04:00
|
|
|
if test.expectedErr != nil && actualErr != test.expectedErr {
|
|
|
|
t.Errorf("Test %d: Expected to fail with %s, but failed with %s", i+1, test.expectedErr, actualErr)
|
|
|
|
}
|
|
|
|
if test.expectedErr == nil && actualErr != nil {
|
|
|
|
t.Errorf("Test %d: Expected to pass, but failed with %s", i+1, actualErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Cleanup backend directories
|
|
|
|
removeRoots(fsDirs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestXLDeleteObjectDiskNotFound(t *testing.T) {
|
|
|
|
// Create an instance of xl backend.
|
2016-08-30 22:22:27 -04:00
|
|
|
obj, fsDirs, err := prepareXL()
|
2016-07-21 16:15:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-16 19:42:23 -05:00
|
|
|
xl := obj.(*xlObjects)
|
2016-07-21 16:15:54 -04:00
|
|
|
|
|
|
|
// Create "bucket"
|
2017-06-01 12:43:20 -04:00
|
|
|
err = obj.MakeBucketWithLocation("bucket", "")
|
2016-07-21 16:15:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := "bucket"
|
|
|
|
object := "object"
|
|
|
|
// Create object "obj" under bucket "bucket".
|
2017-10-22 01:30:34 -04:00
|
|
|
_, err = obj.PutObject(bucket, object, mustGetHashReader(t, bytes.NewReader([]byte("abcd")), int64(len("abcd")), "", ""), nil)
|
2016-09-02 02:10:50 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-07-21 16:15:54 -04:00
|
|
|
// for a 16 disk setup, quorum is 9. To simulate disks not found yet
|
|
|
|
// quorum is available, we remove disks leaving quorum disks behind.
|
|
|
|
for i := range xl.storageDisks[:7] {
|
2016-11-23 18:48:10 -05:00
|
|
|
xl.storageDisks[i] = newNaughtyDisk(xl.storageDisks[i].(*retryStorage), nil, errFaultyDisk)
|
2016-07-21 16:15:54 -04:00
|
|
|
}
|
|
|
|
err = obj.DeleteObject(bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create "obj" under "bucket".
|
2017-10-22 01:30:34 -04:00
|
|
|
_, err = obj.PutObject(bucket, object, mustGetHashReader(t, bytes.NewReader([]byte("abcd")), int64(len("abcd")), "", ""), nil)
|
2016-07-21 16:15:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove one more disk to 'lose' quorum, by setting it to nil.
|
2016-09-09 15:53:09 -04:00
|
|
|
xl.storageDisks[7] = nil
|
|
|
|
xl.storageDisks[8] = nil
|
2016-07-21 16:15:54 -04:00
|
|
|
err = obj.DeleteObject(bucket, object)
|
2017-11-25 14:58:29 -05:00
|
|
|
err = errors.Cause(err)
|
2016-07-21 16:15:54 -04:00
|
|
|
if err != toObjectErr(errXLWriteQuorum, bucket, object) {
|
|
|
|
t.Errorf("Expected deleteObject to fail with %v, but failed with %v", toObjectErr(errXLWriteQuorum, bucket, object), err)
|
|
|
|
}
|
|
|
|
// Cleanup backend directories
|
|
|
|
removeRoots(fsDirs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetObjectNoQuorum(t *testing.T) {
|
|
|
|
// Create an instance of xl backend.
|
2016-08-30 22:22:27 -04:00
|
|
|
obj, fsDirs, err := prepareXL()
|
2016-07-21 16:15:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-16 19:42:23 -05:00
|
|
|
xl := obj.(*xlObjects)
|
2016-07-21 16:15:54 -04:00
|
|
|
|
|
|
|
// Create "bucket"
|
2017-06-01 12:43:20 -04:00
|
|
|
err = obj.MakeBucketWithLocation("bucket", "")
|
2016-07-21 16:15:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := "bucket"
|
|
|
|
object := "object"
|
|
|
|
// Create "object" under "bucket".
|
2017-10-22 01:30:34 -04:00
|
|
|
_, err = obj.PutObject(bucket, object, mustGetHashReader(t, bytes.NewReader([]byte("abcd")), int64(len("abcd")), "", ""), nil)
|
2016-07-21 16:15:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Disable caching to avoid returning early and not covering other code-paths
|
|
|
|
xl.objCacheEnabled = false
|
|
|
|
// Make 9 disks offline, which leaves less than quorum number of disks
|
|
|
|
// in a 16 disk XL setup. The original disks are 'replaced' with
|
2016-09-09 15:53:09 -04:00
|
|
|
// naughtyDisks that fail after 'f' successful StorageAPI method
|
2016-07-21 16:15:54 -04:00
|
|
|
// invocations, where f - [0,2)
|
|
|
|
for f := 0; f < 2; f++ {
|
2016-09-09 15:53:09 -04:00
|
|
|
diskErrors := make(map[int]error)
|
|
|
|
for i := 0; i <= f; i++ {
|
|
|
|
diskErrors[i] = nil
|
|
|
|
}
|
2016-07-21 16:15:54 -04:00
|
|
|
for i := range xl.storageDisks[:9] {
|
|
|
|
switch diskType := xl.storageDisks[i].(type) {
|
2016-11-23 18:48:10 -05:00
|
|
|
case *retryStorage:
|
2016-09-09 15:53:09 -04:00
|
|
|
xl.storageDisks[i] = newNaughtyDisk(diskType, diskErrors, errFaultyDisk)
|
|
|
|
case *naughtyDisk:
|
|
|
|
xl.storageDisks[i] = newNaughtyDisk(diskType.disk, diskErrors, errFaultyDisk)
|
2016-07-21 16:15:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Fetch object from store.
|
|
|
|
err = xl.GetObject(bucket, object, 0, int64(len("abcd")), ioutil.Discard)
|
2017-11-25 14:58:29 -05:00
|
|
|
err = errors.Cause(err)
|
2016-07-21 16:15:54 -04:00
|
|
|
if err != toObjectErr(errXLReadQuorum, bucket, object) {
|
|
|
|
t.Errorf("Expected putObject to fail with %v, but failed with %v", toObjectErr(errXLWriteQuorum, bucket, object), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Cleanup backend directories.
|
|
|
|
removeRoots(fsDirs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutObjectNoQuorum(t *testing.T) {
|
|
|
|
// Create an instance of xl backend.
|
2016-08-30 22:22:27 -04:00
|
|
|
obj, fsDirs, err := prepareXL()
|
2016-07-21 16:15:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-16 19:42:23 -05:00
|
|
|
xl := obj.(*xlObjects)
|
2016-07-21 16:15:54 -04:00
|
|
|
|
|
|
|
// Create "bucket"
|
2017-06-01 12:43:20 -04:00
|
|
|
err = obj.MakeBucketWithLocation("bucket", "")
|
2016-07-21 16:15:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := "bucket"
|
|
|
|
object := "object"
|
|
|
|
// Create "object" under "bucket".
|
2017-10-22 01:30:34 -04:00
|
|
|
_, err = obj.PutObject(bucket, object, mustGetHashReader(t, bytes.NewReader([]byte("abcd")), int64(len("abcd")), "", ""), nil)
|
2016-07-21 16:15:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make 9 disks offline, which leaves less than quorum number of disks
|
|
|
|
// in a 16 disk XL setup. The original disks are 'replaced' with
|
2016-09-09 15:53:09 -04:00
|
|
|
// naughtyDisks that fail after 'f' successful StorageAPI method
|
2016-07-21 16:15:54 -04:00
|
|
|
// invocations, where f - [0,3)
|
|
|
|
for f := 0; f < 3; f++ {
|
2016-09-09 15:53:09 -04:00
|
|
|
diskErrors := make(map[int]error)
|
|
|
|
for i := 0; i <= f; i++ {
|
|
|
|
diskErrors[i] = nil
|
|
|
|
}
|
2016-07-21 16:15:54 -04:00
|
|
|
for i := range xl.storageDisks[:9] {
|
|
|
|
switch diskType := xl.storageDisks[i].(type) {
|
2016-11-23 18:48:10 -05:00
|
|
|
case *retryStorage:
|
2016-09-09 15:53:09 -04:00
|
|
|
xl.storageDisks[i] = newNaughtyDisk(diskType, diskErrors, errFaultyDisk)
|
|
|
|
case *naughtyDisk:
|
|
|
|
xl.storageDisks[i] = newNaughtyDisk(diskType.disk, diskErrors, errFaultyDisk)
|
2016-07-21 16:15:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Upload new content to same object "object"
|
2017-10-22 01:30:34 -04:00
|
|
|
_, err = obj.PutObject(bucket, object, mustGetHashReader(t, bytes.NewReader([]byte("abcd")), int64(len("abcd")), "", ""), nil)
|
2017-11-25 14:58:29 -05:00
|
|
|
err = errors.Cause(err)
|
2016-07-21 16:15:54 -04:00
|
|
|
if err != toObjectErr(errXLWriteQuorum, bucket, object) {
|
|
|
|
t.Errorf("Expected putObject to fail with %v, but failed with %v", toObjectErr(errXLWriteQuorum, bucket, object), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Cleanup backend directories.
|
|
|
|
removeRoots(fsDirs)
|
|
|
|
}
|
2016-10-08 20:08:17 -04:00
|
|
|
|
2016-10-14 22:57:40 -04:00
|
|
|
// Tests both object and bucket healing.
|
|
|
|
func TestHealing(t *testing.T) {
|
2017-03-04 17:53:28 -05:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to initialize test config %v", err)
|
|
|
|
}
|
2017-08-12 22:25:43 -04:00
|
|
|
defer os.RemoveAll(rootPath)
|
2017-03-04 17:53:28 -05:00
|
|
|
|
2016-10-08 20:08:17 -04:00
|
|
|
obj, fsDirs, err := prepareXL()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer removeRoots(fsDirs)
|
2016-11-16 19:42:23 -05:00
|
|
|
xl := obj.(*xlObjects)
|
2016-10-08 20:08:17 -04:00
|
|
|
|
|
|
|
// Create "bucket"
|
2017-06-01 12:43:20 -04:00
|
|
|
err = obj.MakeBucketWithLocation("bucket", "")
|
2016-10-08 20:08:17 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := "bucket"
|
|
|
|
object := "object"
|
|
|
|
|
2016-11-22 21:18:22 -05:00
|
|
|
data := make([]byte, 1*humanize.MiByte)
|
2016-10-08 20:08:17 -04:00
|
|
|
length := int64(len(data))
|
|
|
|
_, err = rand.Read(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-22 01:30:34 -04:00
|
|
|
_, err = obj.PutObject(bucket, object, mustGetHashReader(t, bytes.NewReader(data), length, "", ""), nil)
|
2016-10-08 20:08:17 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
disk := xl.storageDisks[0]
|
|
|
|
xlMetaPreHeal, err := readXLMeta(disk, bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the object - to simulate the case where the disk was down when the object
|
|
|
|
// was created.
|
|
|
|
err = os.RemoveAll(path.Join(fsDirs[0], bucket, object))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-03-31 20:55:15 -04:00
|
|
|
_, _, err = xl.HealObject(bucket, object)
|
2016-10-08 20:08:17 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
xlMetaPostHeal, err := readXLMeta(disk, bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// After heal the meta file should be as expected.
|
|
|
|
if !reflect.DeepEqual(xlMetaPreHeal, xlMetaPostHeal) {
|
|
|
|
t.Fatal("HealObject failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write xl.json with different modtime to simulate the case where a disk had
|
|
|
|
// gone down when an object was replaced by a new object.
|
|
|
|
xlMetaOutDated := xlMetaPreHeal
|
|
|
|
xlMetaOutDated.Stat.ModTime = time.Now()
|
|
|
|
err = writeXLMetadata(disk, bucket, object, xlMetaOutDated)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-03-31 20:55:15 -04:00
|
|
|
_, _, err = xl.HealObject(bucket, object)
|
2016-10-08 20:08:17 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
xlMetaPostHeal, err = readXLMeta(disk, bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// After heal the meta file should be as expected.
|
|
|
|
if !reflect.DeepEqual(xlMetaPreHeal, xlMetaPostHeal) {
|
|
|
|
t.Fatal("HealObject failed")
|
|
|
|
}
|
2016-10-14 14:12:17 -04:00
|
|
|
|
2016-10-14 22:57:40 -04:00
|
|
|
// Remove the bucket - to simulate the case where bucket was
|
|
|
|
// created when the disk was down.
|
2016-10-14 14:12:17 -04:00
|
|
|
err = os.RemoveAll(path.Join(fsDirs[0], bucket))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// This would create the bucket.
|
2016-10-14 22:57:40 -04:00
|
|
|
err = xl.HealBucket(bucket)
|
2016-10-14 14:12:17 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Stat the bucket to make sure that it was created.
|
|
|
|
_, err = xl.storageDisks[0].StatVol(bucket)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-08 20:08:17 -04:00
|
|
|
}
|