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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
|
# -------------------------------------------------------------------------
# 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 tempfile
import unittest
from datetime import datetime, timedelta
from math import ceil
import pytest
from azure.core import MatchConditions
from azure.core.credentials import AzureSasCredential
from azure.core.exceptions import (
ClientAuthenticationError,
HttpResponseError,
ResourceExistsError,
ResourceModifiedError,
ResourceNotFoundError
)
from azure.storage.filedatalake import (
AccountSasPermissions,
ContentSettings,
EncryptionScopeOptions,
FileSasPermissions,
FileSystemSasPermissions,
generate_account_sas,
generate_file_sas,
generate_file_system_sas,
ResourceTypes
)
from azure.storage.filedatalake.aio import DataLakeDirectoryClient, DataLakeFileClient, DataLakeServiceClient, FileSystemClient
from devtools_testutils.aio import recorded_by_proxy_async
from devtools_testutils.storage.aio import AsyncStorageRecordedTestCase
from settings.testcase import DataLakePreparer
from test_helpers_async import AsyncStream, MockStorageTransport, ProgressTracker
# ------------------------------------------------------------------------------
TEST_DIRECTORY_PREFIX = 'directory'
TEST_FILE_PREFIX = 'file'
# ------------------------------------------------------------------------------
class TestFileAsync(AsyncStorageRecordedTestCase):
async def _setUp(self, account_name, account_key):
url = self.account_url(account_name, 'dfs')
self.dsc = DataLakeServiceClient(url, credential=account_key)
self.config = self.dsc._config
self.file_system_name = self.get_resource_name('filesystem')
if not self.is_playback():
file_system = self.dsc.get_file_system_client(self.file_system_name)
try:
await file_system.create_file_system(timeout=5)
except ResourceExistsError:
pass
def tearDown(self):
if not self.is_playback():
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(self.dsc.delete_file_system(self.file_system_name))
loop.run_until_complete(self.dsc.__aexit__())
except:
pass
# --Helpers-----------------------------------------------------------------
def _get_directory_reference(self, prefix=TEST_DIRECTORY_PREFIX):
directory_name = self.get_resource_name(prefix)
return directory_name
def _get_file_reference(self, prefix=TEST_FILE_PREFIX):
file_name = self.get_resource_name(prefix)
return file_name
async def _create_file_system(self):
return await self.dsc.create_file_system(self._get_file_system_reference())
async def _create_directory_and_return_client(self, directory=None):
directory_name = directory if directory else self._get_directory_reference()
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
return directory_client
async def _create_file_and_return_client(self, directory="", file=None):
if directory:
await self._create_directory_and_return_client(directory)
if not file:
file = self._get_file_reference()
file_client = self.dsc.get_file_client(self.file_system_name, directory + '/' + file)
await file_client.create_file()
return file_client
def _is_almost_equal(self, first, second, delta):
if first == second:
return True
diff = abs(first - second)
if delta is not None:
if diff <= delta:
return True
return False
# --Helpers-----------------------------------------------------------------
@DataLakePreparer()
@recorded_by_proxy_async
async def test_create_file(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# Arrange
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client = directory_client.get_file_client('filename')
response = await file_client.create_file()
# Assert
assert response is not None
@DataLakePreparer()
@recorded_by_proxy_async
async def test_create_file_owner_group_acl(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
test_string = '4cf4e284-f6a8-4540-b53e-c3469af032dc'
test_string_acl = 'user::rwx,group::r-x,other::rwx'
# Arrange
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client = directory_client.get_file_client('filename')
await file_client.create_file(owner=test_string, group=test_string, acl=test_string_acl)
# Assert
acl_properties = await file_client.get_access_control()
assert acl_properties is not None
assert acl_properties['owner'] == test_string
assert acl_properties['group'] == test_string
assert acl_properties['acl'] == test_string_acl
@DataLakePreparer()
@recorded_by_proxy_async
async def test_create_file_proposed_lease_id(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
test_string = '4cf4e284-f6a8-4540-b53e-c3469af032dc'
test_duration = 15
# Arrange
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client = directory_client.get_file_client('filename')
await file_client.create_file(lease_id=test_string, lease_duration=test_duration)
# Assert
properties = await file_client.get_file_properties()
assert properties is not None
assert properties.lease['status'] == 'locked'
assert properties.lease['state'] == 'leased'
assert properties.lease['duration'] == 'fixed'
@DataLakePreparer()
@recorded_by_proxy_async
async def test_create_file_relative_expiry(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
test_expiry_time = 86400000 # 1 day in milliseconds
# Arrange
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client = directory_client.get_file_client('filename')
await file_client.create_file(expires_on=test_expiry_time)
# Assert
file_properties = await file_client.get_file_properties()
expiry_time = file_properties['expiry_time']
expiry_time = expiry_time.replace(tzinfo=None) # Strip timezone info to be able to compare
creation_time = file_properties['creation_time']
creation_time = creation_time.replace(tzinfo=None) # Strip timezone info to be able to compare
assert file_properties is not None
assert self._is_almost_equal(expiry_time, creation_time + timedelta(days=1), timedelta(seconds=60)) is True
@DataLakePreparer()
@recorded_by_proxy_async
async def test_create_file_absolute_expiry(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
test_expiry_time = datetime(2075, 4, 4)
# Arrange
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client = directory_client.get_file_client('filename')
await file_client.create_file(expires_on=test_expiry_time)
# Assert
file_properties = await file_client.get_file_properties()
expiry_time = file_properties['expiry_time']
expiry_time = expiry_time.replace(tzinfo=None) # Strip timezone info to be able to compare
assert file_properties is not None
assert self._is_almost_equal(expiry_time, test_expiry_time, timedelta(seconds=1)) is True
@DataLakePreparer()
@recorded_by_proxy_async
async def test_create_file_extra_backslashes(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# Arrange
file_client = await self._create_file_and_return_client()
new_file_client = DataLakeFileClient(self.account_url(datalake_storage_account_name, 'dfs'),
file_client.file_system_name + '/',
'/' + file_client.path_name,
credential=datalake_storage_account_key, logging_enable=True)
response = await new_file_client.create_file()
# Assert
assert response is not None
@DataLakePreparer()
@recorded_by_proxy_async
async def test_file_exists(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# Arrange
directory_name = self._get_directory_reference()
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client1 = directory_client.get_file_client('filename')
file_client2 = directory_client.get_file_client('nonexistentfile')
await file_client1.create_file()
assert await file_client1.exists()
assert not await file_client2.exists()
@DataLakePreparer()
@recorded_by_proxy_async
async def test_create_file_using_oauth_token_credential(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# Arrange
file_name = self._get_file_reference()
token_credential = self.get_credential(DataLakeServiceClient, is_async=True)
# Create a directory to put the file under that
file_client = DataLakeFileClient(self.dsc.url, self.file_system_name, file_name,
credential=token_credential)
response = file_client.create_file()
# Assert
assert response is not None
@DataLakePreparer()
@recorded_by_proxy_async
async def test_create_file_with_existing_name(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# Arrange
file_client = await self._create_file_and_return_client()
with pytest.raises(ResourceExistsError):
# if the file exists then throw error
# if_none_match='*' is to make sure no existing file
await file_client.create_file(match_condition=MatchConditions.IfMissing)
@DataLakePreparer()
@recorded_by_proxy_async
async def test_create_file_with_lease_id(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# Arrange
directory_name = self._get_directory_reference()
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client = directory_client.get_file_client('filename')
# Act
await file_client.create_file()
lease = await file_client.acquire_lease(lease_id='00000000-1111-2222-3333-444444444444')
create_resp = await file_client.create_file(lease=lease)
# Assert
file_properties = await file_client.get_file_properties()
assert file_properties is not None
assert file_properties.etag == create_resp.get('etag')
assert file_properties.last_modified == create_resp.get('last_modified')
@DataLakePreparer()
@recorded_by_proxy_async
async def test_create_file_under_root_directory(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# Arrange
# get a file client to interact with the file under root directory
file_client = self.dsc.get_file_client(self.file_system_name, "filename")
response = await file_client.create_file()
# Assert
assert response is not None
@DataLakePreparer()
@recorded_by_proxy_async
async def test_append_data(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client = directory_client.get_file_client('filename')
await file_client.create_file()
# Act
response = await file_client.append_data(b'abc', 0, 3)
assert response is not None
@DataLakePreparer()
@recorded_by_proxy_async
async def test_append_data_lease_action(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client = directory_client.get_file_client('filename')
await file_client.create_file()
data = b'Hello world'
lease_id = '670d43d1-ecde-4ae9-9c37-d22d340e7719'
# Act / Assert
# ---Acquire---
await file_client.append_data(data, 0, len(data), lease_action='acquire', lease_duration=30, lease=lease_id)
lease = (await file_client.get_file_properties()).lease
assert lease.state == 'leased'
assert lease.duration == 'fixed'
# ---Renew---
await file_client.append_data(data, 0, len(data), lease_action='auto-renew', lease=lease_id)
lease = (await file_client.get_file_properties()).lease
assert lease.state == 'leased'
assert lease.duration == 'fixed'
# ---Release---
await file_client.append_data(data, 0, len(data), flush=True, lease_action='release', lease=lease_id)
lease = (await file_client.get_file_properties()).lease
assert lease.state == 'available'
assert not lease.duration
# ---Acquire and release---
await file_client.append_data(data, 0, len(data), flush=True, lease_action='acquire-release', lease=lease_id)
lease = (await file_client.get_file_properties()).lease
assert lease.state == 'available'
assert not lease.duration
@DataLakePreparer()
@recorded_by_proxy_async
async def test_append_empty_data(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
file_client = await self._create_file_and_return_client()
# Act
await file_client.flush_data(0)
file_props = await file_client.get_file_properties()
assert file_props['size'] == 0
@DataLakePreparer()
@recorded_by_proxy_async
async def test_flush_data(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client = directory_client.get_file_client('filename')
await file_client.create_file()
# Act
await file_client.append_data(b'abc', 0, 3)
response = await file_client.flush_data(3)
# Assert
prop = await file_client.get_file_properties()
assert response is not None
assert prop['size'] == 3
@DataLakePreparer()
@recorded_by_proxy_async
async def test_flush_data_lease_action(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client = directory_client.get_file_client('filename')
await file_client.create_file()
data = b'Hello world'
lease_id = 'c8107e94-ab42-42ac-92d6-6458764982af'
# Act / Assert
# ---Acquire---
await file_client.append_data(data, 0, len(data))
await file_client.flush_data(len(data), lease_action='acquire', lease_duration=30, lease=lease_id)
lease = (await file_client.get_file_properties()).lease
assert lease.state == 'leased'
assert lease.duration == 'fixed'
# ---Renew---
await file_client.append_data(data, 0, len(data), lease=lease_id)
await file_client.flush_data(len(data), lease_action='auto-renew', lease=lease_id)
lease = (await file_client.get_file_properties()).lease
assert lease.state == 'leased'
assert lease.duration == 'fixed'
# ---Release---
await file_client.append_data(data, 0, len(data), lease=lease_id)
await file_client.flush_data(len(data), lease_action='release', lease=lease_id)
lease = (await file_client.get_file_properties()).lease
assert lease.state == 'available'
assert not lease.duration
# ---Acquire and release---
await file_client.append_data(data, 0, len(data))
await file_client.flush_data(len(data), lease_action='acquire-release', lease=lease_id)
lease = (await file_client.get_file_properties()).lease
assert lease.state == 'available'
assert not lease.duration
@DataLakePreparer()
@recorded_by_proxy_async
async def test_flush_data_with_bool(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client = directory_client.get_file_client('filename')
await file_client.create_file()
# Act
response = await file_client.append_data(b'abc', 0, 3, flush=True)
# Assert
prop = await file_client.get_file_properties()
assert response is not None
assert prop['size'] == 3
@DataLakePreparer()
@recorded_by_proxy_async
async def test_flush_data_with_match_condition(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client = directory_client.get_file_client('filename')
resp = await file_client.create_file()
# Act
await file_client.append_data(b'abc', 0, 3)
# flush is successful because it isn't touched
response = await file_client.flush_data(3, etag=resp['etag'], match_condition=MatchConditions.IfNotModified)
await file_client.append_data(b'abc', 3, 3)
with pytest.raises(ResourceModifiedError):
# flush is unsuccessful because extra data were appended.
await file_client.flush_data(6, etag=resp['etag'], match_condition=MatchConditions.IfNotModified)
@pytest.mark.live_test_only
@DataLakePreparer()
async def test_upload_data_in_substreams(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client = directory_client.get_file_client('filename')
# Get 16MB data
raw_data = self.get_random_bytes(16 * 1024 * 1024)
# Ensure chunk size is greater than threshold (8MB > 4MB) - for optimized upload
await file_client.upload_data(raw_data, chunk_size=8 * 1024 * 1024, overwrite=True, max_concurrency=3)
data = await file_client.download_file()
downloaded_data = await data.readall()
assert raw_data == downloaded_data
# Run on single thread
await file_client.upload_data(raw_data, chunk_size=8 * 1024 * 1024, overwrite=True)
data = await file_client.download_file()
downloaded_data = await data.readall()
assert raw_data == downloaded_data
@DataLakePreparer()
@recorded_by_proxy_async
async def test_upload_data(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client = directory_client.get_file_client('filename')
data = self.get_random_bytes(400*1024)
await file_client.upload_data(data, overwrite=True, max_concurrency=5)
downloaded_data = await (await file_client.download_file()).readall()
assert data == downloaded_data
@DataLakePreparer()
@recorded_by_proxy_async
async def test_upload_data_to_existing_file(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
# create an existing file
file_client = directory_client.get_file_client('filename')
await file_client.create_file()
await file_client.append_data(b"abc", 0)
await file_client.flush_data(3)
# to override the existing file
data = self.get_random_bytes(100)
with pytest.raises(HttpResponseError):
await file_client.upload_data(data)
await file_client.upload_data(data, overwrite=True)
downloaded_data = await (await file_client.download_file()).readall()
assert data == downloaded_data
@DataLakePreparer()
@recorded_by_proxy_async
async def test_upload_data_to_existing_file_with_content_settings(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
# create an existing file
file_client = directory_client.get_file_client('filename')
resp = await file_client.create_file()
etag = resp['etag']
# to override the existing file
data = self.get_random_bytes(100)
content_settings = ContentSettings(
content_language='spanish',
content_disposition='inline')
await file_client.upload_data(data, content_settings=content_settings, etag=etag, match_condition=MatchConditions.IfNotModified)
downloaded_data = await (await file_client.download_file()).readall()
properties = await file_client.get_file_properties()
assert data == downloaded_data
assert properties.content_settings.content_language == content_settings.content_language
@DataLakePreparer()
@recorded_by_proxy_async
async def test_upload_data_to_existing_file_with_permissions_and_umask(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
directory_name = self._get_directory_reference()
# Create a directory to put the file under that
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
# create an existing file
file_client = directory_client.get_file_client('filename')
resp = await file_client.create_file()
etag = resp['etag']
# to override the existing file
data = self.get_random_bytes(100)
await file_client.upload_data(data, overwrite=True, permissions='0777', umask="0000", etag=etag, match_condition=MatchConditions.IfNotModified)
downloaded_data = await (await file_client.download_file()).readall()
prop = await file_client.get_access_control()
assert data == downloaded_data
assert prop['permissions'] == 'rwxrwxrwx'
@DataLakePreparer()
@recorded_by_proxy_async
async def test_upload_data_from_async_generator(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# Create a directory to put the file under
directory_name = self._get_directory_reference()
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
data = b'Hello Async World!'
async def data_generator():
for _ in range(3):
yield data
await asyncio.sleep(0.1)
# Act
file_client = directory_client.get_file_client('filename')
await file_client.upload_data(data_generator(), length=len(data*3), overwrite=True)
# Assert
result = await (await file_client.download_file()).readall()
assert result == data*3
@DataLakePreparer()
@recorded_by_proxy_async
async def test_read_file(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
file_client = await self._create_file_and_return_client()
data = self.get_random_bytes(1024)
# upload data to file
await file_client.append_data(data, 0, len(data))
await file_client.flush_data(len(data))
# download the data and make sure it is the same as uploaded data
downloaded_data = await (await file_client.download_file()).readall()
assert data == downloaded_data
@pytest.mark.live_test_only
@DataLakePreparer()
async def test_read_file_with_user_delegation_key(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# SAS URL is calculated from storage key, so this test runs live only
# Create file
file_client = await self._create_file_and_return_client()
data = self.get_random_bytes(1024)
# Upload data to file
await file_client.append_data(data, 0, len(data))
await file_client.flush_data(len(data))
# Get user delegation key
token_credential = self.get_credential(DataLakeServiceClient, is_async=True)
service_client = DataLakeServiceClient(self.account_url(datalake_storage_account_name, 'dfs'), credential=token_credential)
user_delegation_key = await service_client.get_user_delegation_key(datetime.utcnow(),
datetime.utcnow() + timedelta(hours=1))
sas_token = generate_file_sas(file_client.account_name,
file_client.file_system_name,
None,
file_client.path_name,
user_delegation_key,
permission=FileSasPermissions(read=True, create=True, write=True, delete=True),
expiry=datetime.utcnow() + timedelta(hours=1),
)
# download the data and make sure it is the same as uploaded data
new_file_client = DataLakeFileClient(self.account_url(datalake_storage_account_name, 'dfs'),
file_client.file_system_name,
file_client.path_name,
credential=sas_token)
downloaded_data = await (await new_file_client.download_file()).readall()
assert data == downloaded_data
@DataLakePreparer()
@recorded_by_proxy_async
async def test_read_file_into_file(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
file_client = await self._create_file_and_return_client()
data = self.get_random_bytes(1024)
# upload data to file
await file_client.append_data(data, 0, len(data))
await file_client.flush_data(len(data))
# download the data into a file and make sure it is the same as uploaded data
with tempfile.TemporaryFile() as temp_file:
download = await file_client.download_file()
await download.readinto(temp_file)
temp_file.seek(0)
# Assert
actual = temp_file.read()
assert data == actual
@DataLakePreparer()
@recorded_by_proxy_async
async def test_read_file_to_text(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
file_client = await self._create_file_and_return_client()
data = self.get_random_text_data(1024)
# upload data to file
await file_client.append_data(data, 0, len(data))
await file_client.flush_data(len(data))
# download the text data and make sure it is the same as uploaded data
downloaded_data = await (await file_client.download_file(encoding="utf-8")).readall()
# Assert
assert data == downloaded_data
@pytest.mark.live_test_only
@DataLakePreparer()
async def test_account_sas(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# SAS URL is calculated from storage key, so this test runs live only
file_name = self._get_file_reference()
# create a file under root directory
await self._create_file_and_return_client(file=file_name)
# generate a token with file level read permission
token = generate_account_sas(
self.dsc.account_name,
self.dsc.credential.account_key,
ResourceTypes(file_system=True, object=True),
AccountSasPermissions(read=True),
datetime.utcnow() + timedelta(hours=1),
)
for credential in [token, AzureSasCredential(token)]:
# read the created file which is under root directory
file_client = DataLakeFileClient(self.dsc.url, self.file_system_name, file_name, credential=credential)
properties = await file_client.get_file_properties()
# make sure we can read the file properties
assert properties is not None
# try to write to the created file with the token
with pytest.raises(HttpResponseError):
await file_client.append_data(b"abcd", 0, 4)
@DataLakePreparer()
@recorded_by_proxy_async
async def test_account_sas_raises_if_sas_already_in_uri(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
with pytest.raises(ValueError):
DataLakeFileClient(self.dsc.url + "?sig=foo", self.file_system_name, "foo", credential=AzureSasCredential("?foo=bar"))
@pytest.mark.live_test_only
@DataLakePreparer()
async def test_file_sas_only_applies_to_file_level(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# SAS URL is calculated from storage key, so this test runs live only
file_name = self._get_file_reference()
directory_name = self._get_directory_reference()
await self._create_file_and_return_client(directory=directory_name, file=file_name)
# generate a token with file level read and write permissions
token = generate_file_sas(
self.dsc.account_name,
self.file_system_name,
directory_name,
file_name,
self.dsc.credential.account_key,
permission=FileSasPermissions(read=True, write=True),
expiry=datetime.utcnow() + timedelta(hours=1),
)
# read the created file which is under root directory
file_client = DataLakeFileClient(self.dsc.url, self.file_system_name, directory_name + '/' + file_name,
credential=token)
properties = await file_client.get_file_properties()
# make sure we can read the file properties
assert properties is not None
# try to write to the created file with the token
response = await file_client.append_data(b"abcd", 0, 4, validate_content=True)
assert response is not None
# the token is for file level, so users are not supposed to have access to file system level operations
file_system_client = FileSystemClient(self.dsc.url, self.file_system_name, credential=token)
with pytest.raises(ClientAuthenticationError):
await file_system_client.get_file_system_properties()
# the token is for file level, so users are not supposed to have access to directory level operations
directory_client = DataLakeDirectoryClient(self.dsc.url, self.file_system_name, directory_name,
credential=token)
with pytest.raises(ClientAuthenticationError):
await directory_client.get_directory_properties()
@DataLakePreparer()
@recorded_by_proxy_async
async def test_delete_file(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# Arrange
file_client = await self._create_file_and_return_client()
await file_client.delete_file()
with pytest.raises(ResourceNotFoundError):
await file_client.get_file_properties()
@DataLakePreparer()
@recorded_by_proxy_async
async def test_delete_file_oauth(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
# Arrange
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
file_name = self._get_file_reference()
token_credential = self.get_credential(DataLakeServiceClient, is_async=True)
file_client = DataLakeFileClient(
self.dsc.url,
self.file_system_name,
file_name,
credential=token_credential)
await file_client.create_file()
# Act
response = await file_client.delete_file()
# Assert
assert response is not None
@DataLakePreparer()
@recorded_by_proxy_async
async def test_delete_file_with_if_unmodified_since(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# Arrange
file_client = await self._create_file_and_return_client()
prop = await file_client.get_file_properties()
await file_client.delete_file(if_unmodified_since=prop['last_modified'])
# Make sure the file was deleted
with pytest.raises(ResourceNotFoundError):
await file_client.get_file_properties()
@DataLakePreparer()
@recorded_by_proxy_async
async def test_set_access_control(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
file_client = await self._create_file_and_return_client()
response = await file_client.set_access_control(permissions='0777')
# Assert
assert response is not None
@DataLakePreparer()
@recorded_by_proxy_async
async def test_set_access_control_with_match_conditions(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
file_client = await self._create_file_and_return_client()
with pytest.raises(ResourceModifiedError):
await file_client.set_access_control(permissions='0777', match_condition=MatchConditions.IfMissing)
@DataLakePreparer()
@recorded_by_proxy_async
async def test_get_access_control(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
file_client = await self._create_file_and_return_client()
await file_client.set_access_control(permissions='0777')
# Act
response = await file_client.get_access_control()
# Assert
assert response is not None
@DataLakePreparer()
@recorded_by_proxy_async
async def test_get_access_control_with_if_modified_since(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
file_client = await self._create_file_and_return_client()
await file_client.set_access_control(permissions='0777')
prop = await file_client.get_file_properties()
# Act
response = await file_client.get_access_control(if_modified_since=prop['last_modified'] - timedelta(minutes=15))
# Assert
assert response is not None
@DataLakePreparer()
@recorded_by_proxy_async
async def test_get_properties(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# Arrange
directory_client = await self._create_directory_and_return_client()
metadata = {'hello': 'world', 'number': '42'}
content_settings = ContentSettings(
content_language='spanish',
content_disposition='inline')
file_client = await directory_client.create_file("newfile", metadata=metadata,
content_settings=content_settings)
await file_client.append_data(b"abc", 0, 3)
await file_client.flush_data(3)
properties = await file_client.get_file_properties()
# Assert
assert properties
assert properties.size == 3
assert properties.metadata['hello'] == metadata['hello']
assert properties.content_settings.content_language == content_settings.content_language
@DataLakePreparer()
@recorded_by_proxy_async
async def test_set_access_control_recursive(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
acl = 'user::rwx,group::r-x,other::rwx'
file_client = await self._create_file_and_return_client()
summary = await file_client.set_access_control_recursive(acl=acl)
# Assert
assert summary.counters.directories_successful == 0
assert summary.counters.files_successful == 1
assert summary.counters.failure_count == 0
access_control = await file_client.get_access_control()
assert access_control is not None
assert acl == access_control['acl']
@DataLakePreparer()
@recorded_by_proxy_async
async def test_update_access_control_recursive(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
acl = 'user::rwx,group::r-x,other::rwx'
file_client = await self._create_file_and_return_client()
summary = await file_client.update_access_control_recursive(acl=acl)
# Assert
assert summary.counters.directories_successful == 0
assert summary.counters.files_successful == 1
assert summary.counters.failure_count == 0
access_control = await file_client.get_access_control()
assert access_control is not None
assert acl == access_control['acl']
@DataLakePreparer()
@recorded_by_proxy_async
async def test_remove_access_control_recursive(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
acl = "mask," + "default:user,default:group," + \
"user:ec3595d6-2c17-4696-8caa-7e139758d24a,group:ec3595d6-2c17-4696-8caa-7e139758d24a," + \
"default:user:ec3595d6-2c17-4696-8caa-7e139758d24a,default:group:ec3595d6-2c17-4696-8caa-7e139758d24a"
file_client = await self._create_file_and_return_client()
summary = await file_client.remove_access_control_recursive(acl=acl)
# Assert
assert summary.counters.directories_successful == 0
assert summary.counters.files_successful == 1
assert summary.counters.failure_count == 0
@DataLakePreparer()
@recorded_by_proxy_async
async def test_set_expiry(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
variables = kwargs.pop('variables', {})
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# Arrange
directory_client = await self._create_directory_and_return_client()
metadata = {'hello': 'world', 'number': '42'}
content_settings = ContentSettings(
content_language='spanish',
content_disposition='inline')
expiry_time = self.get_datetime_variable(variables, 'expiry_time', datetime.utcnow() + timedelta(hours=1))
file_client = await directory_client.create_file("newfile", metadata=metadata, content_settings=content_settings)
# Act / Assert
await file_client.set_file_expiry("Absolute", expires_on=expiry_time)
properties = await file_client.get_file_properties()
assert properties
assert properties.expiry_time is not None
await file_client.set_file_expiry("NeverExpire")
properties = await file_client.get_file_properties()
assert properties
assert properties.expiry_time is None
return variables
@DataLakePreparer()
@recorded_by_proxy_async
async def test_rename_file_with_non_used_name(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
file_client = await self._create_file_and_return_client()
data_bytes = b"abc"
await file_client.append_data(data_bytes, 0, 3)
await file_client.flush_data(3)
new_client = await file_client.rename_file(file_client.file_system_name + '/' + 'newname')
data = await (await new_client.download_file()).readall()
assert data == data_bytes
assert new_client.path_name == "newname"
@DataLakePreparer()
@recorded_by_proxy_async
async def test_rename_file_to_existing_file(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# create the existing file
existing_file_client = await self._create_file_and_return_client(file="existingfile")
await existing_file_client.append_data(b"a", 0, 1)
await existing_file_client.flush_data(1)
old_url = existing_file_client.url
# prepare to rename the file to the existing file
file_client = await self._create_file_and_return_client()
data_bytes = b"abc"
await file_client.append_data(data_bytes, 0, 3)
await file_client.flush_data(3)
new_client = await file_client.rename_file(file_client.file_system_name + '/' + existing_file_client.path_name)
new_url = file_client.url
data = await (await new_client.download_file()).readall()
# the existing file was overridden
assert data == data_bytes
@DataLakePreparer()
@recorded_by_proxy_async
async def test_file_encryption_scope_from_file_system_async(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
# Arrange
url = self.account_url(datalake_storage_account_name, 'dfs')
self.dsc = DataLakeServiceClient(url, credential=datalake_storage_account_key, logging_enable=True)
self.file_system_name = self.get_resource_name('filesystem')
file_name = 'testfile'
encryption_scope = EncryptionScopeOptions(default_encryption_scope="hnstestscope1")
file_system = self.dsc.get_file_system_client(self.file_system_name)
await file_system.create_file_system(encryption_scope_options=encryption_scope)
file_client = await file_system.create_file(file_name)
props = await file_client.get_file_properties()
# Assert
assert props
assert props['encryption_scope'] is not None
assert props['encryption_scope'] == encryption_scope.default_encryption_scope
@pytest.mark.live_test_only
@DataLakePreparer()
async def test_rename_file_with_file_sas(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# SAS URL is calculated from storage key, so this test runs live only
token = generate_file_sas(self.dsc.account_name,
self.file_system_name,
None,
"oldfile",
datalake_storage_account_key,
permission=FileSasPermissions(read=True, create=True, write=True, delete=True, move=True),
expiry=datetime.utcnow() + timedelta(hours=1),
)
new_token = generate_file_sas(self.dsc.account_name,
self.file_system_name,
None,
"newname",
datalake_storage_account_key,
permission=FileSasPermissions(read=True, create=True, write=True, delete=True),
expiry=datetime.utcnow() + timedelta(hours=1),
)
# read the created file which is under root directory
file_client = DataLakeFileClient(self.dsc.url, self.file_system_name, "oldfile", credential=token)
await file_client.create_file()
data_bytes = b"abc"
await file_client.append_data(data_bytes, 0, 3)
await file_client.flush_data(3)
new_client = await file_client.rename_file(file_client.file_system_name+'/'+'newname'+'?'+new_token)
data = await (await new_client.download_file()).readall()
assert data == data_bytes
assert new_client.path_name == "newname"
@DataLakePreparer()
@recorded_by_proxy_async
async def test_rename_file_will_not_change_existing_directory(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# create none empty directory(with 2 files)
dir1 = await self._create_directory_and_return_client(directory="dir1")
f1 = await dir1.create_file("file1")
await f1.append_data(b"file1", 0, 5)
await f1.flush_data(5)
f2 = await dir1.create_file("file2")
await f2.append_data(b"file2", 0, 5)
await f2.flush_data(5)
# create another none empty directory(with 2 files)
dir2 = await self._create_directory_and_return_client(directory="dir2")
f3 = await dir2.create_file("file3")
await f3.append_data(b"file3", 0, 5)
await f3.flush_data(5)
f4 = await dir2.create_file("file4")
await f4.append_data(b"file4", 0, 5)
await f4.flush_data(5)
new_client = await f3.rename_file(f1.file_system_name + '/' + f1.path_name)
assert await (await new_client.download_file()).readall() == b"file3"
# make sure the data in file2 and file4 weren't touched
f2_data = await (await f2.download_file()).readall()
assert f2_data == b"file2"
f4_data = await (await f4.download_file()).readall()
assert f4_data == b"file4"
with pytest.raises(HttpResponseError):
await (await f3.download_file()).readall()
@DataLakePreparer()
@recorded_by_proxy_async
async def test_rename_file_different_filesystem_with_sas(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# Use filesystem SAS without access to new filesystem
existing_sas = self.generate_sas(
generate_file_system_sas,
self.dsc.account_name,
self.file_system_name,
self.dsc.credential.account_key,
FileSystemSasPermissions(write=True, read=True, delete=True),
datetime.utcnow() + timedelta(hours=1),
)
file_client = DataLakeFileClient(self.dsc.url, self.file_system_name, "oldfile", credential=existing_sas)
await file_client.create_file()
await file_client.append_data(b"abc", 0, 3, flush=True)
# Create another filesystem to rename to
new_file_system = self.dsc.get_file_system_client(self.file_system_name + '2')
await new_file_system.create_file_system()
# Get different SAS to new file system
new_sas = self.generate_sas(
generate_file_system_sas,
self.dsc.account_name,
new_file_system.file_system_name,
self.dsc.credential.account_key,
FileSystemSasPermissions(write=True, read=True, delete=True),
datetime.utcnow() + timedelta(hours=1),
)
# ? in new name to test parsing
new_name = new_file_system.file_system_name + '/' + 'new?file' + '?' + new_sas
new_client = await file_client.rename_file(new_name)
new_props = await new_client.get_file_properties()
assert new_props.name == 'new?file'
await new_file_system.delete_file_system()
@DataLakePreparer()
@recorded_by_proxy_async
async def test_rename_file_special_chars(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
file_client = await self._create_file_and_return_client(file="oldfile")
await file_client.append_data(b"abc", 0, 3, flush=True)
new_client = await file_client.rename_file(file_client.file_system_name + '/' + '?!@#$%^&*.?test')
new_props = await new_client.get_file_properties()
assert new_props.name == '?!@#$%^&*.?test'
@DataLakePreparer()
@recorded_by_proxy_async
async def test_read_file_read(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
self.dsc._config.max_single_get_size = 1024
self.dsc._config.max_chunk_get_size = 1024
file_client = await self._create_file_and_return_client()
data = b'12345' * 205 * 5 # 5125 bytes
await file_client.append_data(data, 0, len(data), flush=True)
stream = await file_client.download_file()
# Act
result = bytearray()
read_size = 512
num_chunks = int(ceil(len(data) / read_size))
for i in range(num_chunks):
content = await stream.read(read_size)
start = i * read_size
end = start + read_size
assert data[start:end] == content
result.extend(content)
# Assert
assert result == data
@DataLakePreparer()
@recorded_by_proxy_async
async def test_create_and_read_file_encryption_context(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
# Arrange
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
url = self.account_url(datalake_storage_account_name, 'dfs')
self.dsc = DataLakeServiceClient(url, credential=datalake_storage_account_key)
self.file_system_name = self.get_resource_name('filesystem')
file_name = 'testfile'
file_system = self.dsc.get_file_system_client(self.file_system_name)
try:
await file_system.create_file_system()
except:
pass
file_client = file_system.get_file_client(file_name)
# Act
await file_client.create_file(encryption_context='encryptionContext')
properties = await file_client.get_file_properties()
read_response = await file_client.download_file()
path_response = []
async for path in file_system.get_paths():
path_response.append(path)
assert properties
assert properties['encryption_context'] is not None
assert properties['encryption_context'] == 'encryptionContext'
assert read_response.properties
assert read_response.properties['encryption_context'] is not None
assert read_response.properties['encryption_context'] == 'encryptionContext'
assert path_response[0]['encryption_context']
assert path_response[0]['encryption_context'] is not None
assert path_response[0]['encryption_context'] == 'encryptionContext'
@DataLakePreparer()
@recorded_by_proxy_async
async def test_upload_file_encryption_context(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
# Arrange
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
url = self.account_url(datalake_storage_account_name, 'dfs')
self.dsc = DataLakeServiceClient(url, credential=datalake_storage_account_key)
self.file_system_name = self.get_resource_name('filesystem')
data = self.get_random_bytes(200 * 1024)
file_name = 'testfile'
file_system = self.dsc.get_file_system_client(self.file_system_name)
try:
await file_system.create_file_system()
except:
pass
file_client = file_system.get_file_client(file_name)
# Act
await file_client.upload_data(data, overwrite=True, encryption_context='encryptionContext')
downloaded_data = await (await file_client.download_file()).readall()
properties = await file_client.get_file_properties()
# Assert
assert data == downloaded_data
assert properties
assert properties['encryption_context'] is not None
assert properties['encryption_context'] == 'encryptionContext'
@DataLakePreparer()
@recorded_by_proxy_async
async def test_dir_and_file_properties_owner_group_acl_permissions(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
# Arrange
directory_name = self._get_directory_reference()
directory_client = self.dsc.get_directory_client(self.file_system_name, directory_name)
await directory_client.create_directory()
file_client1 = directory_client.get_file_client('filename')
await file_client1.create_file()
directory_properties = await directory_client.get_directory_properties()
file_properties = await file_client1.get_file_properties(upn=True)
# Assert
assert directory_properties['owner'] is not None
assert directory_properties['group'] is not None
assert directory_properties['permissions'] is not None
assert directory_properties['acl'] is not None
assert file_properties['owner'] is not None
assert file_properties['group'] is not None
assert file_properties['permissions'] is not None
assert file_properties['acl'] is not None
@DataLakePreparer()
@recorded_by_proxy_async
async def test_storage_account_audience_file_client(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
# Arrange
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
file_client = await self._create_file_and_return_client()
# Act
token_credential = self.get_credential(DataLakeServiceClient, is_async=True)
fc = DataLakeFileClient(
self.account_url(datalake_storage_account_name, 'dfs'),
file_client.file_system_name + '/',
'/' + file_client.path_name,
credential=token_credential,
audience=f'https://{datalake_storage_account_name}.blob.core.windows.net/'
)
# Assert
data = b'Hello world'
response1 = await fc.get_file_properties()
response2 = await fc.upload_data(data, overwrite=True)
assert response1 is not None
assert response2 is not None
@DataLakePreparer()
@recorded_by_proxy_async
async def test_bad_audience_file_client(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
# Arrange
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
file_client = await self._create_file_and_return_client()
# Act
token_credential = self.get_credential(DataLakeServiceClient, is_async=True)
fc = DataLakeFileClient(
self.account_url(datalake_storage_account_name, 'dfs'),
file_client.file_system_name + '/',
'/' + file_client.path_name,
credential=token_credential,
audience=f'https://badaudience.blob.core.windows.net/'
)
# Will not raise ClientAuthenticationError despite bad audience due to Bearer Challenge
data = b'Hello world'
await fc.get_file_properties()
await fc.upload_data(data, overwrite=True)
@DataLakePreparer()
async def test_mock_transport_no_content_validation(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
transport = MockStorageTransport()
file_client = DataLakeFileClient(
self.account_url(datalake_storage_account_name, 'dfs'),
"filesystem/",
"dir/file.txt",
credential=datalake_storage_account_key,
transport=transport,
retry_total=0
)
data = await file_client.download_file()
assert data is not None
props = await file_client.get_file_properties()
assert props is not None
data = b"Hello Async World!"
stream = AsyncStream(data)
resp = await file_client.upload_data(stream, overwrite=True)
assert resp is not None
file_data = await (await file_client.download_file()).read()
assert file_data == b"Hello Async World!" # data is fixed by mock transport
resp = await file_client.delete_file()
assert resp is not None
@DataLakePreparer()
async def test_mock_transport_with_content_validation(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
transport = MockStorageTransport()
file_client = DataLakeFileClient(
self.account_url(datalake_storage_account_name, 'dfs'),
"filesystem/",
"dir/file.txt",
credential=datalake_storage_account_key,
transport=transport,
retry_total=0
)
data = b"Hello Async World!"
stream = AsyncStream(data)
resp = await file_client.upload_data(stream, overwrite=True, validate_content=True)
assert resp is not None
file_data = await (await file_client.download_file(validate_content=True)).read()
assert file_data == b"Hello Async World!" # data is fixed by mock transport
@DataLakePreparer()
@recorded_by_proxy_async
async def test_progress_hook_upload_data(self, **kwargs):
datalake_storage_account_name = kwargs.pop("datalake_storage_account_name")
datalake_storage_account_key = kwargs.pop("datalake_storage_account_key")
# Arrange
await self._setUp(datalake_storage_account_name, datalake_storage_account_key)
file_client = await self._create_file_and_return_client(
directory=self._get_directory_reference(),
file=self._get_file_reference()
)
data = self.get_random_bytes(8 * 1024)
progress = ProgressTracker(len(data), 1024)
# Act
await file_client.upload_data(
data,
overwrite=True,
progress_hook=progress.assert_progress,
max_concurrency=1,
chunk_size=1024
)
# Assert
progress.assert_complete()
# ------------------------------------------------------------------------------
if __name__ == '__main__':
unittest.main()
|