2015-05-11 16:11:47 +00:00
|
|
|
package swift
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
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
|
|
|
|
|
|
|
"github.com/docker/distribution/context"
|
|
|
|
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
|
|
|
"github.com/docker/distribution/registry/storage/driver/testsuites"
|
|
|
|
|
|
|
|
"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 (
|
2015-06-08 08:37:11 +00:00
|
|
|
username string
|
|
|
|
password string
|
|
|
|
authURL string
|
|
|
|
tenant string
|
|
|
|
tenantID string
|
|
|
|
domain string
|
|
|
|
domainID string
|
2015-08-24 15:08:33 +00:00
|
|
|
trustID string
|
2015-06-08 08:37:11 +00:00
|
|
|
container string
|
|
|
|
region string
|
2016-04-13 15:37:45 +00:00
|
|
|
AuthVersion int
|
2015-06-08 08:37:11 +00:00
|
|
|
insecureSkipVerify bool
|
2015-11-03 08:59:50 +00:00
|
|
|
secretKey string
|
|
|
|
accessKey string
|
|
|
|
containerKey bool
|
|
|
|
tempURLMethods []string
|
|
|
|
|
|
|
|
swiftServer *swifttest.SwiftServer
|
|
|
|
err error
|
2015-05-11 16:11:47 +00:00
|
|
|
)
|
2015-06-30 12:24:16 +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")
|
2015-08-24 15:08:33 +00:00
|
|
|
trustID = os.Getenv("SWIFT_TRUST_ID")
|
2015-06-30 12:24:16 +00:00
|
|
|
container = os.Getenv("SWIFT_CONTAINER_NAME")
|
|
|
|
region = os.Getenv("SWIFT_REGION_NAME")
|
2016-04-13 16:06:33 +00:00
|
|
|
AuthVersion = strconv.Atoi(os.Getenv("SWIFT_AUTH_VERSION"))
|
2015-06-30 12:24:16 +00:00
|
|
|
insecureSkipVerify, _ = strconv.ParseBool(os.Getenv("SWIFT_INSECURESKIPVERIFY"))
|
2015-11-03 08:59:50 +00:00
|
|
|
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-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,
|
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,
|
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
|
|
|
|
|
|
|
keys, err := emptyRootDriver.List(ctx, "/")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
keys, err = slashRootDriver.List(ctx, "/")
|
|
|
|
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
|
|
|
}
|