Skip to content

Commit

Permalink
s3store: Adjust Terminate implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Aug 3, 2024
1 parent c9c5c6a commit f7f2f50
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/s3store/s3store.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ func (upload s3Upload) Terminate(ctx context.Context) error {

var wg sync.WaitGroup
wg.Add(2)
errs := make([]error, 0, 3)
errs := make([]error, 0, 4)

go func() {
defer wg.Done()
Expand Down
80 changes: 69 additions & 11 deletions pkg/s3store/s3store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,17 +1184,44 @@ func TestWriteChunkAllowTooSmallLast(t *testing.T) {
}

func TestTerminate(t *testing.T) {
// TODO: Update test because Terminate now fetches info as well
t.SkipNow()

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
assert := assert.New(t)

s3obj := NewMockS3API(mockCtrl)
store := New("bucket", s3obj)

// Order is not important in this situation.
s3obj.EXPECT().GetObject(context.Background(), &s3.GetObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("uploadId.info"),
}).Return(&s3.GetObjectOutput{
Body: io.NopCloser(bytes.NewReader([]byte(`{"ID":"uploadId","Size":500,"Offset":0,"MetaData":{"bar":"menü","foo":"hello"},"IsPartial":false,"IsFinal":false,"PartialUploads":null,"Storage":{"Bucket":"bucket","Key":"uploadId","MultipartUpload":"multipartId","Type":"s3store"}}`))),
}, nil)
s3obj.EXPECT().ListParts(context.Background(), &s3.ListPartsInput{
Bucket: aws.String("bucket"),
Key: aws.String("uploadId"),
UploadId: aws.String("multipartId"),
PartNumberMarker: nil,
}).Return(&s3.ListPartsOutput{
Parts: []types.Part{
{
PartNumber: aws.Int32(1),
Size: aws.Int64(100),
ETag: aws.String("etag-1"),
},
{
PartNumber: aws.Int32(2),
Size: aws.Int64(200),
ETag: aws.String("etag-2"),
},
},
IsTruncated: aws.Bool(false),
}, nil)
s3obj.EXPECT().HeadObject(context.Background(), &s3.HeadObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("uploadId.part"),
}).Return(nil, &types.NoSuchKey{})

s3obj.EXPECT().AbortMultipartUpload(context.Background(), &s3.AbortMultipartUploadInput{
Bucket: aws.String("bucket"),
Key: aws.String("uploadId"),
Expand All @@ -1219,26 +1246,53 @@ func TestTerminate(t *testing.T) {
},
}).Return(&s3.DeleteObjectsOutput{}, nil)

upload, err := store.GetUpload(context.Background(), "uploadId+multipartId")
upload, err := store.GetUpload(context.Background(), "uploadId")
assert.Nil(err)

err = store.AsTerminatableUpload(upload).Terminate(context.Background())
assert.Nil(err)
}

func TestTerminateWithErrors(t *testing.T) {
// TODO: Update test because Terminate now fetches info as well
t.SkipNow()

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
assert := assert.New(t)

s3obj := NewMockS3API(mockCtrl)
store := New("bucket", s3obj)

// Order is not important in this situation.
// NoSuchUpload errors should be ignored
s3obj.EXPECT().GetObject(context.Background(), &s3.GetObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("uploadId.info"),
}).Return(&s3.GetObjectOutput{
Body: io.NopCloser(bytes.NewReader([]byte(`{"ID":"uploadId","Size":500,"Offset":0,"MetaData":{"bar":"menü","foo":"hello"},"IsPartial":false,"IsFinal":false,"PartialUploads":null,"Storage":{"Bucket":"bucket","Key":"uploadId","MultipartUpload":"multipartId","Type":"s3store"}}`))),
}, nil)
s3obj.EXPECT().ListParts(context.Background(), &s3.ListPartsInput{
Bucket: aws.String("bucket"),
Key: aws.String("uploadId"),
UploadId: aws.String("multipartId"),
PartNumberMarker: nil,
}).Return(&s3.ListPartsOutput{
Parts: []types.Part{
{
PartNumber: aws.Int32(1),
Size: aws.Int64(100),
ETag: aws.String("etag-1"),
},
{
PartNumber: aws.Int32(2),
Size: aws.Int64(200),
ETag: aws.String("etag-2"),
},
},
IsTruncated: aws.Bool(false),
}, nil)
s3obj.EXPECT().HeadObject(context.Background(), &s3.HeadObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("uploadId.part"),
}).Return(nil, &types.NoSuchKey{})

// These NoSuchUpload and NoSuchKey errors should be ignored
s3obj.EXPECT().AbortMultipartUpload(context.Background(), &s3.AbortMultipartUploadInput{
Bucket: aws.String("bucket"),
Key: aws.String("uploadId"),
Expand Down Expand Up @@ -1268,10 +1322,14 @@ func TestTerminateWithErrors(t *testing.T) {
Key: aws.String("uploadId"),
Message: aws.String("it's me."),
},
{
Code: aws.String("NoSuchKey"),
Key: aws.String("uploadId.part"),
},
},
}, nil)

upload, err := store.GetUpload(context.Background(), "uploadId+multipartId")
upload, err := store.GetUpload(context.Background(), "uploadId")
assert.Nil(err)

err = store.AsTerminatableUpload(upload).Terminate(context.Background())
Expand Down

0 comments on commit f7f2f50

Please sign in to comment.