2014-10-25 01:37:25 +02:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
2016-08-16 02:12:24 +02:00
|
|
|
"bytes"
|
2021-06-24 20:42:02 +02:00
|
|
|
"errors"
|
2021-08-11 18:16:43 +02:00
|
|
|
"fmt"
|
2014-12-19 18:16:51 +01:00
|
|
|
"io/ioutil"
|
2016-08-16 02:12:24 +02:00
|
|
|
"math/rand"
|
2014-10-25 01:37:25 +02:00
|
|
|
"os"
|
2021-08-12 16:54:11 +02:00
|
|
|
"reflect"
|
2014-10-29 02:15:40 +01:00
|
|
|
"strconv"
|
2021-08-11 18:16:43 +02:00
|
|
|
"strings"
|
2014-10-25 01:37:25 +02:00
|
|
|
"testing"
|
|
|
|
|
2016-08-16 02:12:24 +02:00
|
|
|
"gopkg.in/check.v1"
|
|
|
|
|
2016-01-22 03:17:53 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
|
|
2020-08-24 13:18:39 +02: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-25 01:37:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Hook up gocheck into the "go test" runner.
|
2014-11-18 00:44:07 +01:00
|
|
|
func Test(t *testing.T) { check.TestingT(t) }
|
2014-10-25 01:37:25 +02:00
|
|
|
|
2016-01-22 03:17:53 +01:00
|
|
|
var s3DriverConstructor func(rootDirectory, storageClass string) (*Driver, error)
|
2015-06-30 01:39:45 +02:00
|
|
|
var skipS3 func() string
|
2015-02-20 01:28:32 +01:00
|
|
|
|
2014-10-25 01:37:25 +02:00
|
|
|
func init() {
|
2014-10-29 02:15:40 +01:00
|
|
|
accessKey := os.Getenv("AWS_ACCESS_KEY")
|
|
|
|
secretKey := os.Getenv("AWS_SECRET_KEY")
|
2014-10-25 01:37:25 +02:00
|
|
|
bucket := os.Getenv("S3_BUCKET")
|
|
|
|
encrypt := os.Getenv("S3_ENCRYPT")
|
2016-03-10 01:52:59 +01:00
|
|
|
keyID := os.Getenv("S3_KEY_ID")
|
2014-12-29 21:29:54 +01:00
|
|
|
secure := os.Getenv("S3_SECURE")
|
2017-03-03 20:54:16 +01:00
|
|
|
skipVerify := os.Getenv("S3_SKIP_VERIFY")
|
2016-09-01 22:52:40 +02:00
|
|
|
v4Auth := os.Getenv("S3_V4_AUTH")
|
2014-12-19 18:16:51 +01:00
|
|
|
region := os.Getenv("AWS_REGION")
|
2016-10-06 02:47:12 +02:00
|
|
|
objectACL := os.Getenv("S3_OBJECT_ACL")
|
2014-12-19 18:16:51 +01:00
|
|
|
root, err := ioutil.TempDir("", "driver-")
|
2016-03-05 19:46:44 +01:00
|
|
|
regionEndpoint := os.Getenv("REGION_ENDPOINT")
|
2017-03-27 22:04:00 +02:00
|
|
|
sessionToken := os.Getenv("AWS_SESSION_TOKEN")
|
2014-12-19 18:16:51 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-01-07 11:18:42 +01:00
|
|
|
defer os.Remove(root)
|
2014-10-25 01:37:25 +02:00
|
|
|
|
2016-01-22 03:17:53 +01:00
|
|
|
s3DriverConstructor = func(rootDirectory, storageClass string) (*Driver, error) {
|
2015-01-07 11:18:42 +01:00
|
|
|
encryptBool := false
|
2015-01-07 10:45:31 +01:00
|
|
|
if encrypt != "" {
|
|
|
|
encryptBool, err = strconv.ParseBool(encrypt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-29 02:15:40 +01:00
|
|
|
}
|
2014-12-29 21:29:54 +01:00
|
|
|
|
|
|
|
secureBool := true
|
|
|
|
if secure != "" {
|
|
|
|
secureBool, err = strconv.ParseBool(secure)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-01-07 10:45:31 +01:00
|
|
|
|
2017-03-03 20:54:16 +01:00
|
|
|
skipVerifyBool := false
|
|
|
|
if skipVerify != "" {
|
|
|
|
skipVerifyBool, err = strconv.ParseBool(skipVerify)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 22:52:40 +02:00
|
|
|
v4Bool := true
|
|
|
|
if v4Auth != "" {
|
|
|
|
v4Bool, err = strconv.ParseBool(v4Auth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 11:18:42 +01:00
|
|
|
parameters := DriverParameters{
|
|
|
|
accessKey,
|
|
|
|
secretKey,
|
|
|
|
bucket,
|
2016-01-22 03:17:53 +01:00
|
|
|
region,
|
2016-03-05 19:46:44 +01:00
|
|
|
regionEndpoint,
|
2015-01-07 11:18:42 +01:00
|
|
|
encryptBool,
|
2016-03-10 01:52:59 +01:00
|
|
|
keyID,
|
2015-01-07 11:18:42 +01:00
|
|
|
secureBool,
|
2017-03-03 20:54:16 +01:00
|
|
|
skipVerifyBool,
|
2016-09-01 22:52:40 +02:00
|
|
|
v4Bool,
|
2015-01-24 01:46:43 +01:00
|
|
|
minChunkSize,
|
2016-08-16 02:12:24 +02:00
|
|
|
defaultMultipartCopyChunkSize,
|
|
|
|
defaultMultipartCopyMaxConcurrency,
|
|
|
|
defaultMultipartCopyThresholdSize,
|
2015-02-20 01:28:32 +01:00
|
|
|
rootDirectory,
|
2016-02-02 00:34:36 +01:00
|
|
|
storageClass,
|
2016-01-22 03:17:53 +01:00
|
|
|
driverName + "-test",
|
2016-10-06 02:47:12 +02:00
|
|
|
objectACL,
|
2017-03-27 22:04:00 +02:00
|
|
|
sessionToken,
|
2015-01-07 11:18:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return New(parameters)
|
2014-10-25 01:37:25 +02:00
|
|
|
}
|
|
|
|
|
2014-10-29 02:15:40 +01:00
|
|
|
// Skip S3 storage driver tests if environment variable parameters are not provided
|
2015-06-30 01:39:45 +02:00
|
|
|
skipS3 = func() string {
|
2015-02-04 20:39:41 +01: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 21:24:07 +01:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-06-30 01:39:45 +02:00
|
|
|
testsuites.RegisterSuite(func() (storagedriver.StorageDriver, error) {
|
2016-01-22 03:17:53 +01:00
|
|
|
return s3DriverConstructor(root, s3.StorageClassStandard)
|
2015-06-30 01:39:45 +02:00
|
|
|
}, skipS3)
|
2015-02-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
2015-06-30 01:39:45 +02:00
|
|
|
func TestEmptyRootList(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
2015-02-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
validRoot, err := ioutil.TempDir("", "driver-")
|
2015-06-30 01:39:45 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating temporary directory: %v", err)
|
|
|
|
}
|
2015-02-20 01:28:32 +01:00
|
|
|
defer os.Remove(validRoot)
|
|
|
|
|
2016-01-22 03:17:53 +01:00
|
|
|
rootedDriver, err := s3DriverConstructor(validRoot, s3.StorageClassStandard)
|
2015-06-30 01:39:45 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating rooted driver: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-01-22 03:17:53 +01:00
|
|
|
emptyRootDriver, err := s3DriverConstructor("", s3.StorageClassStandard)
|
2015-06-30 01:39:45 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating empty root driver: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-01-22 03:17:53 +01:00
|
|
|
slashRootDriver, err := s3DriverConstructor("/", s3.StorageClassStandard)
|
2015-06-30 01:39:45 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating slash root driver: %v", err)
|
|
|
|
}
|
2015-02-20 01:28:32 +01:00
|
|
|
|
|
|
|
filename := "/test"
|
|
|
|
contents := []byte("contents")
|
2015-04-28 00:58:58 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
err = rootedDriver.PutContent(ctx, filename, contents)
|
2015-06-30 01:39:45 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
2015-04-28 00:58:58 +02:00
|
|
|
defer rootedDriver.Delete(ctx, filename)
|
2015-02-20 01:28:32 +01:00
|
|
|
|
2018-08-06 23:34:15 +02:00
|
|
|
keys, _ := emptyRootDriver.List(ctx, "/")
|
2015-02-20 01:28:32 +01:00
|
|
|
for _, path := range keys {
|
2015-06-30 01:39:45 +02:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
|
|
|
|
}
|
2015-02-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
2018-08-06 23:34:15 +02:00
|
|
|
keys, _ = slashRootDriver.List(ctx, "/")
|
2015-02-20 01:28:32 +01:00
|
|
|
for _, path := range keys {
|
2015-06-30 01:39:45 +02:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
|
|
|
|
}
|
2015-02-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-02 00:34:36 +01:00
|
|
|
|
2020-11-30 13:04:14 +01:00
|
|
|
// TestWalkEmptySubDirectory assures we list an empty sub directory only once when walking
|
|
|
|
// through its parent directory.
|
|
|
|
func TestWalkEmptySubDirectory(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
|
|
|
drv, err := s3DriverConstructor("", s3.StorageClassStandard)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating rooted driver: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an empty sub directory.
|
|
|
|
s3driver := drv.StorageDriver.(*driver)
|
|
|
|
if _, err := s3driver.S3.PutObject(&s3.PutObjectInput{
|
|
|
|
Bucket: aws.String(os.Getenv("S3_BUCKET")),
|
|
|
|
Key: aws.String("/testdir/emptydir/"),
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("error creating empty directory: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketFiles := []string{}
|
|
|
|
s3driver.Walk(context.Background(), "/testdir", func(fileInfo storagedriver.FileInfo) error {
|
|
|
|
bucketFiles = append(bucketFiles, fileInfo.Path())
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := []string{"/testdir/emptydir"}
|
|
|
|
if !reflect.DeepEqual(bucketFiles, expected) {
|
|
|
|
t.Errorf("expecting files %+v, found %+v instead", expected, bucketFiles)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 00:34:36 +01:00
|
|
|
func TestStorageClass(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
|
|
|
rootDir, err := ioutil.TempDir("", "driver-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating temporary directory: %v", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(rootDir)
|
|
|
|
|
2016-01-22 03:17:53 +01:00
|
|
|
standardDriver, err := s3DriverConstructor(rootDir, s3.StorageClassStandard)
|
2016-02-02 00:34:36 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver with standard storage: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-01-22 03:17:53 +01:00
|
|
|
rrDriver, err := s3DriverConstructor(rootDir, s3.StorageClassReducedRedundancy)
|
2016-02-02 00:34:36 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver with reduced redundancy storage: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-09-01 22:52:40 +02:00
|
|
|
if _, err = s3DriverConstructor(rootDir, noStorageClass); err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver without storage class: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-02-02 00:34:36 +01:00
|
|
|
standardFilename := "/test-standard"
|
|
|
|
rrFilename := "/test-rr"
|
|
|
|
contents := []byte("contents")
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
err = standardDriver.PutContent(ctx, standardFilename, contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
|
|
|
defer standardDriver.Delete(ctx, standardFilename)
|
|
|
|
|
|
|
|
err = rrDriver.PutContent(ctx, rrFilename, contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
|
|
|
defer rrDriver.Delete(ctx, rrFilename)
|
|
|
|
|
|
|
|
standardDriverUnwrapped := standardDriver.Base.StorageDriver.(*driver)
|
2016-01-22 03:17:53 +01:00
|
|
|
resp, err := standardDriverUnwrapped.S3.GetObject(&s3.GetObjectInput{
|
|
|
|
Bucket: aws.String(standardDriverUnwrapped.Bucket),
|
|
|
|
Key: aws.String(standardDriverUnwrapped.s3Path(standardFilename)),
|
|
|
|
})
|
2016-02-02 00:34:36 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error retrieving standard storage file: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
// Amazon only populates this header value for non-standard storage classes
|
2016-01-22 03:17:53 +01:00
|
|
|
if resp.StorageClass != nil {
|
|
|
|
t.Fatalf("unexpected storage class for standard file: %v", resp.StorageClass)
|
2016-02-02 00:34:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rrDriverUnwrapped := rrDriver.Base.StorageDriver.(*driver)
|
2016-01-22 03:17:53 +01:00
|
|
|
resp, err = rrDriverUnwrapped.S3.GetObject(&s3.GetObjectInput{
|
|
|
|
Bucket: aws.String(rrDriverUnwrapped.Bucket),
|
2016-02-17 02:48:07 +01:00
|
|
|
Key: aws.String(rrDriverUnwrapped.s3Path(rrFilename)),
|
2016-01-22 03:17:53 +01:00
|
|
|
})
|
2016-02-02 00:34:36 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error retrieving reduced-redundancy storage file: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2016-01-22 03:17:53 +01:00
|
|
|
if resp.StorageClass == nil {
|
|
|
|
t.Fatalf("unexpected storage class for reduced-redundancy file: %v", s3.StorageClassStandard)
|
|
|
|
} else if *resp.StorageClass != s3.StorageClassReducedRedundancy {
|
|
|
|
t.Fatalf("unexpected storage class for reduced-redundancy file: %v", *resp.StorageClass)
|
2016-02-02 00:34:36 +01:00
|
|
|
}
|
2016-01-22 03:17:53 +01:00
|
|
|
|
2016-02-02 00:34:36 +01:00
|
|
|
}
|
2016-06-28 02:39:25 +02:00
|
|
|
|
2021-08-11 18:16:43 +02:00
|
|
|
func TestDelete(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
|
|
|
rootDir, err := ioutil.TempDir("", "driver-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating temporary directory: %v", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(rootDir)
|
|
|
|
|
|
|
|
driver, err := s3DriverConstructor(rootDir, s3.StorageClassStandard)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-11 20:44:43 +02:00
|
|
|
var objs = []string{
|
|
|
|
"/file1",
|
|
|
|
"/file1-2",
|
|
|
|
"/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 18:16:43 +02:00
|
|
|
tcs := []testCase{
|
|
|
|
{
|
|
|
|
// special case where a given path is a file and has subpaths
|
|
|
|
name: "delete file1",
|
|
|
|
delete: "/file1",
|
|
|
|
expected: []string{
|
|
|
|
"/file1",
|
|
|
|
"/file1/2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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 20:44:43 +02:00
|
|
|
// objects to skip auto-created test case
|
|
|
|
var skipCase = map[string]bool{
|
|
|
|
// special case where deleting "/file1" also deletes "/file1/2" is tested explicitly
|
|
|
|
"/file1": true,
|
|
|
|
}
|
|
|
|
// create a test case for each file
|
2021-08-11 18:16:43 +02:00
|
|
|
for _, path := range objs {
|
|
|
|
if skipCase[path] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tcs = append(tcs, testCase{
|
|
|
|
name: fmt.Sprintf("delete path:'%s'", path),
|
|
|
|
delete: path,
|
|
|
|
expected: []string{path},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
init := func() []string {
|
|
|
|
// init file structure matching objs
|
|
|
|
var created []string
|
|
|
|
for _, path := range objs {
|
|
|
|
err := driver.PutContent(context.Background(), path, []byte("content "+path))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("unable to init file %s: %s\n", path, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
created = append(created, path)
|
|
|
|
}
|
|
|
|
return created
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup := func(objs []string) {
|
|
|
|
var lastErr error
|
|
|
|
for _, path := range objs {
|
|
|
|
err := driver.Delete(context.Background(), path)
|
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case storagedriver.PathNotFoundError:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lastErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if lastErr != nil {
|
|
|
|
t.Fatalf("cleanup failed: %s", lastErr)
|
|
|
|
}
|
|
|
|
}
|
2021-08-11 19:00:06 +02:00
|
|
|
defer cleanup(objs)
|
2021-08-11 18:16:43 +02:00
|
|
|
|
|
|
|
for _, tc := range tcs {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
objs := init()
|
|
|
|
|
|
|
|
err := driver.Delete(context.Background(), tc.delete)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
stat, err := driver.Stat(context.Background(), path)
|
|
|
|
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 20:44:43 +02:00
|
|
|
t.Fatalf(strings.Join(issues, "; \n\t"))
|
2021-08-11 18:16:43 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 20:42:02 +02:00
|
|
|
func TestWalk(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
|
|
|
rootDir, err := ioutil.TempDir("", "driver-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating temporary directory: %v", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(rootDir)
|
|
|
|
|
|
|
|
driver, err := s3DriverConstructor(rootDir, s3.StorageClassStandard)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver with standard storage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var fileset = []string{
|
|
|
|
"/file1",
|
|
|
|
"/folder1/file1",
|
|
|
|
"/folder2/file1",
|
|
|
|
"/folder3/subfolder1/subfolder1/file1",
|
|
|
|
"/folder3/subfolder2/subfolder1/file1",
|
|
|
|
"/folder4/file1",
|
|
|
|
}
|
|
|
|
|
|
|
|
// create file structure matching fileset above
|
|
|
|
var created []string
|
|
|
|
for _, path := range fileset {
|
|
|
|
err := driver.PutContent(context.Background(), path, []byte("content "+path))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("unable to create file %s: %s\n", path, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
created = append(created, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
defer func() {
|
|
|
|
var lastErr error
|
|
|
|
for _, path := range created {
|
|
|
|
err := driver.Delete(context.Background(), path)
|
|
|
|
if err != nil {
|
|
|
|
_ = fmt.Errorf("cleanup failed for path %s: %s", path, err)
|
|
|
|
lastErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if lastErr != nil {
|
|
|
|
t.Fatalf("cleanup failed: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
tcs := []struct {
|
|
|
|
name string
|
|
|
|
fn storagedriver.WalkFn
|
|
|
|
from string
|
|
|
|
expected []string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "walk all",
|
|
|
|
fn: func(fileInfo storagedriver.FileInfo) error { return nil },
|
|
|
|
expected: []string{
|
|
|
|
"/file1",
|
|
|
|
"/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",
|
|
|
|
"/folder1",
|
|
|
|
"/folder1/file1",
|
|
|
|
"/folder2",
|
|
|
|
"/folder2/file1",
|
|
|
|
"/folder3",
|
|
|
|
// folder 3 contents skipped
|
|
|
|
"/folder4",
|
|
|
|
"/folder4/file1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "stop early",
|
|
|
|
fn: func(fileInfo storagedriver.FileInfo) error {
|
|
|
|
if fileInfo.Path() == "/folder1/file1" {
|
|
|
|
return storagedriver.ErrSkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"/file1",
|
|
|
|
"/folder1",
|
|
|
|
"/folder1/file1",
|
|
|
|
// stop early
|
|
|
|
},
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error",
|
|
|
|
fn: func(fileInfo storagedriver.FileInfo) error {
|
|
|
|
return errors.New("foo")
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"/file1",
|
|
|
|
},
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "from folder",
|
|
|
|
fn: func(fileInfo storagedriver.FileInfo) error { return nil },
|
|
|
|
expected: []string{
|
|
|
|
"/folder1",
|
|
|
|
"/folder1/file1",
|
|
|
|
},
|
|
|
|
from: "/folder1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcs {
|
|
|
|
var walked []string
|
|
|
|
if tc.from == "" {
|
|
|
|
tc.from = "/"
|
|
|
|
}
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err := driver.Walk(context.Background(), tc.from, func(fileInfo storagedriver.FileInfo) error {
|
|
|
|
walked = append(walked, fileInfo.Path())
|
|
|
|
return tc.fn(fileInfo)
|
|
|
|
})
|
|
|
|
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 02:39:25 +02:00
|
|
|
func TestOverThousandBlobs(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
|
|
|
rootDir, err := ioutil.TempDir("", "driver-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating temporary directory: %v", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(rootDir)
|
|
|
|
|
|
|
|
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 02:12:24 +02:00
|
|
|
|
|
|
|
func TestMoveWithMultipartCopy(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
|
|
|
rootDir, err := ioutil.TempDir("", "driver-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating temporary directory: %v", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(rootDir)
|
|
|
|
|
|
|
|
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 20:42:02 +02: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|