2015-07-20 17:45:15 +00:00
|
|
|
// +build include_gcs
|
|
|
|
|
|
|
|
package gcs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-06 22:46:25 +00:00
|
|
|
"fmt"
|
2015-07-20 17:45:15 +00:00
|
|
|
ctx "github.com/docker/distribution/context"
|
|
|
|
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
|
|
|
"github.com/docker/distribution/registry/storage/driver/testsuites"
|
2016-01-06 22:46:25 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/google"
|
|
|
|
"google.golang.org/api/googleapi"
|
|
|
|
"google.golang.org/cloud/storage"
|
2015-07-20 17:45:15 +00:00
|
|
|
"gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Hook up gocheck into the "go test" runner.
|
|
|
|
func Test(t *testing.T) { check.TestingT(t) }
|
|
|
|
|
|
|
|
var gcsDriverConstructor func(rootDirectory string) (storagedriver.StorageDriver, error)
|
|
|
|
var skipGCS func() string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bucket := os.Getenv("REGISTRY_STORAGE_GCS_BUCKET")
|
|
|
|
credentials := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
|
|
|
|
|
2016-01-06 22:46:25 +00:00
|
|
|
// Skip GCS storage driver tests if environment variable parameters are not provided
|
|
|
|
skipGCS = func() string {
|
|
|
|
if bucket == "" || credentials == "" {
|
2016-01-15 11:47:26 +00:00
|
|
|
return "The following environment variables must be set to enable these tests: REGISTRY_STORAGE_GCS_BUCKET, GOOGLE_APPLICATION_CREDENTIALS"
|
2016-01-06 22:46:25 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if skipGCS() != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:45:15 +00:00
|
|
|
root, err := ioutil.TempDir("", "driver-")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(root)
|
2016-01-15 11:47:26 +00:00
|
|
|
var ts oauth2.TokenSource
|
|
|
|
var email string
|
|
|
|
var privateKey []byte
|
2015-07-20 17:45:15 +00:00
|
|
|
|
2016-01-15 11:47:26 +00:00
|
|
|
ts, err = google.DefaultTokenSource(ctx.Background(), storage.ScopeFullControl)
|
|
|
|
if err != nil {
|
|
|
|
// Assume that the file contents are within the environment variable since it exists
|
|
|
|
// but does not contain a valid file path
|
|
|
|
jwtConfig, err := google.JWTConfigFromJSON([]byte(credentials), storage.ScopeFullControl)
|
2016-01-06 22:46:25 +00:00
|
|
|
if err != nil {
|
2016-01-15 11:47:26 +00:00
|
|
|
panic(fmt.Sprintf("Error reading JWT config : %s", err))
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
2016-01-15 11:47:26 +00:00
|
|
|
email = jwtConfig.Email
|
|
|
|
privateKey = []byte(jwtConfig.PrivateKey)
|
|
|
|
if len(privateKey) == 0 {
|
|
|
|
panic("Error reading JWT config : missing private_key property")
|
|
|
|
}
|
|
|
|
if email == "" {
|
|
|
|
panic("Error reading JWT config : missing client_email property")
|
|
|
|
}
|
|
|
|
ts = jwtConfig.TokenSource(ctx.Background())
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 22:46:25 +00:00
|
|
|
gcsDriverConstructor = func(rootDirectory string) (storagedriver.StorageDriver, error) {
|
|
|
|
parameters := driverParameters{
|
|
|
|
bucket: bucket,
|
|
|
|
rootDirectory: root,
|
2016-01-15 11:47:26 +00:00
|
|
|
email: email,
|
|
|
|
privateKey: privateKey,
|
|
|
|
client: oauth2.NewClient(ctx.Background(), ts),
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
2016-01-06 22:46:25 +00:00
|
|
|
|
|
|
|
return New(parameters)
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testsuites.RegisterSuite(func() (storagedriver.StorageDriver, error) {
|
|
|
|
return gcsDriverConstructor(root)
|
|
|
|
}, skipGCS)
|
|
|
|
}
|
|
|
|
|
2016-01-06 11:47:28 +00:00
|
|
|
func TestRetry(t *testing.T) {
|
|
|
|
if skipGCS() != "" {
|
|
|
|
t.Skip(skipGCS())
|
|
|
|
}
|
|
|
|
|
|
|
|
assertError := func(expected string, observed error) {
|
|
|
|
observedMsg := "<nil>"
|
|
|
|
if observed != nil {
|
|
|
|
observedMsg = observed.Error()
|
|
|
|
}
|
|
|
|
if observedMsg != expected {
|
|
|
|
t.Fatalf("expected %v, observed %v\n", expected, observedMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := retry(2, func() error {
|
|
|
|
return &googleapi.Error{
|
|
|
|
Code: 503,
|
|
|
|
Message: "google api error",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assertError("googleapi: Error 503: google api error", err)
|
|
|
|
|
|
|
|
err = retry(2, func() error {
|
|
|
|
return &googleapi.Error{
|
|
|
|
Code: 404,
|
|
|
|
Message: "google api error",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assertError("googleapi: Error 404: google api error", err)
|
|
|
|
|
|
|
|
err = retry(2, func() error {
|
|
|
|
return fmt.Errorf("error")
|
|
|
|
})
|
|
|
|
assertError("error", err)
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:45:15 +00:00
|
|
|
func TestEmptyRootList(t *testing.T) {
|
|
|
|
if skipGCS() != "" {
|
|
|
|
t.Skip(skipGCS())
|
|
|
|
}
|
|
|
|
|
|
|
|
validRoot, err := ioutil.TempDir("", "driver-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating temporary directory: %v", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(validRoot)
|
|
|
|
|
|
|
|
rootedDriver, err := gcsDriverConstructor(validRoot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating rooted driver: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyRootDriver, err := gcsDriverConstructor("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating empty root driver: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
slashRootDriver, err := gcsDriverConstructor("/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating slash root driver: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := "/test"
|
|
|
|
contents := []byte("contents")
|
|
|
|
ctx := ctx.Background()
|
|
|
|
err = rootedDriver.PutContent(ctx, filename, contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
2016-01-19 14:09:32 +00:00
|
|
|
defer func() {
|
|
|
|
err := rootedDriver.Delete(ctx, filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to remove %v due to %v\n", filename, err)
|
|
|
|
}
|
|
|
|
}()
|
2015-07-20 17:45:15 +00:00
|
|
|
keys, err := emptyRootDriver.List(ctx, "/")
|
|
|
|
for _, path := range keys {
|
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
keys, err = slashRootDriver.List(ctx, "/")
|
|
|
|
for _, path := range keys {
|
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|