2021-07-06 03:43:13 +00:00
|
|
|
//go:build include_oss
|
2015-07-28 04:45:05 +00:00
|
|
|
// +build include_oss
|
|
|
|
|
2015-05-11 15:26:51 +00:00
|
|
|
package oss
|
|
|
|
|
|
|
|
import (
|
2015-07-31 04:39:55 +00:00
|
|
|
"io/ioutil"
|
2017-08-11 22:31:16 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
2015-07-31 04:39:55 +00:00
|
|
|
|
2015-05-11 15:26:51 +00:00
|
|
|
alioss "github.com/denverdino/aliyungo/oss"
|
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 15:26:51 +00:00
|
|
|
"gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Hook up gocheck into the "go test" runner.
|
|
|
|
func Test(t *testing.T) { check.TestingT(t) }
|
|
|
|
|
2015-07-02 14:42:12 +00:00
|
|
|
var ossDriverConstructor func(rootDirectory string) (*Driver, error)
|
|
|
|
|
|
|
|
var skipCheck func() string
|
2015-05-11 15:26:51 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
accessKey := os.Getenv("ALIYUN_ACCESS_KEY_ID")
|
|
|
|
secretKey := os.Getenv("ALIYUN_ACCESS_KEY_SECRET")
|
|
|
|
bucket := os.Getenv("OSS_BUCKET")
|
|
|
|
region := os.Getenv("OSS_REGION")
|
|
|
|
internal := os.Getenv("OSS_INTERNAL")
|
|
|
|
encrypt := os.Getenv("OSS_ENCRYPT")
|
|
|
|
secure := os.Getenv("OSS_SECURE")
|
2015-06-15 12:03:32 +00:00
|
|
|
endpoint := os.Getenv("OSS_ENDPOINT")
|
2018-12-25 00:30:40 +00:00
|
|
|
encryptionKeyID := os.Getenv("OSS_ENCRYPTIONKEYID")
|
2015-05-11 15:26:51 +00:00
|
|
|
root, err := ioutil.TempDir("", "driver-")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(root)
|
|
|
|
|
2015-07-02 14:42:12 +00:00
|
|
|
ossDriverConstructor = func(rootDirectory string) (*Driver, error) {
|
2015-05-11 15:26:51 +00:00
|
|
|
encryptBool := false
|
|
|
|
if encrypt != "" {
|
|
|
|
encryptBool, err = strconv.ParseBool(encrypt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
secureBool := false
|
|
|
|
if secure != "" {
|
|
|
|
secureBool, err = strconv.ParseBool(secure)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internalBool := false
|
|
|
|
if internal != "" {
|
|
|
|
internalBool, err = strconv.ParseBool(internal)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parameters := DriverParameters{
|
2015-06-15 12:03:32 +00:00
|
|
|
AccessKeyID: accessKey,
|
|
|
|
AccessKeySecret: secretKey,
|
|
|
|
Bucket: bucket,
|
|
|
|
Region: alioss.Region(region),
|
|
|
|
Internal: internalBool,
|
|
|
|
ChunkSize: minChunkSize,
|
|
|
|
RootDirectory: rootDirectory,
|
|
|
|
Encrypt: encryptBool,
|
|
|
|
Secure: secureBool,
|
|
|
|
Endpoint: endpoint,
|
2018-12-25 00:30:40 +00:00
|
|
|
EncryptionKeyID: encryptionKeyID,
|
2015-05-11 15:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return New(parameters)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip OSS storage driver tests if environment variable parameters are not provided
|
2015-07-02 14:42:12 +00:00
|
|
|
skipCheck = func() string {
|
2015-05-11 15:26:51 +00:00
|
|
|
if accessKey == "" || secretKey == "" || region == "" || bucket == "" || encrypt == "" {
|
|
|
|
return "Must set ALIYUN_ACCESS_KEY_ID, ALIYUN_ACCESS_KEY_SECRET, OSS_REGION, OSS_BUCKET, and OSS_ENCRYPT to run OSS tests"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:42:12 +00:00
|
|
|
testsuites.RegisterSuite(func() (storagedriver.StorageDriver, error) {
|
2015-05-11 15:26:51 +00:00
|
|
|
return ossDriverConstructor(root)
|
2015-07-02 14:42:12 +00:00
|
|
|
}, skipCheck)
|
2015-05-11 15:26:51 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 14:42:12 +00:00
|
|
|
func TestEmptyRootList(t *testing.T) {
|
|
|
|
if skipCheck() != "" {
|
|
|
|
t.Skip(skipCheck())
|
2015-05-11 15:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
validRoot, err := ioutil.TempDir("", "driver-")
|
2015-07-02 14:42:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating temporary directory: %v", err)
|
|
|
|
}
|
2015-05-11 15:26:51 +00:00
|
|
|
defer os.Remove(validRoot)
|
|
|
|
|
2015-07-02 14:42:12 +00:00
|
|
|
rootedDriver, err := ossDriverConstructor(validRoot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating rooted driver: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyRootDriver, err := ossDriverConstructor("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating empty root driver: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
slashRootDriver, err := ossDriverConstructor("/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating slash root driver: %v", err)
|
|
|
|
}
|
2015-05-11 15:26:51 +00:00
|
|
|
|
|
|
|
filename := "/test"
|
|
|
|
contents := []byte("contents")
|
|
|
|
ctx := context.Background()
|
|
|
|
err = rootedDriver.PutContent(ctx, filename, contents)
|
2015-07-02 14:42:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
2015-05-11 15:26:51 +00:00
|
|
|
defer rootedDriver.Delete(ctx, filename)
|
|
|
|
|
|
|
|
keys, err := emptyRootDriver.List(ctx, "/")
|
|
|
|
for _, path := range keys {
|
2015-07-02 14:42:12 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
|
|
|
|
}
|
2015-05-11 15:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keys, err = slashRootDriver.List(ctx, "/")
|
|
|
|
for _, path := range keys {
|
2015-07-02 14:42:12 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
|
|
|
|
}
|
2015-05-11 15:26:51 +00:00
|
|
|
}
|
|
|
|
}
|