class StorageShareTest(FileTestCase): def setUp(self): super(StorageShareTest, self).setUp() file_url = self.get_file_url() credentials = self.get_shared_key_credential() self.fsc = ShareServiceClient(account_url=file_url, credential=credentials, transport=AiohttpTestTransport()) loop = asyncio.get_event_loop() loop.run_until_complete(self.fsc.__aenter__()) self.test_shares = [] def tearDown(self): if not self.is_playback(): loop = asyncio.get_event_loop() try: for s in self.test_shares: loop.run_until_complete( self.fsc.delete_share(s.share_name, delete_snapshots=True)) loop.run_until_complete(self.fsc.__aexit__()) except: pass return super(StorageShareTest, self).tearDown() # --Helpers----------------------------------------------------------------- def _get_share_reference(self, prefix=TEST_SHARE_PREFIX): share_name = self.get_resource_name(prefix) share = self.fsc.get_share_client(share_name) self.test_shares.append(share) return share async def _create_share(self, prefix=TEST_SHARE_PREFIX): share_client = self._get_share_reference(prefix) share = await share_client.create_share() return share_client # --Test cases for shares ----------------------------------------- async def _test_create_share_async(self): # Arrange share = self._get_share_reference() # Act created = await share.create_share() # Assert self.assertTrue(created) @record def test_create_share_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_create_share_async()) async def _test_create_share_snapshot_async(self): # Arrange share = self._get_share_reference() # Act created = await share.create_share() snapshot = await share.create_snapshot() # Assert self.assertTrue(created) self.assertIsNotNone(snapshot['snapshot']) self.assertIsNotNone(snapshot['etag']) self.assertIsNotNone(snapshot['last_modified']) @record def test_create_share_snapshot_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_create_share_snapshot_async()) async def _test_create_snapshot_with_metadata_async(self): # Arrange share = self._get_share_reference() metadata = {"test1": "foo", "test2": "bar"} metadata2 = {"test100": "foo100", "test200": "bar200"} # Act created = await share.create_share(metadata=metadata) snapshot = await share.create_snapshot(metadata=metadata2) share_props = await share.get_share_properties() snapshot_client = ShareClient( self.get_file_url(), share_name=share.share_name, snapshot=snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY) snapshot_props = await snapshot_client.get_share_properties() # Assert self.assertTrue(created) self.assertIsNotNone(snapshot['snapshot']) self.assertIsNotNone(snapshot['etag']) self.assertIsNotNone(snapshot['last_modified']) self.assertEqual(share_props.metadata, metadata) self.assertEqual(snapshot_props.metadata, metadata2) @record def test_create_snapshot_with_metadata_async(self): loop = asyncio.get_event_loop() loop.run_until_complete( self._test_create_snapshot_with_metadata_async()) async def _test_delete_share_with_snapshots_async(self): # Arrange share = self._get_share_reference() await share.create_share() snapshot = await share.create_snapshot() # Act with self.assertRaises(HttpResponseError): await share.delete_share() deleted = await share.delete_share(delete_snapshots=True) self.assertIsNone(deleted) @record def test_delete_share_with_snapshots_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_delete_share_with_snapshots_async()) async def _test_delete_snapshot_async(self): # Arrange share = self._get_share_reference() await share.create_share() snapshot = await share.create_snapshot() # Act with self.assertRaises(HttpResponseError): await share.delete_share() snapshot_client = ShareClient( self.get_file_url(), share_name=share.share_name, snapshot=snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY) deleted = await snapshot_client.delete_share() self.assertIsNone(deleted) @record def test_delete_snapshot_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_delete_snapshot_async()) async def _test_create_share_fail_on_exist(self): # Arrange share = self._get_share_reference() # Act created = await share.create_share() # Assert self.assertTrue(created) @record def test_create_share_fail_on_exist(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_create_share_fail_on_exist()) async def _test_create_share_with_already_existing_share_fail_on_exist_async( self): # Arrange share = self._get_share_reference() # Act created = await share.create_share() with self.assertRaises(HttpResponseError): await share.create_share() # Assert self.assertTrue(created) @record def test_create_share_with_already_existing_share_fail_on_exist_async( self): loop = asyncio.get_event_loop() loop.run_until_complete( self. _test_create_share_with_already_existing_share_fail_on_exist_async( )) async def _test_create_share_with_metadata_async(self): # Arrange metadata = {'hello': 'world', 'number': '42'} # Act client = self._get_share_reference() created = await client.create_share(metadata=metadata) # Assert self.assertTrue(created) props = await client.get_share_properties() self.assertDictEqual(props.metadata, metadata) @record def test_create_share_with_metadata_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_create_share_with_metadata_async()) async def _test_create_share_with_quota_async(self): # Arrange # Act client = self._get_share_reference() created = await client.create_share(quota=1) # Assert props = await client.get_share_properties() self.assertTrue(created) self.assertEqual(props.quota, 1) @record def test_create_share_with_quota_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_create_share_with_quota_async()) async def _test_share_exists_async(self): # Arrange share = await self._create_share() # Act exists = await share.get_share_properties() # Assert self.assertTrue(exists) @record def test_share_exists_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_share_exists_async()) async def _test_share_not_exists_async(self): # Arrange share = self._get_share_reference() # Act with self.assertRaises(ResourceNotFoundError): await share.get_share_properties() # Assert @record def test_share_not_exists_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_share_not_exists_async()) async def _test_share_snapshot_exists_async(self): # Arrange share = await self._create_share() snapshot = await share.create_snapshot() # Act snapshot_client = self.fsc.get_share_client(share.share_name, snapshot=snapshot) exists = await snapshot_client.get_share_properties() # Assert self.assertTrue(exists) @record def test_share_snapshot_exists_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_share_snapshot_exists_async()) async def _test_share_snapshot_not_exists_async(self): # Arrange share = await self._create_share() made_up_snapshot = '2017-07-19T06:53:46.0000000Z' # Act snapshot_client = self.fsc.get_share_client(share.share_name, snapshot=made_up_snapshot) with self.assertRaises(ResourceNotFoundError): await snapshot_client.get_share_properties() # Assert @record def test_share_snapshot_not_exists_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_share_snapshot_not_exists_async()) async def _test_unicode_create_share_unicode_name_async(self): # Arrange share_name = u'啊齄丂狛狜' # Act with self.assertRaises(HttpResponseError): # not supported - share name must be alphanumeric, lowercase client = self.fsc.get_share_client(share_name) await client.create_share() # Assert @record def test_unicode_create_share_unicode_name_async(self): loop = asyncio.get_event_loop() loop.run_until_complete( self._test_unicode_create_share_unicode_name_async()) async def _test_list_shares_no_options_async(self): # Arrange share = await self._create_share() # Act shares = [] async for s in self.fsc.list_shares(): shares.append(s) # Assert self.assertIsNotNone(shares) self.assertGreaterEqual(len(shares), 1) self.assertIsNotNone(shares[0]) self.assertNamedItemInContainer(shares, share.share_name) @record def test_list_shares_no_options_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_list_shares_no_options_async()) async def _test_list_shares_with_snapshot_async(self): # Arrange share = self._get_share_reference() await share.create_share() snapshot1 = await share.create_snapshot() snapshot2 = await share.create_snapshot() # Act shares = self.fsc.list_shares(include_snapshots=True) # Assert self.assertIsNotNone(shares) all_shares = [] async for s in shares: all_shares.append(s) self.assertEqual(len(all_shares), 3) self.assertNamedItemInContainer(all_shares, share.share_name) self.assertNamedItemInContainer(all_shares, snapshot1['snapshot']) self.assertNamedItemInContainer(all_shares, snapshot2['snapshot']) @record def test_list_shares_with_snapshot_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_list_shares_with_snapshot_async()) async def _test_list_shares_with_prefix_async(self): # Arrange share = self._get_share_reference() await share.create_share() # Act shares = [] async for s in self.fsc.list_shares(name_starts_with=share.share_name): shares.append(s) # Assert self.assertEqual(len(shares), 1) self.assertIsNotNone(shares[0]) self.assertEqual(shares[0].name, share.share_name) self.assertIsNone(shares[0].metadata) @record def test_list_shares_with_prefix_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_list_shares_with_prefix_async()) async def _test_list_shares_with_include_metadata_async(self): # Arrange metadata = {'hello': 'world', 'number': '42'} share = self._get_share_reference() await share.create_share(metadata=metadata) # Act shares = [] async for s in self.fsc.list_shares(share.share_name, include_metadata=True): shares.append(s) # Assert self.assertIsNotNone(shares) self.assertGreaterEqual(len(shares), 1) self.assertIsNotNone(shares[0]) self.assertNamedItemInContainer(shares, share.share_name) self.assertDictEqual(shares[0].metadata, metadata) @record def test_list_shares_with_include_metadata_async(self): loop = asyncio.get_event_loop() loop.run_until_complete( self._test_list_shares_with_include_metadata_async()) async def _test_list_shares_with_num_results_and_marker_async(self): # Arrange prefix = 'listshare' share_names = [] for i in range(0, 4): share = await self._create_share(prefix + str(i)) share_names.append(share.share_name) share_names.sort() # Act generator1 = self.fsc.list_shares(prefix, results_per_page=2).by_page() shares1 = [] async for s in await generator1.__anext__(): shares1.append(s) generator2 = self.fsc.list_shares(prefix, results_per_page=2).by_page( continuation_token=generator1.continuation_token) shares2 = [] async for s in await generator2.__anext__(): shares2.append(s) # Assert self.assertIsNotNone(shares1) self.assertEqual(len(shares1), 2) self.assertNamedItemInContainer(shares1, share_names[0]) self.assertNamedItemInContainer(shares1, share_names[1]) self.assertIsNotNone(shares2) self.assertEqual(len(shares2), 2) self.assertNamedItemInContainer(shares2, share_names[2]) self.assertNamedItemInContainer(shares2, share_names[3]) @record def test_list_shares_with_num_results_and_marker_async(self): loop = asyncio.get_event_loop() loop.run_until_complete( self._test_list_shares_with_num_results_and_marker_async()) async def _test_set_share_metadata_async(self): # Arrange share = await self._create_share() metadata = {'hello': 'world', 'number': '42'} # Act await share.set_share_metadata(metadata) # Assert props = await share.get_share_properties() md = props.metadata self.assertDictEqual(md, metadata) @record def test_set_share_metadata_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_set_share_metadata_async()) async def _test_get_share_metadata_async(self): # Arrange metadata = {'hello': 'world', 'number': '42'} # Act client = self._get_share_reference() created = await client.create_share(metadata=metadata) # Assert self.assertTrue(created) props = await client.get_share_properties() self.assertDictEqual(props.metadata, metadata) @record def test_get_share_metadata_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_get_share_metadata_async()) async def _test_get_share_metadata_with_snapshot_async(self): # Arrange metadata = {'hello': 'world', 'number': '42'} # Act client = self._get_share_reference() created = await client.create_share(metadata=metadata) snapshot = await client.create_snapshot() snapshot_client = self.fsc.get_share_client(client.share_name, snapshot=snapshot) # Assert self.assertTrue(created) props = await snapshot_client.get_share_properties() self.assertDictEqual(props.metadata, metadata) @record def test_get_share_metadata_with_snapshot_async(self): loop = asyncio.get_event_loop() loop.run_until_complete( self._test_get_share_metadata_with_snapshot_async()) async def _test_set_share_properties_async(self): # Arrange share = await self._create_share() await share.set_share_quota(1) # Act props = await share.get_share_properties() # Assert self.assertIsNotNone(props) self.assertEqual(props.quota, 1) @record def test_set_share_properties_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_set_share_properties_async()) async def _test_delete_share_with_existing_share_async(self): # Arrange share = self._get_share_reference() await share.create_share() # Act deleted = await share.delete_share() # Assert self.assertIsNone(deleted) @record def test_delete_share_with_existing_share_async(self): loop = asyncio.get_event_loop() loop.run_until_complete( self._test_delete_share_with_existing_share_async()) async def _test_delete_share_with_existing_share_fail_not_exist_async( self): # Arrange client = self._get_share_reference() # Act with LogCaptured(self) as log_captured: with self.assertRaises(HttpResponseError): await client.delete_share() log_as_str = log_captured.getvalue() @record def test_delete_share_with_existing_share_fail_not_exist_async(self): loop = asyncio.get_event_loop() loop.run_until_complete( self._test_delete_share_with_existing_share_fail_not_exist_async()) async def _test_delete_share_with_non_existing_share_async(self): # Arrange client = self._get_share_reference() # Act with LogCaptured(self) as log_captured: with self.assertRaises(HttpResponseError): deleted = await client.delete_share() log_as_str = log_captured.getvalue() self.assertTrue('ERROR' not in log_as_str) @record def test_delete_share_with_non_existing_share_async(self): loop = asyncio.get_event_loop() loop.run_until_complete( self._test_delete_share_with_non_existing_share_async()) async def _test_delete_share_with_non_existing_share_fail_not_exist_async( self): # Arrange client = self._get_share_reference() # Act with LogCaptured(self) as log_captured: with self.assertRaises(HttpResponseError): await client.delete_share() log_as_str = log_captured.getvalue() @record def test_delete_share_with_non_existing_share_fail_not_exist_async(self): loop = asyncio.get_event_loop() loop.run_until_complete( self. _test_delete_share_with_non_existing_share_fail_not_exist_async()) async def _test_get_share_stats_async(self): # Arrange share = self._get_share_reference() await share.create_share() # Act share_usage = await share.get_share_stats() # Assert self.assertEqual(share_usage, 0) @record def test_get_share_stats_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_get_share_stats_async()) async def _test_set_share_acl_async(self): # Arrange share = self._get_share_reference() await share.create_share() # Act resp = await share.set_share_access_policy(signed_identifiers=dict()) # Assert acl = await share.get_share_access_policy() self.assertIsNotNone(acl) @record def test_set_share_acl_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_set_share_acl_async()) async def _test_set_share_acl_with_empty_signed_identifiers_async(self): # Arrange share = self._get_share_reference() await share.create_share() # Act resp = await share.set_share_access_policy(dict()) # Assert acl = await share.get_share_access_policy() self.assertIsNotNone(acl) self.assertEqual(len(acl.get('signed_identifiers')), 0) @record def test_set_share_acl_with_empty_signed_identifiers_async(self): loop = asyncio.get_event_loop() loop.run_until_complete( self._test_set_share_acl_with_empty_signed_identifiers_async()) async def _test_set_share_acl_with_signed_identifiers_async(self): # Arrange share = self._get_share_reference() await share.create_share() # Act identifiers = dict() identifiers['testid'] = AccessPolicy( permission=ShareSasPermissions(write=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) resp = await share.set_share_access_policy(identifiers) # Assert acl = await share.get_share_access_policy() self.assertIsNotNone(acl) self.assertEqual(len(acl['signed_identifiers']), 1) self.assertEqual(acl['signed_identifiers'][0].id, 'testid') @record def test_set_share_acl_with_signed_identifiers_async(self): loop = asyncio.get_event_loop() loop.run_until_complete( self._test_set_share_acl_with_signed_identifiers_async()) async def _test_set_share_acl_too_many_ids_async(self): # Arrange share = self._get_share_reference() await share.create_share() # Act identifiers = dict() for i in range(0, 6): identifiers['id{}'.format(i)] = AccessPolicy() # Assert with self.assertRaises(ValueError) as e: await share.set_share_access_policy(identifiers) self.assertEqual( str(e.exception), 'Too many access policies provided. The server does not support setting more than 5 access policies on a single resource.' ) @record def test_set_share_acl_too_many_ids_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_set_share_acl_too_many_ids_async()) async def _test_list_directories_and_files_async(self): # Arrange share = await self._create_share() dir0 = share.get_directory_client() await dir0.upload_file('file1', 'data1') dir1 = share.get_directory_client('dir1') await dir1.create_directory() await dir1.upload_file('file2', 'data2') dir2 = share.get_directory_client('dir2') await dir2.create_directory() # Act resp = [] async for d in share.list_directories_and_files(): resp.append(d) # Assert self.assertIsNotNone(resp) self.assertEqual(len(resp), 3) self.assertIsNotNone(resp[0]) self.assertNamedItemInContainer(resp, 'dir1') self.assertNamedItemInContainer(resp, 'dir2') self.assertNamedItemInContainer(resp, 'file1') @record def test_list_directories_and_files_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_list_directories_and_files_async()) async def _test_list_directories_and_files_with_snapshot_async(self): # Arrange share_name = await self._create_share() dir1 = share_name.get_directory_client('dir1') await dir1.create_directory() dir2 = share_name.get_directory_client('dir2') await dir2.create_directory() snapshot1 = await share_name.create_snapshot() dir3 = share_name.get_directory_client('dir3') await dir3.create_directory() file1 = share_name.get_file_client('file1') await file1.upload_file('data') # Act snapshot_client = self.fsc.get_share_client(share_name.share_name, snapshot=snapshot1) resp = [] async for d in snapshot_client.list_directories_and_files(): resp.append(d) # Assert self.assertIsNotNone(resp) self.assertEqual(len(resp), 2) self.assertIsNotNone(resp[0]) self.assertNamedItemInContainer(resp, 'dir1') self.assertNamedItemInContainer(resp, 'dir2') @record def test_list_directories_and_files_with_snapshot_async(self): loop = asyncio.get_event_loop() loop.run_until_complete( self._test_list_directories_and_files_with_snapshot_async()) async def _test_list_directories_and_files_with_num_results_async(self): # Arrange share_name = await self._create_share() dir1 = await share_name.create_directory('dir1') root = share_name.get_directory_client() await root.upload_file('filea1', '1024') await root.upload_file('filea2', '1024') await root.upload_file('filea3', '1024') await root.upload_file('fileb1', '1024') # Act result = share_name.list_directories_and_files( results_per_page=2).by_page() results = [] async for r in await result.__anext__(): results.append(r) # Assert self.assertIsNotNone(result) self.assertEqual(len(results), 2) self.assertNamedItemInContainer(results, 'dir1') self.assertNamedItemInContainer(results, 'filea1') @record def test_list_directories_and_files_with_num_results_async(self): loop = asyncio.get_event_loop() loop.run_until_complete( self._test_list_directories_and_files_with_num_results_async()) async def _test_list_directories_and_files_with_num_results_and_marker_async( self): # Arrange share_name = await self._create_share() dir1 = share_name.get_directory_client('dir1') await dir1.create_directory() await dir1.upload_file('filea1', '1024') await dir1.upload_file('filea2', '1024') await dir1.upload_file('filea3', '1024') await dir1.upload_file('fileb1', '1024') # Act generator1 = share_name.list_directories_and_files( 'dir1', results_per_page=2).by_page() result1 = [] async for r in await generator1.__anext__(): result1.append(r) generator2 = share_name.list_directories_and_files( 'dir1', results_per_page=2).by_page( continuation_token=generator1.continuation_token) result2 = [] async for r in await generator2.__anext__(): result2.append(r) # Assert self.assertEqual(len(result1), 2) self.assertEqual(len(result2), 2) self.assertNamedItemInContainer(result1, 'filea1') self.assertNamedItemInContainer(result1, 'filea2') self.assertNamedItemInContainer(result2, 'filea3') self.assertNamedItemInContainer(result2, 'fileb1') self.assertEqual(generator2.continuation_token, None) @record def test_list_directories_and_files_with_num_results_and_marker_async( self): loop = asyncio.get_event_loop() loop.run_until_complete( self. _test_list_directories_and_files_with_num_results_and_marker_async( )) async def _test_list_directories_and_files_with_prefix_async(self): # Arrange share = await self._create_share() dir1 = await share.create_directory('dir1') await share.create_directory('dir1/pref_dir3') await share.create_directory('dir2') root = share.get_directory_client() await root.upload_file('file1', '1024') await dir1.upload_file('pref_file2', '1025') await dir1.upload_file('file3', '1025') # Act resp = [] async for d in share.list_directories_and_files( 'dir1', name_starts_with='pref'): resp.append(d) # Assert self.assertIsNotNone(resp) self.assertEqual(len(resp), 2) self.assertIsNotNone(resp[0]) self.assertNamedItemInContainer(resp, 'pref_file2') self.assertNamedItemInContainer(resp, 'pref_dir3') @record def test_list_directories_and_files_with_prefix_async(self): loop = asyncio.get_event_loop() loop.run_until_complete( self._test_list_directories_and_files_with_prefix_async()) async def _test_shared_access_share_async(self): # SAS URL is calculated from storage key, so this test runs live only if TestMode.need_recording_file(self.test_mode): return # Arrange file_name = 'file1' dir_name = 'dir1' data = b'hello world' share = await self._create_share() dir1 = await share.create_directory(dir_name) await dir1.upload_file(file_name, data) token = generate_share_sas( share.account_name, share.share_name, share.credential.account_key, expiry=datetime.utcnow() + timedelta(hours=1), permission=ShareSasPermissions(read=True), ) sas_client = ShareFileClient( self.get_file_url(), share_name=share.share_name, file_path=dir_name + '/' + file_name, credential=token, ) # Act response = requests.get(sas_client.url) # Assert self.assertTrue(response.ok) self.assertEqual(data, response.content) @record def test_shared_access_share_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_shared_access_share_async()) async def _test_create_permission_for_share(self): user_given_permission = "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)" share_client = await self._create_share() permission_key = await share_client.create_permission_for_share( user_given_permission) self.assertIsNotNone(permission_key) server_returned_permission = await share_client.get_permission_for_share( permission_key) self.assertIsNotNone(server_returned_permission) permission_key2 = await share_client.create_permission_for_share( server_returned_permission) # the permission key obtained from user_given_permission should be the same as the permission key obtained from # server returned permission self.assertEqual(permission_key, permission_key2) @record def test_create_permission_for_share_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_create_permission_for_share()) async def _test_transport_closed_only_once_async(self): if TestMode.need_recording_file(self.test_mode): return transport = AioHttpTransport() url = self.get_file_url() credential = self.get_shared_key_credential() prefix = TEST_SHARE_PREFIX share_name = self.get_resource_name(prefix) async with ShareServiceClient(url, credential=credential, transport=transport) as fsc: await fsc.get_service_properties() assert transport.session is not None async with fsc.get_share_client(share_name) as fc: assert transport.session is not None await fsc.get_service_properties() assert transport.session is not None @record def test_transport_closed_only_once_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_transport_closed_only_once_async())
class StorageDirectoryTest(FileTestCase): def setUp(self): super(StorageDirectoryTest, self).setUp() url = self.get_file_url() credential = self.get_shared_key_credential() self.fsc = ShareServiceClient(url, credential=credential, transport=AiohttpTestTransport()) self.share_name = self.get_resource_name('utshare') def tearDown(self): if not self.is_playback(): loop = asyncio.get_event_loop() try: loop.run_until_complete(self.fsc.delete_share(self.share_name, delete_snapshots=True)) except: pass return super(StorageDirectoryTest, self).tearDown() # --Helpers----------------------------------------------------------------- async def _setup(self): if not self.is_playback(): try: await self.fsc.create_share(self.share_name) except: pass # --Test cases for directories ---------------------------------------------- async def _test_create_directories_async(self): # Arrange await self._setup() share_client = self.fsc.get_share_client(self.share_name) # Act created = await share_client.create_directory('dir1') # Assert self.assertTrue(created) @record def test_create_directories_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_create_directories_async()) async def _test_create_directories_with_metadata_async(self): # Arrange await self._setup() 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) @record def test_create_directories_with_metadata_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_create_directories_with_metadata_async()) async def _test_create_directories_fail_on_exist_async(self): # Arrange await self._setup() 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) @record def test_create_directories_fail_on_exist_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_create_directories_fail_on_exist_async()) async def _test_create_subdirectories_async(self): # Arrange await self._setup() 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') @record def test_create_subdirectories_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_create_subdirectories_async()) async def _test_create_subdirectories_with_metadata_async(self): # Arrange await self._setup() 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) @record def test_create_subdirectories_with_metadata_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_create_subdirectories_with_metadata_async()) async def _test_create_file_in_directory_async(self): # Arrange await self._setup() 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) def test_create_file_in_directory_async(self): if TestMode.need_recording_file(self.test_mode): return loop = asyncio.get_event_loop() loop.run_until_complete(self._test_create_file_in_directory_async()) async def _test_delete_file_in_directory_async(self): # Arrange await self._setup() 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() @record def test_delete_file_in_directory_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_delete_file_in_directory_async()) async def _test_delete_subdirectories_async(self): # Arrange await self._setup() 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() @record def test_delete_subdirectories_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_delete_subdirectories_async()) async def _test_get_directory_properties_async(self): # Arrange await self._setup() 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) @record def test_get_directory_properties_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_get_directory_properties_async()) async def _test_get_directory_properties_with_snapshot_async(self): # Arrange await self._setup() 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) @record def test_get_directory_properties_with_snapshot_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_get_directory_properties_with_snapshot_async()) async def _test_get_directory_metadata_with_snapshot_async(self): # Arrange await self._setup() 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) @record def test_get_directory_metadata_with_snapshot_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_get_directory_metadata_with_snapshot_async()) async def _test_get_directory_properties_with_non_existing_directory_async(self): # Arrange await self._setup() 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 @record def test_get_directory_properties_with_non_existing_directory_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_get_directory_properties_with_non_existing_directory_async()) async def _test_directory_exists_async(self): # Arrange await self._setup() 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) @record def test_directory_exists_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_directory_exists_async()) async def _test_directory_not_exists_async(self): # Arrange await self._setup() 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 @record def test_directory_not_exists_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_directory_not_exists_async()) async def _test_directory_parent_not_exists_async(self): # Arrange await self._setup() 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) @record def test_directory_parent_not_exists_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_directory_parent_not_exists_async()) async def _test_directory_exists_with_snapshot_async(self): # Arrange await self._setup() 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) @record def test_directory_exists_with_snapshot_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_directory_exists_with_snapshot_async()) async def _test_directory_not_exists_with_snapshot_async(self): # Arrange await self._setup() 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 @record def test_directory_not_exists_with_snapshot_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_directory_not_exists_with_snapshot_async()) async def _test_get_set_directory_metadata_async(self): # Arrange await self._setup() 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) @record def test_get_set_directory_metadata_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_get_set_directory_metadata_async()) async def _test_set_directory_properties_with_empty_smb_properties(self): # Arrange await self._setup() 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) @record def test_set_directory_properties_with_empty_smb_properties_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_set_directory_properties_with_empty_smb_properties()) async def _test_set_directory_properties_with_file_permission_key(self): # Arrange await self._setup() 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) @record def test_set_directory_properties_with_file_permission_key_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_set_directory_properties_with_file_permission_key()) async def _test_list_subdirectories_and_files_async(self): # Arrange await self._setup() 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) @record def test_list_subdirectories_and_files_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_list_subdirectories_and_files_async()) async def _test_list_subdirectories_and_files_with_prefix_async(self): # Arrange await self._setup() 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) @record def test_list_subdirectories_and_files_with_prefix_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_list_subdirectories_and_files_with_prefix_async()) async def _test_list_subdirectories_and_files_with_snapshot_async(self): # Arrange await self._setup() 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) @record def test_list_subdirectories_and_files_with_snapshot_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_list_subdirectories_and_files_with_snapshot_async()) async def _test_list_nested_subdirectories_and_files_async(self): # Arrange await self._setup() 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) @record def test_list_nested_subdirectories_and_files_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_list_nested_subdirectories_and_files_async()) async def _test_delete_directory_with_existing_share_async(self): # Arrange await self._setup() 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() @record def test_delete_directory_with_existing_share_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_delete_directory_with_existing_share_async()) async def _test_delete_directory_with_non_existing_directory_async(self): # Arrange await self._setup() 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 @record def test_delete_directory_with_non_existing_directory_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_delete_directory_with_non_existing_directory_async()) async def _test_get_directory_properties_server_encryption_async(self): # Arrange await self._setup() 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) if self.is_file_encryption_enabled(): self.assertTrue(props.server_encrypted) else: self.assertFalse(props.server_encrypted) @record def test_get_directory_properties_server_encryption_async(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._test_get_directory_properties_server_encryption_async())