gcsstore: Avoid panic when composing with no passed objects (#360)

This commit is contained in:
Tom Berger 2022-05-04 13:18:41 -04:00 committed by GitHub
parent 0ef0e6f9e0
commit ebf767b2e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 0 deletions

View File

@ -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 // 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 { 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{ dstParams := GCSObjectParams{
Bucket: bucket, Bucket: bucket,
ID: dst, ID: dst,

View File

@ -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) { func TestGetObjectAttrs(t *testing.T) {
defer gock.Off() defer gock.Off()

View File

@ -270,6 +270,10 @@ func (upload gcsUpload) FinishUpload(ctx context.Context) error {
return err return err
} }
if len(names) == 0 {
return fmt.Errorf("no GCS objects found with FilterObjects %+v", filterParams)
}
composeParams := GCSComposeParams{ composeParams := GCSComposeParams{
Bucket: store.Bucket, Bucket: store.Bucket,
Destination: store.keyWithPrefix(id), Destination: store.keyWithPrefix(id),