From ebf767b2e94b985e9b2eaf1bbd32f9dd02447df9 Mon Sep 17 00:00:00 2001 From: Tom Berger Date: Wed, 4 May 2022 13:18:41 -0400 Subject: [PATCH] gcsstore: Avoid panic when composing with no passed objects (#360) --- pkg/gcsstore/gcsservice.go | 4 ++++ pkg/gcsstore/gcsservice_test.go | 24 ++++++++++++++++++++++++ pkg/gcsstore/gcsstore.go | 4 ++++ 3 files changed, 32 insertions(+) diff --git a/pkg/gcsstore/gcsservice.go b/pkg/gcsstore/gcsservice.go index 3acbcbd..c28c4ab 100644 --- a/pkg/gcsstore/gcsservice.go +++ b/pkg/gcsstore/gcsservice.go @@ -129,6 +129,10 @@ const COMPOSE_RETRIES = 3 // Compose takes a bucket name, a list of initial source names, and a destination string to compose multiple GCS objects together func (service *GCSService) compose(ctx context.Context, bucket string, srcs []string, dst string) error { + if len(srcs) < 1 { + return fmt.Errorf("empty srcs passed to compose for bucket: %s dest: %s", bucket, dst) + } + dstParams := GCSObjectParams{ Bucket: bucket, ID: dst, diff --git a/pkg/gcsstore/gcsservice_test.go b/pkg/gcsstore/gcsservice_test.go index 575220c..2fe2171 100644 --- a/pkg/gcsstore/gcsservice_test.go +++ b/pkg/gcsstore/gcsservice_test.go @@ -195,6 +195,30 @@ func TestComposeObjects(t *testing.T) { } } +func TestComposeNoObjects(t *testing.T) { + ctx := context.Background() + client, err := storage.NewClient(ctx, option.WithAPIKey("foo")) + if err != nil { + t.Fatal(err) + return + } + + service := GCSService{ + Client: client, + } + + err = service.ComposeObjects(ctx, GCSComposeParams{ + Bucket: "test-bucket", + Sources: []string{}, + Destination: "test_all", + }) + + if err == nil { + t.Errorf("Error: %v", err) + return + } +} + func TestGetObjectAttrs(t *testing.T) { defer gock.Off() diff --git a/pkg/gcsstore/gcsstore.go b/pkg/gcsstore/gcsstore.go index 7c8ca28..04a757c 100644 --- a/pkg/gcsstore/gcsstore.go +++ b/pkg/gcsstore/gcsstore.go @@ -270,6 +270,10 @@ func (upload gcsUpload) FinishUpload(ctx context.Context) error { return err } + if len(names) == 0 { + return fmt.Errorf("no GCS objects found with FilterObjects %+v", filterParams) + } + composeParams := GCSComposeParams{ Bucket: store.Bucket, Destination: store.keyWithPrefix(id),