2014-10-24 23:37:25 +00:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
2016-08-16 00:12:24 +00:00
|
|
|
"bytes"
|
2023-08-27 10:06:16 +00:00
|
|
|
"crypto/rand"
|
2021-06-24 18:42:02 +00:00
|
|
|
"errors"
|
2021-08-11 16:16:43 +00:00
|
|
|
"fmt"
|
2014-10-24 23:37:25 +00:00
|
|
|
"os"
|
2022-04-09 11:16:46 +00:00
|
|
|
"path"
|
2021-08-12 14:54:11 +00:00
|
|
|
"reflect"
|
2022-04-09 11:16:46 +00:00
|
|
|
"sort"
|
2014-10-29 01:15:40 +00:00
|
|
|
"strconv"
|
2021-08-11 16:16:43 +00:00
|
|
|
"strings"
|
2014-10-24 23:37:25 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3/context"
|
|
|
|
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver/testsuites"
|
2014-10-24 23:37:25 +00:00
|
|
|
)
|
|
|
|
|
2023-09-27 14:57:40 +00:00
|
|
|
var (
|
|
|
|
s3DriverConstructor func(rootDirectory, storageClass string) (*Driver, error)
|
|
|
|
skipS3 func() string
|
|
|
|
)
|
2015-02-20 00:28:32 +00:00
|
|
|
|
2014-10-24 23:37:25 +00:00
|
|
|
func init() {
|
2022-11-02 21:05:45 +00:00
|
|
|
var (
|
|
|
|
accessKey = os.Getenv("AWS_ACCESS_KEY")
|
|
|
|
secretKey = os.Getenv("AWS_SECRET_KEY")
|
|
|
|
bucket = os.Getenv("S3_BUCKET")
|
|
|
|
encrypt = os.Getenv("S3_ENCRYPT")
|
|
|
|
keyID = os.Getenv("S3_KEY_ID")
|
|
|
|
secure = os.Getenv("S3_SECURE")
|
|
|
|
skipVerify = os.Getenv("S3_SKIP_VERIFY")
|
|
|
|
v4Auth = os.Getenv("S3_V4_AUTH")
|
|
|
|
region = os.Getenv("AWS_REGION")
|
|
|
|
objectACL = os.Getenv("S3_OBJECT_ACL")
|
|
|
|
regionEndpoint = os.Getenv("REGION_ENDPOINT")
|
|
|
|
forcePathStyle = os.Getenv("AWS_S3_FORCE_PATH_STYLE")
|
|
|
|
sessionToken = os.Getenv("AWS_SESSION_TOKEN")
|
|
|
|
useDualStack = os.Getenv("S3_USE_DUALSTACK")
|
|
|
|
combineSmallPart = os.Getenv("MULTIPART_COMBINE_SMALL_PART")
|
|
|
|
accelerate = os.Getenv("S3_ACCELERATE")
|
2023-09-27 14:57:40 +00:00
|
|
|
logLevel = os.Getenv("S3_LOGLEVEL")
|
2022-11-02 21:05:45 +00:00
|
|
|
)
|
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
root, err := os.MkdirTemp("", "driver-")
|
2014-12-19 17:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-01-07 10:18:42 +00:00
|
|
|
defer os.Remove(root)
|
2014-10-24 23:37:25 +00:00
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
s3DriverConstructor = func(rootDirectory, storageClass string) (*Driver, error) {
|
2015-01-07 10:18:42 +00:00
|
|
|
encryptBool := false
|
2015-01-07 09:45:31 +00:00
|
|
|
if encrypt != "" {
|
|
|
|
encryptBool, err = strconv.ParseBool(encrypt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-29 01:15:40 +00:00
|
|
|
}
|
2014-12-29 20:29:54 +00:00
|
|
|
|
|
|
|
secureBool := true
|
|
|
|
if secure != "" {
|
|
|
|
secureBool, err = strconv.ParseBool(secure)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-01-07 09:45:31 +00:00
|
|
|
|
2017-03-03 19:54:16 +00:00
|
|
|
skipVerifyBool := false
|
|
|
|
if skipVerify != "" {
|
|
|
|
skipVerifyBool, err = strconv.ParseBool(skipVerify)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 20:52:40 +00:00
|
|
|
v4Bool := true
|
|
|
|
if v4Auth != "" {
|
|
|
|
v4Bool, err = strconv.ParseBool(v4Auth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-04-19 14:49:19 +00:00
|
|
|
forcePathStyleBool := true
|
|
|
|
if forcePathStyle != "" {
|
|
|
|
forcePathStyleBool, err = strconv.ParseBool(forcePathStyle)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-09-01 20:52:40 +00:00
|
|
|
|
2019-12-11 20:41:40 +00:00
|
|
|
useDualStackBool := false
|
|
|
|
if useDualStack != "" {
|
|
|
|
useDualStackBool, err = strconv.ParseBool(useDualStack)
|
2021-12-31 06:13:16 +00:00
|
|
|
}
|
2017-01-20 17:12:37 +00:00
|
|
|
|
2021-12-31 06:13:16 +00:00
|
|
|
multipartCombineSmallPart := true
|
|
|
|
if combineSmallPart != "" {
|
|
|
|
multipartCombineSmallPart, err = strconv.ParseBool(combineSmallPart)
|
2019-12-11 20:41:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-21 14:44:19 +00:00
|
|
|
accelerateBool := true
|
|
|
|
if accelerate != "" {
|
|
|
|
accelerateBool, err = strconv.ParseBool(accelerate)
|
2017-01-20 17:12:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 10:18:42 +00:00
|
|
|
parameters := DriverParameters{
|
|
|
|
accessKey,
|
|
|
|
secretKey,
|
|
|
|
bucket,
|
2016-01-22 02:17:53 +00:00
|
|
|
region,
|
2016-03-05 18:46:44 +00:00
|
|
|
regionEndpoint,
|
2022-04-19 14:49:19 +00:00
|
|
|
forcePathStyleBool,
|
2015-01-07 10:18:42 +00:00
|
|
|
encryptBool,
|
2016-03-10 00:52:59 +00:00
|
|
|
keyID,
|
2015-01-07 10:18:42 +00:00
|
|
|
secureBool,
|
2017-03-03 19:54:16 +00:00
|
|
|
skipVerifyBool,
|
2016-09-01 20:52:40 +00:00
|
|
|
v4Bool,
|
2015-01-24 00:46:43 +00:00
|
|
|
minChunkSize,
|
2016-08-16 00:12:24 +00:00
|
|
|
defaultMultipartCopyChunkSize,
|
|
|
|
defaultMultipartCopyMaxConcurrency,
|
|
|
|
defaultMultipartCopyThresholdSize,
|
2021-12-31 06:13:16 +00:00
|
|
|
multipartCombineSmallPart,
|
2015-02-20 00:28:32 +00:00
|
|
|
rootDirectory,
|
2016-02-01 23:34:36 +00:00
|
|
|
storageClass,
|
2016-01-22 02:17:53 +00:00
|
|
|
driverName + "-test",
|
2016-10-06 00:47:12 +00:00
|
|
|
objectACL,
|
2017-03-27 20:04:00 +00:00
|
|
|
sessionToken,
|
2019-12-11 20:41:40 +00:00
|
|
|
useDualStackBool,
|
2021-02-21 14:44:19 +00:00
|
|
|
accelerateBool,
|
2023-09-27 14:57:40 +00:00
|
|
|
getS3LogLevelFromParam(logLevel),
|
2015-01-07 10:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return New(parameters)
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// Skip S3 storage driver tests if environment variable parameters are not provided
|
2015-06-29 23:39:45 +00:00
|
|
|
skipS3 = func() string {
|
2015-02-04 19:39:41 +00:00
|
|
|
if accessKey == "" || secretKey == "" || region == "" || bucket == "" || encrypt == "" {
|
|
|
|
return "Must set AWS_ACCESS_KEY, AWS_SECRET_KEY, AWS_REGION, S3_BUCKET, and S3_ENCRYPT to run S3 tests"
|
2014-10-27 20:24:07 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-06-29 23:39:45 +00:00
|
|
|
testsuites.RegisterSuite(func() (storagedriver.StorageDriver, error) {
|
2016-01-22 02:17:53 +00:00
|
|
|
return s3DriverConstructor(root, s3.StorageClassStandard)
|
2015-06-29 23:39:45 +00:00
|
|
|
}, skipS3)
|
2015-02-20 00:28:32 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 23:39:45 +00:00
|
|
|
func TestEmptyRootList(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
2015-02-20 00:28:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
validRoot := t.TempDir()
|
2016-01-22 02:17:53 +00:00
|
|
|
rootedDriver, err := s3DriverConstructor(validRoot, s3.StorageClassStandard)
|
2015-06-29 23:39:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating rooted driver: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
emptyRootDriver, err := s3DriverConstructor("", s3.StorageClassStandard)
|
2015-06-29 23:39:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating empty root driver: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
slashRootDriver, err := s3DriverConstructor("/", s3.StorageClassStandard)
|
2015-06-29 23:39:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating slash root driver: %v", err)
|
|
|
|
}
|
2015-02-20 00:28:32 +00:00
|
|
|
|
|
|
|
filename := "/test"
|
|
|
|
contents := []byte("contents")
|
2015-04-27 22:58:58 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
err = rootedDriver.PutContent(ctx, filename, contents)
|
2015-06-29 23:39:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
2015-04-27 22:58:58 +00:00
|
|
|
defer rootedDriver.Delete(ctx, filename)
|
2015-02-20 00:28:32 +00:00
|
|
|
|
2018-08-06 21:34:15 +00:00
|
|
|
keys, _ := emptyRootDriver.List(ctx, "/")
|
2015-02-20 00:28:32 +00:00
|
|
|
for _, path := range keys {
|
2015-06-29 23:39:45 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
|
|
|
|
}
|
2015-02-20 00:28:32 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:34:15 +00:00
|
|
|
keys, _ = slashRootDriver.List(ctx, "/")
|
2022-11-02 21:55:22 +00:00
|
|
|
for _, p := range keys {
|
|
|
|
if !storagedriver.PathRegexp.MatchString(p) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", p, storagedriver.PathRegexp)
|
2015-06-29 23:39:45 +00:00
|
|
|
}
|
2015-02-20 00:28:32 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-01 23:34:36 +00:00
|
|
|
|
|
|
|
func TestStorageClass(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
rootDir := t.TempDir()
|
2016-02-01 23:34:36 +00:00
|
|
|
contents := []byte("contents")
|
|
|
|
ctx := context.Background()
|
2023-10-17 01:10:43 +00:00
|
|
|
|
|
|
|
// We don't need to test all the storage classes, just that its selectable.
|
|
|
|
// The first 3 are common to AWS and MinIO, so use those.
|
|
|
|
for _, storageClass := range s3StorageClasses[:3] {
|
2022-04-04 07:55:51 +00:00
|
|
|
filename := "/test-" + storageClass
|
|
|
|
s3Driver, err := s3DriverConstructor(rootDir, storageClass)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver with storage class %v: %v", storageClass, err)
|
|
|
|
}
|
2016-02-01 23:34:36 +00:00
|
|
|
|
2022-08-12 15:06:03 +00:00
|
|
|
// Can only test outposts if using s3 outposts
|
|
|
|
if storageClass == s3.StorageClassOutposts {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-04 07:55:51 +00:00
|
|
|
err = s3Driver.PutContent(ctx, filename, contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content with storage class %v: %v", storageClass, err)
|
|
|
|
}
|
|
|
|
defer s3Driver.Delete(ctx, filename)
|
|
|
|
|
|
|
|
driverUnwrapped := s3Driver.Base.StorageDriver.(*driver)
|
|
|
|
resp, err := driverUnwrapped.S3.GetObject(&s3.GetObjectInput{
|
|
|
|
Bucket: aws.String(driverUnwrapped.Bucket),
|
|
|
|
Key: aws.String(driverUnwrapped.s3Path(filename)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error retrieving file with storage class %v: %v", storageClass, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
// Amazon only populates this header value for non-standard storage classes
|
2022-08-12 15:04:52 +00:00
|
|
|
if storageClass == noStorageClass {
|
|
|
|
// We haven't specified a storage class so we can't confirm what it is
|
|
|
|
} else if storageClass == s3.StorageClassStandard && resp.StorageClass != nil {
|
2022-04-04 07:55:51 +00:00
|
|
|
t.Fatalf(
|
|
|
|
"unexpected response storage class for file with storage class %v: %v",
|
|
|
|
storageClass,
|
|
|
|
*resp.StorageClass,
|
|
|
|
)
|
|
|
|
} else if storageClass != s3.StorageClassStandard && resp.StorageClass == nil {
|
|
|
|
t.Fatalf(
|
|
|
|
"unexpected response storage class for file with storage class %v: %v",
|
|
|
|
storageClass,
|
|
|
|
s3.StorageClassStandard,
|
|
|
|
)
|
|
|
|
} else if storageClass != s3.StorageClassStandard && storageClass != *resp.StorageClass {
|
|
|
|
t.Fatalf(
|
|
|
|
"unexpected response storage class for file with storage class %v: %v",
|
|
|
|
storageClass,
|
|
|
|
*resp.StorageClass,
|
|
|
|
)
|
|
|
|
}
|
2016-02-01 23:34:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-28 00:39:25 +00:00
|
|
|
|
2021-08-11 16:16:43 +00:00
|
|
|
func TestDelete(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
rootDir := t.TempDir()
|
2021-08-11 16:16:43 +00:00
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
drvr, err := s3DriverConstructor(rootDir, s3.StorageClassStandard)
|
2021-08-11 16:16:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver with standard storage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type errFn func(error) bool
|
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
delete string
|
|
|
|
expected []string
|
|
|
|
// error validation function
|
|
|
|
err errFn
|
|
|
|
}
|
|
|
|
|
|
|
|
errPathNotFound := func(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch err.(type) {
|
|
|
|
case storagedriver.PathNotFoundError:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
errInvalidPath := func(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch err.(type) {
|
|
|
|
case storagedriver.InvalidPathError:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:05:45 +00:00
|
|
|
objs := []string{
|
2021-08-11 18:44:43 +00:00
|
|
|
"/file1",
|
|
|
|
"/file1-2",
|
|
|
|
"/folder1/file1",
|
|
|
|
"/folder2/file1",
|
|
|
|
"/folder3/file1",
|
|
|
|
"/folder3/subfolder1/subfolder1/file1",
|
|
|
|
"/folder3/subfolder2/subfolder1/file1",
|
|
|
|
"/folder4/file1",
|
|
|
|
"/folder1-v2/file1",
|
|
|
|
"/folder1-v2/subfolder1/file1",
|
|
|
|
}
|
|
|
|
|
2021-08-11 16:16:43 +00:00
|
|
|
tcs := []testCase{
|
|
|
|
{
|
|
|
|
name: "delete folder1",
|
|
|
|
delete: "/folder1",
|
|
|
|
expected: []string{
|
|
|
|
"/folder1/file1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "delete folder2",
|
|
|
|
delete: "/folder2",
|
|
|
|
expected: []string{
|
|
|
|
"/folder2/file1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "delete folder3",
|
|
|
|
delete: "/folder3",
|
|
|
|
expected: []string{
|
|
|
|
"/folder3/file1",
|
|
|
|
"/folder3/subfolder1/subfolder1/file1",
|
|
|
|
"/folder3/subfolder2/subfolder1/file1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "delete path that doesn't exist",
|
|
|
|
delete: "/path/does/not/exist",
|
|
|
|
expected: []string{},
|
|
|
|
err: errPathNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "delete path invalid: trailing slash",
|
|
|
|
delete: "/path/is/invalid/",
|
|
|
|
expected: []string{},
|
|
|
|
err: errInvalidPath,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "delete path invalid: trailing special character",
|
|
|
|
delete: "/path/is/invalid*",
|
|
|
|
expected: []string{},
|
|
|
|
err: errInvalidPath,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-08-11 18:44:43 +00:00
|
|
|
// create a test case for each file
|
2022-11-02 21:55:22 +00:00
|
|
|
for _, p := range objs {
|
2021-08-11 16:16:43 +00:00
|
|
|
tcs = append(tcs, testCase{
|
2022-11-02 21:55:22 +00:00
|
|
|
name: fmt.Sprintf("delete path:'%s'", p),
|
|
|
|
delete: p,
|
|
|
|
expected: []string{p},
|
2021-08-11 16:16:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
init := func() []string {
|
|
|
|
// init file structure matching objs
|
|
|
|
var created []string
|
2022-11-02 21:55:22 +00:00
|
|
|
for _, p := range objs {
|
|
|
|
err := drvr.PutContent(context.Background(), p, []byte("content "+p))
|
2021-08-11 16:16:43 +00:00
|
|
|
if err != nil {
|
2022-11-02 21:55:22 +00:00
|
|
|
fmt.Printf("unable to init file %s: %s\n", p, err)
|
2021-08-11 16:16:43 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-11-02 21:55:22 +00:00
|
|
|
created = append(created, p)
|
2021-08-11 16:16:43 +00:00
|
|
|
}
|
|
|
|
return created
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup := func(objs []string) {
|
|
|
|
var lastErr error
|
2022-11-02 21:55:22 +00:00
|
|
|
for _, p := range objs {
|
|
|
|
err := drvr.Delete(context.Background(), p)
|
2021-08-11 16:16:43 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case storagedriver.PathNotFoundError:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lastErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if lastErr != nil {
|
|
|
|
t.Fatalf("cleanup failed: %s", lastErr)
|
|
|
|
}
|
|
|
|
}
|
2021-08-11 17:00:06 +00:00
|
|
|
defer cleanup(objs)
|
2021-08-11 16:16:43 +00:00
|
|
|
|
|
|
|
for _, tc := range tcs {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
objs := init()
|
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
err := drvr.Delete(context.Background(), tc.delete)
|
2021-08-11 16:16:43 +00:00
|
|
|
|
|
|
|
if tc.err != nil {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error")
|
|
|
|
}
|
|
|
|
if !tc.err(err) {
|
|
|
|
t.Fatalf("error does not match expected: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tc.err == nil && err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var issues []string
|
|
|
|
|
|
|
|
// validate all files expected to be deleted are deleted
|
|
|
|
// and all files not marked for deletion still remain
|
|
|
|
expected := tc.expected
|
|
|
|
isExpected := func(path string) bool {
|
|
|
|
for _, epath := range expected {
|
|
|
|
if epath == path {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, path := range objs {
|
2022-11-02 21:55:22 +00:00
|
|
|
stat, err := drvr.Stat(context.Background(), path)
|
2021-08-11 16:16:43 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case storagedriver.PathNotFoundError:
|
|
|
|
if !isExpected(path) {
|
|
|
|
issues = append(issues, fmt.Sprintf("unexpected path was deleted: %s", path))
|
|
|
|
}
|
|
|
|
// path was deleted & was supposed to be
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatalf("stat: %s", err)
|
|
|
|
}
|
|
|
|
if stat.IsDir() {
|
|
|
|
// for special cases where an object path has subpaths (eg /file1)
|
|
|
|
// once /file1 is deleted it's now a directory according to stat
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if isExpected(path) {
|
|
|
|
issues = append(issues, fmt.Sprintf("expected path was not deleted: %s", path))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(issues) > 0 {
|
2021-08-11 18:44:43 +00:00
|
|
|
t.Fatalf(strings.Join(issues, "; \n\t"))
|
2021-08-11 16:16:43 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 18:42:02 +00:00
|
|
|
func TestWalk(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
rootDir := t.TempDir()
|
2021-06-24 18:42:02 +00:00
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
drvr, err := s3DriverConstructor(rootDir, s3.StorageClassStandard)
|
2021-06-24 18:42:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver with standard storage: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:05:45 +00:00
|
|
|
fileset := []string{
|
2021-06-24 18:42:02 +00:00
|
|
|
"/file1",
|
2022-07-10 02:04:50 +00:00
|
|
|
"/folder1-suffix/file1",
|
2021-06-24 18:42:02 +00:00
|
|
|
"/folder1/file1",
|
|
|
|
"/folder2/file1",
|
|
|
|
"/folder3/subfolder1/subfolder1/file1",
|
|
|
|
"/folder3/subfolder2/subfolder1/file1",
|
|
|
|
"/folder4/file1",
|
|
|
|
}
|
|
|
|
|
|
|
|
// create file structure matching fileset above
|
2023-09-03 22:26:32 +00:00
|
|
|
created := make([]string, 0, len(fileset))
|
2022-11-02 21:55:22 +00:00
|
|
|
for _, p := range fileset {
|
|
|
|
err := drvr.PutContent(context.Background(), p, []byte("content "+p))
|
2021-06-24 18:42:02 +00:00
|
|
|
if err != nil {
|
2022-11-02 21:55:22 +00:00
|
|
|
fmt.Printf("unable to create file %s: %s\n", p, err)
|
2021-06-24 18:42:02 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-11-02 21:55:22 +00:00
|
|
|
created = append(created, p)
|
2021-06-24 18:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
defer func() {
|
|
|
|
var lastErr error
|
2022-11-02 21:55:22 +00:00
|
|
|
for _, p := range created {
|
|
|
|
err := drvr.Delete(context.Background(), p)
|
2021-06-24 18:42:02 +00:00
|
|
|
if err != nil {
|
2022-11-02 21:55:22 +00:00
|
|
|
_ = fmt.Errorf("cleanup failed for path %s: %s", p, err)
|
2021-06-24 18:42:02 +00:00
|
|
|
lastErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if lastErr != nil {
|
|
|
|
t.Fatalf("cleanup failed: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-07-10 02:04:50 +00:00
|
|
|
noopFn := func(fileInfo storagedriver.FileInfo) error { return nil }
|
|
|
|
|
2021-06-24 18:42:02 +00:00
|
|
|
tcs := []struct {
|
|
|
|
name string
|
|
|
|
fn storagedriver.WalkFn
|
|
|
|
from string
|
2022-07-10 02:04:50 +00:00
|
|
|
options []func(*storagedriver.WalkOptions)
|
2021-06-24 18:42:02 +00:00
|
|
|
expected []string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "walk all",
|
2022-07-10 02:04:50 +00:00
|
|
|
fn: noopFn,
|
2021-06-24 18:42:02 +00:00
|
|
|
expected: []string{
|
|
|
|
"/file1",
|
2022-07-10 02:04:50 +00:00
|
|
|
"/folder1-suffix",
|
|
|
|
"/folder1-suffix/file1",
|
2021-06-24 18:42:02 +00:00
|
|
|
"/folder1",
|
|
|
|
"/folder1/file1",
|
|
|
|
"/folder2",
|
|
|
|
"/folder2/file1",
|
|
|
|
"/folder3",
|
|
|
|
"/folder3/subfolder1",
|
|
|
|
"/folder3/subfolder1/subfolder1",
|
|
|
|
"/folder3/subfolder1/subfolder1/file1",
|
|
|
|
"/folder3/subfolder2",
|
|
|
|
"/folder3/subfolder2/subfolder1",
|
|
|
|
"/folder3/subfolder2/subfolder1/file1",
|
|
|
|
"/folder4",
|
|
|
|
"/folder4/file1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "skip directory",
|
|
|
|
fn: func(fileInfo storagedriver.FileInfo) error {
|
|
|
|
if fileInfo.Path() == "/folder3" {
|
|
|
|
return storagedriver.ErrSkipDir
|
|
|
|
}
|
|
|
|
if strings.Contains(fileInfo.Path(), "/folder3") {
|
|
|
|
t.Fatalf("skipped dir %s and should not walk %s", "/folder3", fileInfo.Path())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"/file1",
|
2022-07-10 02:04:50 +00:00
|
|
|
"/folder1-suffix",
|
|
|
|
"/folder1-suffix/file1",
|
2021-06-24 18:42:02 +00:00
|
|
|
"/folder1",
|
|
|
|
"/folder1/file1",
|
|
|
|
"/folder2",
|
|
|
|
"/folder2/file1",
|
|
|
|
"/folder3",
|
|
|
|
// folder 3 contents skipped
|
|
|
|
"/folder4",
|
|
|
|
"/folder4/file1",
|
|
|
|
},
|
|
|
|
},
|
2022-07-10 02:04:50 +00:00
|
|
|
{
|
|
|
|
name: "start late without from",
|
|
|
|
fn: noopFn,
|
|
|
|
options: []func(*storagedriver.WalkOptions){
|
|
|
|
storagedriver.WithStartAfterHint("/folder3/subfolder1/subfolder1/file1"),
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
// start late
|
|
|
|
"/folder3",
|
|
|
|
"/folder3/subfolder2",
|
|
|
|
"/folder3/subfolder2/subfolder1",
|
|
|
|
"/folder3/subfolder2/subfolder1/file1",
|
|
|
|
"/folder4",
|
|
|
|
"/folder4/file1",
|
|
|
|
},
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "start late with from",
|
|
|
|
fn: noopFn,
|
|
|
|
from: "/folder3",
|
|
|
|
options: []func(*storagedriver.WalkOptions){
|
|
|
|
storagedriver.WithStartAfterHint("/folder3/subfolder1/subfolder1/file1"),
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
// start late
|
|
|
|
"/folder3/subfolder2",
|
|
|
|
"/folder3/subfolder2/subfolder1",
|
|
|
|
"/folder3/subfolder2/subfolder1/file1",
|
|
|
|
},
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "start after from",
|
|
|
|
fn: noopFn,
|
|
|
|
from: "/folder1",
|
|
|
|
options: []func(*storagedriver.WalkOptions){
|
|
|
|
storagedriver.WithStartAfterHint("/folder2"),
|
|
|
|
},
|
|
|
|
expected: []string{},
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "start matches from",
|
|
|
|
fn: noopFn,
|
|
|
|
from: "/folder3",
|
|
|
|
options: []func(*storagedriver.WalkOptions){
|
|
|
|
storagedriver.WithStartAfterHint("/folder3"),
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"/folder3/subfolder1",
|
|
|
|
"/folder3/subfolder1/subfolder1",
|
|
|
|
"/folder3/subfolder1/subfolder1/file1",
|
|
|
|
"/folder3/subfolder2",
|
|
|
|
"/folder3/subfolder2/subfolder1",
|
|
|
|
"/folder3/subfolder2/subfolder1/file1",
|
|
|
|
},
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "start doesn't exist",
|
|
|
|
fn: noopFn,
|
|
|
|
from: "/folder3",
|
|
|
|
options: []func(*storagedriver.WalkOptions){
|
|
|
|
storagedriver.WithStartAfterHint("/folder3/notafolder/notafile"),
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"/folder3/subfolder1",
|
|
|
|
"/folder3/subfolder1/subfolder1",
|
|
|
|
"/folder3/subfolder1/subfolder1/file1",
|
|
|
|
"/folder3/subfolder2",
|
|
|
|
"/folder3/subfolder2/subfolder1",
|
|
|
|
"/folder3/subfolder2/subfolder1/file1",
|
|
|
|
},
|
|
|
|
err: false,
|
|
|
|
},
|
2021-06-24 18:42:02 +00:00
|
|
|
{
|
|
|
|
name: "stop early",
|
|
|
|
fn: func(fileInfo storagedriver.FileInfo) error {
|
|
|
|
if fileInfo.Path() == "/folder1/file1" {
|
2022-07-10 02:04:50 +00:00
|
|
|
return storagedriver.ErrFilledBuffer
|
2021-06-24 18:42:02 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"/file1",
|
2022-07-10 02:04:50 +00:00
|
|
|
"/folder1-suffix",
|
|
|
|
"/folder1-suffix/file1",
|
2021-06-24 18:42:02 +00:00
|
|
|
"/folder1",
|
|
|
|
"/folder1/file1",
|
|
|
|
// stop early
|
|
|
|
},
|
|
|
|
err: false,
|
|
|
|
},
|
2022-07-10 02:04:50 +00:00
|
|
|
|
2021-06-24 18:42:02 +00:00
|
|
|
{
|
|
|
|
name: "error",
|
|
|
|
fn: func(fileInfo storagedriver.FileInfo) error {
|
|
|
|
return errors.New("foo")
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"/file1",
|
|
|
|
},
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "from folder",
|
2022-07-10 02:04:50 +00:00
|
|
|
fn: noopFn,
|
2021-06-24 18:42:02 +00:00
|
|
|
expected: []string{
|
|
|
|
"/folder1/file1",
|
|
|
|
},
|
|
|
|
from: "/folder1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcs {
|
|
|
|
var walked []string
|
|
|
|
if tc.from == "" {
|
|
|
|
tc.from = "/"
|
|
|
|
}
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-11-02 21:55:22 +00:00
|
|
|
err := drvr.Walk(context.Background(), tc.from, func(fileInfo storagedriver.FileInfo) error {
|
2021-06-24 18:42:02 +00:00
|
|
|
walked = append(walked, fileInfo.Path())
|
|
|
|
return tc.fn(fileInfo)
|
2022-07-10 02:04:50 +00:00
|
|
|
}, tc.options...)
|
2021-06-24 18:42:02 +00:00
|
|
|
if tc.err && err == nil {
|
|
|
|
t.Fatalf("expected err")
|
|
|
|
}
|
|
|
|
if !tc.err && err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
compareWalked(t, tc.expected, walked)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 00:39:25 +00:00
|
|
|
func TestOverThousandBlobs(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
rootDir := t.TempDir()
|
2016-06-28 00:39:25 +00:00
|
|
|
standardDriver, err := s3DriverConstructor(rootDir, s3.StorageClassStandard)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver with standard storage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
for i := 0; i < 1005; i++ {
|
|
|
|
filename := "/thousandfiletest/file" + strconv.Itoa(i)
|
|
|
|
contents := []byte("contents")
|
|
|
|
err = standardDriver.PutContent(ctx, filename, contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cant actually verify deletion because read-after-delete is inconsistent, but can ensure no errors
|
|
|
|
err = standardDriver.Delete(ctx, "/thousandfiletest")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error deleting thousand files: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 00:12:24 +00:00
|
|
|
|
|
|
|
func TestMoveWithMultipartCopy(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
rootDir := t.TempDir()
|
2016-08-16 00:12:24 +00:00
|
|
|
d, err := s3DriverConstructor(rootDir, s3.StorageClassStandard)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
sourcePath := "/source"
|
|
|
|
destPath := "/dest"
|
|
|
|
|
|
|
|
defer d.Delete(ctx, sourcePath)
|
|
|
|
defer d.Delete(ctx, destPath)
|
|
|
|
|
|
|
|
// An object larger than d's MultipartCopyThresholdSize will cause d.Move() to perform a multipart copy.
|
|
|
|
multipartCopyThresholdSize := d.baseEmbed.Base.StorageDriver.(*driver).MultipartCopyThresholdSize
|
|
|
|
contents := make([]byte, 2*multipartCopyThresholdSize)
|
|
|
|
rand.Read(contents)
|
|
|
|
|
|
|
|
err = d.PutContent(ctx, sourcePath, contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.Move(ctx, sourcePath, destPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error moving file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
received, err := d.GetContent(ctx, destPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error getting content: %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(contents, received) {
|
|
|
|
t.Fatal("content differs")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.GetContent(ctx, sourcePath)
|
|
|
|
switch err.(type) {
|
|
|
|
case storagedriver.PathNotFoundError:
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected error getting content: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 18:42:02 +00:00
|
|
|
|
2022-04-09 11:16:46 +00:00
|
|
|
func TestListObjectsV2(t *testing.T) {
|
2022-04-23 11:37:09 +00:00
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
rootDir := t.TempDir()
|
2022-04-09 11:16:46 +00:00
|
|
|
d, err := s3DriverConstructor(rootDir, s3.StorageClassStandard)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
n := 6
|
|
|
|
prefix := "/test-list-objects-v2"
|
|
|
|
var filePaths []string
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
filePaths = append(filePaths, fmt.Sprintf("%s/%d", prefix, i))
|
|
|
|
}
|
2022-11-02 21:55:22 +00:00
|
|
|
for _, p := range filePaths {
|
|
|
|
if err := d.PutContent(ctx, p, []byte(p)); err != nil {
|
2022-04-09 11:16:46 +00:00
|
|
|
t.Fatalf("unexpected error putting content: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := d.Stat(ctx, filePaths[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error stating: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() || info.Size() != int64(len(filePaths[0])) || info.Path() != filePaths[0] {
|
|
|
|
t.Fatal("unexcepted state info")
|
|
|
|
}
|
|
|
|
|
|
|
|
subDirPath := prefix + "/sub/0"
|
|
|
|
if err := d.PutContent(ctx, subDirPath, []byte(subDirPath)); err != nil {
|
|
|
|
t.Fatalf("unexpected error putting content: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
subPaths := append(filePaths, path.Dir(subDirPath))
|
|
|
|
|
|
|
|
result, err := d.List(ctx, prefix)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error listing: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(subPaths)
|
|
|
|
sort.Strings(result)
|
|
|
|
if !reflect.DeepEqual(subPaths, result) {
|
|
|
|
t.Fatalf("unexpected list result")
|
|
|
|
}
|
|
|
|
|
2022-04-09 11:31:27 +00:00
|
|
|
var walkPaths []string
|
2022-04-09 11:16:46 +00:00
|
|
|
if err := d.Walk(ctx, prefix, func(fileInfo storagedriver.FileInfo) error {
|
2022-04-09 11:31:27 +00:00
|
|
|
walkPaths = append(walkPaths, fileInfo.Path())
|
2022-04-09 11:16:46 +00:00
|
|
|
if fileInfo.Path() == path.Dir(subDirPath) {
|
|
|
|
if !fileInfo.IsDir() {
|
|
|
|
t.Fatalf("unexpected walking file info")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if fileInfo.IsDir() || fileInfo.Size() != int64(len(fileInfo.Path())) {
|
|
|
|
t.Fatalf("unexpected walking file info")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("unexpected error walking: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
subPaths = append(subPaths, subDirPath)
|
2022-04-09 11:31:27 +00:00
|
|
|
sort.Strings(walkPaths)
|
2022-04-09 11:16:46 +00:00
|
|
|
sort.Strings(subPaths)
|
2022-04-09 11:31:27 +00:00
|
|
|
if !reflect.DeepEqual(subPaths, walkPaths) {
|
|
|
|
t.Fatalf("unexpected walking paths")
|
2022-04-09 11:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.Delete(ctx, prefix); err != nil {
|
|
|
|
t.Fatalf("unexpected error deleting: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 18:42:02 +00:00
|
|
|
func compareWalked(t *testing.T, expected, walked []string) {
|
|
|
|
if len(walked) != len(expected) {
|
|
|
|
t.Fatalf("Mismatch number of fileInfo walked %d expected %d; walked %s; expected %s;", len(walked), len(expected), walked, expected)
|
|
|
|
}
|
|
|
|
for i := range walked {
|
|
|
|
if walked[i] != expected[i] {
|
|
|
|
t.Fatalf("walked in unexpected order: expected %s; walked %s", expected, walked)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|