-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathtest_common_blob.py
2019 lines (1607 loc) · 79.3 KB
/
test_common_blob.py
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.
# --------------------------------------------------------------------------
from enum import Enum
import pytest
import requests
import time
import unittest
import uuid
import os
import sys
from datetime import datetime, timedelta
from azure.core.exceptions import (
HttpResponseError,
ResourceNotFoundError,
ResourceExistsError,
ClientAuthenticationError)
from azure.core.pipeline.transport import RequestsTransport
from azure.storage.blob import (
upload_blob_to_url,
download_blob_from_url,
generate_account_sas,
generate_blob_sas,
generate_container_sas,
BlobServiceClient,
ContainerClient,
BlobClient,
BlobType,
StorageErrorCode,
BlobSasPermissions,
ContainerSasPermissions,
ContentSettings,
BlobProperties,
RetentionPolicy,
AccessPolicy,
ResourceTypes,
AccountSasPermissions,
StandardBlobTier,
)
from azure.storage.blob._generated.models import RehydratePriority
from devtools_testutils import ResourceGroupPreparer, StorageAccountPreparer
from _shared.testcase import StorageTestCase, GlobalStorageAccountPreparer
#------------------------------------------------------------------------------
TEST_CONTAINER_PREFIX = 'container'
TEST_BLOB_PREFIX = 'blob'
#------------------------------------------------------------------------------
class StorageCommonBlobTest(StorageTestCase):
def _setup(self, storage_account, key):
self.bsc = BlobServiceClient(self.account_url(storage_account, "blob"), credential=key)
self.container_name = self.get_resource_name('utcontainer')
if self.is_live:
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)
def _setup_remote(self, storage_account, key):
self.bsc2 = BlobServiceClient(self.account_url(storage_account, "blob"), credential=key)
self.remote_container_name = 'rmt'
def _teardown(self, FILE_PATH):
if os.path.isfile(FILE_PATH):
try:
os.remove(FILE_PATH)
except:
pass
#--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, overwrite=True)
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 self.is_live:
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 ----------------------------------------------
@GlobalStorageAccountPreparer()
def test_blob_exists(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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)
@GlobalStorageAccountPreparer()
def test_blob_not_exists(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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()
@GlobalStorageAccountPreparer()
def test_blob_snapshot_exists(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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)
@GlobalStorageAccountPreparer()
def test_blob_snapshot_not_exists(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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()
@GlobalStorageAccountPreparer()
def test_blob_container_not_exists(self, resource_group, location, storage_account, storage_account_key):
# In this case both the blob and container do not exist
self._setup(storage_account, storage_account_key)
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()
@GlobalStorageAccountPreparer()
def test_create_blob_with_question_mark(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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(encoding='utf-8')
self.assertIsNotNone(data)
self.assertEqual(data.readall(), blob_data)
@GlobalStorageAccountPreparer()
def test_create_blob_with_special_chars(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
# 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(encoding='utf-8')
self.assertEqual(data.readall(), blob_data)
# Assert
@GlobalStorageAccountPreparer()
def test_create_blob_with_lease_id(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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 = blob.download_blob(lease=lease).readall()
self.assertEqual(content, data)
@GlobalStorageAccountPreparer()
def test_create_blob_with_generator(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
# Act
def gen():
yield "hello"
yield "world!"
yield " eom"
blob = self.bsc.get_blob_client(self.container_name, "gen_blob")
resp = blob.upload_blob(data=gen())
# Assert
self.assertIsNotNone(resp.get('etag'))
content = blob.download_blob().readall()
self.assertEqual(content, b"helloworld! eom")
@pytest.mark.live_test_only
@GlobalStorageAccountPreparer()
def test_create_blob_with_requests(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
# Act
uri = "http://www.gutenberg.org/files/59466/59466-0.txt"
data = requests.get(uri, stream=True)
blob = self.bsc.get_blob_client(self.container_name, "gutenberg")
resp = blob.upload_blob(data=data.raw)
self.assertIsNotNone(resp.get('etag'))
@GlobalStorageAccountPreparer()
def test_create_blob_with_metadata(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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)
@GlobalStorageAccountPreparer()
def test_get_blob_with_existing_blob(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
blob_name = self._create_block_blob()
# Act
blob = self.bsc.get_blob_client(self.container_name, blob_name)
data = blob.download_blob()
# Assert
content = data.readall()
self.assertEqual(content, self.byte_data)
@GlobalStorageAccountPreparer()
def test_get_blob_with_snapshot(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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 = data.readall()
self.assertEqual(content, self.byte_data)
@GlobalStorageAccountPreparer()
def test_get_blob_with_snapshot_previous(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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(blob_previous.readall(), self.byte_data)
self.assertEqual(blob_latest.readall(), b'hello world again')
@GlobalStorageAccountPreparer()
def test_get_blob_with_range(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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(data.readall(), self.byte_data[:5])
@GlobalStorageAccountPreparer()
def test_get_blob_with_lease(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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(data.readall(), self.byte_data)
@GlobalStorageAccountPreparer()
def test_get_blob_with_non_existing_blob(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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
@GlobalStorageAccountPreparer()
def test_set_blob_properties_with_existing_blob(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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')
@GlobalStorageAccountPreparer()
def test_set_blob_properties_with_blob_settings_param(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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')
@GlobalStorageAccountPreparer()
def test_get_blob_properties(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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.
@GlobalStorageAccountPreparer()
def test_get_blob_properties_fail(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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.
@GlobalStorageAccountPreparer()
def test_get_blob_metadata_fail(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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)
@GlobalStorageAccountPreparer()
def test_get_blob_server_encryption(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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)
@GlobalStorageAccountPreparer()
def test_get_blob_properties_server_encryption(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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)
@GlobalStorageAccountPreparer()
def test_list_blobs_server_encryption(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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)
@GlobalStorageAccountPreparer()
def test_no_server_encryption(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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)
@GlobalStorageAccountPreparer()
def test_get_blob_properties_with_snapshot(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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))
@GlobalStorageAccountPreparer()
def test_get_blob_properties_with_leased_blob(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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')
@GlobalStorageAccountPreparer()
def test_get_blob_metadata(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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)
@pytest.mark.live_test_only
@GlobalStorageAccountPreparer()
def test_set_blob_metadata_with_upper_case(self, resource_group, location, storage_account, storage_account_key):
# bug in devtools...converts upper case header to lowercase
# passes live.
self._setup(storage_account, storage_account_key)
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)
@GlobalStorageAccountPreparer()
def test_delete_blob_with_existing_blob(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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)
@GlobalStorageAccountPreparer()
def test_delete_blob_with_non_existing_blob(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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
@GlobalStorageAccountPreparer()
def test_delete_blob_snapshot(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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)
@GlobalStorageAccountPreparer()
def test_delete_blob_snapshots(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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)
@GlobalStorageAccountPreparer()
def test_delete_blob_with_snapshots(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
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)
@GlobalStorageAccountPreparer()
def test_soft_delete_blob_without_snapshots(self, resource_group, location, storage_account, storage_account_key):
try:
self._setup(storage_account, storage_account_key)
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()
@GlobalStorageAccountPreparer()
def test_soft_delete_single_blob_snapshot(self, resource_group, location, storage_account, storage_account_key):
try:
self._setup(storage_account, storage_account_key)
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()
@GlobalStorageAccountPreparer()
def test_soft_delete_only_snapshots_of_blob(self, resource_group, location, storage_account, storage_account_key):
try:
self._setup(storage_account, storage_account_key)
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()
@GlobalStorageAccountPreparer()
def test_soft_delete_blob_including_all_snapshots(self, resource_group, location, storage_account, storage_account_key):
try:
self._setup(storage_account, storage_account_key)
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()
@GlobalStorageAccountPreparer()
def test_soft_delete_with_leased_blob(self, resource_group, location, storage_account, storage_account_key):
try:
self._setup(storage_account, storage_account_key)
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()
@GlobalStorageAccountPreparer()
def test_copy_blob_with_existing_blob(self, resource_group, location, storage_account, storage_account_key):
self._setup(storage_account, storage_account_key)
blob_name = self._create_block_blob()
blob = self.bsc.get_blob_client(self.container_name, blob_name)
# Act
sourceblob = '{0}/{1}/{2}'.format(
self.account_url(storage_account, "blob"), 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.assertFalse(isinstance(copy['copy_status'], Enum))
self.assertIsNotNone(copy['copy_id'])
copy_content = copyblob.download_blob().readall()
self.assertEqual(copy_content, self.byte_data)
@GlobalStorageAccountPreparer()
def test_copy_blob_with_blob_tier_specified(self, resource_group, location, storage_account, storage_account_key):
pytest.skip("Unable to set premium account")
# Arrange
self._setup(storage_account, storage_account_key)
blob_name = self._create_block_blob()
self.bsc.get_blob_client(self.container_name, blob_name)
# Act
sourceblob = '{0}/{1}/{2}'.format(
self.account_url(storage_account, "blob"), self.container_name, blob_name)
copyblob = self.bsc.get_blob_client(self.container_name, 'blob1copy')
blob_tier = StandardBlobTier.Cool
copyblob.start_copy_from_url(sourceblob, standard_blob_tier=blob_tier)
copy_blob_properties = copyblob.get_blob_properties()
# Assert
self.assertEqual(copy_blob_properties.blob_tier, blob_tier)
@GlobalStorageAccountPreparer()
def test_copy_blob_with_rehydrate_priority(self, resource_group, location, storage_account, storage_account_key):
# Arrange
pytest.skip("Unabe to set up premium storage account type")
self._setup(storage_account, storage_account_key)
blob_name = self._create_block_blob()
# Act
sourceblob = '{0}/{1}/{2}'.format(
self.account_url(storage_account, "blob"), self.container_name, blob_name)
blob_tier = StandardBlobTier.Archive
rehydrate_priority = RehydratePriority.high
copyblob = self.bsc.get_blob_client(self.container_name, 'blob1copy')
copy = copyblob.start_copy_from_url(sourceblob,
standard_blob_tier=blob_tier,
rehydrate_priority=rehydrate_priority)
copy_blob_properties = copyblob.get_blob_properties()
copyblob.set_standard_blob_tier(StandardBlobTier.Hot)
second_resp = copyblob.get_blob_properties()
# Assert
self.assertIsNotNone(copy)
self.assertIsNotNone(copy.get('copy_id'))
self.assertEqual(copy_blob_properties.blob_tier, blob_tier)
self.assertEqual(second_resp.archive_status, 'rehydrate-pending-to-hot')
# TODO: external copy was supported since 2019-02-02
# @record
# def test_copy_blob_with_external_blob_fails(self):
# # Arrange
# source_blob = "http://www.google.com"
# 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, 'success')
# self.assertIsNotNone(props.copy.id)
@GlobalStorageAccountPreparer()
@StorageAccountPreparer(random_name_enabled=True, name_prefix='pyrmtstorage', parameter_name='rmt')
def test_copy_blob_async_private_blob_no_sas(self, resource_group, location, storage_account, storage_account_key, rmt, rmt_key):
self._setup(storage_account, storage_account_key)
self._setup_remote(rmt, rmt_key)
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)
@GlobalStorageAccountPreparer()
@StorageAccountPreparer(random_name_enabled=True, name_prefix='pyrmtstorage', parameter_name='rmt')