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
|
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import unittest
import asyncio
from datetime import timedelta
from azure.core.exceptions import ResourceNotFoundError, ResourceExistsError
from azure.core.pipeline.transport import AioHttpTransport
from multidict import CIMultiDict, CIMultiDictProxy
from azure.storage.fileshare import StorageErrorCode
from azure.storage.fileshare.aio import ShareServiceClient
from devtools_testutils import ResourceGroupPreparer, StorageAccountPreparer
from _shared.testcase import (
LogCaptured,
GlobalStorageAccountPreparer
)
from _shared.asynctestcase import AsyncStorageTestCase
# ------------------------------------------------------------------------------
class AiohttpTestTransport(AioHttpTransport):
"""Workaround to vcrpy bug: https://github.com/kevin1024/vcrpy/pull/461
"""
async def send(self, request, **config):
response = await super(AiohttpTestTransport, self).send(request, **config)
if not isinstance(response.headers, CIMultiDictProxy):
response.headers = CIMultiDictProxy(CIMultiDict(response.internal_response.headers))
response.content_type = response.headers.get("content-type")
return response
class StorageDirectoryTest(AsyncStorageTestCase):
# --Helpers-----------------------------------------------------------------
async def _setup(self, storage_account, storage_account_key):
url = self.account_url(storage_account, "file")
credential = storage_account_key
self.fsc = ShareServiceClient(url, credential=credential, transport=AiohttpTestTransport())
self.share_name = self.get_resource_name('utshare')
if not self.is_playback():
try:
await self.fsc.create_share(self.share_name)
except:
pass
def _teardown(self, FILE_PATH):
if os.path.isfile(FILE_PATH):
try:
os.remove(FILE_PATH)
except:
pass
# --Test cases for directories ----------------------------------------------
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_create_directories_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
# Act
created = await share_client.create_directory('dir1')
# Assert
self.assertTrue(created)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_create_directories_with_metadata_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
metadata = {'hello': 'world', 'number': '42'}
# Act
directory = await share_client.create_directory('dir1', metadata=metadata)
# Assert
props = await directory.get_directory_properties()
self.assertDictEqual(props.metadata, metadata)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_create_directories_fail_on_exist_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
# Act
created = await share_client.create_directory('dir1')
with self.assertRaises(ResourceExistsError):
await share_client.create_directory('dir1')
# Assert
self.assertTrue(created)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_create_subdirectories_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
# Act
created = await directory.create_subdirectory('dir2')
# Assert
self.assertTrue(created)
self.assertEqual(created.directory_path, 'dir1/dir2')
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_create_subdirectories_with_metadata_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
metadata = {'hello': 'world', 'number': '42'}
# Act
created = await directory.create_subdirectory('dir2', metadata=metadata)
# Assert
self.assertTrue(created)
self.assertEqual(created.directory_path, 'dir1/dir2')
properties = await created.get_directory_properties()
self.assertEqual(properties.metadata, metadata)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_create_file_in_directory_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
file_data = b'12345678' * 1024
file_name = self.get_resource_name('file')
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
# Act
new_file = await directory.upload_file(file_name, file_data)
# Assert
file_content = await new_file.download_file()
file_content = await file_content.readall()
self.assertEqual(file_content, file_data)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_delete_file_in_directory_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
file_name = self.get_resource_name('file')
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
new_file = await directory.upload_file(file_name, "hello world")
# Act
deleted = await directory.delete_file(file_name)
# Assert
self.assertIsNone(deleted)
with self.assertRaises(ResourceNotFoundError):
await new_file.get_file_properties()
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_delete_subdirectories_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
await directory.create_subdirectory('dir2')
# Act
deleted = await directory.delete_subdirectory('dir2')
# Assert
self.assertIsNone(deleted)
subdir = directory.get_subdirectory_client('dir2')
with self.assertRaises(ResourceNotFoundError):
await subdir.get_directory_properties()
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_get_directory_properties_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
# Act
props = await directory.get_directory_properties()
# Assert
self.assertIsNotNone(props)
self.assertIsNotNone(props.etag)
self.assertIsNotNone(props.last_modified)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_get_directory_properties_with_snapshot_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
metadata = {"test1": "foo", "test2": "bar"}
directory = await share_client.create_directory('dir1', metadata=metadata)
snapshot1 = await share_client.create_snapshot()
metadata2 = {"test100": "foo100", "test200": "bar200"}
await directory.set_directory_metadata(metadata2)
# Act
share_client = self.fsc.get_share_client(self.share_name, snapshot=snapshot1)
snap_dir = share_client.get_directory_client('dir1')
props = await snap_dir.get_directory_properties()
# Assert
self.assertIsNotNone(props)
self.assertIsNotNone(props.etag)
self.assertIsNotNone(props.last_modified)
self.assertDictEqual(metadata, props.metadata)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_get_directory_metadata_with_snapshot_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
metadata = {"test1": "foo", "test2": "bar"}
directory = await share_client.create_directory('dir1', metadata=metadata)
snapshot1 = await share_client.create_snapshot()
metadata2 = {"test100": "foo100", "test200": "bar200"}
await directory.set_directory_metadata(metadata2)
# Act
share_client = self.fsc.get_share_client(self.share_name, snapshot=snapshot1)
snap_dir = share_client.get_directory_client('dir1')
snapshot_props = await snap_dir.get_directory_properties()
# Assert
self.assertIsNotNone(snapshot_props.metadata)
self.assertDictEqual(metadata, snapshot_props.metadata)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_get_directory_properties_with_non_existing_directory_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = share_client.get_directory_client('dir1')
# Act
with self.assertRaises(ResourceNotFoundError):
await directory.get_directory_properties()
# Assert
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_directory_exists_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
# Act
exists = await directory.get_directory_properties()
# Assert
self.assertTrue(exists)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_directory_not_exists_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = share_client.get_directory_client('dir1')
# Act
with self.assertRaises(ResourceNotFoundError):
await directory.get_directory_properties()
# Assert
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_directory_parent_not_exists_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = share_client.get_directory_client('missing1/missing2')
# Act
with self.assertRaises(ResourceNotFoundError) as e:
await directory.get_directory_properties()
# Assert
self.assertEqual(e.exception.error_code, StorageErrorCode.parent_not_found)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_directory_exists_with_snapshot_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
snapshot = await share_client.create_snapshot()
await directory.delete_directory()
# Act
share_client = self.fsc.get_share_client(self.share_name, snapshot=snapshot)
snap_dir = share_client.get_directory_client('dir1')
exists = await snap_dir.get_directory_properties()
# Assert
self.assertTrue(exists)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_directory_not_exists_with_snapshot_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
snapshot = await share_client.create_snapshot()
directory = await share_client.create_directory('dir1')
# Act
share_client = self.fsc.get_share_client(self.share_name, snapshot=snapshot)
snap_dir = share_client.get_directory_client('dir1')
with self.assertRaises(ResourceNotFoundError):
await snap_dir.get_directory_properties()
# Assert
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_get_set_directory_metadata_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
metadata = {'hello': 'world', 'number': '43'}
# Act
await directory.set_directory_metadata(metadata)
props = await directory.get_directory_properties()
# Assert
self.assertDictEqual(props.metadata, metadata)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_set_directory_properties_with_empty_smb_properties(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory_client = await share_client.create_directory('dir1')
directory_properties_on_creation = await directory_client.get_directory_properties()
# Act
await directory_client.set_http_headers()
directory_properties = await directory_client.get_directory_properties()
# Assert
# Make sure set empty smb_properties doesn't change smb_properties
self.assertEqual(directory_properties_on_creation.creation_time,
directory_properties.creation_time)
self.assertEqual(directory_properties_on_creation.last_write_time,
directory_properties.last_write_time)
self.assertEqual(directory_properties_on_creation.permission_key,
directory_properties.permission_key)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_set_directory_properties_with_file_permission_key(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory_client = await share_client.create_directory('dir1')
directory_properties_on_creation = await directory_client.get_directory_properties()
permission_key = directory_properties_on_creation.permission_key
last_write_time = directory_properties_on_creation.last_write_time
creation_time = directory_properties_on_creation.creation_time
new_last_write_time = last_write_time + timedelta(hours=1)
new_creation_time = creation_time + timedelta(hours=1)
# Act
await directory_client.set_http_headers(file_attributes='None', file_creation_time=new_creation_time,
file_last_write_time=new_last_write_time,
permission_key=permission_key)
directory_properties = await directory_client.get_directory_properties()
# Assert
self.assertIsNotNone(directory_properties)
self.assertEqual(directory_properties.creation_time, new_creation_time)
self.assertEqual(directory_properties.last_write_time, new_last_write_time)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_list_subdirectories_and_files_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
await asyncio.gather(
directory.create_subdirectory("subdir1"),
directory.create_subdirectory("subdir2"),
directory.create_subdirectory("subdir3"),
directory.upload_file("file1", "data1"),
directory.upload_file("file2", "data2"),
directory.upload_file("file3", "data3"))
# Act
list_dir = []
async for d in directory.list_directories_and_files():
list_dir.append(d)
# Assert
expected = [
{'name': 'subdir1', 'is_directory': True},
{'name': 'subdir2', 'is_directory': True},
{'name': 'subdir3', 'is_directory': True},
{'name': 'file1', 'is_directory': False, 'size': 5},
{'name': 'file2', 'is_directory': False, 'size': 5},
{'name': 'file3', 'is_directory': False, 'size': 5},
]
self.assertEqual(len(list_dir), 6)
self.assertEqual(list_dir, expected)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_list_subdirectories_and_files_with_prefix_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
await asyncio.gather(
directory.create_subdirectory("subdir1"),
directory.create_subdirectory("subdir2"),
directory.create_subdirectory("subdir3"),
directory.upload_file("file1", "data1"),
directory.upload_file("file2", "data2"),
directory.upload_file("file3", "data3"))
# Act
list_dir = []
async for d in directory.list_directories_and_files(name_starts_with="sub"):
list_dir.append(d)
# Assert
expected = [
{'name': 'subdir1', 'is_directory': True},
{'name': 'subdir2', 'is_directory': True},
{'name': 'subdir3', 'is_directory': True},
]
self.assertEqual(len(list_dir), 3)
self.assertEqual(list_dir, expected)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_list_subdirectories_and_files_with_snapshot_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
await asyncio.gather(
directory.create_subdirectory("subdir1"),
directory.create_subdirectory("subdir2"),
directory.upload_file("file1", "data1"))
snapshot = await share_client.create_snapshot()
await asyncio.gather(
directory.create_subdirectory("subdir3"),
directory.upload_file("file2", "data2"),
directory.upload_file("file3", "data3"))
share_client = self.fsc.get_share_client(self.share_name, snapshot=snapshot)
snapshot_dir = share_client.get_directory_client('dir1')
# Act
list_dir = []
async for d in snapshot_dir.list_directories_and_files():
list_dir.append(d)
# Assert
expected = [
{'name': 'subdir1', 'is_directory': True},
{'name': 'subdir2', 'is_directory': True},
{'name': 'file1', 'is_directory': False, 'size': 5},
]
self.assertEqual(len(list_dir), 3)
self.assertEqual(list_dir, expected)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_list_nested_subdirectories_and_files_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
subdir = await directory.create_subdirectory("subdir1")
await subdir.create_subdirectory("subdir2")
await subdir.create_subdirectory("subdir3")
await asyncio.gather(
directory.upload_file("file1", "data1"),
subdir.upload_file("file2", "data2"),
subdir.upload_file("file3", "data3"))
# Act
list_dir = []
async for d in directory.list_directories_and_files():
list_dir.append(d)
# Assert
expected = [
{'name': 'subdir1', 'is_directory': True},
{'name': 'file1', 'is_directory': False, 'size': 5},
]
self.assertEqual(len(list_dir), 2)
self.assertEqual(list_dir, expected)
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_delete_directory_with_existing_share_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
# Act
deleted = await directory.delete_directory()
# Assert
self.assertIsNone(deleted)
with self.assertRaises(ResourceNotFoundError):
await directory.get_directory_properties()
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_delete_directory_with_non_existing_directory_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = share_client.get_directory_client('dir1')
# Act
with self.assertRaises(ResourceNotFoundError):
await directory.delete_directory()
# Assert
@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_get_directory_properties_server_encryption_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
await self._setup(storage_account, storage_account_key)
share_client = self.fsc.get_share_client(self.share_name)
directory = await share_client.create_directory('dir1')
# Act
props = await directory.get_directory_properties()
# Assert
self.assertIsNotNone(props)
self.assertIsNotNone(props.etag)
self.assertIsNotNone(props.last_modified)
self.assertTrue(props.server_encrypted)
# ------------------------------------------------------------------------------
|