2015-05-11 16:11:47 +00:00
|
|
|
package swift
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-08-24 17:09:25 +00:00
|
|
|
"reflect"
|
2015-06-08 08:37:11 +00:00
|
|
|
"strconv"
|
2015-11-03 08:59:50 +00:00
|
|
|
"strings"
|
2015-05-11 16:11:47 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-05-23 07:22:41 +00:00
|
|
|
"github.com/ncw/swift/swifttest"
|
2015-05-11 16:11:47 +00:00
|
|
|
|
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"
|
2015-05-11 16:11:47 +00:00
|
|
|
|
|
|
|
"gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Hook up gocheck into the "go test" runner.
|
|
|
|
func Test(t *testing.T) { check.TestingT(t) }
|
|
|
|
|
2015-06-30 21:09:02 +00:00
|
|
|
var swiftDriverConstructor func(prefix string) (*Driver, error)
|
2015-05-11 16:11:47 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
var (
|
2022-11-02 21:05:45 +00:00
|
|
|
username = os.Getenv("SWIFT_USERNAME")
|
|
|
|
password = os.Getenv("SWIFT_PASSWORD")
|
|
|
|
authURL = os.Getenv("SWIFT_AUTH_URL")
|
|
|
|
tenant = os.Getenv("SWIFT_TENANT_NAME")
|
|
|
|
tenantID = os.Getenv("SWIFT_TENANT_ID")
|
|
|
|
domain = os.Getenv("SWIFT_DOMAIN_NAME")
|
|
|
|
domainID = os.Getenv("SWIFT_DOMAIN_ID")
|
|
|
|
tenantDomain = os.Getenv("SWIFT_DOMAIN_NAME")
|
|
|
|
tenantDomainID = os.Getenv("SWIFT_DOMAIN_ID")
|
|
|
|
trustID = os.Getenv("SWIFT_TRUST_ID")
|
|
|
|
container = os.Getenv("SWIFT_CONTAINER_NAME")
|
|
|
|
region = os.Getenv("SWIFT_REGION_NAME")
|
|
|
|
AuthVersion, _ = strconv.Atoi(os.Getenv("SWIFT_AUTH_VERSION"))
|
|
|
|
endpointType = os.Getenv("SWIFT_ENDPOINT_TYPE")
|
|
|
|
insecureSkipVerify, _ = strconv.ParseBool(os.Getenv("SWIFT_INSECURESKIPVERIFY"))
|
|
|
|
secretKey = os.Getenv("SWIFT_SECRET_KEY")
|
|
|
|
accessKey = os.Getenv("SWIFT_ACCESS_KEY")
|
|
|
|
containerKey, _ = strconv.ParseBool(os.Getenv("SWIFT_TEMPURL_CONTAINERKEY"))
|
|
|
|
tempURLMethods = strings.Split(os.Getenv("SWIFT_TEMPURL_METHODS"), ",")
|
2015-11-03 08:59:50 +00:00
|
|
|
|
|
|
|
swiftServer *swifttest.SwiftServer
|
|
|
|
err error
|
2015-05-11 16:11:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if username == "" || password == "" || authURL == "" || container == "" {
|
|
|
|
if swiftServer, err = swifttest.NewSwiftServer("localhost"); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
username = "swifttest"
|
|
|
|
password = "swifttest"
|
|
|
|
authURL = swiftServer.AuthURL
|
|
|
|
container = "test"
|
|
|
|
}
|
|
|
|
|
2015-06-30 21:09:02 +00:00
|
|
|
prefix, err := ioutil.TempDir("", "driver-")
|
2015-05-11 16:11:47 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-06-30 21:09:02 +00:00
|
|
|
defer os.Remove(prefix)
|
2015-05-11 16:11:47 +00:00
|
|
|
|
2015-06-30 21:09:02 +00:00
|
|
|
swiftDriverConstructor = func(root string) (*Driver, error) {
|
2015-06-30 12:22:41 +00:00
|
|
|
parameters := Parameters{
|
2015-05-11 16:11:47 +00:00
|
|
|
username,
|
|
|
|
password,
|
|
|
|
authURL,
|
|
|
|
tenant,
|
2015-06-08 08:37:11 +00:00
|
|
|
tenantID,
|
|
|
|
domain,
|
|
|
|
domainID,
|
2016-08-15 09:21:16 +00:00
|
|
|
tenantDomain,
|
|
|
|
tenantDomainID,
|
2015-08-24 15:08:33 +00:00
|
|
|
trustID,
|
2015-05-11 16:11:47 +00:00
|
|
|
region,
|
2016-04-13 15:37:45 +00:00
|
|
|
AuthVersion,
|
2015-05-11 16:11:47 +00:00
|
|
|
container,
|
2015-06-30 21:09:02 +00:00
|
|
|
root,
|
2016-03-07 21:41:20 +00:00
|
|
|
endpointType,
|
2015-06-08 08:37:11 +00:00
|
|
|
insecureSkipVerify,
|
2015-05-11 16:11:47 +00:00
|
|
|
defaultChunkSize,
|
2015-11-03 08:59:50 +00:00
|
|
|
secretKey,
|
|
|
|
accessKey,
|
|
|
|
containerKey,
|
|
|
|
tempURLMethods,
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return New(parameters)
|
|
|
|
}
|
|
|
|
|
|
|
|
driverConstructor := func() (storagedriver.StorageDriver, error) {
|
2015-06-30 21:09:02 +00:00
|
|
|
return swiftDriverConstructor(prefix)
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 06:02:51 +00:00
|
|
|
testsuites.RegisterSuite(driverConstructor, testsuites.NeverSkip)
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 21:09:02 +00:00
|
|
|
func TestEmptyRootList(t *testing.T) {
|
|
|
|
validRoot, err := ioutil.TempDir("", "driver-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating temporary directory: %v", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(validRoot)
|
2015-05-11 16:11:47 +00:00
|
|
|
|
2015-06-30 21:09:02 +00:00
|
|
|
rootedDriver, err := swiftDriverConstructor(validRoot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating rooted driver: %v", err)
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 21:09:02 +00:00
|
|
|
emptyRootDriver, err := swiftDriverConstructor("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating empty root driver: %v", err)
|
|
|
|
}
|
2015-05-11 16:11:47 +00:00
|
|
|
|
2015-06-30 21:09:02 +00:00
|
|
|
slashRootDriver, err := swiftDriverConstructor("/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating slash root driver: %v", err)
|
|
|
|
}
|
2015-05-11 16:11:47 +00:00
|
|
|
|
|
|
|
filename := "/test"
|
|
|
|
contents := []byte("contents")
|
|
|
|
ctx := context.Background()
|
|
|
|
err = rootedDriver.PutContent(ctx, filename, contents)
|
2015-06-30 21:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
2015-05-11 16:11:47 +00:00
|
|
|
|
2018-08-06 21:34:15 +00:00
|
|
|
keys, _ := emptyRootDriver.List(ctx, "/")
|
2015-05-11 16:11:47 +00:00
|
|
|
for _, path := range keys {
|
2015-06-30 21:09:02 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
|
|
|
|
}
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:34:15 +00:00
|
|
|
keys, _ = slashRootDriver.List(ctx, "/")
|
2015-05-11 16:11:47 +00:00
|
|
|
for _, path := range keys {
|
2015-06-30 21:09:02 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
|
|
|
|
}
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
2015-10-30 16:08:56 +00:00
|
|
|
|
|
|
|
// Create an object with a path nested under the existing object
|
|
|
|
err = rootedDriver.PutContent(ctx, filename+"/file1", contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rootedDriver.Delete(ctx, filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to delete: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys, err = rootedDriver.List(ctx, "/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to list objects after deletion: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(keys) != 0 {
|
|
|
|
t.Fatal("delete did not remove nested objects")
|
|
|
|
}
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
2016-08-24 17:09:25 +00:00
|
|
|
|
|
|
|
func TestFilenameChunking(t *testing.T) {
|
|
|
|
// Test valid input and sizes
|
|
|
|
input := []string{"a", "b", "c", "d", "e"}
|
|
|
|
expecteds := [][][]string{
|
|
|
|
{
|
|
|
|
{"a"},
|
|
|
|
{"b"},
|
|
|
|
{"c"},
|
|
|
|
{"d"},
|
|
|
|
{"e"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"a", "b"},
|
|
|
|
{"c", "d"},
|
|
|
|
{"e"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"a", "b", "c"},
|
|
|
|
{"d", "e"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"a", "b", "c", "d"},
|
|
|
|
{"e"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"a", "b", "c", "d", "e"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"a", "b", "c", "d", "e"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, expected := range expecteds {
|
|
|
|
actual, err := chunkFilenames(input, i+1)
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("chunk %v didn't match expected value %v", actual, expected)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error chunking filenames: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test nil input
|
|
|
|
actual, err := chunkFilenames(nil, 5)
|
|
|
|
if len(actual) != 0 {
|
|
|
|
t.Fatal("chunks were returned when passed nil")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error chunking filenames: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test 0 and < 0 sizes
|
2018-08-06 21:34:15 +00:00
|
|
|
_, err = chunkFilenames(nil, 0)
|
2016-08-24 17:09:25 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error for size = 0")
|
|
|
|
}
|
2018-08-06 21:34:15 +00:00
|
|
|
_, err = chunkFilenames(nil, -1)
|
2016-08-24 17:09:25 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error for size = -1")
|
|
|
|
}
|
|
|
|
}
|
2019-06-23 06:44:43 +00:00
|
|
|
|
|
|
|
func TestSwiftSegmentPath(t *testing.T) {
|
|
|
|
d := &driver{
|
|
|
|
Prefix: "/test/segment/path",
|
|
|
|
}
|
|
|
|
|
|
|
|
s1, err := d.swiftSegmentPath("foo-baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error generating segment path: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s2, err := d.swiftSegmentPath("foo-baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error generating segment path: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(s1, "test/segment/path/segments/") {
|
|
|
|
t.Fatalf("expected to be prefixed: %s", s1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(s1, "test/segment/path/segments/") {
|
|
|
|
t.Fatalf("expected to be prefixed: %s", s2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s1) != 68 {
|
|
|
|
t.Fatalf("unexpected segment path length, %d != %d", len(s1), 68)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s2) != 68 {
|
|
|
|
t.Fatalf("unexpected segment path length, %d != %d", len(s2), 68)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s1 == s2 {
|
|
|
|
t.Fatalf("expected segment paths to differ, %s == %s", s1, s2)
|
|
|
|
}
|
|
|
|
}
|