From 022d223e1da39ecf661fbde704231a2c1c9755ef Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Mon, 17 Feb 2020 08:29:55 -0800 Subject: [PATCH] blob: fixed GCS post-submit test --- repo/blob/gcs/gcs_storage.go | 21 +++++++++++---------- repo/blob/gcs/gcs_storage_test.go | 15 ++++----------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/repo/blob/gcs/gcs_storage.go b/repo/blob/gcs/gcs_storage.go index 600eebebc..4e174e1fb 100644 --- a/repo/blob/gcs/gcs_storage.go +++ b/repo/blob/gcs/gcs_storage.go @@ -74,7 +74,7 @@ func exponentialBackoff(desc string, att retry.AttemptFunc) (interface{}, error) func isRetriableError(err error) bool { if apiError, ok := err.(*googleapi.Error); ok { - return apiError.Code >= 500 + return apiError.Code >= 500 || apiError.Code == 403 } switch err { @@ -95,8 +95,6 @@ func translateError(err error) error { return nil case gcsclient.ErrObjectNotExist: return blob.ErrBlobNotFound - case gcsclient.ErrBucketNotExist: - return blob.ErrBlobNotFound default: return errors.Wrap(err, "unexpected GCS error") } @@ -267,19 +265,22 @@ func New(ctx context.Context, opt *Options) (blob.Storage, error) { return nil, errors.New("bucket name must be specified") } - // make sure the bucket exists - if _, err := cli.Bucket(opt.BucketName).Attrs(ctx); err != nil { - return nil, errors.Wrap(err, "unable to read bucket") - } - - return &gcsStorage{ + gcs := &gcsStorage{ Options: *opt, ctx: ctx, storageClient: cli, bucket: cli.Bucket(opt.BucketName), downloadThrottler: downloadThrottler, uploadThrottler: uploadThrottler, - }, nil + } + + // verify GCS connection is functional by fetching one blob, + // which must return success or ErrBlobNotFound. Any other error indicates problem with connection. + if _, err = gcs.GetBlob(ctx, "kopia.repository", 0, 100); err != nil && err != blob.ErrBlobNotFound { + return nil, err + } + + return gcs, nil } func init() { diff --git a/repo/blob/gcs/gcs_storage_test.go b/repo/blob/gcs/gcs_storage_test.go index bbe1e3afe..9ab0263b0 100644 --- a/repo/blob/gcs/gcs_storage_test.go +++ b/repo/blob/gcs/gcs_storage_test.go @@ -61,18 +61,11 @@ func TestGCSStorageInvalid(t *testing.T) { } ctx := context.Background() - st, err := gcs.New(ctx, &gcs.Options{ + + if _, err := gcs.New(ctx, &gcs.Options{ BucketName: bucket + "-no-such-bucket", ServiceAccountCredentialsFile: os.Getenv("KOPIA_GCS_CREDENTIALS_FILE"), - }) - - if err != nil { - t.Fatalf("unable to connect to GCS: %v", err) - } - - defer st.Close(ctx) - - if err := st.PutBlob(ctx, "xxx", []byte{1, 2, 3}); err == nil { - t.Errorf("unexpecte success when adding to non-existent bucket") + }); err == nil { + t.Fatalf("unexpected success connecting to GCS, wanted error") } }