-
Notifications
You must be signed in to change notification settings - Fork 361
/
multipart_suite.go
310 lines (265 loc) · 10.7 KB
/
multipart_suite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package blocktest
import (
"bytes"
"context"
"io"
"strconv"
"testing"
"github.com/stretchr/testify/require"
"github.com/thanhpk/randstr"
"github.com/treeverse/lakefs/pkg/block"
)
const (
multipartNumberOfParts = 3
multipartPartSize = 5 * 1024 * 1024 // generally the minimum size for multi-part upload
)
// AdapterMultipartTest Test suite of adapter multipart functionality
func AdapterMultipartTest(t *testing.T, adapter block.Adapter, storageNamespace, externalPath string) {
t.Run("Adapter_MultipartUpload", func(t *testing.T) { testAdapterMultipartUpload(t, adapter, storageNamespace) })
t.Run("Adapter_AbortMultiPartUpload", func(t *testing.T) { testAdapterAbortMultipartUpload(t, adapter, storageNamespace) })
t.Run("Adapter_CopyPart", func(t *testing.T) { testAdapterCopyPart(t, adapter, storageNamespace) })
t.Run("Adapter_CopyPartRange", func(t *testing.T) { testAdapterCopyPartRange(t, adapter, storageNamespace) })
}
// Parameterized test of the Multipart Upload APIs. After successful upload we Get the result and compare to the original
func testAdapterMultipartUpload(t *testing.T, adapter block.Adapter, storageNamespace string) {
ctx := context.Background()
cases := []struct {
name string
path string
lastPartPartial bool
}{
{"simple", "abc", false},
{"partial", "abc", true},
{"nested", "foo/bar", false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
blockstoreType := adapter.BlockstoreType()
obj := block.ObjectPointer{
StorageNamespace: storageNamespace,
Identifier: c.path,
IdentifierType: block.IdentifierTypeRelative,
}
parts, full := createMultipartContents(c.lastPartPartial)
verifyListInvalid(t, ctx, adapter, obj, "invalidId")
resp, err := adapter.CreateMultiPartUpload(ctx, obj, nil, block.CreateMultiPartUploadOpts{})
require.NoError(t, err)
multiParts := uploadParts(t, ctx, adapter, obj, resp.UploadID, parts)
// List parts after upload
listResp, err := adapter.ListParts(ctx, obj, resp.UploadID, block.ListPartsOpts{})
if blockstoreType != block.BlockstoreTypeS3 {
require.ErrorIs(t, err, block.ErrOperationNotSupported)
} else {
require.NoError(t, err)
require.Equal(t, len(parts), len(listResp.Parts))
for i, part := range listResp.Parts {
require.Equal(t, multiParts[i].PartNumber, part.PartNumber)
require.Equal(t, int64(len(parts[i])), part.Size)
require.Equal(t, multiParts[i].ETag, part.ETag)
require.False(t, listResp.IsTruncated)
}
}
// List parts partial
const maxPartsConst = 2
maxParts := int32(maxPartsConst)
listResp, err = adapter.ListParts(ctx, obj, resp.UploadID, block.ListPartsOpts{MaxParts: &maxParts})
if blockstoreType != block.BlockstoreTypeS3 {
require.ErrorIs(t, err, block.ErrOperationNotSupported)
} else {
require.NoError(t, err)
require.Equal(t, int(maxParts), len(listResp.Parts))
require.True(t, listResp.IsTruncated)
require.Equal(t, strconv.Itoa(int(maxParts)), *listResp.NextPartNumberMarker)
}
_, err = adapter.CompleteMultiPartUpload(ctx, obj, resp.UploadID, &block.MultipartUploadCompletion{
Part: multiParts,
})
require.NoError(t, err)
verifyListInvalid(t, ctx, adapter, obj, resp.UploadID)
getAndCheckContents(t, ctx, adapter, full, obj)
})
}
}
// Test aborting a multipart upload
func testAdapterAbortMultipartUpload(t *testing.T, adapter block.Adapter, storageNamespace string) {
ctx := context.Background()
parts, _ := createMultipartContents(false)
obj, _ := objPointers(storageNamespace)
// Start an upload
resp, err := adapter.CreateMultiPartUpload(ctx, obj, nil, block.CreateMultiPartUploadOpts{})
require.NoError(t, err)
uploadID := resp.UploadID
uploadParts(t, ctx, adapter, obj, uploadID, parts)
listResp, err := adapter.ListParts(ctx, obj, uploadID, block.ListPartsOpts{})
if err == nil {
require.Equal(t, multipartNumberOfParts, len(listResp.Parts))
}
// Test abort
err = adapter.AbortMultiPartUpload(ctx, obj, uploadID)
require.NoError(t, err)
// verify no parts to list
verifyListInvalid(t, ctx, adapter, obj, uploadID)
}
// Test of the Multipart Copy API
func testAdapterCopyPart(t *testing.T, adapter block.Adapter, storageNamespace string) {
ctx := context.Background()
parts, full := createMultipartContents(false)
obj, objCopy := objPointers(storageNamespace)
uploadMultiPart(t, ctx, adapter, obj, parts)
// Begin Multipart Copy
resp, err := adapter.CreateMultiPartUpload(ctx, objCopy, nil, block.CreateMultiPartUploadOpts{})
require.NoError(t, err)
multiParts, err := copyPart(t, ctx, adapter, obj, objCopy, resp.UploadID)
if adapter.BlockstoreType() == block.BlockstoreTypeAzure {
require.ErrorContains(t, err, "not implemented") // azurite block store emulator did not yet implement this
return
}
_, err = adapter.CompleteMultiPartUpload(ctx, objCopy, resp.UploadID, &block.MultipartUploadCompletion{
Part: multiParts,
})
require.NoError(t, err)
getAndCheckContents(t, ctx, adapter, full, objCopy)
}
// Test of the Multipart CopyRange API
func testAdapterCopyPartRange(t *testing.T, adapter block.Adapter, storageNamespace string) {
ctx := context.Background()
parts, full := createMultipartContents(false)
obj, objCopy := objPointers(storageNamespace)
uploadMultiPart(t, ctx, adapter, obj, parts)
// Begin Multipart Copy
resp, err := adapter.CreateMultiPartUpload(ctx, objCopy, nil, block.CreateMultiPartUploadOpts{})
require.NoError(t, err)
multiParts := copyPartRange(t, ctx, adapter, obj, objCopy, resp.UploadID)
if adapter.BlockstoreType() == block.BlockstoreTypeAzure {
// not implemented in Azure
require.Nil(t, multiParts) // azurite block store emulator did not yet implement this
return
}
_, err = adapter.CompleteMultiPartUpload(ctx, objCopy, resp.UploadID, &block.MultipartUploadCompletion{
Part: multiParts,
})
require.NoError(t, err)
getAndCheckContents(t, ctx, adapter, full, objCopy)
}
func createMultipartContents(lastPartPartial bool) ([][]byte, []byte) {
parts := make([][]byte, multipartNumberOfParts)
var partsConcat []byte
for i := 0; i < multipartNumberOfParts; i++ {
if lastPartPartial && (i == multipartNumberOfParts-1) {
parts[i] = randstr.Bytes(multipartPartSize/2 + i)
} else {
parts[i] = randstr.Bytes(multipartPartSize + i)
}
partsConcat = append(partsConcat, parts[i]...)
}
return parts, partsConcat
}
func uploadMultiPart(t *testing.T, ctx context.Context, adapter block.Adapter, obj block.ObjectPointer, parts [][]byte) {
t.Helper()
resp, err := adapter.CreateMultiPartUpload(ctx, obj, nil, block.CreateMultiPartUploadOpts{})
require.NoError(t, err)
multiParts := uploadParts(t, ctx, adapter, obj, resp.UploadID, parts)
_, err = adapter.CompleteMultiPartUpload(ctx, obj, resp.UploadID, &block.MultipartUploadCompletion{
Part: multiParts,
})
require.NoError(t, err)
}
// List parts when there are none
func verifyListInvalid(t *testing.T, ctx context.Context, adapter block.Adapter, obj block.ObjectPointer, uploadID string) {
t.Helper()
_, err := adapter.ListParts(ctx, obj, uploadID, block.ListPartsOpts{})
if adapter.BlockstoreType() != block.BlockstoreTypeS3 {
require.ErrorIs(t, err, block.ErrOperationNotSupported)
} else {
require.NotNil(t, err)
}
}
// Upload parts after starting a multipart upload
func uploadParts(t *testing.T, ctx context.Context, adapter block.Adapter, obj block.ObjectPointer, uploadID string, parts [][]byte) []block.MultipartPart {
t.Helper()
multiParts := make([]block.MultipartPart, len(parts))
for i, content := range parts {
partNumber := i + 1
partResp, err := adapter.UploadPart(ctx, obj, int64(len(content)), bytes.NewReader(content), uploadID, partNumber)
require.NoError(t, err)
multiParts[i].PartNumber = partNumber
multiParts[i].ETag = partResp.ETag
}
return multiParts
}
// Copy parts after starting a multipart upload
func copyPartRange(t *testing.T, ctx context.Context, adapter block.Adapter, obj, objCopy block.ObjectPointer, uploadID string) []block.MultipartPart {
t.Helper()
multiParts := make([]block.MultipartPart, multipartNumberOfParts)
var startPosition int64 = 0
for i := 0; i < multipartNumberOfParts; i++ {
partNumber := i + 1
var endPosition = startPosition + multipartPartSize
partResp, err := adapter.UploadCopyPartRange(ctx, obj, objCopy, uploadID, partNumber, startPosition, endPosition)
if adapter.BlockstoreType() == block.BlockstoreTypeAzure {
require.ErrorContains(t, err, "not implemented") // azurite block store emulator did not yet implement this
return nil
}
require.NoError(t, err)
multiParts[i].PartNumber = partNumber
multiParts[i].ETag = partResp.ETag
startPosition = endPosition + 1
}
return multiParts
}
// Copy one and only part after starting a multipart upload
func copyPart(t *testing.T, ctx context.Context, adapter block.Adapter, obj, objCopy block.ObjectPointer, uploadID string) ([]block.MultipartPart, error) {
t.Helper()
multiParts := make([]block.MultipartPart, 1)
partNumber := 1
partResp, err := adapter.UploadCopyPart(ctx, obj, objCopy, uploadID, partNumber)
if err != nil {
return nil, err
}
multiParts[0].PartNumber = partNumber
multiParts[0].ETag = partResp.ETag
return multiParts, nil
}
// getAndCheckContents GETs the Object from the Store, then compares its contents to the exp byte array
func getAndCheckContents(t *testing.T, ctx context.Context, adapter block.Adapter, exp []byte, obj block.ObjectPointer) {
t.Helper()
// first check exists
ok, err := adapter.Exists(ctx, obj)
require.NoError(t, err, "Exists failed")
require.True(t, ok, "Returned Non-OK Status")
reader, err := adapter.Get(ctx, obj)
require.NoError(t, err, "Get Object failed")
got, err := io.ReadAll(reader)
require.NoError(t, err, "ReadAll returned error")
requireEqualBigByteSlice(t, exp, got)
}
// compare two big bytearrays one slice at a time(so that we don't blow up the console on error)
func requireEqualBigByteSlice(t *testing.T, exp, actual []byte) {
t.Helper()
require.Equal(t, len(exp), len(actual))
const sliceLen = 100
sliceCount := len(exp) / sliceLen
if len(exp)%sliceLen > 0 {
sliceCount++
}
for i := 0; i < sliceCount; i++ {
var start = i * sliceLen
var end = min((i+1)*sliceLen, len(exp)-1)
var expSlice = exp[start:end]
var actualSlice = actual[start:end]
require.Equalf(t, expSlice, actualSlice, "Failed on slice "+strconv.Itoa(i+1)+"/"+strconv.Itoa(sliceCount))
}
}
func objPointers(storageNamespace string) (block.ObjectPointer, block.ObjectPointer) {
var obj = block.ObjectPointer{
StorageNamespace: storageNamespace,
Identifier: "abc",
IdentifierType: block.IdentifierTypeRelative,
}
var objCopy = block.ObjectPointer{
StorageNamespace: storageNamespace,
Identifier: "abcCopy",
IdentifierType: block.IdentifierTypeRelative,
}
return obj, objCopy
}