2015-07-20 17:45:15 +00:00
|
|
|
package gcs
|
|
|
|
|
|
|
|
import (
|
2023-10-18 09:34:10 +00:00
|
|
|
"context"
|
2017-08-11 22:31:16 +00:00
|
|
|
"fmt"
|
2015-07-20 17:45:15 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2023-05-23 08:42:39 +00:00
|
|
|
"cloud.google.com/go/storage"
|
2023-10-24 17:16:58 +00:00
|
|
|
"github.com/distribution/distribution/v3/internal/dcontext"
|
2020-08-24 11:18:39 +00:00
|
|
|
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
|
|
|
"github.com/distribution/distribution/v3/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"
|
2023-05-23 08:42:39 +00:00
|
|
|
"google.golang.org/api/option"
|
2015-07-20 17:45:15 +00:00
|
|
|
)
|
|
|
|
|
2022-11-02 21:05:45 +00:00
|
|
|
var (
|
|
|
|
gcsDriverConstructor func(rootDirectory string) (storagedriver.StorageDriver, error)
|
2023-12-08 02:33:35 +00:00
|
|
|
skipCheck func(tb testing.TB)
|
2022-11-02 21:05:45 +00:00
|
|
|
)
|
2015-07-20 17:45:15 +00:00
|
|
|
|
|
|
|
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
|
2023-12-08 02:33:35 +00:00
|
|
|
skipCheck = func(tb testing.TB) {
|
|
|
|
tb.Helper()
|
|
|
|
|
2016-01-06 22:46:25 +00:00
|
|
|
if bucket == "" || credentials == "" {
|
2023-12-08 02:33:35 +00:00
|
|
|
tb.Skip("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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-26 11:54:14 +00:00
|
|
|
gcsDriverConstructor = func(rootDirectory string) (storagedriver.StorageDriver, error) {
|
|
|
|
jsonKey, err := os.ReadFile(credentials)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Error reading JSON key : %v", err))
|
|
|
|
}
|
2023-05-23 08:42:39 +00:00
|
|
|
|
2023-12-26 11:54:14 +00:00
|
|
|
var ts oauth2.TokenSource
|
|
|
|
var email string
|
|
|
|
var privateKey []byte
|
2015-07-20 17:45:15 +00:00
|
|
|
|
2023-12-26 11:54:14 +00:00
|
|
|
ts, err = google.DefaultTokenSource(dcontext.Background(), storage.ScopeFullControl)
|
2016-01-06 22:46:25 +00:00
|
|
|
if err != nil {
|
2023-12-26 11:54:14 +00:00
|
|
|
// 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(jsonKey, storage.ScopeFullControl)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Error reading JWT config : %s", err))
|
|
|
|
}
|
|
|
|
email = jwtConfig.Email
|
|
|
|
privateKey = 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(dcontext.Background())
|
2016-01-15 11:47:26 +00:00
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
|
2023-12-26 11:54:14 +00:00
|
|
|
gcs, err := storage.NewClient(dcontext.Background(), option.WithCredentialsJSON(jsonKey))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Error initializing gcs client : %v", err))
|
|
|
|
}
|
2023-05-23 08:42:39 +00:00
|
|
|
|
2016-01-06 22:46:25 +00:00
|
|
|
parameters := driverParameters{
|
2023-05-23 08:42:39 +00:00
|
|
|
bucket: bucket,
|
2023-12-07 18:00:27 +00:00
|
|
|
rootDirectory: rootDirectory,
|
2023-05-23 08:42:39 +00:00
|
|
|
email: email,
|
|
|
|
privateKey: privateKey,
|
|
|
|
client: oauth2.NewClient(dcontext.Background(), ts),
|
|
|
|
chunkSize: defaultChunkSize,
|
|
|
|
gcs: gcs,
|
|
|
|
maxConcurrency: 8,
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
2016-01-06 22:46:25 +00:00
|
|
|
|
2023-11-03 06:14:13 +00:00
|
|
|
return New(context.Background(), parameters)
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
2023-12-07 18:00:27 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 02:33:35 +00:00
|
|
|
func newDriverConstructor(tb testing.TB) testsuites.DriverConstructor {
|
2023-12-07 18:00:27 +00:00
|
|
|
root := tb.TempDir()
|
2015-07-20 17:45:15 +00:00
|
|
|
|
2023-12-08 02:33:35 +00:00
|
|
|
return func() (storagedriver.StorageDriver, error) {
|
2015-07-20 17:45:15 +00:00
|
|
|
return gcsDriverConstructor(root)
|
2023-12-08 02:33:35 +00:00
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 02:33:35 +00:00
|
|
|
func TestGCSDriverSuite(t *testing.T) {
|
|
|
|
skipCheck(t)
|
|
|
|
testsuites.Driver(t, newDriverConstructor(t))
|
2023-12-07 18:00:27 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 03:46:44 +00:00
|
|
|
func BenchmarkGCSDriverSuite(b *testing.B) {
|
2023-12-08 02:33:35 +00:00
|
|
|
skipCheck(b)
|
|
|
|
testsuites.BenchDriver(b, newDriverConstructor(b))
|
2023-12-07 18:00:27 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
// Test Committing a FileWriter without having called Write
|
|
|
|
func TestCommitEmpty(t *testing.T) {
|
2023-12-08 02:33:35 +00:00
|
|
|
skipCheck(t)
|
2016-02-12 13:30:57 +00:00
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
validRoot := t.TempDir()
|
2016-02-12 13:30:57 +00:00
|
|
|
|
|
|
|
driver, err := gcsDriverConstructor(validRoot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating rooted driver: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := "/test"
|
2017-08-11 22:31:16 +00:00
|
|
|
ctx := dcontext.Background()
|
2016-02-12 13:30:57 +00:00
|
|
|
|
|
|
|
writer, err := driver.Writer(ctx, filename, false)
|
2023-11-18 06:50:40 +00:00
|
|
|
// nolint:errcheck
|
2016-02-12 13:30:57 +00:00
|
|
|
defer driver.Delete(ctx, filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("driver.Writer: unexpected error: %v", err)
|
|
|
|
}
|
2023-10-18 09:34:10 +00:00
|
|
|
err = writer.Commit(context.Background())
|
2016-02-12 13:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("writer.Commit: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
err = writer.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("writer.Close: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if writer.Size() != 0 {
|
|
|
|
t.Fatalf("writer.Size: %d != 0", writer.Size())
|
|
|
|
}
|
|
|
|
readContents, err := driver.GetContent(ctx, filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("driver.GetContent: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if len(readContents) != 0 {
|
|
|
|
t.Fatalf("len(driver.GetContent(..)): %d != 0", len(readContents))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Committing a FileWriter after having written exactly
|
|
|
|
// defaultChunksize bytes.
|
|
|
|
func TestCommit(t *testing.T) {
|
2023-12-08 02:33:35 +00:00
|
|
|
skipCheck(t)
|
2016-02-12 13:30:57 +00:00
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
validRoot := t.TempDir()
|
2016-02-12 13:30:57 +00:00
|
|
|
|
|
|
|
driver, err := gcsDriverConstructor(validRoot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating rooted driver: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := "/test"
|
2017-08-11 22:31:16 +00:00
|
|
|
ctx := dcontext.Background()
|
2016-02-12 13:30:57 +00:00
|
|
|
|
|
|
|
contents := make([]byte, defaultChunkSize)
|
|
|
|
writer, err := driver.Writer(ctx, filename, false)
|
2023-11-18 06:50:40 +00:00
|
|
|
// nolint:errcheck
|
2016-02-12 13:30:57 +00:00
|
|
|
defer driver.Delete(ctx, filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("driver.Writer: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
_, err = writer.Write(contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("writer.Write: unexpected error: %v", err)
|
|
|
|
}
|
2023-10-18 09:34:10 +00:00
|
|
|
err = writer.Commit(context.Background())
|
2016-02-12 13:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("writer.Commit: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
err = writer.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("writer.Close: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if writer.Size() != int64(len(contents)) {
|
|
|
|
t.Fatalf("writer.Size: %d != %d", writer.Size(), len(contents))
|
|
|
|
}
|
|
|
|
readContents, err := driver.GetContent(ctx, filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("driver.GetContent: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if len(readContents) != len(contents) {
|
|
|
|
t.Fatalf("len(driver.GetContent(..)): %d != %d", len(readContents), len(contents))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 11:47:28 +00:00
|
|
|
func TestRetry(t *testing.T) {
|
2023-12-08 02:33:35 +00:00
|
|
|
skipCheck(t)
|
2016-01-06 11:47:28 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
err := retry(func() error {
|
2016-01-06 11:47:28 +00:00
|
|
|
return &googleapi.Error{
|
|
|
|
Code: 503,
|
|
|
|
Message: "google api error",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assertError("googleapi: Error 503: google api error", err)
|
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
err = retry(func() error {
|
2016-01-06 11:47:28 +00:00
|
|
|
return &googleapi.Error{
|
|
|
|
Code: 404,
|
|
|
|
Message: "google api error",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assertError("googleapi: Error 404: google api error", err)
|
|
|
|
|
2016-02-12 13:30:57 +00:00
|
|
|
err = retry(func() error {
|
2016-01-06 11:47:28 +00:00
|
|
|
return fmt.Errorf("error")
|
|
|
|
})
|
|
|
|
assertError("error", err)
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:45:15 +00:00
|
|
|
func TestEmptyRootList(t *testing.T) {
|
2023-12-08 02:33:35 +00:00
|
|
|
skipCheck(t)
|
2015-07-20 17:45:15 +00:00
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
validRoot := t.TempDir()
|
2015-07-20 17:45:15 +00:00
|
|
|
|
|
|
|
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")
|
2017-08-11 22:31:16 +00:00
|
|
|
ctx := dcontext.Background()
|
2015-07-20 17:45:15 +00:00
|
|
|
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, "/")
|
2023-06-28 10:41:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error listing empty root content: %v", err)
|
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
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, "/")
|
2023-06-28 10:41:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error listing slash root content: %v", err)
|
|
|
|
}
|
2015-07-20 17:45:15 +00:00
|
|
|
for _, path := range keys {
|
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-22 14:40:21 +00:00
|
|
|
|
|
|
|
// TestMoveDirectory checks that moving a directory returns an error.
|
|
|
|
func TestMoveDirectory(t *testing.T) {
|
2023-12-08 02:33:35 +00:00
|
|
|
skipCheck(t)
|
2016-01-22 14:40:21 +00:00
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
validRoot := t.TempDir()
|
2016-01-22 14:40:21 +00:00
|
|
|
|
|
|
|
driver, err := gcsDriverConstructor(validRoot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating rooted driver: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-11 22:31:16 +00:00
|
|
|
ctx := dcontext.Background()
|
2016-01-22 14:40:21 +00:00
|
|
|
contents := []byte("contents")
|
|
|
|
// Create a regular file.
|
|
|
|
err = driver.PutContent(ctx, "/parent/dir/foo", contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err := driver.Delete(ctx, "/parent")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to remove /parent due to %v\n", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = driver.Move(ctx, "/parent/dir", "/parent/other")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Moving directory /parent/dir /parent/other should have return a non-nil error\n")
|
|
|
|
}
|
|
|
|
}
|