2016-11-17 01:42:23 +01:00
|
|
|
/*
|
2019-04-09 20:39:42 +02:00
|
|
|
* MinIO Cloud Storage, (C) 2016, 2017 MinIO, Inc.
|
2016-11-17 01:42:23 +01: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-03-17 17:25:49 +01:00
|
|
|
"bytes"
|
2018-03-15 21:27:16 +01:00
|
|
|
"context"
|
2017-03-17 17:25:49 +01:00
|
|
|
"path/filepath"
|
2016-11-17 01:42:23 +01:00
|
|
|
"testing"
|
2019-03-14 21:08:51 +01:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/madmin"
|
2016-11-17 01:42:23 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Tests undoes and validates if the undoing completes successfully.
|
|
|
|
func TestUndoMakeBucket(t *testing.T) {
|
|
|
|
nDisks := 16
|
|
|
|
fsDirs, err := getRandomDisks(nDisks)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer removeRoots(fsDirs)
|
|
|
|
|
|
|
|
// Remove format.json on 16 disks.
|
2019-11-20 02:42:27 +01:00
|
|
|
obj, _, err := initObjectLayer(mustGetZoneEndpoints(fsDirs...))
|
2016-11-17 01:42:23 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketName := getRandomBucketName()
|
2018-03-15 21:27:16 +01:00
|
|
|
if err = obj.MakeBucketWithLocation(context.Background(), bucketName, ""); err != nil {
|
2016-11-17 01:42:23 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-20 02:42:27 +01:00
|
|
|
z := obj.(*xlZones)
|
|
|
|
xl := z.zones[0].sets[0]
|
|
|
|
undoMakeBucket(xl.getDisks(), bucketName)
|
2016-11-17 01:42:23 +01:00
|
|
|
|
|
|
|
// Validate if bucket was deleted properly.
|
2018-03-15 21:27:16 +01:00
|
|
|
_, err = obj.GetBucketInfo(context.Background(), bucketName)
|
2016-11-17 01:42:23 +01:00
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case BucketNotFound:
|
|
|
|
default:
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 22:57:44 +01:00
|
|
|
func TestHealObjectCorrupted(t *testing.T) {
|
2020-03-03 01:29:30 +01:00
|
|
|
resetGlobalHealState()
|
|
|
|
|
|
|
|
defer resetGlobalHealState()
|
|
|
|
|
2019-03-26 22:57:44 +01:00
|
|
|
nDisks := 16
|
|
|
|
fsDirs, err := getRandomDisks(nDisks)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer removeRoots(fsDirs)
|
|
|
|
|
|
|
|
// Everything is fine, should return nil
|
2019-11-20 02:42:27 +01:00
|
|
|
objLayer, _, err := initObjectLayer(mustGetZoneEndpoints(fsDirs...))
|
2019-03-26 22:57:44 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := "bucket"
|
|
|
|
object := "object"
|
|
|
|
data := bytes.Repeat([]byte("a"), 5*1024*1024)
|
|
|
|
var opts ObjectOptions
|
|
|
|
|
2019-10-01 22:12:15 +02:00
|
|
|
err = objLayer.MakeBucketWithLocation(context.Background(), bucket, "")
|
2019-03-26 22:57:44 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to make a bucket - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an object with multiple parts uploaded in decreasing
|
|
|
|
// part number.
|
2019-10-01 22:12:15 +02:00
|
|
|
uploadID, err := objLayer.NewMultipartUpload(context.Background(), bucket, object, opts)
|
2019-03-26 22:57:44 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a multipart upload - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var uploadedParts []CompletePart
|
|
|
|
for _, partID := range []int{2, 1} {
|
2019-10-01 22:12:15 +02:00
|
|
|
pInfo, err1 := objLayer.PutObjectPart(context.Background(), bucket, object, uploadID, partID, mustGetPutObjReader(t, bytes.NewReader(data), int64(len(data)), "", ""), opts)
|
2019-03-26 22:57:44 +01:00
|
|
|
if err1 != nil {
|
|
|
|
t.Fatalf("Failed to upload a part - %v", err1)
|
|
|
|
}
|
|
|
|
uploadedParts = append(uploadedParts, CompletePart{
|
|
|
|
PartNumber: pInfo.PartNumber,
|
|
|
|
ETag: pInfo.ETag,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-01 22:12:15 +02:00
|
|
|
_, err = objLayer.CompleteMultipartUpload(context.Background(), bucket, object, uploadID, uploadedParts, ObjectOptions{})
|
2019-03-26 22:57:44 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to complete multipart upload - %v", err)
|
|
|
|
}
|
|
|
|
|
2019-07-13 01:29:44 +02:00
|
|
|
// Test 1: Remove the object backend files from the first disk.
|
2019-11-20 02:42:27 +01:00
|
|
|
z := objLayer.(*xlZones)
|
|
|
|
xl := z.zones[0].sets[0]
|
|
|
|
firstDisk := xl.getDisks()[0]
|
2019-03-26 22:57:44 +01:00
|
|
|
err = firstDisk.DeleteFile(bucket, filepath.Join(object, xlMetaJSONFile))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to delete a file - %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-19 01:50:00 +01:00
|
|
|
_, err = objLayer.HealObject(context.Background(), bucket, object, madmin.HealOpts{ScanMode: madmin.HealNormalScan})
|
2019-03-26 22:57:44 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to heal object - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = firstDisk.StatFile(bucket, filepath.Join(object, xlMetaJSONFile))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected xl.json file to be present but stat failed - %v", err)
|
|
|
|
}
|
|
|
|
|
2019-07-13 01:29:44 +02:00
|
|
|
// Test 2: Heal when part.1 is empty
|
|
|
|
partSt1, err := firstDisk.StatFile(bucket, filepath.Join(object, "part.1"))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected part.1 file to be present but stat failed - %v", err)
|
|
|
|
}
|
|
|
|
err = firstDisk.DeleteFile(bucket, filepath.Join(object, "part.1"))
|
|
|
|
if err != nil {
|
2019-10-01 22:12:15 +02:00
|
|
|
t.Errorf("Failure during deleting part.1 - %v", err)
|
2019-07-13 01:29:44 +02:00
|
|
|
}
|
2019-10-01 22:12:15 +02:00
|
|
|
err = firstDisk.WriteAll(bucket, filepath.Join(object, "part.1"), bytes.NewReader([]byte{}))
|
2019-07-13 01:29:44 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failure during creating part.1 - %v", err)
|
|
|
|
}
|
2020-03-19 01:50:00 +01:00
|
|
|
_, err = objLayer.HealObject(context.Background(), bucket, object, madmin.HealOpts{DryRun: false, Remove: true, ScanMode: madmin.HealDeepScan})
|
2019-07-13 01:29:44 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected nil but received %v", err)
|
|
|
|
}
|
|
|
|
partSt2, err := firstDisk.StatFile(bucket, filepath.Join(object, "part.1"))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected from part.1 file to be present but stat failed - %v", err)
|
|
|
|
}
|
|
|
|
if partSt1.Size != partSt2.Size {
|
|
|
|
t.Errorf("part.1 file size is not the same before and after heal")
|
|
|
|
}
|
|
|
|
|
2019-10-01 22:12:15 +02:00
|
|
|
// Test 3: Heal when part.1 is correct in size but corrupted
|
|
|
|
partSt1, err = firstDisk.StatFile(bucket, filepath.Join(object, "part.1"))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected part.1 file to be present but stat failed - %v", err)
|
|
|
|
}
|
|
|
|
err = firstDisk.DeleteFile(bucket, filepath.Join(object, "part.1"))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failure during deleting part.1 - %v", err)
|
|
|
|
}
|
|
|
|
bdata := bytes.Repeat([]byte("b"), int(partSt1.Size))
|
|
|
|
err = firstDisk.WriteAll(bucket, filepath.Join(object, "part.1"), bytes.NewReader(bdata))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failure during creating part.1 - %v", err)
|
|
|
|
}
|
2020-03-19 01:50:00 +01:00
|
|
|
_, err = objLayer.HealObject(context.Background(), bucket, object, madmin.HealOpts{DryRun: false, Remove: true, ScanMode: madmin.HealDeepScan})
|
2019-10-01 22:12:15 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected nil but received %v", err)
|
|
|
|
}
|
|
|
|
partSt2, err = firstDisk.StatFile(bucket, filepath.Join(object, "part.1"))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected from part.1 file to be present but stat failed - %v", err)
|
|
|
|
}
|
|
|
|
if partSt1.Size != partSt2.Size {
|
|
|
|
t.Errorf("part.1 file size is not the same before and after heal")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test 4: checks if HealObject returns an error when xl.json is not found
|
2019-07-13 01:29:44 +02:00
|
|
|
// in more than read quorum number of disks, to create a corrupted situation.
|
|
|
|
|
2019-11-20 02:42:27 +01:00
|
|
|
for i := 0; i <= len(xl.getDisks())/2; i++ {
|
|
|
|
xl.getDisks()[i].DeleteFile(bucket, filepath.Join(object, xlMetaJSONFile))
|
2019-03-26 22:57:44 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 22:18:32 +01:00
|
|
|
// Try healing now, expect to receive errFileNotFound.
|
2020-03-19 01:50:00 +01:00
|
|
|
_, err = objLayer.HealObject(context.Background(), bucket, object, madmin.HealOpts{DryRun: false, Remove: true, ScanMode: madmin.HealDeepScan})
|
2019-03-26 22:57:44 +01:00
|
|
|
if err != nil {
|
2019-11-21 22:18:32 +01:00
|
|
|
if _, ok := err.(ObjectNotFound); !ok {
|
|
|
|
t.Errorf("Expect %v but received %v", ObjectNotFound{Bucket: bucket, Object: object}, err)
|
|
|
|
}
|
2019-03-26 22:57:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// since majority of xl.jsons are not available, object should be successfully deleted.
|
2019-10-01 22:12:15 +02:00
|
|
|
_, err = objLayer.GetObjectInfo(context.Background(), bucket, object, ObjectOptions{})
|
2019-03-26 22:57:44 +01:00
|
|
|
if _, ok := err.(ObjectNotFound); !ok {
|
|
|
|
t.Errorf("Expect %v but received %v", ObjectNotFound{Bucket: bucket, Object: object}, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 17:25:49 +01:00
|
|
|
// Tests healing of object.
|
|
|
|
func TestHealObjectXL(t *testing.T) {
|
|
|
|
nDisks := 16
|
|
|
|
fsDirs, err := getRandomDisks(nDisks)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer removeRoots(fsDirs)
|
|
|
|
|
|
|
|
// Everything is fine, should return nil
|
2019-11-20 02:42:27 +01:00
|
|
|
obj, _, err := initObjectLayer(mustGetZoneEndpoints(fsDirs...))
|
2017-03-17 17:25:49 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := "bucket"
|
|
|
|
object := "object"
|
2017-03-22 18:15:16 +01:00
|
|
|
data := bytes.Repeat([]byte("a"), 5*1024*1024)
|
2018-09-10 18:42:43 +02:00
|
|
|
var opts ObjectOptions
|
2017-03-22 18:15:16 +01:00
|
|
|
|
2018-03-15 21:27:16 +01:00
|
|
|
err = obj.MakeBucketWithLocation(context.Background(), bucket, "")
|
2017-03-17 17:25:49 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to make a bucket - %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-22 18:15:16 +01:00
|
|
|
// Create an object with multiple parts uploaded in decreasing
|
|
|
|
// part number.
|
2019-02-09 06:31:06 +01:00
|
|
|
uploadID, err := obj.NewMultipartUpload(context.Background(), bucket, object, opts)
|
2017-03-22 18:15:16 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a multipart upload - %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-14 09:25:10 +01:00
|
|
|
var uploadedParts []CompletePart
|
2017-03-22 18:15:16 +01:00
|
|
|
for _, partID := range []int{2, 1} {
|
2018-11-15 02:36:41 +01:00
|
|
|
pInfo, err1 := obj.PutObjectPart(context.Background(), bucket, object, uploadID, partID, mustGetPutObjReader(t, bytes.NewReader(data), int64(len(data)), "", ""), opts)
|
2017-04-01 10:06:06 +02:00
|
|
|
if err1 != nil {
|
|
|
|
t.Fatalf("Failed to upload a part - %v", err1)
|
2017-03-22 18:15:16 +01:00
|
|
|
}
|
2017-11-14 09:25:10 +01:00
|
|
|
uploadedParts = append(uploadedParts, CompletePart{
|
2017-03-22 18:15:16 +01:00
|
|
|
PartNumber: pInfo.PartNumber,
|
|
|
|
ETag: pInfo.ETag,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-15 02:36:41 +01:00
|
|
|
_, err = obj.CompleteMultipartUpload(context.Background(), bucket, object, uploadID, uploadedParts, ObjectOptions{})
|
2017-03-17 17:25:49 +01:00
|
|
|
if err != nil {
|
2017-03-22 18:15:16 +01:00
|
|
|
t.Fatalf("Failed to complete multipart upload - %v", err)
|
2017-03-17 17:25:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the object backend files from the first disk.
|
2019-11-20 02:42:27 +01:00
|
|
|
z := obj.(*xlZones)
|
|
|
|
xl := z.zones[0].sets[0]
|
|
|
|
firstDisk := xl.getDisks()[0]
|
2017-03-17 17:25:49 +01:00
|
|
|
err = firstDisk.DeleteFile(bucket, filepath.Join(object, xlMetaJSONFile))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to delete a file - %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-19 01:50:00 +01:00
|
|
|
_, err = obj.HealObject(context.Background(), bucket, object, madmin.HealOpts{ScanMode: madmin.HealNormalScan})
|
2017-03-17 17:25:49 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to heal object - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = firstDisk.StatFile(bucket, filepath.Join(object, xlMetaJSONFile))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected xl.json file to be present but stat failed - %v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-20 02:42:27 +01:00
|
|
|
xlDisks := xl.getDisks()
|
2020-01-23 20:50:09 +01:00
|
|
|
z.zones[0].xlDisksMu.Lock()
|
2019-11-20 02:42:27 +01:00
|
|
|
xl.getDisks = func() []StorageAPI {
|
|
|
|
// Nil more than half the disks, to remove write quorum.
|
|
|
|
for i := 0; i <= len(xlDisks)/2; i++ {
|
|
|
|
xlDisks[i] = nil
|
|
|
|
}
|
|
|
|
return xlDisks
|
2017-03-17 17:25:49 +01:00
|
|
|
}
|
2020-01-23 20:50:09 +01:00
|
|
|
z.zones[0].xlDisksMu.Unlock()
|
2017-03-17 17:25:49 +01:00
|
|
|
|
|
|
|
// Try healing now, expect to receive errDiskNotFound.
|
2020-03-19 01:50:00 +01:00
|
|
|
_, err = obj.HealObject(context.Background(), bucket, object, madmin.HealOpts{ScanMode: madmin.HealDeepScan})
|
2017-12-22 12:28:13 +01:00
|
|
|
// since majority of xl.jsons are not available, object quorum can't be read properly and error will be errXLReadQuorum
|
2018-04-25 20:56:39 +02:00
|
|
|
if _, ok := err.(InsufficientReadQuorum); !ok {
|
2018-07-31 09:23:29 +02:00
|
|
|
t.Errorf("Expected %v but received %v", InsufficientReadQuorum{}, err)
|
2017-03-17 17:25:49 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-01 23:13:06 +02:00
|
|
|
|
|
|
|
// Tests healing of empty directories
|
|
|
|
func TestHealEmptyDirectoryXL(t *testing.T) {
|
|
|
|
nDisks := 16
|
|
|
|
fsDirs, err := getRandomDisks(nDisks)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer removeRoots(fsDirs)
|
|
|
|
|
|
|
|
// Everything is fine, should return nil
|
2019-11-20 02:42:27 +01:00
|
|
|
obj, _, err := initObjectLayer(mustGetZoneEndpoints(fsDirs...))
|
2019-08-01 23:13:06 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := "bucket"
|
|
|
|
object := "empty-dir/"
|
|
|
|
var opts ObjectOptions
|
|
|
|
|
|
|
|
err = obj.MakeBucketWithLocation(context.Background(), bucket, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to make a bucket - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload an empty directory
|
2019-11-20 02:42:27 +01:00
|
|
|
_, err = obj.PutObject(context.Background(), bucket, object, mustGetPutObjReader(t,
|
|
|
|
bytes.NewReader([]byte{}), 0, "", ""), opts)
|
2019-08-01 23:13:06 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the object backend files from the first disk.
|
2019-11-20 02:42:27 +01:00
|
|
|
z := obj.(*xlZones)
|
|
|
|
xl := z.zones[0].sets[0]
|
|
|
|
firstDisk := xl.getDisks()[0]
|
2019-08-01 23:13:06 +02:00
|
|
|
err = firstDisk.DeleteFile(bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to delete a file - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Heal the object
|
2020-03-19 01:50:00 +01:00
|
|
|
hr, err := obj.HealObject(context.Background(), bucket, object, madmin.HealOpts{ScanMode: madmin.HealNormalScan})
|
2019-08-01 23:13:06 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to heal object - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the empty directory is restored in the first disk
|
|
|
|
_, err = firstDisk.StatVol(pathJoin(bucket, object))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected object to be present but stat failed - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the state of the object in the first disk (should be missing)
|
|
|
|
if hr.Before.Drives[0].State != madmin.DriveStateMissing {
|
|
|
|
t.Fatalf("Unexpected drive state: %v", hr.Before.Drives[0].State)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the state of all other disks (should be ok)
|
|
|
|
for i, h := range append(hr.Before.Drives[1:], hr.After.Drives...) {
|
|
|
|
if h.State != madmin.DriveStateOk {
|
|
|
|
t.Fatalf("Unexpected drive state (%d): %v", i+1, h.State)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Heal the same object again
|
2020-03-19 01:50:00 +01:00
|
|
|
hr, err = obj.HealObject(context.Background(), bucket, object, madmin.HealOpts{ScanMode: madmin.HealNormalScan})
|
2019-08-01 23:13:06 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to heal object - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that Before & After states are all okay
|
|
|
|
for i, h := range append(hr.Before.Drives, hr.After.Drives...) {
|
|
|
|
if h.State != madmin.DriveStateOk {
|
|
|
|
t.Fatalf("Unexpected drive state (%d): %v", i+1, h.State)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|