2021-04-18 12:41:13 -07:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-09-16 21:06:49 +01:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-03-15 13:27:16 -07:00
|
|
|
"context"
|
2017-08-12 19:25:43 -07:00
|
|
|
"os"
|
2016-09-16 21:06:49 +01:00
|
|
|
"path/filepath"
|
2020-11-05 11:48:55 -08:00
|
|
|
"runtime"
|
2020-03-19 00:19:29 +01:00
|
|
|
"sync"
|
2016-09-16 21:06:49 +01:00
|
|
|
"testing"
|
2017-08-10 14:11:57 -07:00
|
|
|
"time"
|
2016-09-16 21:06:49 +01:00
|
|
|
)
|
|
|
|
|
2017-11-30 18:11:42 -08:00
|
|
|
// Tests cleanup multipart uploads for filesystem backend.
|
2017-08-10 14:11:57 -07:00
|
|
|
func TestFSCleanupMultipartUploadsInRoutine(t *testing.T) {
|
|
|
|
// Prepare for tests
|
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
2017-08-12 19:25:43 -07:00
|
|
|
defer os.RemoveAll(disk)
|
2017-08-10 14:11:57 -07:00
|
|
|
|
|
|
|
obj := initFSObjects(disk, t)
|
2018-02-20 12:21:12 -08:00
|
|
|
fs := obj.(*FSObjects)
|
2017-08-10 14:11:57 -07:00
|
|
|
|
|
|
|
bucketName := "bucket"
|
|
|
|
objectName := "object"
|
|
|
|
|
2020-03-19 00:19:29 +01:00
|
|
|
// Create a context we can cancel.
|
2020-04-09 09:30:02 -07:00
|
|
|
ctx, cancel := context.WithCancel(GlobalContext)
|
2020-06-12 20:04:01 -07:00
|
|
|
obj.MakeBucketWithLocation(ctx, bucketName, BucketOptions{})
|
2020-03-19 00:19:29 +01:00
|
|
|
|
|
|
|
uploadID, err := obj.NewMultipartUpload(ctx, bucketName, objectName, ObjectOptions{})
|
2017-08-10 14:11:57 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected err: ", err)
|
|
|
|
}
|
|
|
|
|
2020-03-19 00:19:29 +01:00
|
|
|
var cleanupWg sync.WaitGroup
|
|
|
|
cleanupWg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer cleanupWg.Done()
|
2020-09-08 15:55:40 -07:00
|
|
|
fs.cleanupStaleUploads(ctx, time.Millisecond, 0)
|
2020-03-19 00:19:29 +01:00
|
|
|
}()
|
2017-08-10 14:11:57 -07:00
|
|
|
|
2020-03-19 00:19:29 +01:00
|
|
|
// Wait for 100ms such that - we have given enough time for
|
|
|
|
// cleanup routine to kick in. Flaky on slow systems...
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
cancel()
|
|
|
|
cleanupWg.Wait()
|
2017-08-10 14:11:57 -07:00
|
|
|
|
|
|
|
// Check if upload id was already purged.
|
2020-09-14 15:57:13 -07:00
|
|
|
if err = obj.AbortMultipartUpload(GlobalContext, bucketName, objectName, uploadID, ObjectOptions{}); err != nil {
|
2017-08-10 14:11:57 -07:00
|
|
|
if _, ok := err.(InvalidUploadID); !ok {
|
|
|
|
t.Fatal("Unexpected err: ", err)
|
|
|
|
}
|
2020-03-19 00:19:29 +01:00
|
|
|
} else {
|
|
|
|
t.Error("Item was not cleaned up.")
|
2017-08-10 14:11:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 21:06:49 +01:00
|
|
|
// TestNewMultipartUploadFaultyDisk - test NewMultipartUpload with faulty disks
|
|
|
|
func TestNewMultipartUploadFaultyDisk(t *testing.T) {
|
|
|
|
// Prepare for tests
|
2016-12-16 07:25:05 +01:00
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
2017-08-12 19:25:43 -07:00
|
|
|
defer os.RemoveAll(disk)
|
2016-10-05 12:48:07 -07:00
|
|
|
obj := initFSObjects(disk, t)
|
2016-09-16 21:06:49 +01:00
|
|
|
|
2018-02-20 12:21:12 -08:00
|
|
|
fs := obj.(*FSObjects)
|
2016-09-16 21:06:49 +01:00
|
|
|
bucketName := "bucket"
|
|
|
|
objectName := "object"
|
|
|
|
|
2020-06-12 20:04:01 -07:00
|
|
|
if err := obj.MakeBucketWithLocation(GlobalContext, bucketName, BucketOptions{}); err != nil {
|
2016-09-16 21:06:49 +01:00
|
|
|
t.Fatal("Cannot create bucket, err: ", err)
|
|
|
|
}
|
|
|
|
|
2017-01-16 17:05:00 -08:00
|
|
|
// Test with disk removed.
|
2018-05-23 03:11:29 -07:00
|
|
|
os.RemoveAll(disk)
|
2020-04-09 09:30:02 -07:00
|
|
|
if _, err := fs.NewMultipartUpload(GlobalContext, bucketName, objectName, ObjectOptions{UserDefined: map[string]string{"X-Amz-Meta-xid": "3f"}}); err != nil {
|
2018-04-10 09:36:37 -07:00
|
|
|
if !isSameType(err, BucketNotFound{}) {
|
2017-01-16 17:05:00 -08:00
|
|
|
t.Fatal("Unexpected error ", err)
|
2016-09-16 21:06:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestPutObjectPartFaultyDisk - test PutObjectPart with faulty disks
|
|
|
|
func TestPutObjectPartFaultyDisk(t *testing.T) {
|
|
|
|
// Prepare for tests
|
2016-12-16 07:25:05 +01:00
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
2017-08-12 19:25:43 -07:00
|
|
|
defer os.RemoveAll(disk)
|
2016-10-05 12:48:07 -07:00
|
|
|
obj := initFSObjects(disk, t)
|
2018-08-14 21:41:47 -07:00
|
|
|
|
2016-09-16 21:06:49 +01:00
|
|
|
bucketName := "bucket"
|
|
|
|
objectName := "object"
|
|
|
|
data := []byte("12345")
|
|
|
|
dataLen := int64(len(data))
|
|
|
|
|
2020-06-12 20:04:01 -07:00
|
|
|
if err := obj.MakeBucketWithLocation(GlobalContext, bucketName, BucketOptions{}); err != nil {
|
2016-09-16 21:06:49 +01:00
|
|
|
t.Fatal("Cannot create bucket, err: ", err)
|
|
|
|
}
|
|
|
|
|
2020-04-09 09:30:02 -07:00
|
|
|
uploadID, err := obj.NewMultipartUpload(GlobalContext, bucketName, objectName, ObjectOptions{UserDefined: map[string]string{"X-Amz-Meta-xid": "3f"}})
|
2016-09-16 21:06:49 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error ", err)
|
|
|
|
}
|
|
|
|
|
2016-11-21 13:51:05 -08:00
|
|
|
md5Hex := getMD5Hash(data)
|
2016-10-03 04:21:49 +05:30
|
|
|
sha256sum := ""
|
2016-09-16 21:06:49 +01:00
|
|
|
|
2019-09-22 10:45:33 -07:00
|
|
|
newDisk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
|
|
|
defer os.RemoveAll(newDisk)
|
|
|
|
obj = initFSObjects(newDisk, t)
|
2020-04-09 09:30:02 -07:00
|
|
|
if _, err = obj.PutObjectPart(GlobalContext, bucketName, objectName, uploadID, 1, mustGetPutObjReader(t, bytes.NewReader(data), dataLen, md5Hex, sha256sum), ObjectOptions{}); err != nil {
|
2019-09-22 10:45:33 -07:00
|
|
|
if !isSameType(err, BucketNotFound{}) {
|
|
|
|
t.Fatal("Unexpected error ", err)
|
|
|
|
}
|
2016-09-16 21:06:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCompleteMultipartUploadFaultyDisk - test CompleteMultipartUpload with faulty disks
|
|
|
|
func TestCompleteMultipartUploadFaultyDisk(t *testing.T) {
|
|
|
|
// Prepare for tests
|
2016-12-16 07:25:05 +01:00
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
2017-08-12 19:25:43 -07:00
|
|
|
defer os.RemoveAll(disk)
|
2016-10-05 12:48:07 -07:00
|
|
|
obj := initFSObjects(disk, t)
|
2016-09-16 21:06:49 +01:00
|
|
|
|
|
|
|
bucketName := "bucket"
|
|
|
|
objectName := "object"
|
|
|
|
data := []byte("12345")
|
|
|
|
|
2020-06-12 20:04:01 -07:00
|
|
|
if err := obj.MakeBucketWithLocation(GlobalContext, bucketName, BucketOptions{}); err != nil {
|
2016-09-16 21:06:49 +01:00
|
|
|
t.Fatal("Cannot create bucket, err: ", err)
|
|
|
|
}
|
|
|
|
|
2020-04-09 09:30:02 -07:00
|
|
|
uploadID, err := obj.NewMultipartUpload(GlobalContext, bucketName, objectName, ObjectOptions{UserDefined: map[string]string{"X-Amz-Meta-xid": "3f"}})
|
2016-09-16 21:06:49 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error ", err)
|
|
|
|
}
|
|
|
|
|
2016-11-21 13:51:05 -08:00
|
|
|
md5Hex := getMD5Hash(data)
|
2016-09-16 21:06:49 +01:00
|
|
|
|
2017-11-14 00:25:10 -08:00
|
|
|
parts := []CompletePart{{PartNumber: 1, ETag: md5Hex}}
|
2019-09-22 10:45:33 -07:00
|
|
|
newDisk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
|
|
|
defer os.RemoveAll(newDisk)
|
|
|
|
obj = initFSObjects(newDisk, t)
|
2020-04-09 09:30:02 -07:00
|
|
|
if _, err := obj.CompleteMultipartUpload(GlobalContext, bucketName, objectName, uploadID, parts, ObjectOptions{}); err != nil {
|
2018-04-10 09:36:37 -07:00
|
|
|
if !isSameType(err, BucketNotFound{}) {
|
2017-01-16 17:05:00 -08:00
|
|
|
t.Fatal("Unexpected error ", err)
|
2016-09-16 21:06:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 13:17:24 -08:00
|
|
|
// TestCompleteMultipartUpload - test CompleteMultipartUpload
|
2017-10-19 02:56:20 +05:30
|
|
|
func TestCompleteMultipartUpload(t *testing.T) {
|
|
|
|
// Prepare for tests
|
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
|
|
|
defer os.RemoveAll(disk)
|
|
|
|
obj := initFSObjects(disk, t)
|
|
|
|
|
|
|
|
bucketName := "bucket"
|
|
|
|
objectName := "object"
|
|
|
|
data := []byte("12345")
|
|
|
|
|
2020-06-12 20:04:01 -07:00
|
|
|
if err := obj.MakeBucketWithLocation(GlobalContext, bucketName, BucketOptions{}); err != nil {
|
2017-10-19 02:56:20 +05:30
|
|
|
t.Fatal("Cannot create bucket, err: ", err)
|
|
|
|
}
|
|
|
|
|
2020-04-09 09:30:02 -07:00
|
|
|
uploadID, err := obj.NewMultipartUpload(GlobalContext, bucketName, objectName, ObjectOptions{UserDefined: map[string]string{"X-Amz-Meta-xid": "3f"}})
|
2017-10-19 02:56:20 +05:30
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
md5Hex := getMD5Hash(data)
|
|
|
|
|
2020-04-09 09:30:02 -07:00
|
|
|
if _, err := obj.PutObjectPart(GlobalContext, bucketName, objectName, uploadID, 1, mustGetPutObjReader(t, bytes.NewReader(data), 5, md5Hex, ""), ObjectOptions{}); err != nil {
|
2017-10-19 02:56:20 +05:30
|
|
|
t.Fatal("Unexpected error ", err)
|
|
|
|
}
|
|
|
|
|
2017-11-14 00:25:10 -08:00
|
|
|
parts := []CompletePart{{PartNumber: 1, ETag: md5Hex}}
|
2020-04-09 09:30:02 -07:00
|
|
|
if _, err := obj.CompleteMultipartUpload(GlobalContext, bucketName, objectName, uploadID, parts, ObjectOptions{}); err != nil {
|
2017-10-19 02:56:20 +05:30
|
|
|
t.Fatal("Unexpected error ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 13:17:24 -08:00
|
|
|
// TestCompleteMultipartUpload - test CompleteMultipartUpload
|
2017-10-19 02:56:20 +05:30
|
|
|
func TestAbortMultipartUpload(t *testing.T) {
|
2020-11-05 11:48:55 -08:00
|
|
|
if runtime.GOOS == globalWindowsOSName {
|
|
|
|
// Concurrent AbortMultipartUpload() fails on windows
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
|
2017-10-19 02:56:20 +05:30
|
|
|
// Prepare for tests
|
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
|
|
|
defer os.RemoveAll(disk)
|
|
|
|
obj := initFSObjects(disk, t)
|
|
|
|
|
|
|
|
bucketName := "bucket"
|
|
|
|
objectName := "object"
|
|
|
|
data := []byte("12345")
|
|
|
|
|
2020-06-12 20:04:01 -07:00
|
|
|
if err := obj.MakeBucketWithLocation(GlobalContext, bucketName, BucketOptions{}); err != nil {
|
2017-10-19 02:56:20 +05:30
|
|
|
t.Fatal("Cannot create bucket, err: ", err)
|
|
|
|
}
|
|
|
|
|
2020-04-09 09:30:02 -07:00
|
|
|
uploadID, err := obj.NewMultipartUpload(GlobalContext, bucketName, objectName, ObjectOptions{UserDefined: map[string]string{"X-Amz-Meta-xid": "3f"}})
|
2017-10-19 02:56:20 +05:30
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
md5Hex := getMD5Hash(data)
|
|
|
|
|
2020-09-14 15:57:13 -07:00
|
|
|
opts := ObjectOptions{}
|
|
|
|
if _, err := obj.PutObjectPart(GlobalContext, bucketName, objectName, uploadID, 1, mustGetPutObjReader(t, bytes.NewReader(data), 5, md5Hex, ""), opts); err != nil {
|
2017-10-19 02:56:20 +05:30
|
|
|
t.Fatal("Unexpected error ", err)
|
|
|
|
}
|
2020-09-14 15:57:13 -07:00
|
|
|
if err := obj.AbortMultipartUpload(GlobalContext, bucketName, objectName, uploadID, opts); err != nil {
|
2017-10-19 02:56:20 +05:30
|
|
|
t.Fatal("Unexpected error ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 21:06:49 +01:00
|
|
|
// TestListMultipartUploadsFaultyDisk - test ListMultipartUploads with faulty disks
|
|
|
|
func TestListMultipartUploadsFaultyDisk(t *testing.T) {
|
|
|
|
// Prepare for tests
|
2016-12-16 07:25:05 +01:00
|
|
|
disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
2017-08-12 19:25:43 -07:00
|
|
|
defer os.RemoveAll(disk)
|
2017-01-16 17:05:00 -08:00
|
|
|
|
2016-10-05 12:48:07 -07:00
|
|
|
obj := initFSObjects(disk, t)
|
2017-01-16 17:05:00 -08:00
|
|
|
|
2016-09-16 21:06:49 +01:00
|
|
|
bucketName := "bucket"
|
|
|
|
objectName := "object"
|
|
|
|
|
2020-06-12 20:04:01 -07:00
|
|
|
if err := obj.MakeBucketWithLocation(GlobalContext, bucketName, BucketOptions{}); err != nil {
|
2016-09-16 21:06:49 +01:00
|
|
|
t.Fatal("Cannot create bucket, err: ", err)
|
|
|
|
}
|
|
|
|
|
2020-04-09 09:30:02 -07:00
|
|
|
_, err := obj.NewMultipartUpload(GlobalContext, bucketName, objectName, ObjectOptions{UserDefined: map[string]string{"X-Amz-Meta-xid": "3f"}})
|
2016-09-16 21:06:49 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error ", err)
|
|
|
|
}
|
|
|
|
|
2019-09-22 10:45:33 -07:00
|
|
|
newDisk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
|
|
|
|
defer os.RemoveAll(newDisk)
|
|
|
|
obj = initFSObjects(newDisk, t)
|
2020-04-09 09:30:02 -07:00
|
|
|
if _, err := obj.ListMultipartUploads(GlobalContext, bucketName, objectName, "", "", "", 1000); err != nil {
|
2018-04-10 09:36:37 -07:00
|
|
|
if !isSameType(err, BucketNotFound{}) {
|
2017-01-16 17:05:00 -08:00
|
|
|
t.Fatal("Unexpected error ", err)
|
2016-09-16 21:06:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|