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 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
|
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import os
import unittest
from azure.common import AzureHttpError
from azure.storage.blob import (
BlobBlock,
BlobBlockList,
BlockBlobService,
ContentSettings,
)
from azure.storage.blob.models import StandardBlobTier
from tests.testcase import (
StorageTestCase,
TestMode,
record,
)
#------------------------------------------------------------------------------
TEST_BLOB_PREFIX = 'blob'
FILE_PATH = 'blob_input.temp.dat'
LARGE_BLOB_SIZE = 64 * 1024 + 5
#------------------------------------------------------------------------------
class StorageBlockBlobTest(StorageTestCase):
def setUp(self):
super(StorageBlockBlobTest, self).setUp()
self.bs = self._create_storage_service(BlockBlobService, self.settings)
self.container_name = self.get_resource_name('utcontainer')
if not self.is_playback():
self.bs.create_container(self.container_name)
# test chunking functionality by reducing the threshold
# for chunking and the size of each chunk, otherwise
# the tests would take too long to execute
self.bs.MAX_BLOCK_SIZE = 4 * 1024
self.bs.MAX_SINGLE_PUT_SIZE = 32 * 1024
def tearDown(self):
if not self.is_playback():
try:
self.bs.delete_container(self.container_name)
except:
pass
if os.path.isfile(FILE_PATH):
try:
os.remove(FILE_PATH)
except:
pass
return super(StorageBlockBlobTest, self).tearDown()
#--Helpers-----------------------------------------------------------------
def _get_blob_reference(self):
return self.get_resource_name(TEST_BLOB_PREFIX)
def _create_blob(self):
blob_name = self._get_blob_reference()
self.bs.create_blob_from_bytes(self.container_name, blob_name, b'')
return blob_name
def assertBlobEqual(self, container_name, blob_name, expected_data):
actual_data = self.bs.get_blob_to_bytes(container_name, blob_name)
self.assertEqual(actual_data.content, expected_data)
class NonSeekableFile(object):
def __init__(self, wrapped_file):
self.wrapped_file = wrapped_file
def write(self, data):
self.wrapped_file.write(data)
def read(self, count):
return self.wrapped_file.read(count)
#--Test cases for block blobs --------------------------------------------
@record
def test_put_block(self):
# Arrange
blob_name = self._create_blob()
# Act
for i in range(5):
resp = self.bs.put_block(self.container_name,
blob_name,
'block {0}'.format(i).encode('utf-8'),
i)
self.assertIsNone(resp)
# Assert
@record
def test_put_block_unicode(self):
# Arrange
blob_name = self._create_blob()
# Act
with self.assertRaises(TypeError):
resp = self.bs.put_block(self.container_name, blob_name, u'啊齄丂狛狜', '1')
# Assert
@record
def test_put_block_with_md5(self):
# Arrange
blob_name = self._create_blob()
# Act
self.bs.put_block(self.container_name,
blob_name,
b'block',
1,
validate_content=True)
# Assert
@record
def test_put_block_list(self):
# Arrange
blob_name = self._get_blob_reference()
self.bs.put_block(self.container_name, blob_name, b'AAA', '1')
self.bs.put_block(self.container_name, blob_name, b'BBB', '2')
self.bs.put_block(self.container_name, blob_name, b'CCC', '3')
# Act
block_list = [BlobBlock(id='1'), BlobBlock(id='2'), BlobBlock(id='3')]
put_block_list_resp = self.bs.put_block_list(self.container_name, blob_name, block_list)
# Assert
blob = self.bs.get_blob_to_bytes(self.container_name, blob_name)
self.assertEqual(blob.content, b'AAABBBCCC')
self.assertEqual(blob.properties.etag, put_block_list_resp.etag)
self.assertEqual(blob.properties.last_modified, put_block_list_resp.last_modified)
@record
def test_put_block_list_invalid_block_id(self):
# Arrange
blob_name = self._get_blob_reference()
self.bs.put_block(self.container_name, blob_name, b'AAA', '1')
self.bs.put_block(self.container_name, blob_name, b'BBB', '2')
self.bs.put_block(self.container_name, blob_name, b'CCC', '3')
# Act
try:
block_list = [ BlobBlock(id='1'), BlobBlock(id='2'), BlobBlock(id='4')]
self.bs.put_block_list(self.container_name, blob_name, block_list)
self.fail()
except AzureHttpError as e:
self.assertGreaterEqual(str(e).find('specified block list is invalid'), 0)
# Assert
@record
def test_put_block_list_with_md5(self):
# Arrange
blob_name = self._get_blob_reference()
self.bs.put_block(self.container_name, blob_name, b'AAA', '1')
self.bs.put_block(self.container_name, blob_name, b'BBB', '2')
self.bs.put_block(self.container_name, blob_name, b'CCC', '3')
# Act
block_list = [BlobBlock(id='1'), BlobBlock(id='2'), BlobBlock(id='3')]
self.bs.put_block_list(self.container_name, blob_name, block_list, validate_content=True)
# Assert
@record
def test_get_block_list_no_blocks(self):
# Arrange
blob_name = self._create_blob()
# Act
block_list = self.bs.get_block_list(self.container_name, blob_name, None, 'all')
# Assert
self.assertIsNotNone(block_list)
self.assertIsInstance(block_list, BlobBlockList)
self.assertEqual(len(block_list.uncommitted_blocks), 0)
self.assertEqual(len(block_list.committed_blocks), 0)
@record
def test_get_block_list_uncommitted_blocks(self):
# Arrange
blob_name = self._get_blob_reference()
self.bs.put_block(self.container_name, blob_name, b'AAA', '1')
self.bs.put_block(self.container_name, blob_name, b'BBB', '2')
self.bs.put_block(self.container_name, blob_name, b'CCC', '3')
# Act
block_list = self.bs.get_block_list(self.container_name, blob_name, None, 'uncommitted')
# Assert
self.assertIsNotNone(block_list)
self.assertIsInstance(block_list, BlobBlockList)
self.assertEqual(len(block_list.uncommitted_blocks), 3)
self.assertEqual(len(block_list.committed_blocks), 0)
self.assertEqual(block_list.uncommitted_blocks[0].id, '1')
self.assertEqual(block_list.uncommitted_blocks[0].size, 3)
self.assertEqual(block_list.uncommitted_blocks[1].id, '2')
self.assertEqual(block_list.uncommitted_blocks[1].size, 3)
self.assertEqual(block_list.uncommitted_blocks[2].id, '3')
self.assertEqual(block_list.uncommitted_blocks[2].size, 3)
@record
def test_get_block_list_committed_blocks(self):
# Arrange
blob_name = self._get_blob_reference()
self.bs.put_block(self.container_name, blob_name, b'AAA', '1')
self.bs.put_block(self.container_name, blob_name, b'BBB', '2')
self.bs.put_block(self.container_name, blob_name, b'CCC', '3')
block_list = [BlobBlock(id='1'), BlobBlock(id='2'), BlobBlock(id='3')]
self.bs.put_block_list(self.container_name, blob_name, block_list)
# Act
block_list = self.bs.get_block_list(self.container_name, blob_name, None, 'committed')
# Assert
self.assertIsNotNone(block_list)
self.assertIsInstance(block_list, BlobBlockList)
self.assertEqual(len(block_list.uncommitted_blocks), 0)
self.assertEqual(len(block_list.committed_blocks), 3)
self.assertEqual(block_list.committed_blocks[0].id, '1')
self.assertEqual(block_list.committed_blocks[0].size, 3)
self.assertEqual(block_list.committed_blocks[1].id, '2')
self.assertEqual(block_list.committed_blocks[1].size, 3)
self.assertEqual(block_list.committed_blocks[2].id, '3')
self.assertEqual(block_list.committed_blocks[2].size, 3)
@record
def test_create_blob_from_bytes_single_put(self):
# Arrange
blob_name = self._get_blob_reference()
data = b'hello world'
# Act
create_resp = self.bs.create_blob_from_bytes(self.container_name, blob_name, data)
blob = self.bs.get_blob_properties(self.container_name, blob_name)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
self.assertEqual(blob.properties.etag, create_resp.etag)
self.assertEqual(blob.properties.last_modified, create_resp.last_modified)
@record
def test_create_blob_from_0_bytes(self):
# Arrange
blob_name = self._get_blob_reference()
data = b''
# Act
create_resp = self.bs.create_blob_from_bytes(self.container_name, blob_name, data)
blob = self.bs.get_blob_properties(self.container_name, blob_name)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
self.assertEqual(blob.properties.etag, create_resp.etag)
self.assertEqual(blob.properties.last_modified, create_resp.last_modified)
@record
def test_create_from_bytes_blob_unicode(self):
# Arrange
blob_name = self._get_blob_reference()
data = u'hello world'
# Act
create_resp = self.bs.create_blob_from_bytes(self.container_name, blob_name, data)
blob = self.bs.get_blob_properties(self.container_name, blob_name)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
self.assertEqual(blob.properties.etag, create_resp.etag)
self.assertEqual(blob.properties.last_modified, create_resp.last_modified)
@record
def test_create_from_bytes_blob_unicode(self):
# Arrange
blob_name = self._get_blob_reference()
# Act
data = u'hello world'
with self.assertRaises(TypeError):
create_resp = self.bs.create_blob_from_bytes(self.container_name, blob_name, data)
# Assert
def test_create_from_bytes_blob_with_lease_id(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._create_blob()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
lease_id = self.bs.acquire_blob_lease(self.container_name, blob_name)
# Act
create_resp = self.bs.create_blob_from_bytes(self.container_name, blob_name, data, lease_id=lease_id)
# Assert
blob = self.bs.get_blob_to_bytes(self.container_name, blob_name, lease_id=lease_id)
self.assertEqual(blob.content, data)
self.assertEqual(blob.properties.etag, create_resp.etag)
self.assertEqual(blob.properties.last_modified, create_resp.last_modified)
def test_create_blob_from_bytes_with_metadata(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
metadata = {'hello': 'world', 'number': '42'}
# Act
self.bs.create_blob_from_bytes(self.container_name, blob_name, data, metadata=metadata)
# Assert
md = self.bs.get_blob_metadata(self.container_name, blob_name)
self.assertDictEqual(md, metadata)
def test_create_blob_from_bytes_with_properties(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
# Act
content_settings=ContentSettings(
content_type='image/png',
content_language='spanish')
self.bs.create_blob_from_bytes(self.container_name, blob_name, data,
content_settings=content_settings)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
properties = self.bs.get_blob_properties(self.container_name, blob_name).properties
self.assertEqual(properties.content_settings.content_type, content_settings.content_type)
self.assertEqual(properties.content_settings.content_language, content_settings.content_language)
def test_create_blob_from_bytes_with_progress(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
# Act
progress = []
def callback(current, total):
progress.append((current, total))
create_resp = self.bs.create_blob_from_bytes(self.container_name, blob_name, data, progress_callback=callback)
blob = self.bs.get_blob_properties(self.container_name, blob_name)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
self.assert_upload_progress(len(data), self.bs.MAX_BLOCK_SIZE, progress)
self.assertEqual(blob.properties.etag, create_resp.etag)
self.assertEqual(blob.properties.last_modified, create_resp.last_modified)
def test_create_blob_from_bytes_with_index(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
# Act
self.bs.create_blob_from_bytes(self.container_name, blob_name, data, 3)
# Assert
self.assertEqual(data[3:], self.bs.get_blob_to_bytes(self.container_name, blob_name).content)
@record
def test_create_blob_from_bytes_with_index_and_count(self):
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
# Act
self.bs.create_blob_from_bytes(self.container_name, blob_name, data, 3, 5)
# Assert
self.assertEqual(data[3:8], self.bs.get_blob_to_bytes(self.container_name, blob_name).content)
@record
def test_create_blob_from_bytes_with_index_and_count_and_properties(self):
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
# Act
content_settings=ContentSettings(
content_type='image/png',
content_language='spanish')
self.bs.create_blob_from_bytes(self.container_name, blob_name, data, 3, 5, content_settings=content_settings)
# Assert
self.assertEqual(data[3:8], self.bs.get_blob_to_bytes(self.container_name, blob_name).content)
properties = self.bs.get_blob_properties(self.container_name, blob_name).properties
self.assertEqual(properties.content_settings.content_type, content_settings.content_type)
self.assertEqual(properties.content_settings.content_language, content_settings.content_language)
@record
def test_create_blob_from_bytes_non_parallel(self):
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
# Act
self.bs.create_blob_from_bytes(self.container_name, blob_name, data, max_connections=1)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
def test_create_blob_from_path(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
with open(FILE_PATH, 'wb') as stream:
stream.write(data)
# Act
create_resp = self.bs.create_blob_from_path(self.container_name, blob_name, FILE_PATH)
blob = self.bs.get_blob_properties(self.container_name, blob_name)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
self.assertEqual(blob.properties.etag, create_resp.etag)
self.assertEqual(blob.properties.last_modified, create_resp.last_modified)
@record
def test_create_blob_from_path_non_parallel(self):
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(100)
with open(FILE_PATH, 'wb') as stream:
stream.write(data)
# Act
create_resp = self.bs.create_blob_from_path(self.container_name, blob_name, FILE_PATH, max_connections=1)
blob = self.bs.get_blob_properties(self.container_name, blob_name)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
self.assertEqual(blob.properties.etag, create_resp.etag)
self.assertEqual(blob.properties.last_modified, create_resp.last_modified)
def test_create_blob_from_path_with_progress(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
with open(FILE_PATH, 'wb') as stream:
stream.write(data)
# Act
progress = []
def callback(current, total):
progress.append((current, total))
self.bs.create_blob_from_path(self.container_name, blob_name, FILE_PATH,
progress_callback=callback)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
self.assert_upload_progress(len(data), self.bs.MAX_BLOCK_SIZE, progress)
def test_create_blob_from_path_with_properties(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
with open(FILE_PATH, 'wb') as stream:
stream.write(data)
# Act
content_settings=ContentSettings(
content_type='image/png',
content_language='spanish')
self.bs.create_blob_from_path(self.container_name, blob_name, FILE_PATH, content_settings=content_settings)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
properties = self.bs.get_blob_properties(self.container_name, blob_name).properties
self.assertEqual(properties.content_settings.content_type, content_settings.content_type)
self.assertEqual(properties.content_settings.content_language, content_settings.content_language)
def test_create_blob_from_stream_chunked_upload(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
with open(FILE_PATH, 'wb') as stream:
stream.write(data)
# Act
with open(FILE_PATH, 'rb') as stream:
create_resp = self.bs.create_blob_from_stream(self.container_name, blob_name, stream)
blob = self.bs.get_blob_properties(self.container_name, blob_name)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
self.assertEqual(blob.properties.etag, create_resp.etag)
self.assertEqual(blob.properties.last_modified, create_resp.last_modified)
def test_create_blob_from_stream_non_seekable_chunked_upload_known_size(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
blob_size = len(data) - 66
with open(FILE_PATH, 'wb') as stream:
stream.write(data)
# Act
with open(FILE_PATH, 'rb') as stream:
non_seekable_file = StorageBlockBlobTest.NonSeekableFile(stream)
self.bs.create_blob_from_stream(self.container_name, blob_name, non_seekable_file,
count=blob_size, max_connections=1)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data[:blob_size])
def test_create_blob_from_stream_non_seekable_chunked_upload_unknown_size(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
with open(FILE_PATH, 'wb') as stream:
stream.write(data)
# Act
with open(FILE_PATH, 'rb') as stream:
non_seekable_file = StorageBlockBlobTest.NonSeekableFile(stream)
self.bs.create_blob_from_stream(self.container_name, blob_name,
non_seekable_file, max_connections=1)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
def test_create_blob_from_stream_with_progress_chunked_upload(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
with open(FILE_PATH, 'wb') as stream:
stream.write(data)
# Act
progress = []
def callback(current, total):
progress.append((current, total))
with open(FILE_PATH, 'rb') as stream:
self.bs.create_blob_from_stream(self.container_name, blob_name, stream, progress_callback=callback)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
self.assert_upload_progress(len(data), self.bs.MAX_BLOCK_SIZE, progress, unknown_size=True)
def test_create_blob_from_stream_chunked_upload_with_count(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
with open(FILE_PATH, 'wb') as stream:
stream.write(data)
# Act
blob_size = len(data) - 301
with open(FILE_PATH, 'rb') as stream:
resp = self.bs.create_blob_from_stream(self.container_name, blob_name, stream, blob_size)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data[:blob_size])
def test_create_blob_from_stream_chunked_upload_with_count_and_properties(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
with open(FILE_PATH, 'wb') as stream:
stream.write(data)
# Act
content_settings=ContentSettings(
content_type='image/png',
content_language='spanish')
blob_size = len(data) - 301
with open(FILE_PATH, 'rb') as stream:
self.bs.create_blob_from_stream(self.container_name, blob_name, stream,
blob_size, content_settings=content_settings)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data[:blob_size])
properties = self.bs.get_blob_properties(self.container_name, blob_name).properties
self.assertEqual(properties.content_settings.content_type, content_settings.content_type)
self.assertEqual(properties.content_settings.content_language, content_settings.content_language)
def test_create_blob_from_stream_chunked_upload_with_properties(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
with open(FILE_PATH, 'wb') as stream:
stream.write(data)
# Act
content_settings=ContentSettings(
content_type='image/png',
content_language='spanish')
with open(FILE_PATH, 'rb') as stream:
self.bs.create_blob_from_stream(self.container_name, blob_name, stream,
content_settings=content_settings)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
properties = self.bs.get_blob_properties(self.container_name, blob_name).properties
self.assertEqual(properties.content_settings.content_type, content_settings.content_type)
self.assertEqual(properties.content_settings.content_language, content_settings.content_language)
@record
def test_create_blob_from_text(self):
# Arrange
blob_name = self._get_blob_reference()
text = u'hello 啊齄丂狛狜 world'
data = text.encode('utf-8')
# Act
create_resp = self.bs.create_blob_from_text(self.container_name, blob_name, text)
blob = self.bs.get_blob_properties(self.container_name, blob_name)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
self.assertEqual(blob.properties.etag, create_resp.etag)
self.assertEqual(blob.properties.last_modified, create_resp.last_modified)
@record
def test_create_blob_from_text_with_encoding(self):
# Arrange
blob_name = self._get_blob_reference()
text = u'hello 啊齄丂狛狜 world'
data = text.encode('utf-16')
# Act
self.bs.create_blob_from_text(self.container_name, blob_name, text, 'utf-16')
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
@record
def test_create_blob_from_text_with_encoding_and_progress(self):
# Arrange
blob_name = self._get_blob_reference()
text = u'hello 啊齄丂狛狜 world'
data = text.encode('utf-16')
# Act
progress = []
def callback(current, total):
progress.append((current, total))
self.bs.create_blob_from_text(self.container_name, blob_name, text, 'utf-16',
progress_callback=callback)
# Assert
self.assertBlobEqual(self.container_name, blob_name, data)
self.assert_upload_progress(len(data), self.bs.MAX_BLOCK_SIZE, progress)
def test_create_blob_from_text_chunked_upload(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_text_data(LARGE_BLOB_SIZE)
encoded_data = data.encode('utf-8')
# Act
self.bs.create_blob_from_text(self.container_name, blob_name, data)
# Assert
self.assertBlobEqual(self.container_name, blob_name, encoded_data)
# Assert
self.assertBlobEqual(self.container_name, blob_name, encoded_data)
@record
def test_create_blob_with_md5(self):
# Arrange
blob_name = self._get_blob_reference()
data = b'hello world'
# Act
self.bs.create_blob_from_bytes(self.container_name, blob_name, data,
validate_content=True)
# Assert
def test_create_blob_with_md5_chunked(self):
# parallel tests introduce random order of requests, can only run live
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
blob_name = self._get_blob_reference()
data = self.get_random_bytes(LARGE_BLOB_SIZE)
# Act
self.bs.create_blob_from_bytes(self.container_name, blob_name, data,
validate_content=True)
# Assert
#------------------------------------------------------------------------------
if __name__ == '__main__':
unittest.main()
|