forked from Azure/azure-sdk-for-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_common_blob.py
More file actions
1778 lines (1427 loc) · 60.1 KB
/
test_common_blob.py
File metadata and controls
1778 lines (1427 loc) · 60.1 KB
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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 pytest
import requests
import time
import unittest
import os
from datetime import datetime, timedelta
from azure.core.exceptions import (
HttpResponseError,
ResourceNotFoundError,
ResourceExistsError,
ClientAuthenticationError)
from azure.storage.blob import (
upload_blob_to_url,
download_blob_from_url,
BlobServiceClient,
ContainerClient,
BlobClient,
BlobType,
StorageErrorCode,
BlobPermissions,
ContainerPermissions,
ContentSettings,
BlobProperties,
RetentionPolicy,
AccessPolicy,
ResourceTypes,
AccountPermissions,
)
from testcase import (
StorageTestCase,
TestMode,
record,
)
#------------------------------------------------------------------------------
TEST_CONTAINER_PREFIX = 'container'
TEST_BLOB_PREFIX = 'blob'
FILE_PATH = 'blob_data.temp.dat'
#------------------------------------------------------------------------------
class StorageCommonBlobTest(StorageTestCase):
def setUp(self):
super(StorageCommonBlobTest, self).setUp()
url = self._get_account_url()
credential = self._get_shared_key_credential()
self.bsc = BlobServiceClient(url, credential=credential)
self.container_name = self.get_resource_name('utcontainer')
if not self.is_playback():
container = self.bsc.get_container_client(self.container_name)
try:
container.create_container(timeout=5)
except ResourceExistsError:
pass
self.byte_data = self.get_random_bytes(1024)
remote_url = self._get_remote_account_url()
remote_credential = self._get_remote_shared_key_credential()
self.bsc2 = BlobServiceClient(remote_url, credential=remote_credential)
self.remote_container_name = None
def tearDown(self):
if not self.is_playback():
try:
self.bsc.delete_container(self.container_name, timeout=5)
except:
pass
if self.remote_container_name:
try:
self.bsc2.delete_container(self.remote_container_name)
except:
pass
if os.path.isfile(FILE_PATH):
try:
os.remove(FILE_PATH)
except:
pass
return super(StorageCommonBlobTest, self).tearDown()
#--Helpers-----------------------------------------------------------------
def _get_container_reference(self):
return self.get_resource_name(TEST_CONTAINER_PREFIX)
def _get_blob_reference(self):
return self.get_resource_name(TEST_BLOB_PREFIX)
def _create_block_blob(self):
blob_name = self._get_blob_reference()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
blob.upload_blob(self.byte_data, length=len(self.byte_data))
return blob_name
def _create_remote_container(self):
self.remote_container_name = self.get_resource_name('remotectnr')
remote_container = self.bsc2.get_container_client(self.remote_container_name)
try:
remote_container.create_container()
except ResourceExistsError:
pass
def _create_remote_block_blob(self, blob_data=None):
if not blob_data:
blob_data = b'12345678' * 1024 * 1024
source_blob_name = self._get_blob_reference()
source_blob = self.bsc2.get_blob_client(self.remote_container_name, source_blob_name)
source_blob.upload_blob(blob_data)
return source_blob
def _wait_for_async_copy(self, blob):
count = 0
props = blob.get_blob_properties()
while props.copy.status == 'pending':
count = count + 1
if count > 10:
self.fail('Timed out waiting for async copy to complete.')
self.sleep(6)
props = blob.get_blob_properties()
return props
def _enable_soft_delete(self):
delete_retention_policy = RetentionPolicy(enabled=True, days=2)
self.bsc.set_service_properties(delete_retention_policy=delete_retention_policy)
# wait until the policy has gone into effect
if not self.is_playback():
time.sleep(30)
def _disable_soft_delete(self):
delete_retention_policy = RetentionPolicy(enabled=False)
self.bsc.set_service_properties(delete_retention_policy=delete_retention_policy)
def _assert_blob_is_soft_deleted(self, blob):
self.assertTrue(blob.deleted)
self.assertIsNotNone(blob.deleted_time)
self.assertIsNotNone(blob.remaining_retention_days)
def _assert_blob_not_soft_deleted(self, blob):
self.assertFalse(blob.deleted)
self.assertIsNone(blob.deleted_time)
self.assertIsNone(blob.remaining_retention_days)
#-- Common test cases for blobs ----------------------------------------------
@record
def test_blob_exists(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
exists = blob.get_blob_properties()
# Assert
self.assertTrue(exists)
@record
def test_blob_not_exists(self):
# Arrange
blob_name = self._get_blob_reference()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
with self.assertRaises(ResourceNotFoundError):
blob.get_blob_properties()
@record
def test_blob_snapshot_exists(self):
# Arrange
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
snapshot = blob.create_snapshot()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name, snapshot=snapshot)
exists = blob.get_blob_properties()
# Assert
self.assertTrue(exists)
@record
def test_blob_snapshot_not_exists(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name, snapshot="1988-08-18T07:52:31.6690068Z")
with self.assertRaises(ResourceNotFoundError):
blob.get_blob_properties()
@record
def test_blob_container_not_exists(self):
# In this case both the blob and container do not exist
# Arrange
blob_name = self._get_blob_reference()
# Act
blob = self.bsc.get_blob_client(self._get_container_reference(), blob_name)
with self.assertRaises(ResourceNotFoundError):
blob.get_blob_properties()
@record
def test_create_blob_with_question_mark(self):
# Arrange
blob_name = '?ques?tion?'
blob_data = u'???'
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
blob.upload_blob(blob_data)
# Assert
data = blob.download_blob()
self.assertIsNotNone(data)
content = b"".join(list(data))
self.assertEqual(content.decode('utf-8'), blob_data)
@record
def test_create_blob_with_special_chars(self):
# Arrange
# Act
for c in '-._ /()$=\',~':
blob_name = '{0}a{0}a{0}'.format(c)
blob_data = c
blob = self.bsc.get_blob_client(self.container_name, blob_name)
blob.upload_blob(blob_data, length=len(blob_data))
data = blob.download_blob()
content = b"".join(list(data))
self.assertEqual(content.decode('utf-8'), blob_data)
# Assert
@record
def test_create_blob_with_lease_id(self):
# Arrange
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
lease = blob.acquire_lease()
# Act
data = b'hello world again'
resp = blob.upload_blob(data, length=len(data), lease=lease)
# Assert
self.assertIsNotNone(resp.get('etag'))
content = b"".join(list(blob.download_blob(lease=lease)))
self.assertEqual(content, data)
@record
def test_create_blob_with_metadata(self):
# Arrange
blob_name = self._get_blob_reference()
metadata={'hello': 'world', 'number': '42'}
# Act
data = b'hello world'
blob = self.bsc.get_blob_client(self.container_name, blob_name)
resp = blob.upload_blob(data, length=len(data), metadata=metadata)
# Assert
self.assertIsNotNone(resp.get('etag'))
md = blob.get_blob_properties().metadata
self.assertDictEqual(md, metadata)
@record
def test_get_blob_with_existing_blob(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
data = blob.download_blob()
# Assert
content = b"".join(list(data))
self.assertEqual(content, self.byte_data)
@record
def test_get_blob_with_snapshot(self):
# Arrange
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
snapshot = self.bsc.get_blob_client(
self.container_name, blob_name, snapshot=blob.create_snapshot())
# Act
data = snapshot.download_blob()
# Assert
content = b"".join(list(data))
self.assertEqual(content, self.byte_data)
@record
def test_get_blob_with_snapshot_previous(self):
# Arrange
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
snapshot = self.bsc.get_blob_client(
self.container_name, blob_name, snapshot=blob.create_snapshot())
upload_data = b'hello world again'
blob.upload_blob(upload_data, length=len(upload_data), overwrite=True)
# Act
blob_previous = snapshot.download_blob()
blob_latest = blob.download_blob()
# Assert
self.assertEqual(b"".join(list(blob_previous)), self.byte_data)
self.assertEqual(b"".join(list(blob_latest)), b'hello world again')
@record
def test_get_blob_with_range(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
data = blob.download_blob(offset=0, length=5)
# Assert
self.assertEqual(b"".join(list(data)), self.byte_data[:6])
@record
def test_get_blob_with_lease(self):
# Arrange
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
lease = blob.acquire_lease()
# Act
data = blob.download_blob(lease=lease)
lease.release()
# Assert
self.assertEqual(b"".join(list(data)), self.byte_data)
@record
def test_get_blob_with_non_existing_blob(self):
# Arrange
blob_name = self._get_blob_reference()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
with self.assertRaises(ResourceNotFoundError):
blob.download_blob()
# Assert
@record
def test_set_blob_properties_with_existing_blob(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
blob.set_http_headers(
content_settings=ContentSettings(
content_language='spanish',
content_disposition='inline'),
)
# Assert
props = blob.get_blob_properties()
self.assertEqual(props.content_settings.content_language, 'spanish')
self.assertEqual(props.content_settings.content_disposition, 'inline')
@record
def test_set_blob_properties_with_blob_settings_param(self):
# Arrange
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
props = blob.get_blob_properties()
# Act
props.content_settings.content_language = 'spanish'
props.content_settings.content_disposition = 'inline'
blob.set_http_headers(content_settings=props.content_settings)
# Assert
props = blob.get_blob_properties()
self.assertEqual(props.content_settings.content_language, 'spanish')
self.assertEqual(props.content_settings.content_disposition, 'inline')
@record
def test_get_blob_properties(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
props = blob.get_blob_properties()
# Assert
self.assertIsInstance(props, BlobProperties)
self.assertEqual(props.blob_type, BlobType.BlockBlob)
self.assertEqual(props.size, len(self.byte_data))
self.assertEqual(props.lease.status, 'unlocked')
self.assertIsNotNone(props.creation_time)
# This test is to validate that the ErrorCode is retrieved from the header during a
# HEAD request.
@record
def test_get_blob_properties_fail(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name, snapshot=1)
with self.assertRaises(HttpResponseError) as e:
blob.get_blob_properties() # Invalid snapshot value of 1
# Assert
# TODO: No error code returned
#self.assertEqual(StorageErrorCode.invalid_query_parameter_value, e.exception.error_code)
# This test is to validate that the ErrorCode is retrieved from the header during a
# GET request. This is preferred to relying on the ErrorCode in the body.
@ record
def test_get_blob_metadata_fail(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name, snapshot=1)
with self.assertRaises(HttpResponseError) as e:
blob.get_blob_properties().metadata # Invalid snapshot value of 1
# Assert
# TODO: No error code returned
#self.assertEqual(StorageErrorCode.invalid_query_parameter_value, e.exception.error_code)
@record
def test_get_blob_server_encryption(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
data = blob.download_blob()
# Assert
self.assertTrue(data.properties.server_encrypted)
@record
def test_get_blob_properties_server_encryption(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
props = blob.get_blob_properties()
# Assert
self.assertTrue(props.server_encrypted)
@record
def test_list_blobs_server_encryption(self):
#Arrange
self._create_block_blob()
self._create_block_blob()
container = self.bsc.get_container_client(self.container_name)
blob_list = container.list_blobs()
#Act
#Assert
for blob in blob_list:
self.assertTrue(blob.server_encrypted)
@record
def test_no_server_encryption(self):
# Arrange
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
#Act
def callback(response):
response.http_response.headers['x-ms-server-encrypted'] = 'false'
props = blob.get_blob_properties(raw_response_hook=callback)
#Assert
self.assertFalse(props.server_encrypted)
@record
def test_get_blob_properties_with_snapshot(self):
# Arrange
blob_name = self._create_block_blob()
container = self.bsc.get_container_client(self.container_name)
blob = self.bsc.get_blob_client(self.container_name, blob_name)
res = blob.create_snapshot()
blobs = list(container.list_blobs(include='snapshots'))
self.assertEqual(len(blobs), 2)
# Act
snapshot = self.bsc.get_blob_client(self.container_name, blob_name, snapshot=res)
props = snapshot.get_blob_properties()
# Assert
self.assertIsNotNone(blob)
self.assertEqual(props.blob_type, BlobType.BlockBlob)
self.assertEqual(props.size, len(self.byte_data))
@record
def test_get_blob_properties_with_leased_blob(self):
# Arrange
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
lease = blob.acquire_lease()
# Act
props = blob.get_blob_properties()
# Assert
self.assertIsInstance(props, BlobProperties)
self.assertEqual(props.blob_type, BlobType.BlockBlob)
self.assertEqual(props.size, len(self.byte_data))
self.assertEqual(props.lease.status, 'locked')
self.assertEqual(props.lease.state, 'leased')
self.assertEqual(props.lease.duration, 'infinite')
@record
def test_get_blob_metadata(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
md = blob.get_blob_properties().metadata
# Assert
self.assertIsNotNone(md)
@record
def test_set_blob_metadata_with_upper_case(self):
# Arrange
metadata = {'hello': 'world', 'number': '42', 'UP': 'UPval'}
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
blob.set_blob_metadata(metadata)
# Assert
md = blob.get_blob_properties().metadata
self.assertEqual(3, len(md))
self.assertEqual(md['hello'], 'world')
self.assertEqual(md['number'], '42')
self.assertEqual(md['UP'], 'UPval')
self.assertFalse('up' in md)
@record
def test_delete_blob_with_existing_blob(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
resp = blob.delete_blob()
# Assert
self.assertIsNone(resp)
@record
def test_delete_blob_with_non_existing_blob(self):
# Arrange
blob_name = self._get_blob_reference()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
with self.assertRaises(ResourceNotFoundError):
blob.delete_blob()
# Assert
@record
def test_delete_blob_snapshot(self):
# Arrange
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
snapshot = self.bsc.get_blob_client(
self.container_name, blob_name, snapshot=blob.create_snapshot())
# Act
snapshot.delete_blob()
# Assert
container = self.bsc.get_container_client(self.container_name)
blobs = list(container.list_blobs(include='snapshots'))
self.assertEqual(len(blobs), 1)
self.assertEqual(blobs[0].name, blob_name)
self.assertIsNone(blobs[0].snapshot)
@record
def test_delete_blob_snapshots(self):
# Arrange
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
blob.create_snapshot()
# Act
blob.delete_blob(delete_snapshots='only')
# Assert
container = self.bsc.get_container_client(self.container_name)
blobs = list(container.list_blobs(include='snapshots'))
self.assertEqual(len(blobs), 1)
self.assertIsNone(blobs[0].snapshot)
@record
def test_delete_blob_with_snapshots(self):
# Arrange
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
blob.create_snapshot()
# Act
#with self.assertRaises(HttpResponseError):
# blob.delete_blob()
blob.delete_blob(delete_snapshots='include')
# Assert
container = self.bsc.get_container_client(self.container_name)
blobs = list(container.list_blobs(include='snapshots'))
self.assertEqual(len(blobs), 0)
@record
def test_soft_delete_blob_without_snapshots(self):
try:
# Arrange
self._enable_soft_delete()
blob_name = self._create_block_blob()
container = self.bsc.get_container_client(self.container_name)
blob = container.get_blob_client(blob_name)
# Soft delete the blob
blob.delete_blob()
blob_list = list(container.list_blobs(include='deleted'))
# Assert
self.assertEqual(len(blob_list), 1)
self._assert_blob_is_soft_deleted(blob_list[0])
# list_blobs should not list soft deleted blobs if Include(deleted=True) is not specified
blob_list = list(container.list_blobs())
# Assert
self.assertEqual(len(blob_list), 0)
# Restore blob with undelete
blob.undelete_blob()
blob_list = list(container.list_blobs(include='deleted'))
# Assert
self.assertEqual(len(blob_list), 1)
self._assert_blob_not_soft_deleted(blob_list[0])
finally:
self._disable_soft_delete()
@record
def test_soft_delete_single_blob_snapshot(self):
try:
# Arrange
self._enable_soft_delete()
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
blob_snapshot_1 = blob.create_snapshot()
blob_snapshot_2 = blob.create_snapshot()
# Soft delete blob_snapshot_1
snapshot_1 = self.bsc.get_blob_client(
self.container_name, blob_name, snapshot=blob_snapshot_1)
snapshot_1.delete_blob()
with self.assertRaises(ValueError):
snapshot_1.delete_blob(delete_snapshots='only')
container = self.bsc.get_container_client(self.container_name)
blob_list = list(container.list_blobs(include=["snapshots", "deleted"]))
# Assert
self.assertEqual(len(blob_list), 3)
for listedblob in blob_list:
if listedblob.snapshot == blob_snapshot_1['snapshot']:
self._assert_blob_is_soft_deleted(listedblob)
else:
self._assert_blob_not_soft_deleted(listedblob)
# list_blobs should not list soft deleted blob snapshots if Include(deleted=True) is not specified
blob_list = list(container.list_blobs(include='snapshots'))
# Assert
self.assertEqual(len(blob_list), 2)
# Restore snapshot with undelete
blob.undelete_blob()
blob_list = list(container.list_blobs(include=["snapshots", "deleted"]))
# Assert
self.assertEqual(len(blob_list), 3)
for blob in blob_list:
self._assert_blob_not_soft_deleted(blob)
finally:
self._disable_soft_delete()
@record
def test_soft_delete_only_snapshots_of_blob(self):
try:
# Arrange
self._enable_soft_delete()
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
blob_snapshot_1 = blob.create_snapshot()
blob_snapshot_2 = blob.create_snapshot()
# Soft delete all snapshots
blob.delete_blob(delete_snapshots='only')
container = self.bsc.get_container_client(self.container_name)
blob_list = list(container.list_blobs(include=["snapshots", "deleted"]))
# Assert
self.assertEqual(len(blob_list), 3)
for listedblob in blob_list:
if listedblob.snapshot == blob_snapshot_1['snapshot']:
self._assert_blob_is_soft_deleted(listedblob)
elif listedblob.snapshot == blob_snapshot_2['snapshot']:
self._assert_blob_is_soft_deleted(listedblob)
else:
self._assert_blob_not_soft_deleted(listedblob)
# list_blobs should not list soft deleted blob snapshots if Include(deleted=True) is not specified
blob_list = list(container.list_blobs(include="snapshots"))
# Assert
self.assertEqual(len(blob_list), 1)
# Restore snapshots with undelete
blob.undelete_blob()
blob_list = list(container.list_blobs(include=["snapshots", "deleted"]))
# Assert
self.assertEqual(len(blob_list), 3)
for blob in blob_list:
self._assert_blob_not_soft_deleted(blob)
finally:
self._disable_soft_delete()
@record
def test_soft_delete_blob_including_all_snapshots(self):
try:
# Arrange
self._enable_soft_delete()
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
blob_snapshot_1 = blob.create_snapshot()
blob_snapshot_2 = blob.create_snapshot()
# Soft delete blob and all snapshots
blob.delete_blob(delete_snapshots='include')
container = self.bsc.get_container_client(self.container_name)
blob_list = list(container.list_blobs(include=["snapshots", "deleted"]))
# Assert
self.assertEqual(len(blob_list), 3)
for listedblob in blob_list:
self._assert_blob_is_soft_deleted(listedblob)
# list_blobs should not list soft deleted blob snapshots if Include(deleted=True) is not specified
blob_list = list(container.list_blobs(include=["snapshots"]))
# Assert
self.assertEqual(len(blob_list), 0)
# Restore blob and snapshots with undelete
blob.undelete_blob()
blob_list = list(container.list_blobs(include=["snapshots", "deleted"]))
# Assert
self.assertEqual(len(blob_list), 3)
for blob in blob_list:
self._assert_blob_not_soft_deleted(blob)
finally:
self._disable_soft_delete()
@record
def test_soft_delete_with_leased_blob(self):
try:
# Arrange
self._enable_soft_delete()
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
lease = blob.acquire_lease()
# Soft delete the blob without lease_id should fail
with self.assertRaises(HttpResponseError):
blob.delete_blob()
# Soft delete the blob
blob.delete_blob(lease=lease)
container = self.bsc.get_container_client(self.container_name)
blob_list = list(container.list_blobs(include="deleted"))
# Assert
self.assertEqual(len(blob_list), 1)
self._assert_blob_is_soft_deleted(blob_list[0])
# list_blobs should not list soft deleted blobs if Include(deleted=True) is not specified
blob_list = list(container.list_blobs())
# Assert
self.assertEqual(len(blob_list), 0)
# Restore blob with undelete, this also gets rid of the lease
blob.undelete_blob()
blob_list = list(container.list_blobs(include="deleted"))
# Assert
self.assertEqual(len(blob_list), 1)
self._assert_blob_not_soft_deleted(blob_list[0])
finally:
self._disable_soft_delete()
@record
def test_copy_blob_with_existing_blob(self):
# Arrange
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
# Act
sourceblob = '{0}/{1}/{2}'.format(
self._get_account_url(), self.container_name, blob_name)
copyblob = self.bsc.get_blob_client(self.container_name, 'blob1copy')
copy = copyblob.start_copy_from_url(sourceblob)
# Assert
self.assertIsNotNone(copy)
self.assertEqual(copy['copy_status'], 'success')
self.assertIsNotNone(copy['copy_id'])
copy_content = copyblob.download_blob().content_as_bytes()
self.assertEqual(copy_content, self.byte_data)
@record
def test_copy_blob_with_external_blob_fails(self):
# Arrange
source_blob = "http://www.gutenberg.org/files/59466/59466-0.txt"
copied_blob = self.bsc.get_blob_client(self.container_name, '59466-0.txt')
# Act
copy = copied_blob.start_copy_from_url(source_blob)
self.assertEqual(copy['copy_status'], 'pending')
props = self._wait_for_async_copy(copied_blob)
# Assert
self.assertEqual(props.copy.status_description, '500 InternalServerError "Copy failed."')
self.assertEqual(props.copy.status, 'failed')
self.assertIsNotNone(props.copy.id)
@record
def test_copy_blob_async_private_blob_no_sas(self):
# Arrange
self._create_remote_container()
source_blob = self._create_remote_block_blob()
# Act
target_blob_name = 'targetblob'
target_blob = self.bsc.get_blob_client(self.container_name, target_blob_name)
# Assert
with self.assertRaises(ResourceNotFoundError):
target_blob.start_copy_from_url(source_blob.url)
@record
def test_copy_blob_async_private_blob_with_sas(self):
# Arrange
data = b'12345678' * 1024 * 1024
self._create_remote_container()
source_blob = self._create_remote_block_blob(blob_data=data)
sas_token = source_blob.generate_shared_access_signature(
permission=BlobPermissions.READ,
expiry=datetime.utcnow() + timedelta(hours=1),
)
blob = BlobClient(source_blob.url, credential=sas_token)
# Act
target_blob_name = 'targetblob'
target_blob = self.bsc.get_blob_client(self.container_name, target_blob_name)
copy_resp = target_blob.start_copy_from_url(blob.url)
# Assert
props = self._wait_for_async_copy(target_blob)
self.assertEqual(props.copy.status, 'success')
actual_data = target_blob.download_blob()
self.assertEqual(b"".join(list(actual_data)), data)
@record
def test_abort_copy_blob(self):
# Arrange
source_blob = "http://www.gutenberg.org/files/59466/59466-0.txt"
copied_blob = self.bsc.get_blob_client(self.container_name, '59466-0.txt')
# Act
copy = copied_blob.start_copy_from_url(source_blob)
self.assertEqual(copy['copy_status'], 'pending')
copied_blob.abort_copy(copy)
props = self._wait_for_async_copy(copied_blob)
self.assertEqual(props.copy.status, 'aborted')
# Assert
actual_data = copied_blob.download_blob()
self.assertEqual(b"".join(list(actual_data)), b"")
self.assertEqual(actual_data.properties.copy.status, 'aborted')
@record
def test_abort_copy_blob_with_synchronous_copy_fails(self):
# Arrange
source_blob_name = self._create_block_blob()
source_blob = self.bsc.get_blob_client(self.container_name, source_blob_name)
# Act
target_blob_name = 'targetblob'
target_blob = self.bsc.get_blob_client(self.container_name, target_blob_name)
copy_resp = target_blob.start_copy_from_url(source_blob.url)
with self.assertRaises(HttpResponseError):
target_blob.abort_copy(copy_resp)
# Assert
self.assertEqual(copy_resp['copy_status'], 'success')
@record
def test_snapshot_blob(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
resp = blob.create_snapshot()
# Assert
self.assertIsNotNone(resp)
self.assertIsNotNone(resp['snapshot'])
@record
def test_lease_blob_acquire_and_release(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
lease = blob.acquire_lease()
lease.release()
lease2 = blob.acquire_lease()
# Assert
self.assertIsNotNone(lease)
self.assertIsNotNone(lease2)
@record
def test_lease_blob_with_duration(self):
# Arrange
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
lease = blob.acquire_lease(lease_duration=15)
resp = blob.upload_blob(b'hello 2', length=7, lease=lease)
self.sleep(15)
# Assert
with self.assertRaises(HttpResponseError):
blob.upload_blob(b'hello 3', length=7, lease=lease)
@record
def test_lease_blob_with_proposed_lease_id(self):
# Arrange