forked from Azure/azure-sdk-for-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_file_async.py
More file actions
4190 lines (3486 loc) · 172 KB
/
Copy pathtest_file_async.py
File metadata and controls
4190 lines (3486 loc) · 172 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
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import asyncio
import base64
import os
import tempfile
from datetime import datetime, timedelta, timezone
import pytest
import requests
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential
from azure.core.exceptions import ClientAuthenticationError, HttpResponseError, ResourceExistsError, ResourceNotFoundError
from azure.storage.blob.aio import BlobServiceClient
from azure.storage.fileshare import (
AccessPolicy,
AccountSasPermissions,
ContentSettings,
FileSasPermissions,
generate_account_sas,
generate_file_sas,
generate_share_sas,
NTFSAttributes,
ResourceTypes,
ShareSasPermissions,
StorageErrorCode
)
from azure.storage.fileshare.aio import ShareFileClient, ShareServiceClient
from devtools_testutils.aio import recorded_by_proxy_async
from devtools_testutils.storage.aio import AsyncStorageRecordedTestCase
from settings.testcase import FileSharePreparer
from test_helpers_async import AsyncStream, MockStorageTransport, ProgressTracker
# ------------------------------------------------------------------------------
TEST_SHARE_PREFIX = 'share'
TEST_DIRECTORY_PREFIX = 'dir'
TEST_FILE_PREFIX = 'file'
TEST_BLOB_PREFIX = 'blob'
LARGE_FILE_SIZE = 64 * 1024 + 5
TEST_FILE_PERMISSIONS = 'O:S-1-5-21-2127521184-1604012920-1887927527-21560751G:S-1-5-21-2127521184-' \
'1604012920-1887927527-513D:AI(A;;FA;;;SY)(A;;FA;;;BA)(A;;0x1200a9;;;' \
'S-1-5-21-397955417-626881126-188441444-3053964)'
TEST_INTENT = "backup"
# ------------------------------------------------------------------------------
class TestStorageFileAsync(AsyncStorageRecordedTestCase):
def _setup(self, storage_account_name, storage_account_key, rmt_account=None, rmt_key=None):
url = self.account_url(storage_account_name, "file")
blob_url = self.account_url(storage_account_name, "blob")
credential = storage_account_key
# 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.fsc = ShareServiceClient(
url, credential=credential.secret, max_range_size=4 * 1024)
self.bsc = BlobServiceClient(blob_url, credential=credential.secret)
self.source_container_name = self.get_resource_name('sourceshare')
self.share_name = self.get_resource_name('utshare')
self.short_byte_data = self.get_random_bytes(1024)
remote_url = self.account_url(rmt_account, "file")
remote_credential = rmt_key
if rmt_account:
self.fsc2 = ShareServiceClient(remote_url, credential=remote_credential)
self.remote_share_name = None
def _teardown(self, FILE_PATH):
if os.path.isfile(FILE_PATH):
try:
os.remove(FILE_PATH)
except:
pass
# --Helpers-----------------------------------------------------------------
def _get_file_reference(self):
return self.get_resource_name(TEST_FILE_PREFIX)
async def _create_source_blob(self):
try:
await self.bsc.create_container(self.source_container_name)
except:
pass
blob_client = self.bsc.get_blob_client(self.source_container_name, self.get_resource_name(TEST_BLOB_PREFIX))
await blob_client.upload_blob(b'abcdefghijklmnop' * 32, overwrite=True)
return blob_client
async def _setup_share(self, storage_account_name, storage_account_key, remote=False):
share_name = self.remote_share_name if remote else self.share_name
async with ShareServiceClient(
self.account_url(storage_account_name, "file"),
credential=storage_account_key.secret,
max_range_size=4 * 1024) as fsc:
if not self.is_playback():
try:
await fsc.create_share(share_name)
except:
pass
async def _create_file(self, storage_account_name, storage_account_key, file_name=None):
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference() if file_name is None else file_name
share_client = self.fsc.get_share_client(self.share_name)
file_client = share_client.get_file_client(file_name)
await file_client.upload_file(self.short_byte_data)
return file_client
async def _create_empty_file(self, storage_account_name, storage_account_key, file_name=None, file_size=2048):
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference() if file_name is None else file_name
share_client = self.fsc.get_share_client(self.share_name)
file_client = share_client.get_file_client(file_name)
await file_client.create_file(file_size)
return file_client
async def _get_file_client(self, storage_account_name, storage_account_key):
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference()
share_client = self.fsc.get_share_client(self.share_name)
file_client = share_client.get_file_client(file_name)
return file_client
async def _create_remote_share(self):
self.remote_share_name = self.get_resource_name('remoteshare')
remote_share = self.fsc2.get_share_client(self.remote_share_name)
try:
await remote_share.create_share()
except ResourceExistsError:
pass
return remote_share
async def _create_remote_file(self, file_data=None):
if not file_data:
file_data = b'12345678' * 1024
source_file_name = self._get_file_reference()
remote_share = self.fsc2.get_share_client(self.remote_share_name)
remote_file = remote_share.get_file_client(source_file_name)
await remote_file.upload_file(file_data)
return remote_file
async def _wait_for_async_copy(self, share_name, file_path):
count = 0
share_client = self.fsc.get_share_client(share_name)
file_client = share_client.get_file_client(file_path)
properties = await file_client.get_file_properties()
while properties.copy.status != 'success':
count = count + 1
if count > 15:
pytest.fail('Timed out waiting for async copy to complete.')
self.sleep(6)
properties = await file_client.get_file_properties()
assert properties.copy.status == 'success'
async def assertFileEqual(self, file_client, expected_data, **kwargs):
content = await file_client.download_file(**kwargs)
actual_data = await content.readall()
assert actual_data == 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 files ----------------------------------------------
@FileSharePreparer()
@recorded_by_proxy_async
async def test_make_file_url(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
share = self.fsc.get_share_client("vhds")
file_client = share.get_file_client("vhd_dir/my.vhd")
# Act
res = file_client.url
# Assert
assert res == ('https://' + storage_account_name + '.file.core.windows.net/vhds/vhd_dir/my.vhd')
@FileSharePreparer()
@recorded_by_proxy_async
async def test_make_file_url_no_directory(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
share = self.fsc.get_share_client("vhds")
file_client = share.get_file_client("my.vhd")
# Act
res = file_client.url
# Assert
assert res == ('https://' + storage_account_name + '.file.core.windows.net/vhds/my.vhd')
@FileSharePreparer()
@recorded_by_proxy_async
async def test_make_file_url_with_protocol(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
url = self.account_url(storage_account_name, "file").replace('https', 'http')
fsc = ShareServiceClient(url, credential=storage_account_key.secret)
share = fsc.get_share_client("vhds")
file_client = share.get_file_client("vhd_dir/my.vhd")
# Act
res = file_client.url
# Assert
assert res == ('http://' + storage_account_name + '.file.core.windows.net/vhds/vhd_dir/my.vhd')
@FileSharePreparer()
@recorded_by_proxy_async
async def test_make_file_url_with_sas(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
# cspell:disable-next-line
sas = '?sv=2015-04-05&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sr=b&sp=rw&sip=168.1.5.60-168.1.5.70&spr=https&sig=Z%2FRHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk%3D'
file_client = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name="vhds",
file_path="vhd_dir/my.vhd",
credential=sas
)
# Act
res = file_client.url
# Assert
assert res == ('https://' + storage_account_name + '.file.core.windows.net/vhds/vhd_dir/my.vhd{}'.format(sas))
@FileSharePreparer()
@recorded_by_proxy_async
async def test_exists(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference()
async with ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name,
credential=storage_account_key.secret) as file_client:
# Act / Assert
assert not await file_client.exists()
await file_client.create_file(1024)
assert await file_client.exists()
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference()
async with ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name,
credential=storage_account_key.secret) as file_client:
# Act
resp = await file_client.create_file(1024)
# Assert
props = await file_client.get_file_properties()
assert props is not None
assert props.etag == resp['etag']
assert props.last_modified == resp['last_modified']
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_with_oauth(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
token_credential = self.get_credential(ShareServiceClient, is_async=True)
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference()
async with ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name,
credential=token_credential,
token_intent=TEST_INTENT) as file_client:
# Act
resp = await file_client.create_file(1024)
# Assert
props = await file_client.get_file_properties()
assert props is not None
assert props.etag == resp['etag']
assert props.last_modified == resp['last_modified']
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_with_oauth_no_token_intent(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
token_credential = self.get_credential(ShareServiceClient, is_async=True)
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference()
# Assert
with pytest.raises(ValueError):
file_client = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name,
credential=token_credential
)
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_with_trailing_dot(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference()
async with ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name + '.',
credential=storage_account_key.secret,
allow_trailing_dot=True) as file_client:
# Act
resp = await file_client.create_file(1024)
# Assert
props = await file_client.get_file_properties()
assert props is not None
assert props.etag == resp['etag']
assert props.last_modified == resp['last_modified']
assert props.name == file_name + '.'
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_with_trailing_dot_false(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference()
file_client = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name + '.',
credential=storage_account_key.secret,
allow_trailing_dot=False)
# Act
resp = await file_client.create_file(1024)
# create file client with dot
file_client_dotted = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name + '.',
credential=storage_account_key.secret,
allow_trailing_dot=False)
# create file client without dot
file_client_no_dot = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name,
credential=storage_account_key.secret,
allow_trailing_dot=False)
props = await file_client.get_file_properties()
props_dotted = await file_client_dotted.get_file_properties()
props_no_dot = await file_client_no_dot.get_file_properties()
# Assert
assert props.name == file_name + '.'
assert props.path == file_name + '.'
assert props_dotted.name == file_name + '.'
assert props_dotted.path == file_name + '.'
assert props_no_dot.name == file_name
assert props_no_dot.path == file_name
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_with_metadata(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
metadata = {'hello': 'world', 'number': '42'}
file_name = self._get_file_reference()
async with ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name,
credential=storage_account_key.secret) as file_client:
# Act
resp = await file_client.create_file(1024, metadata=metadata)
# Assert
props = await file_client.get_file_properties()
assert props is not None
assert props.etag == resp['etag']
assert props.last_modified == resp['last_modified']
assert props.metadata == metadata
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_with_metadata_with_trailing_dot(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
metadata = {'hello': 'world', 'number': '42'}
file_name = self._get_file_reference()
async with ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name + '.',
credential=storage_account_key.secret,
allow_trailing_dot=True) as file_client:
# Act
resp = await file_client.create_file(1024, metadata=metadata)
# Assert
props = await file_client.get_file_properties()
assert props is not None
assert props.etag == resp['etag']
assert props.last_modified == resp['last_modified']
assert props.metadata == metadata
assert props.name == file_name + '.'
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_when_file_permission_is_too_long(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
file_client = await self._get_file_client(storage_account_name, storage_account_key)
permission = str(self.get_random_bytes(8 * 1024 + 1))
with pytest.raises(ValueError):
await file_client.create_file(1024, file_permission=permission)
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_with_invalid_file_permission(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
file_name = await self._get_file_client(storage_account_name, storage_account_key)
with pytest.raises(HttpResponseError):
await file_name.create_file(1024, file_permission="abcde")
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_semantics(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference()
file1 = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name + "file1",
credential=storage_account_key.secret
)
await file1.create_file(1024, file_property_semantics=None)
props = await file1.get_file_properties()
assert props is not None
file2 = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name + "file2",
credential=storage_account_key.secret
)
await file2.create_file(1024, file_property_semantics="New")
props = await file2.get_file_properties()
assert props is not None
file3 = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name + "file2",
credential=storage_account_key.secret
)
await file3.create_file(1024, file_property_semantics="Restore", file_permission=TEST_FILE_PERMISSIONS)
props = await file3.get_file_properties()
assert props is not None
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_with_lease(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
file_client = await self._get_file_client(storage_account_name, storage_account_key)
await file_client.create_file(1024)
lease = await file_client.acquire_lease(lease_id='00000000-1111-2222-3333-444444444444')
resp = await file_client.create_file(1024, lease=lease)
assert resp is not None
# There is currently a lease on the file so there should be an exception when delete the file without lease
with pytest.raises(HttpResponseError):
await file_client.delete_file()
# There is currently a lease on the file so delete the file with the lease will succeed
await file_client.delete_file(lease=lease)
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_with_lease_oauth(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
token_credential = self.get_credential(ShareServiceClient, is_async=True)
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference()
file_client = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name,
credential=token_credential,
token_intent=TEST_INTENT)
await file_client.create_file(1024)
lease = await file_client.acquire_lease(lease_id='00000000-1111-2222-3333-444444444444')
resp = await file_client.create_file(1024, lease=lease)
assert resp is not None
# There is currently a lease on the file so there should be an exception when delete the file without lease
with pytest.raises(HttpResponseError):
await file_client.delete_file()
# There is currently a lease on the file so delete the file with the lease will succeed
await file_client.delete_file(lease=lease)
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_with_changed_lease(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
file_client = await self._get_file_client(storage_account_name, storage_account_key)
await file_client.create_file(1024)
lease = await file_client.acquire_lease(lease_id='00000000-1111-2222-3333-444444444444')
old_lease_id = lease.id
await lease.change('44444444-3333-2222-1111-000000000000')
# use the old lease id to create file will throw exception.
with pytest.raises(HttpResponseError):
await file_client.create_file(1024, lease=old_lease_id)
# use the new lease to create file will succeed.
resp = await file_client.create_file(1024, lease=lease)
assert resp is not None
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_with_changed_lease_oauth(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
token_credential = self.get_credential(ShareServiceClient, is_async=True)
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference()
file_client = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name,
credential=token_credential,
token_intent=TEST_INTENT)
await file_client.create_file(1024)
lease = await file_client.acquire_lease(lease_id='00000000-1111-2222-3333-444444444444')
old_lease_id = lease.id
await lease.change('44444444-3333-2222-1111-000000000000')
# use the old lease id to create file will throw exception.
with pytest.raises(HttpResponseError):
await file_client.create_file(1024, lease=old_lease_id)
# use the new lease to create file will succeed.
resp = await file_client.create_file(1024, lease=lease)
assert resp is not None
@FileSharePreparer()
@recorded_by_proxy_async
async def test_lease_operations_trailing_dot(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference()
file_client = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name + '.',
credential=storage_account_key.secret,
allow_trailing_dot=True)
await file_client.create_file(1024)
lease = await file_client.acquire_lease(lease_id='00000000-1111-2222-3333-444444444444')
old_lease_id = lease.id
await lease.change('44444444-3333-2222-1111-000000000000')
# use the old lease id to create file will throw exception.
with pytest.raises(HttpResponseError):
await file_client.create_file(1024, lease=old_lease_id)
# use the new lease to create file will succeed.
resp = await file_client.create_file(1024, lease=lease)
# break the lease
await lease.break_lease()
# create file without lease to show lease is broken
resp = await file_client.create_file(1024)
props = await file_client.get_file_properties()
# Assert
assert resp is not None
assert props.name == file_name + '.'
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_will_set_all_smb_properties(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
file_client = await self._get_file_client(storage_account_name, storage_account_key)
# Act
await file_client.create_file(1024)
file_properties = await file_client.get_file_properties()
# Assert
assert file_properties is not None
assert file_properties.change_time is not None
assert file_properties.creation_time is not None
assert file_properties.file_attributes is not None
assert file_properties.last_write_time is not None
@FileSharePreparer()
@recorded_by_proxy_async
async def test_create_file_set_smb_properties(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
file_client = await self._get_file_client(storage_account_name, storage_account_key)
file_attributes = NTFSAttributes(read_only=True, archive=True)
file_creation_time = file_last_write_time = file_change_time = datetime(2022, 3, 10, 10, 14, 30, 500000, tzinfo=timezone.utc)
# Act
await file_client.create_file(
size=1024,
file_attributes=file_attributes,
file_creation_time=file_creation_time,
file_last_write_time=file_last_write_time,
file_change_time=file_change_time)
file_properties = await file_client.get_file_properties()
# Assert
assert file_properties is not None
assert file_creation_time == file_properties.creation_time
assert file_last_write_time == file_properties.last_write_time
assert file_change_time == file_properties.change_time
assert 'ReadOnly' in file_properties.file_attributes
assert 'Archive' in file_properties.file_attributes
@FileSharePreparer()
@recorded_by_proxy_async
async def test_file_exists(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
file_client = await self._create_file(storage_account_name, storage_account_key)
# Act
exists = await file_client.get_file_properties()
# Assert
assert exists
@FileSharePreparer()
@recorded_by_proxy_async
async def test_snapshot_exists(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory_name = self.get_resource_name("directory")
directory_client = await share_client.create_directory(directory_name)
file_name = self._get_file_reference()
file_client = directory_client.get_file_client(file_name)
await file_client.upload_file(self.short_byte_data)
snapshot = await share_client.create_snapshot()
share_snapshot_client = self.fsc.get_share_client(self.share_name, snapshot=snapshot)
file_snapshot_client = share_snapshot_client.get_directory_client(directory_name).get_file_client(file_name)
await file_client.delete_file()
# Act
props = await file_snapshot_client.download_file()
# Assert
assert props
@FileSharePreparer()
@recorded_by_proxy_async
async def test_file_not_exists(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
file_name = self._get_file_reference()
file_client = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path="missingdir/" + file_name,
credential=storage_account_key.secret)
# Act
with pytest.raises(ResourceNotFoundError):
await file_client.get_file_properties()
@FileSharePreparer()
@recorded_by_proxy_async
async def test_file_exists_with_snapshot(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
file_client = await self._create_file(storage_account_name, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
snapshot = await share_client.create_snapshot()
await file_client.delete_file()
# Act
snapshot_client = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_client.file_name,
snapshot=snapshot,
credential=storage_account_key.secret)
props = await snapshot_client.get_file_properties()
# Assert
assert props
@FileSharePreparer()
@recorded_by_proxy_async
async def test_file_not_exists_with_snapshot(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
snapshot = await share_client.create_snapshot()
file_client = await self._create_file(storage_account_name, storage_account_key)
# Act
snapshot_client = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_client.file_name,
snapshot=snapshot,
credential=storage_account_key.secret)
# Assert
with pytest.raises(ResourceNotFoundError):
await snapshot_client.get_file_properties()
@FileSharePreparer()
@recorded_by_proxy_async
async def test_resize_file(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
file_client = await self._create_file(storage_account_name, storage_account_key)
# Act
await file_client.resize_file(5)
# Assert
props = await file_client.get_file_properties()
assert props.size == 5
@FileSharePreparer()
@recorded_by_proxy_async
async def test_resize_file_with_lease(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
file_client = await self._create_file(storage_account_name, storage_account_key)
lease = await file_client.acquire_lease(lease_id='00000000-1111-2222-3333-444444444444')
# Act
with pytest.raises(HttpResponseError):
await file_client.resize_file(5)
await file_client.resize_file(5, lease=lease)
# Assert
props = await file_client.get_file_properties()
assert props.size == 5
@FileSharePreparer()
@recorded_by_proxy_async
async def test_set_file_properties(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
file_client = await self._create_file(storage_account_name, storage_account_key)
# Act
content_settings = ContentSettings(
content_language='spanish',
content_disposition='inline')
resp = await file_client.set_http_headers(content_settings=content_settings)
# Assert
properties = await file_client.get_file_properties()
assert properties.content_settings.content_language == content_settings.content_language
assert properties.content_settings.content_disposition == content_settings.content_disposition
assert properties.last_write_time is not None
assert properties.creation_time is not None
assert properties.permission_key is not None
@FileSharePreparer()
@recorded_by_proxy_async
async def test_set_datetime_all_ms_precision(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
file_client = await self._create_file(storage_account_name, storage_account_key)
date_times = [
datetime(3005, 5, 11, 12, 24, 7),
datetime(3005, 5, 11, 12, 24, 7, 0),
datetime(3005, 5, 11, 12, 24, 7, 1),
datetime(3005, 5, 11, 12, 24, 7, 12),
datetime(3005, 5, 11, 12, 24, 7, 123),
datetime(3005, 5, 11, 12, 24, 7, 1234),
datetime(3005, 5, 11, 12, 24, 7, 12345),
datetime(3005, 5, 11, 12, 24, 7, 123456),
datetime(2023, 12, 8, tzinfo=timezone(-timedelta(hours=7))),
datetime(2023, 12, 8, tzinfo=timezone(-timedelta(hours=8))),
]
# Act / Assert
content_settings = ContentSettings(
content_language='spanish',
content_disposition='inline')
for date1, date2 in zip(date_times[::2], date_times[1::2]):
await file_client.set_http_headers(
content_settings=content_settings,
file_creation_time=date1,
file_last_write_time=date2
)
@FileSharePreparer()
@recorded_by_proxy_async
async def test_set_file_properties_trailing_dot(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference()
file_client = ShareFileClient(
self.account_url(storage_account_name, "file"),
share_name=self.share_name,
file_path=file_name + '.',
credential=storage_account_key.secret,
allow_trailing_dot=True)
await file_client.create_file(1024)
# Act
content_settings = ContentSettings(
content_language='spanish',
content_disposition='inline')
resp = await file_client.set_http_headers(content_settings=content_settings)
# Assert
properties = await file_client.get_file_properties()
assert properties.content_settings.content_language == content_settings.content_language
assert properties.content_settings.content_disposition == content_settings.content_disposition
assert properties.last_write_time is not None
assert properties.creation_time is not None
assert properties.permission_key is not None
assert properties.name == file_name + '.'
@FileSharePreparer()
@recorded_by_proxy_async
async def test_set_file_properties_with_file_permission(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
self._setup(storage_account_name, storage_account_key)
file_client = await self._create_file(storage_account_name, storage_account_key)
properties_on_creation = await file_client.get_file_properties()
content_settings = ContentSettings(
content_language='spanish',
content_disposition='inline')
ntfs_attributes = NTFSAttributes(archive=True, temporary=True)
last_write_time = properties_on_creation.last_write_time + timedelta(hours=3)
creation_time = properties_on_creation.creation_time + timedelta(hours=3)
change_time = properties_on_creation.change_time + timedelta(hours=3)
# Act
await file_client.set_http_headers(
content_settings=content_settings,
file_attributes=ntfs_attributes,
file_last_write_time=last_write_time,
file_creation_time=creation_time,
file_change_time=change_time
)
# Assert
properties = await file_client.get_file_properties()
assert properties.content_settings.content_language == content_settings.content_language
assert properties.content_settings.content_disposition == content_settings.content_disposition
assert properties.creation_time == creation_time
assert properties.last_write_time == last_write_time
assert properties.change_time == change_time
assert "Archive" in properties.file_attributes
assert "Temporary" in properties.file_attributes
@FileSharePreparer()
@recorded_by_proxy_async
async def test_set_file_properties_with_oauth(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
token_credential = self.get_credential(ShareServiceClient, is_async=True)
self._setup(storage_account_name, storage_account_key)
await self._setup_share(storage_account_name, storage_account_key)
file_name = self._get_file_reference()
async with ShareFileClient(
self.account_url(storage_account_name, "file"),