/
zt_highlevel_test.go
574 lines (477 loc) · 16.6 KB
/
zt_highlevel_test.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package azblob
import (
"context"
"errors"
"io/ioutil"
"os"
"sync/atomic"
"testing"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/internal/recording"
"github.com/stretchr/testify/require"
)
// create a test file
func generateFile(fileName string, fileSize int) []byte {
// generate random data
_, bigBuff := generateData(fileSize)
// write to file and return the data
_ = ioutil.WriteFile(fileName, bigBuff, 0666)
return bigBuff
}
func performUploadStreamToBlockBlobTest(t *testing.T, testName string, blobSize, bufferSize, maxBuffers int) {
recording.LiveOnly(t)
svcClient, err := createServiceClient(t, testAccountDefault)
require.NoError(t, err)
containerName := generateContainerName(testName)
containerClient := createNewContainer(t, containerName, svcClient)
defer deleteContainer(t, containerClient)
// Set up test blob
blobName := generateBlobName(testName)
blobClient := getBlockBlobClient(blobName, containerClient)
// Create some data to test the upload stream
blobContentReader, blobData := generateData(blobSize)
// Perform UploadStreamToBlockBlob
uploadResp, err := blobClient.UploadStreamToBlockBlob(ctx, blobContentReader,
UploadStreamToBlockBlobOptions{BufferSize: bufferSize, MaxBuffers: maxBuffers})
// Assert that upload was successful
require.Equal(t, err, nil)
require.Equal(t, uploadResp.RawResponse.StatusCode, 201)
// Download the blob to verify
downloadResponse, err := blobClient.Download(ctx, nil)
require.NoError(t, err)
// Assert that the content is correct
actualBlobData, err := ioutil.ReadAll(downloadResponse.RawResponse.Body)
require.NoError(t, err)
require.Equal(t, len(actualBlobData), blobSize)
require.EqualValues(t, actualBlobData, blobData)
}
func TestUploadStreamToBlockBlobInChunks(t *testing.T) {
stop := start(t)
defer stop()
blobSize := 8 * 1024
bufferSize := 1024
maxBuffers := 3
performUploadStreamToBlockBlobTest(t, t.Name(), blobSize, bufferSize, maxBuffers)
}
func TestUploadStreamToBlockBlobSingleBuffer(t *testing.T) {
stop := start(t)
defer stop()
blobSize := 8 * 1024
bufferSize := 1024
maxBuffers := 1
performUploadStreamToBlockBlobTest(t, t.Name(), blobSize, bufferSize, maxBuffers)
}
func TestUploadStreamToBlockBlobSingleIO(t *testing.T) {
stop := start(t)
defer stop()
blobSize := 1024
bufferSize := 8 * 1024
maxBuffers := 3
performUploadStreamToBlockBlobTest(t, t.Name(), blobSize, bufferSize, maxBuffers)
}
func TestUploadStreamToBlockBlobSingleIOEdgeCase(t *testing.T) {
stop := start(t)
defer stop()
blobSize := 8 * 1024
bufferSize := 8 * 1024
maxBuffers := 3
performUploadStreamToBlockBlobTest(t, t.Name(), blobSize, bufferSize, maxBuffers)
}
func TestUploadStreamToBlockBlobEmpty(t *testing.T) {
stop := start(t)
defer stop()
blobSize := 0
bufferSize := 8 * 1024
maxBuffers := 3
performUploadStreamToBlockBlobTest(t, t.Name(), blobSize, bufferSize, maxBuffers)
}
func TestUploadStreamToBlobBlobProperties(t *testing.T) {
recording.LiveOnly(t)
svcClient, err := createServiceClient(t, testAccountDefault)
require.NoError(t, err)
blobSize := 1024
bufferSize := 8 * 1024
maxBuffers := 3
testName := t.Name()
containerName := generateContainerName(testName)
containerClient := createNewContainer(t, containerName, svcClient)
defer deleteContainer(t, containerClient)
// Set up test blob
blobName := generateBlobName(testName)
blobClient := getBlockBlobClient(blobName, containerClient)
// Create some data to test the upload stream
blobContentReader, blobData := generateData(blobSize)
// Perform UploadStreamToBlockBlob
uploadResp, err := blobClient.UploadStreamToBlockBlob(ctx, blobContentReader,
UploadStreamToBlockBlobOptions{
BufferSize: bufferSize,
MaxBuffers: maxBuffers,
Metadata: basicMetadata,
BlobTagsMap: basicBlobTagsMap,
HTTPHeaders: &basicHeaders,
})
// Assert that upload was successful
require.Equal(t, err, nil)
require.Equal(t, uploadResp.RawResponse.StatusCode, 201)
getPropertiesResp, err := blobClient.GetProperties(ctx, nil)
require.NoError(t, err)
require.EqualValues(t, getPropertiesResp.Metadata, basicMetadata)
require.Equal(t, *getPropertiesResp.TagCount, int64(len(basicBlobTagsMap)))
require.Equal(t, getPropertiesResp.GetHTTPHeaders(), basicHeaders)
getTagsResp, err := blobClient.GetTags(ctx, nil)
require.NoError(t, err)
require.Len(t, getTagsResp.BlobTagSet, 3)
for _, blobTag := range getTagsResp.BlobTagSet {
require.Equal(t, basicBlobTagsMap[*blobTag.Key], *blobTag.Value)
}
// Download the blob to verify
downloadResponse, err := blobClient.Download(ctx, nil)
require.NoError(t, err)
// Assert that the content is correct
actualBlobData, err := ioutil.ReadAll(downloadResponse.RawResponse.Body)
require.NoError(t, err)
require.Equal(t, len(actualBlobData), blobSize)
require.EqualValues(t, actualBlobData, blobData)
}
func performUploadAndDownloadFileTest(t *testing.T, testName string, fileSize, blockSize, parallelism, downloadOffset, downloadCount int) {
recording.LiveOnly(t)
// Set up file to upload
fileName := "BigFile.bin"
fileData := generateFile(fileName, fileSize)
// Open the file to upload
file, err := os.Open(fileName)
require.NoError(t, err)
defer func(file *os.File) {
_ = file.Close()
}(file)
defer func(name string) {
_ = os.Remove(name)
}(fileName)
svcClient, err := createServiceClient(t, testAccountDefault)
require.NoError(t, err)
containerClient := createNewContainer(t, generateContainerName(testName), svcClient)
defer deleteContainer(t, containerClient)
// Set up test blob
bbClient := getBlockBlobClient(generateBlobName(testName), containerClient)
// Upload the file to a block blob
response, err := bbClient.UploadFileToBlockBlob(context.Background(), file,
HighLevelUploadToBlockBlobOption{
BlockSize: int64(blockSize),
Parallelism: uint16(parallelism),
// If Progress is non-nil, this function is called periodically as bytes are uploaded.
Progress: func(bytesTransferred int64) {
require.Equal(t, bytesTransferred > 0 && bytesTransferred <= int64(fileSize), true)
},
})
require.Equal(t, err, nil)
require.Equal(t, response.StatusCode, 201)
// Set up file to download the blob to
destFileName := "BigFile-downloaded.bin"
destFile, err := os.Create(destFileName)
require.Equal(t, err, nil)
defer func(destFile *os.File) {
_ = destFile.Close()
}(destFile)
defer func(name string) {
_ = os.Remove(name)
}(destFileName)
// Perform download
err = bbClient.DownloadBlobToFile(context.Background(), int64(downloadOffset), int64(downloadCount),
destFile,
HighLevelDownloadFromBlobOptions{
BlockSize: int64(blockSize),
Parallelism: uint16(parallelism),
// If Progress is non-nil, this function is called periodically as bytes are uploaded.
Progress: func(bytesTransferred int64) {
require.Equal(t, bytesTransferred > 0 && bytesTransferred <= int64(fileSize), true)
},
})
// Assert download was successful
require.Equal(t, err, nil)
// Assert downloaded data is consistent
var destBuffer []byte
if downloadCount == CountToEnd {
destBuffer = make([]byte, fileSize-downloadOffset)
} else {
destBuffer = make([]byte, downloadCount)
}
n, err := destFile.Read(destBuffer)
require.Equal(t, err, nil)
if downloadOffset == 0 && downloadCount == 0 {
require.EqualValues(t, destBuffer, fileData)
} else {
if downloadCount == 0 {
require.Equal(t, n, fileSize-downloadOffset)
require.EqualValues(t, destBuffer, fileData[downloadOffset:])
} else {
require.Equal(t, n, downloadCount)
require.EqualValues(t, destBuffer, fileData[downloadOffset:downloadOffset+downloadCount])
}
}
}
func TestUploadAndDownloadFileInChunks(t *testing.T) {
stop := start(t)
defer stop()
fileSize := 8 * 1024
blockSize := 1024
parallelism := 3
performUploadAndDownloadFileTest(t, t.Name(), fileSize, blockSize, parallelism, 0, 0)
}
func TestUploadAndDownloadFileSingleIO(t *testing.T) {
stop := start(t)
defer stop()
fileSize := 1024
blockSize := 2048
parallelism := 3
performUploadAndDownloadFileTest(t, t.Name(), fileSize, blockSize, parallelism, 0, 0)
}
func TestUploadAndDownloadFileSingleRoutine(t *testing.T) {
stop := start(t)
defer stop()
fileSize := 8 * 1024
blockSize := 1024
parallelism := 1
performUploadAndDownloadFileTest(t, t.Name(), fileSize, blockSize, parallelism, 0, 0)
}
func TestUploadAndDownloadFileEmpty(t *testing.T) {
stop := start(t)
defer stop()
fileSize := 0
blockSize := 1024
parallelism := 3
performUploadAndDownloadFileTest(t, t.Name(), fileSize, blockSize, parallelism, 0, 0)
}
func TestUploadAndDownloadFileNonZeroOffset(t *testing.T) {
stop := start(t)
defer stop()
fileSize := 8 * 1024
blockSize := 1024
parallelism := 3
downloadOffset := 1000
downloadCount := 0
performUploadAndDownloadFileTest(t, t.Name(), fileSize, blockSize, parallelism, downloadOffset, downloadCount)
}
func TestUploadAndDownloadFileNonZeroCount(t *testing.T) {
stop := start(t)
defer stop()
fileSize := 8 * 1024
blockSize := 1024
parallelism := 3
downloadOffset := 0
downloadCount := 6000
performUploadAndDownloadFileTest(t, t.Name(), fileSize, blockSize, parallelism, downloadOffset, downloadCount)
}
func TestUploadAndDownloadFileNonZeroOffsetAndCount(t *testing.T) {
stop := start(t)
defer stop()
fileSize := 8 * 1024
blockSize := 1024
parallelism := 3
downloadOffset := 1000
downloadCount := 6000
performUploadAndDownloadFileTest(t, t.Name(), fileSize, blockSize, parallelism, downloadOffset, downloadCount)
}
func performUploadAndDownloadBufferTest(t *testing.T, testName string, blobSize, blockSize, parallelism, downloadOffset, downloadCount int) {
recording.LiveOnly(t)
// Set up buffer to upload
_, bytesToUpload := generateData(blobSize)
// Set up test container
svcClient, err := createServiceClient(t, testAccountDefault)
require.NoError(t, err)
containerClient := createNewContainer(t, generateContainerName(testName), svcClient)
defer deleteContainer(t, containerClient)
// Set up test blob
bbClient := getBlockBlobClient(generateBlobName(testName), containerClient)
// Pass the Context, stream, stream size, block blob URL, and options to StreamToBlockBlob
response, err := bbClient.UploadBufferToBlockBlob(context.Background(), bytesToUpload,
HighLevelUploadToBlockBlobOption{
BlockSize: int64(blockSize),
Parallelism: uint16(parallelism),
// If Progress is non-nil, this function is called periodically as bytes are uploaded.
Progress: func(bytesTransferred int64) {
require.Equal(t, bytesTransferred > 0 && bytesTransferred <= int64(blobSize), true)
},
})
require.Equal(t, err, nil)
require.Equal(t, response.StatusCode, 201)
// Set up buffer to download the blob to
var destBuffer []byte
if downloadCount == CountToEnd {
destBuffer = make([]byte, blobSize-downloadOffset)
} else {
destBuffer = make([]byte, downloadCount)
}
// Download the blob to a buffer
err = bbClient.DownloadBlobToBuffer(context.Background(), int64(downloadOffset), int64(downloadCount),
destBuffer, HighLevelDownloadFromBlobOptions{
BlockSize: int64(blockSize),
Parallelism: uint16(parallelism),
// If Progress is non-nil, this function is called periodically as bytes are uploaded.
Progress: func(bytesTransferred int64) {
require.Equal(t, bytesTransferred > 0 && bytesTransferred <= int64(blobSize), true)
},
})
require.Equal(t, err, nil)
if downloadOffset == 0 && downloadCount == 0 {
require.EqualValues(t, destBuffer, bytesToUpload)
} else {
if downloadCount == 0 {
require.EqualValues(t, destBuffer, bytesToUpload[downloadOffset:])
} else {
require.EqualValues(t, destBuffer, bytesToUpload[downloadOffset:downloadOffset+downloadCount])
}
}
}
func TestUploadAndDownloadBufferInChunks(t *testing.T) {
stop := start(t)
defer stop()
blobSize := 8 * 1024
blockSize := 1024
parallelism := 3
performUploadAndDownloadBufferTest(t, t.Name(), blobSize, blockSize, parallelism, 0, 0)
}
func TestUploadAndDownloadBufferSingleIO(t *testing.T) {
stop := start(t)
defer stop()
blobSize := 1024
blockSize := 8 * 1024
parallelism := 3
performUploadAndDownloadBufferTest(t, t.Name(), blobSize, blockSize, parallelism, 0, 0)
}
func TestUploadAndDownloadBufferSingleRoutine(t *testing.T) {
stop := start(t)
defer stop()
blobSize := 8 * 1024
blockSize := 1024
parallelism := 1
performUploadAndDownloadBufferTest(t, t.Name(), blobSize, blockSize, parallelism, 0, 0)
}
func TestUploadAndDownloadBufferEmpty(t *testing.T) {
stop := start(t)
defer stop()
blobSize := 0
blockSize := 1024
parallelism := 3
performUploadAndDownloadBufferTest(t, t.Name(), blobSize, blockSize, parallelism, 0, 0)
}
func TestDownloadBufferWithNonZeroOffset(t *testing.T) {
stop := start(t)
defer stop()
blobSize := 8 * 1024
blockSize := 1024
parallelism := 3
downloadOffset := 1000
downloadCount := 0
performUploadAndDownloadBufferTest(t, t.Name(), blobSize, blockSize, parallelism, downloadOffset, downloadCount)
}
func TestDownloadBufferWithNonZeroCount(t *testing.T) {
stop := start(t)
defer stop()
blobSize := 8 * 1024
blockSize := 1024
parallelism := 3
downloadOffset := 0
downloadCount := 6000
performUploadAndDownloadBufferTest(t, t.Name(), blobSize, blockSize, parallelism, downloadOffset, downloadCount)
}
func TestDownloadBufferWithNonZeroOffsetAndCount(t *testing.T) {
stop := start(t)
defer stop()
blobSize := 8 * 1024
blockSize := 1024
parallelism := 3
downloadOffset := 2000
downloadCount := 6 * 1024
performUploadAndDownloadBufferTest(t, t.Name(), blobSize, blockSize, parallelism, downloadOffset, downloadCount)
}
func TestBasicDoBatchTransfer(t *testing.T) {
// test the basic multi-routine processing
type testInstance struct {
transferSize int64
chunkSize int64
parallelism uint16
expectError bool
}
testMatrix := []testInstance{
{transferSize: 100, chunkSize: 10, parallelism: 5, expectError: false},
{transferSize: 100, chunkSize: 9, parallelism: 4, expectError: false},
{transferSize: 100, chunkSize: 8, parallelism: 15, expectError: false},
{transferSize: 100, chunkSize: 1, parallelism: 3, expectError: false},
{transferSize: 0, chunkSize: 100, parallelism: 5, expectError: false}, // empty file works
{transferSize: 100, chunkSize: 0, parallelism: 5, expectError: true}, // 0 chunk size on the other hand must fail
{transferSize: 0, chunkSize: 0, parallelism: 5, expectError: true},
}
for _, test := range testMatrix {
ctx := context.Background()
// maintain some counts to make sure the right number of chunks were queued, and the total size is correct
totalSizeCount := int64(0)
runCount := int64(0)
err := DoBatchTransfer(ctx, BatchTransferOptions{
TransferSize: test.transferSize,
ChunkSize: test.chunkSize,
Parallelism: test.parallelism,
Operation: func(offset int64, chunkSize int64, ctx context.Context) error {
atomic.AddInt64(&totalSizeCount, chunkSize)
atomic.AddInt64(&runCount, 1)
return nil
},
OperationName: "TestHappyPath",
})
if test.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, totalSizeCount, test.transferSize)
require.Equal(t, runCount, ((test.transferSize-1)/test.chunkSize)+1)
}
}
}
// mock a memory mapped file (low-quality mock, meant to simulate the scenario only)
type mockMMF struct {
isClosed bool
failHandle *require.Assertions
}
// accept input
func (m *mockMMF) write(_ string) {
if m.isClosed {
// simulate panic
m.failHandle.Fail("")
}
}
func TestDoBatchTransferWithError(t *testing.T) {
recording.LiveOnly(t)
ctx := context.Background()
mmf := mockMMF{failHandle: require.New(t)}
expectedFirstError := errors.New("#3 means trouble")
err := DoBatchTransfer(ctx, BatchTransferOptions{
TransferSize: 5,
ChunkSize: 1,
Parallelism: 5,
Operation: func(offset int64, chunkSize int64, ctx context.Context) error {
// simulate doing some work (HTTP call in real scenarios)
// later chunks later longer to finish
recording.Sleep(time.Second * time.Duration(offset))
// simulate having gotten data and write it to the memory mapped file
mmf.write("input")
// with one of the chunks, pretend like an error occurred (like the network connection breaks)
if offset == 3 {
return expectedFirstError
} else if offset > 3 {
// anything after offset=3 are canceled
// so verify that the context indeed got canceled
ctxErr := ctx.Err()
require.Equal(t, ctxErr, context.Canceled)
return ctxErr
}
// anything before offset=3 should be done without problem
return nil
},
OperationName: "TestErrorPath",
})
require.Equal(t, err, expectedFirstError)
// simulate closing the mmf and make sure no panic occurs (as reported in #139)
mmf.isClosed = true
recording.Sleep(time.Second * 5)
}