def test_invalid_api_version(self): with pytest.raises(ValueError) as error: ShareServiceClient( "https://foo.file.core.windows.net/account", credential="fake_key", api_version="foo") self.assertTrue(str(error.value).startswith("Unsupported API version 'foo'.")) with pytest.raises(ValueError) as error: ShareClient( "https://foo.file.core.windows.net/account", "share_name", credential="fake_key", api_version="foo") self.assertTrue(str(error.value).startswith("Unsupported API version 'foo'.")) with pytest.raises(ValueError) as error: ShareDirectoryClient( "https://foo.file.core.windows.net/account", "share_name", "dir_path", credential="fake_key", api_version="foo") self.assertTrue(str(error.value).startswith("Unsupported API version 'foo'.")) with pytest.raises(ValueError) as error: ShareFileClient( "https://foo.file.core.windows.net/account", "share", self._get_file_reference(), credential="fake_key", api_version="foo") self.assertTrue(str(error.value).startswith("Unsupported API version 'foo'."))
def test_user_agent_append(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) service = ShareServiceClient(self.account_url(storage_account_name, "file"), credential=self.account_key) def callback(response): self.assertTrue('User-Agent' in response.http_request.headers) assert ( "customer_user_agent azsdk-python-storage-file-share/{} Python/{} ({})" .format(VERSION, platform.python_version(), platform.platform()) ) in response.http_request.headers['User-Agent'] service.get_service_properties(raw_response_hook=callback, user_agent='customer_user_agent')
def get_share_client(self): # [START get_share_client] from azure.storage.fileshare import ShareServiceClient file_service = ShareServiceClient.from_connection_string( self.connection_string) # Get a share client to interact with a specific share share = file_service.get_share_client("fileshare2")
def test_user_agent_append(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) service = ShareServiceClient(self.get_file_url(storage_account.name), credential=self.account_key) def callback(response): self.assertTrue('User-Agent' in response.http_request.headers) self.assertEqual( response.http_request.headers['User-Agent'], "azsdk-python-storage-file-share/{} Python/{} ({}) customer_user_agent" .format(VERSION, platform.python_version(), platform.platform())) custom_headers = {'User-Agent': 'customer_user_agent'} service.get_service_properties(raw_response_hook=callback, headers=custom_headers)
def __init__(self): self.conn_str = settings.FILES_CONN_STRING self.share = f"{settings.ENVIRONMENT}-{settings.FILES_SHARE}" self.client = ShareServiceClient.from_connection_string(self.conn_str) try: self.share_client = ShareClient.from_connection_string( self.conn_str, self.share) self.share_client.create_share() except ResourceExistsError: pass
def test_user_agent_custom(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) custom_app = "TestApp/v1.0" service = ShareServiceClient(self.account_url(storage_account, "file"), credential=self.account_key, user_agent=custom_app) def callback1(response): self.assertTrue('User-Agent' in response.http_request.headers) assert ( "TestApp/v1.0 azsdk-python-storage-file-share/{} Python/{} ({})" .format(VERSION, platform.python_version(), platform.platform()) ) in response.http_request.headers['User-Agent'] service.get_service_properties(raw_response_hook=callback1) def callback2(response): self.assertTrue('User-Agent' in response.http_request.headers) assert ( "TestApp/v2.0 TestApp/v1.0 azsdk-python-storage-file-share/{} Python/{} ({})" .format(VERSION, platform.python_version(), platform.platform()) ) in response.http_request.headers['User-Agent'] service.get_service_properties(raw_response_hook=callback2, user_agent="TestApp/v2.0")
def test_client_request_id_echo(self): # client request id is different for every request, so it will never match the recorded one pytest.skip("Issue tracked here: https://github.com/Azure/azure-sdk-for-python/issues/8098") if TestMode.need_recording_file(self.test_mode): return # Arrange request_id_header_name = 'x-ms-client-request-id' service = ShareServiceClient(self.get_file_url(), credential=self.account_key) # Act make the client request ID slightly different def callback(response): response.http_response.status_code = 200 response.http_response.headers[request_id_header_name] += '1' # Assert the client request ID validation is working with self.assertRaises(AzureError): service.get_service_properties(raw_response_hook=callback) # Act remove the echoed client request ID def callback(response): response.status_code = 200 del response.http_response.headers[request_id_header_name] # Assert the client request ID validation is not throwing when the ID is not echoed service.get_service_properties(raw_response_hook=callback)
def test_user_agent_custom(self): custom_app = "TestApp/v1.0" service = ShareServiceClient( self.get_file_url(), credential=self.account_key, user_agent=custom_app) def callback1(response): self.assertTrue('User-Agent' in response.http_request.headers) self.assertEqual( response.http_request.headers['User-Agent'], "TestApp/v1.0 azsdk-python-storage-file-share/{} Python/{} ({})".format( VERSION, platform.python_version(), platform.platform())) service.get_service_properties(raw_response_hook=callback1) def callback2(response): self.assertTrue('User-Agent' in response.http_request.headers) self.assertEqual( response.http_request.headers['User-Agent'], "TestApp/v2.0 azsdk-python-storage-file-share/{} Python/{} ({})".format( VERSION, platform.python_version(), platform.platform())) service.get_service_properties(raw_response_hook=callback2, user_agent="TestApp/v2.0")
def get_first_snapshot(self, connection_string): try: # Create a ShareServiceClient from a connection string service_client = ShareServiceClient.from_connection_string(connection_string) # List the shares in the file service shares = list(service_client.list_shares(include_snapshots=True)) for share in shares: if (share["snapshot"]): return share["snapshot"] except ResourceNotFoundError as ex: print("ResourceNotFoundError:", ex.message)
def __init__(self, arguments): super().__init__(arguments) connection_string = self.get_from_env( "AZURE_STORAGE_CONNECTION_STRING") kwargs = {} if self.args.max_range_size: kwargs['max_range_size'] = self.args.max_range_size if not _ServiceTest.service_client or self.args.no_client_share: _ServiceTest.service_client = SyncShareServiceClient.from_connection_string( conn_str=connection_string, **kwargs) _ServiceTest.async_service_client = AsyncShareServiceClient.from_connection_string( conn_str=connection_string, **kwargs) self.service_client = _ServiceTest.service_client self.async_service_client = _ServiceTest.async_service_client
def test_transport_closed_only_once(self): if TestMode.need_recording_file(self.test_mode): return transport = RequestsTransport() url = self.get_file_url() credential = self.get_shared_key_credential() prefix = TEST_SHARE_PREFIX share_name = self.get_resource_name(prefix) with ShareServiceClient(url, credential=credential, transport=transport) as fsc: fsc.get_service_properties() assert transport.session is not None with fsc.get_share_client(share_name) as fc: assert transport.session is not None fsc.get_service_properties() assert transport.session is not None
def authentication_shared_access_signature(self): # Instantiate a ShareServiceClient using a connection string # [START generate_sas_token] from azure.storage.fileshare import ShareServiceClient share_service_client = ShareServiceClient.from_connection_string( self.connection_string) # Create a SAS token to use to authenticate a new client from azure.storage.fileshare import generate_account_sas sas_token = generate_account_sas( share_service_client.account_name, share_service_client.credential.account_key, resource_types="object", permission="read", expiry=datetime.utcnow() + timedelta(hours=1))
def test_transport_closed_only_once(self, resource_group, location, storage_account, storage_account_key): if not self.is_live: return self._setup(storage_account, storage_account_key) transport = RequestsTransport() url = self.account_url(storage_account, "file") credential = storage_account_key prefix = TEST_SHARE_PREFIX share_name = self.get_resource_name(prefix) with ShareServiceClient(url, credential=credential, transport=transport) as fsc: fsc.get_service_properties() assert transport.session is not None with fsc.get_share_client(share_name) as fc: assert transport.session is not None fsc.get_service_properties() assert transport.session is not None
def file_service_properties(self): # Instantiate the ShareServiceClient from a connection string from azure.storage.fileshare import ShareServiceClient file_service = ShareServiceClient.from_connection_string( self.connection_string) # [START set_service_properties] # Create service properties from azure.storage.fileshare import Metrics, CorsRule, RetentionPolicy # Create metrics for requests statistics hour_metrics = Metrics(enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=5)) minute_metrics = Metrics(enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=5)) # Create CORS rules cors_rule1 = CorsRule(['www.xyz.com'], ['GET']) allowed_origins = ['www.xyz.com', "www.ab.com", "www.bc.com"] allowed_methods = ['GET', 'PUT'] max_age_in_seconds = 500 exposed_headers = [ "x-ms-meta-data*", "x-ms-meta-source*", "x-ms-meta-abc", "x-ms-meta-bcd" ] allowed_headers = [ "x-ms-meta-data*", "x-ms-meta-target*", "x-ms-meta-xyz", "x-ms-meta-foo" ] cors_rule2 = CorsRule(allowed_origins, allowed_methods, max_age_in_seconds=max_age_in_seconds, exposed_headers=exposed_headers, allowed_headers=allowed_headers) cors = [cors_rule1, cors_rule2] # Set the service properties file_service.set_service_properties(hour_metrics, minute_metrics, cors) # [END set_service_properties] # [START get_service_properties] properties = file_service.get_service_properties()
def list_shares_snapshots(self, connection_string): try: # <Snippet_CreateShareServiceClient> # Create a ShareServiceClient from a connection string service_client = ShareServiceClient.from_connection_string(connection_string) # </Snippet_CreateShareServiceClient> # List the shares in the file service shares = list(service_client.list_shares(include_snapshots=True)) for share in shares: if (share["snapshot"]): print("Share:", share["name"], "Snapshot:", share["snapshot"]) else: print("Share:", share["name"]) except ResourceNotFoundError as ex: print("ResourceNotFoundError:", ex.message)
def set_logging_to_debug(): """ Prints in stdout Input: - env:AZ_CONN_STR Returns: Connection_object ShareServiceClient() """ # Create a logger for the 'azure.storage.fileshare' SDK logger = logging.getLogger('azure.storage.fileshare') logger.setLevel(logging.DEBUG) # Configure a console output handler = logging.StreamHandler(stream=sys.stdout) logger.addHandler(handler) # This client will log detailed information about its HTTP sessions, at DEBUG level service_client = ShareServiceClient.from_connection_string(AZ_CONN_STR, logging_enable=True) return service_client
def list_shares_in_service(self): # Instantiate the ShareServiceClient from a connection string from azure.storage.fileshare import ShareServiceClient file_service = ShareServiceClient.from_connection_string(self.connection_string) # [START fsc_create_shares] file_service.create_share(share_name="fileshare1") # [END fsc_create_shares] try: # [START fsc_list_shares] # List the shares in the file service my_shares = list(file_service.list_shares()) # Print the shares for share in my_shares: print(share) # [END fsc_list_shares] finally: # [START fsc_delete_shares] file_service.delete_share(share_name="fileshare1")
def clean_storage_account(connection_string): pool = ThreadPool(16) no_retry = azure.storage.common.retry.no_retry try: blob_service = BlobServiceClient.from_connection_string( connection_string) blob_service.retry = no_retry pool.map( lambda container: delete_container(blob_service, container.name), blob_service.list_containers(timeout=3)) except azure.core.exceptions.ServiceRequestError: print("No blob service") try: file_service = ShareServiceClient.from_connection_string( connection_string) file_service.retry = no_retry pool.map(lambda share: delete_file_share(file_service, share.name), file_service.list_shares(timeout=3)) except azure.core.exceptions.ServiceRequestError: print("No file service") try: queue_service = QueueServiceClient.from_connection_string( connection_string) queue_service.retry = no_retry pool.map(lambda queue: delete_queue(queue_service, queue.name), queue_service.list_queues(timeout=3)) except azure.core.exceptions.ServiceRequestError: print("No queue service") try: table_service = TableService(connection_string=connection_string) table_service.retry = no_retry pool.map(lambda table: delete_table(table_service, table.name), table_service.list_tables(timeout=3)) except azure.common.AzureException: print("No table service")
def get_a_sas_token(put_env=False): """ Input: - env:ACCOUNT_NAME - env:AZURE_STORAGE_ACCESS_KEY Returns: Connection_object ShareServiceClient() """ refresh_var_env() sas_token = generate_account_sas( account_name=ACCOUNT_NAME, account_key=AZURE_STORAGE_ACCESS_KEY, resource_types=ResourceTypes(service=True), permission=AccountSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1) ) if put_env != False:os.putenv(AZ_SAS_TOKEN, connection_string) share_service_client = ShareServiceClient( account_url=f"https://{ACCOUNT_NAME}.file.core.windows.net", credential=sas_token ) return share_service_client
def run_all_samples(self, connection_string): print('Azure Storage File Basis samples - Starting.') #declare variables filename = 'filesample' + self.random_data.get_random_name(6) sharename = 'sharesample' + self.random_data.get_random_name(6) try: # Create an instance of ShareServiceClient service = ShareServiceClient.from_connection_string( conn_str=connection_string) print('\n\n* Basic file operations *\n') self.basic_file_operations(sharename, filename, service) except Exception as e: print('error:' + e) finally: # Delete all Azure Files created in this sample self.file_delete_samples(sharename, filename, service) print('\nAzure Storage File Basic samples - Completed.\n')
def test_service_client_api_version_property(self): service_client = ShareServiceClient( "https://foo.file.core.windows.net/account", credential="fake_key") self.assertEqual(service_client.api_version, self.api_version_2) self.assertEqual(service_client._client._config.version, self.api_version_2) with pytest.raises(AttributeError): service_client.api_version = self.api_version_1 service_client = ShareServiceClient( "https://foo.file.core.windows.net/account", credential="fake_key", api_version=self.api_version_1) self.assertEqual(service_client.api_version, self.api_version_1) self.assertEqual(service_client._client._config.version, self.api_version_1) share_client = service_client.get_share_client("foo") self.assertEqual(share_client.api_version, self.api_version_1) self.assertEqual(share_client._client._config.version, self.api_version_1)
class AzureStorageAccount: def __init__(self, name, key): self.name = name self.key = key self.url = 'https://' + self.name + '.file.core.windows.net' self._client = ShareServiceClient(account_url=self.url, credential=self.key) def get_share_names(self): return [share.name for share in self._client.list_shares()] def get_share(self, name): share = self._client.get_share_client(name) return AzureStorageShare(account=self, name=name, client=share) def create_share(self, name): self._client.create_share(share_name=name) def delete_share(self, name): self._client.delete_share(share_name=name)
def authentication_connection_string(self): # Instantiate the ShareServiceClient from a connection string # [START create_share_service_client_from_conn_string] from azure.storage.fileshare import ShareServiceClient share_service_client = ShareServiceClient.from_connection_string( self.connection_string)
def test_create_service_with_custom_account_endpoint_path(self): custom_account_url = "http://local-machine:11002/custom/account/path/" + self.sas_token for service_type in SERVICES.items(): conn_string = 'DefaultEndpointsProtocol=http;AccountName={};AccountKey={};FileEndpoint={};'.format( self.account_name, self.account_key, custom_account_url) # Act service = service_type[0].from_connection_string( conn_string, share_name="foo", directory_path="bar", file_path="baz") # Assert self.assertEqual(service.account_name, self.account_name) self.assertEqual(service.credential.account_name, self.account_name) self.assertEqual(service.credential.account_key, self.account_key) self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path') service = ShareServiceClient(account_url=custom_account_url) self.assertEqual(service.account_name, None) self.assertEqual(service.credential, None) self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path') self.assertTrue(service.url.startswith('http://local-machine:11002/custom/account/path/?')) service = ShareClient(account_url=custom_account_url, share_name="foo", snapshot="snap") self.assertEqual(service.account_name, None) self.assertEqual(service.share_name, "foo") self.assertEqual(service.snapshot, "snap") self.assertEqual(service.credential, None) self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path') self.assertTrue(service.url.startswith('http://local-machine:11002/custom/account/path/foo?sharesnapshot=snap&')) service = ShareDirectoryClient(account_url=custom_account_url, share_name='foo', directory_path="bar/baz", snapshot="snap") self.assertEqual(service.account_name, None) self.assertEqual(service.share_name, "foo") self.assertEqual(service.directory_path, "bar/baz") self.assertEqual(service.snapshot, "snap") self.assertEqual(service.credential, None) self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path') self.assertTrue(service.url.startswith('http://local-machine:11002/custom/account/path/foo/bar%2Fbaz?sharesnapshot=snap&')) service = ShareDirectoryClient(account_url=custom_account_url, share_name='foo', directory_path="") self.assertEqual(service.account_name, None) self.assertEqual(service.share_name, "foo") self.assertEqual(service.directory_path, "") self.assertEqual(service.snapshot, None) self.assertEqual(service.credential, None) self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path') self.assertTrue(service.url.startswith('http://local-machine:11002/custom/account/path/foo?')) service = ShareFileClient(account_url=custom_account_url, share_name="foo", file_path="bar/baz/file", snapshot="snap") self.assertEqual(service.account_name, None) self.assertEqual(service.share_name, "foo") self.assertEqual(service.directory_path, "bar/baz") self.assertEqual(service.file_path, ["bar", "baz", "file"]) self.assertEqual(service.file_name, "file") self.assertEqual(service.snapshot, "snap") self.assertEqual(service.credential, None) self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path') self.assertTrue(service.url.startswith('http://local-machine:11002/custom/account/path/foo/bar/baz/file?sharesnapshot=snap&')) service = ShareFileClient(account_url=custom_account_url, share_name="foo", file_path="file") self.assertEqual(service.account_name, None) self.assertEqual(service.share_name, "foo") self.assertEqual(service.directory_path, "") self.assertEqual(service.file_path, ["file"]) self.assertEqual(service.file_name, "file") self.assertEqual(service.snapshot, None) self.assertEqual(service.credential, None) self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path') self.assertTrue(service.url.startswith('http://local-machine:11002/custom/account/path/foo/file?'))
def create_client_with_connection_string(self): # Instantiate the ShareServiceClient from a connection string from azure.storage.fileshare import ShareServiceClient file_service = ShareServiceClient.from_connection_string( self.connection_string)
class StorageHandleTest(FileTestCase): def setUp(self): super(StorageHandleTest, self).setUp() file_url = self.get_file_url() credentials = self.get_shared_key_credential() self.fsc = ShareServiceClient(account_url=file_url, credential=credentials) self.test_shares = [] def tearDown(self): if not self.is_playback(): for share in self.test_shares: self.fsc.delete_share(share.share_name, delete_snapshots=True) return super(StorageHandleTest, 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 def _create_share(self, prefix=TEST_SHARE_PREFIX): share_client = self._get_share_reference(prefix) share = share_client.create_share() return share_client def _validate_handles(self, handles): # Assert self.assertIsNotNone(handles) self.assertGreaterEqual(len(handles), 1) self.assertIsNotNone(handles[0]) # verify basic fields # path may or may not be present # last_connect_time_string has been missing in the test self.assertIsNotNone(handles[0].id) self.assertIsNotNone(handles[0].file_id) self.assertIsNotNone(handles[0].parent_id) self.assertIsNotNone(handles[0].session_id) self.assertIsNotNone(handles[0].client_ip) self.assertIsNotNone(handles[0].open_time) @record def test_list_handles_on_share(self): #pytest.skip("") # don't run live, since the test set up was highly manual # only run when recording, or playing back in CI if not TestMode.need_recording_file(self.test_mode): return share = self.fsc.get_share_client(TEST_SHARE_NAME) root = share.get_directory_client() # Act handles = list(root.list_handles(recursive=True)) # Assert self._validate_handles(handles) # @record def test_list_handles_on_share_snapshot(self): # don't run live, since the test set up was highly manual # only run when recording, or playing back in CI if not TestMode.need_recording_file(self.test_mode): return share = self.fsc.get_share_client( TEST_SHARE_NAME, snapshot="2019-05-08T23:27:24.0000000Z") root = share.get_directory_client() # Act handles = list(root.list_handles(recursive=True)) # Assert self._validate_handles(handles) @record def test_list_handles_with_marker(self): # don't run live, since the test set up was highly manual # only run when recording, or playing back in CI if not TestMode.need_recording_file(self.test_mode): return share = self.fsc.get_share_client(TEST_SHARE_NAME) root = share.get_directory_client() # Act handle_generator = root.list_handles(recursive=True, results_per_page=1).by_page() handles = list(next(handle_generator)) # Assert self.assertIsNotNone(handle_generator.continuation_token) self._validate_handles(handles) # Note down a handle that we saw old_handle = handles[0] # Continue listing remaining_handles = list( next( root.list_handles(recursive=True).by_page( continuation_token=handle_generator.continuation_token))) self._validate_handles(handles) # Make sure the old handle did not appear # In other words, the marker worked old_handle_not_present = all( [old_handle.id != handle.id for handle in remaining_handles]) self.assertTrue(old_handle_not_present) @record def test_list_handles_on_directory(self): # don't run live, since the test set up was highly manual # only run when recording, or playing back in CI if not TestMode.need_recording_file(self.test_mode): return share = self.fsc.get_share_client(TEST_SHARE_NAME) dir = share.get_directory_client('wut') # Act handles = list(dir.list_handles(recursive=True)) # Assert self._validate_handles(handles) # Act handles = list(dir.list_handles(recursive=False)) # Assert recursive option is functioning when disabled self.assertTrue(len(handles) == 0) @record def test_list_handles_on_file(self): # don't run live, since the test set up was highly manual # only run when recording, or playing back in CI if not TestMode.need_recording_file(self.test_mode): return share = self.fsc.get_share_client(TEST_SHARE_NAME) client = share.get_file_client('wut/bla.txt') # Act handles = list(client.list_handles()) # Assert self._validate_handles(handles) @record def test_close_single_handle(self): # don't run live, since the test set up was highly manual # only run when recording, or playing back in CI if not TestMode.need_recording_file(self.test_mode): return # Arrange share = self.fsc.get_share_client(TEST_SHARE_NAME) root = share.get_directory_client() handles = list(root.list_handles(recursive=True)) self._validate_handles(handles) # Act with self.assertRaises(ValueError): root.close_handle('*') handles_info = root.close_handle(handles[0]) # Assert 1 handle has been closed self.assertEqual(1, handles_info['closed_handles_count']) @record def test_close_all_handle(self): # don't run live, since the test set up was highly manual # only run when recording, or playing back in CI if not TestMode.need_recording_file(self.test_mode): return # Arrange share = self.fsc.get_share_client(TEST_SHARE_NAME) root = share.get_directory_client() handles = list(root.list_handles(recursive=True)) self._validate_handles(handles) # Act handles_info = root.close_all_handles() # Assert at least 1 handle has been closed self.assertTrue(handles_info['closed_handles_count'] > 1)
def authentication_shared_access_key(self): # Instantiate a ShareServiceClient using a shared access key # [START create_share_service_client] from azure.storage.fileshare import ShareServiceClient share_service_client = ShareServiceClient( account_url=self.account_url, credential=self.shared_access_key)
def __init__(self, name, key): self.name = name self.key = key self.url = 'https://' + self.name + '.file.core.windows.net' self._client = ShareServiceClient(account_url=self.url, credential=self.key)
class StorageGetFileTest(FileTestCase): def setUp(self): super(StorageGetFileTest, self).setUp() # test chunking functionality by reducing the threshold # for chunking and the size of each chunk, otherwise # the tests would take too long to execute self.MAX_SINGLE_GET_SIZE = 32 * 1024 self.MAX_CHUNK_GET_SIZE = 4 * 1024 url = self.get_file_url() credential = self.get_shared_key_credential() self.fsc = ShareServiceClient( url, credential=credential, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) self.share_name = self.get_resource_name('utshare') self.directory_name = self.get_resource_name('utdir') if not self.is_playback(): share = self.fsc.create_share(self.share_name) share.create_directory(self.directory_name) self.byte_file = self.get_resource_name('bytefile') self.byte_data = self.get_random_bytes(64 * 1024 + 5) if not self.is_playback(): byte_file = self.directory_name + '/' + self.byte_file file_client = ShareFileClient(self.get_file_url(), share_name=self.share_name, file_path=byte_file, credential=credential) file_client.upload_file(self.byte_data) def tearDown(self): if not self.is_playback(): try: self.fsc.delete_share(self.share_name, delete_snapshots='include') except: pass if os.path.isfile(FILE_PATH): try: os.remove(FILE_PATH) except: pass return super(StorageGetFileTest, self).tearDown() # --Helpers----------------------------------------------------------------- def _get_file_reference(self): return self.get_resource_name(TEST_FILE_PREFIX) class NonSeekableFile(object): def __init__(self, wrapped_file): self.wrapped_file = wrapped_file def write(self, data): self.wrapped_file.write(data) def read(self, count): return self.wrapped_file.read(count) def seekable(self): return False # -- Get test cases for files ---------------------------------------------- @record def test_unicode_get_file_unicode_data(self): # Arrange file_data = u'hello world啊齄丂狛狜'.encode('utf-8') file_name = self._get_file_reference() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(file_data) # Act file_content = file_client.download_file().readall() # Assert self.assertEqual(file_content, file_data) @record def test_unicode_get_file_binary_data(self): # Arrange base64_data = 'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/wABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4fICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9AQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVpbXF1eX2BhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ent8fX5/gIGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJmam5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1tre4ubq7vL2+v8DBwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5ufo6err7O3u7/Dx8vP09fb3+Pn6+/z9/v8AAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXp7fH1+f4CBgoOEhYaHiImKi4yNjo+QkZKTlJWWl5iZmpucnZ6foKGio6SlpqeoqaqrrK2ur7CxsrO0tba3uLm6u7y9vr/AwcLDxMXGx8jJysvMzc7P0NHS09TV1tfY2drb3N3e3+Dh4uPk5ebn6Onq6+zt7u/w8fLz9PX29/j5+vv8/f7/AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==' binary_data = base64.b64decode(base64_data) file_name = self._get_file_reference() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(binary_data) # Act file_content = file_client.download_file().readall() # Assert self.assertEqual(file_content, binary_data) @record def test_get_file_no_content(self): # Arrange file_data = b'' file_name = self._get_file_reference() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(file_data) # Act file_output = file_client.download_file() # Assert self.assertEqual(file_data, file_output.readall()) self.assertEqual(0, file_output.properties.size) def test_get_file_to_bytes(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act file_content = file_client.download_file(max_concurrency=2).readall() # Assert self.assertEqual(self.byte_data, file_content) def test_get_file_to_bytes_with_progress(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act file_content = file_client.download_file(raw_response_hook=callback, max_concurrency=2).readall() # Assert self.assertEqual(self.byte_data, file_content) self.assert_download_progress(len(self.byte_data), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) @record def test_get_file_to_bytes_non_parallel(self): # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act file_content = file_client.download_file( raw_response_hook=callback).readall() # Assert self.assertEqual(self.byte_data, file_content) self.assert_download_progress(len(self.byte_data), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) @record def test_get_file_to_bytes_small(self): # Arrange file_data = self.get_random_bytes(1024) file_name = self._get_file_reference() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(file_data) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act file_content = file_client.download_file( raw_response_hook=callback).readall() # Assert self.assertEqual(file_data, file_content) self.assert_download_progress(len(file_data), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) def test_get_file_with_iter(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act with open(FILE_PATH, 'wb') as stream: for data in file_client.download_file().chunks(): stream.write(data) # Assert with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(self.byte_data, actual) def test_get_file_to_stream(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act with open(FILE_PATH, 'wb') as stream: bytes_read = file_client.download_file( max_concurrency=2).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(self.byte_data, actual) def test_get_file_to_stream_with_progress(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act with open(FILE_PATH, 'wb') as stream: bytes_read = file_client.download_file( raw_response_hook=callback, max_concurrency=2).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(self.byte_data, actual) self.assert_download_progress(len(self.byte_data), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) @record def test_get_file_to_stream_non_parallel(self): # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act with open(FILE_PATH, 'wb') as stream: bytes_read = file_client.download_file( raw_response_hook=callback, max_concurrency=1).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(self.byte_data, actual) self.assert_download_progress(len(self.byte_data), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) @record def test_get_file_to_stream_small(self): # Arrange file_data = self.get_random_bytes(1024) file_name = self._get_file_reference() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(file_data) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act with open(FILE_PATH, 'wb') as stream: bytes_read = file_client.download_file( raw_response_hook=callback, max_concurrency=1).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(file_data, actual) self.assert_download_progress(len(file_data), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) def test_get_file_to_stream_from_snapshot(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange # Create a snapshot of the share and delete the file share_client = self.fsc.get_share_client(self.share_name) share_snapshot = share_client.create_snapshot() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY) file_client.delete_file() snapshot_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, snapshot=share_snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act with open(FILE_PATH, 'wb') as stream: bytes_read = snapshot_client.download_file( max_concurrency=2).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(self.byte_data, actual) def test_get_file_to_stream_with_progress_from_snapshot(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange # Create a snapshot of the share and delete the file share_client = self.fsc.get_share_client(self.share_name) share_snapshot = share_client.create_snapshot() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY) file_client.delete_file() snapshot_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, snapshot=share_snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act with open(FILE_PATH, 'wb') as stream: bytes_read = snapshot_client.download_file( raw_response_hook=callback, max_concurrency=2).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(self.byte_data, actual) self.assert_download_progress(len(self.byte_data), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) @record def test_get_file_to_stream_non_parallel_from_snapshot(self): # Arrange # Create a snapshot of the share and delete the file share_client = self.fsc.get_share_client(self.share_name) share_snapshot = share_client.create_snapshot() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY) file_client.delete_file() snapshot_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, snapshot=share_snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act with open(FILE_PATH, 'wb') as stream: bytes_read = snapshot_client.download_file( raw_response_hook=callback, max_concurrency=1).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(self.byte_data, actual) self.assert_download_progress(len(self.byte_data), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) @record def test_get_file_to_stream_small_from_snapshot(self): # Arrange file_data = self.get_random_bytes(1024) file_name = self._get_file_reference() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) file_client.upload_file(file_data) # Create a snapshot of the share and delete the file share_client = self.fsc.get_share_client(self.share_name) share_snapshot = share_client.create_snapshot() file_client.delete_file() snapshot_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, snapshot=share_snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act with open(FILE_PATH, 'wb') as stream: bytes_read = snapshot_client.download_file( raw_response_hook=callback, max_concurrency=1).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(file_data, actual) self.assert_download_progress(len(file_data), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) def test_ranged_get_file_to_path(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act end_range = self.MAX_SINGLE_GET_SIZE + 1024 with open(FILE_PATH, 'wb') as stream: bytes_read = file_client.download_file( offset=1, length=end_range, max_concurrency=2).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(self.byte_data[1:end_range + 1], actual) def test_ranged_get_file_to_path_with_single_byte(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act end_range = self.MAX_SINGLE_GET_SIZE + 1024 with open(FILE_PATH, 'wb') as stream: bytes_read = file_client.download_file(offset=0, length=1).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(1, len(actual)) self.assertEqual(self.byte_data[0], actual[0]) @record def test_ranged_get_file_to_bytes_with_zero_byte(self): # Arrange file_data = b'' file_name = self._get_file_reference() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(file_data) # Act # the get request should fail in this case since the blob is empty and yet there is a range specified with self.assertRaises(HttpResponseError): file_client.download_file(offset=0, length=5).readall() with self.assertRaises(HttpResponseError): file_client.download_file(offset=3, length=5).readall() def test_ranged_get_file_to_path_with_progress(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act start_range = 3 end_range = self.MAX_SINGLE_GET_SIZE + 1024 with open(FILE_PATH, 'wb') as stream: length = end_range - start_range + 1 bytes_read = file_client.download_file( offset=start_range, length=length, raw_response_hook=callback, max_concurrency=2).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(self.byte_data[start_range:end_range + 1], actual) self.assert_download_progress(end_range - start_range + 1, self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) @record def test_ranged_get_file_to_path_small(self): # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act with open(FILE_PATH, 'wb') as stream: bytes_read = file_client.download_file( offset=1, length=4, max_concurrency=1).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(self.byte_data[1:5], actual) @record def test_ranged_get_file_to_path_non_parallel(self): # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act with open(FILE_PATH, 'wb') as stream: bytes_read = file_client.download_file( offset=1, length=3, max_concurrency=1).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(self.byte_data[1:4], actual) @record def test_ranged_get_file_to_path_invalid_range_parallel(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_size = self.MAX_SINGLE_GET_SIZE + 1 file_data = self.get_random_bytes(file_size) file_name = self._get_file_reference() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(file_data) # Act start = 3 end_range = 2 * self.MAX_SINGLE_GET_SIZE with open(FILE_PATH, 'wb') as stream: length = end_range - start + 1 bytes_read = file_client.download_file( offset=start, length=length, max_concurrency=2).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(file_data[start:file_size], actual) @record def test_ranged_get_file_to_path_invalid_range_non_parallel(self): # Arrange file_size = 1024 file_data = self.get_random_bytes(file_size) file_name = self._get_file_reference() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(file_data) # Act start = 3 end_range = 2 * self.MAX_SINGLE_GET_SIZE with open(FILE_PATH, 'wb') as stream: length = end_range - start + 1 bytes_read = file_client.download_file( offset=start, length=length, max_concurrency=1).readinto(stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(file_data[start:file_size], actual) def test_get_file_to_text(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange text_file = self.get_resource_name('textfile') text_data = self.get_random_text_data(self.MAX_SINGLE_GET_SIZE + 1) file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + text_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(text_data) # Act file_content = file_client.download_file(max_concurrency=2, encoding='utf-8').readall() # Assert self.assertEqual(text_data, file_content) def test_get_file_to_text_with_progress(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange text_file = self.get_resource_name('textfile') text_data = self.get_random_text_data(self.MAX_SINGLE_GET_SIZE + 1) file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + text_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(text_data) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act file_content = file_client.download_file(raw_response_hook=callback, max_concurrency=2, encoding='utf-8').readall() # Assert self.assertEqual(text_data, file_content) self.assert_download_progress(len(text_data.encode('utf-8')), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) @record def test_get_file_to_text_non_parallel(self): # Arrange text_file = self._get_file_reference() text_data = self.get_random_text_data(self.MAX_SINGLE_GET_SIZE + 1) file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + text_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(text_data) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act file_content = file_client.download_file(raw_response_hook=callback, max_concurrency=1, encoding='utf-8').readall() # Assert self.assertEqual(text_data, file_content) self.assert_download_progress(len(text_data), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) @record def test_get_file_to_text_small(self): # Arrange file_data = self.get_random_text_data(1024) file_name = self._get_file_reference() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(file_data) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act file_content = file_client.download_file(raw_response_hook=callback, encoding='utf-8').readall() # Assert self.assertEqual(file_data, file_content) self.assert_download_progress(len(file_data), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) @record def test_get_file_to_text_with_encoding(self): # Arrange text = u'hello 啊齄丂狛狜 world' data = text.encode('utf-16') file_name = self._get_file_reference() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(data) # Act file_content = file_client.download_file(encoding='UTF-16').readall() # Assert self.assertEqual(text, file_content) @record def test_get_file_to_text_with_encoding_and_progress(self): # Arrange text = u'hello 啊齄丂狛狜 world' data = text.encode('utf-16') file_name = self._get_file_reference() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(data) # Act progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) file_content = file_client.download_file(raw_response_hook=callback, encoding='UTF-16').readall() # Assert self.assertEqual(text, file_content) self.assert_download_progress(len(data), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) @record def test_get_file_non_seekable(self): # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act with open(FILE_PATH, 'wb') as stream: non_seekable_stream = StorageGetFileTest.NonSeekableFile(stream) bytes_read = file_client.download_file( max_concurrency=1).readinto(non_seekable_stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(self.byte_data, actual) def test_get_file_non_seekable_parallel(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act with open(FILE_PATH, 'wb') as stream: non_seekable_stream = StorageGetFileTest.NonSeekableFile(stream) with self.assertRaises(ValueError): file_client.download_file( max_concurrency=2).readinto(non_seekable_stream) # Assert @record def test_get_file_non_seekable_from_snapshot(self): # Arrange # Create a snapshot of the share and delete the file share_client = self.fsc.get_share_client(self.share_name) share_snapshot = share_client.create_snapshot() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY) file_client.delete_file() snapshot_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, snapshot=share_snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act with open(FILE_PATH, 'wb') as stream: non_seekable_stream = StorageGetFileTest.NonSeekableFile(stream) bytes_read = snapshot_client.download_file( max_concurrency=1).readinto(non_seekable_stream) # Assert self.assertIsInstance(bytes_read, int) with open(FILE_PATH, 'rb') as stream: actual = stream.read() self.assertEqual(self.byte_data, actual) def test_get_file_non_seekable_parallel_from_snapshot(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange # Create a snapshot of the share and delete the file share_client = self.fsc.get_share_client(self.share_name) share_snapshot = share_client.create_snapshot() file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY) file_client.delete_file() snapshot_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, snapshot=share_snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act with open(FILE_PATH, 'wb') as stream: non_seekable_stream = StorageGetFileTest.NonSeekableFile(stream) with self.assertRaises(ValueError): snapshot_client.download_file( max_concurrency=2).readinto(non_seekable_stream) @record def test_get_file_exact_get_size(self): # Arrange file_name = self._get_file_reference() byte_data = self.get_random_bytes(self.MAX_SINGLE_GET_SIZE) file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(byte_data) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act file_content = file_client.download_file(raw_response_hook=callback) # Assert self.assertEqual(byte_data, file_content.readall()) self.assert_download_progress(len(byte_data), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) def test_get_file_exact_chunk_size(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_name = self._get_file_reference() byte_data = self.get_random_bytes(self.MAX_SINGLE_GET_SIZE + self.MAX_CHUNK_GET_SIZE) file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_client.upload_file(byte_data) progress = [] def callback(response): current = response.context['download_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) # Act file_content = file_client.download_file(raw_response_hook=callback, max_concurrency=2) # Assert self.assertEqual(byte_data, file_content.readall()) self.assert_download_progress(len(byte_data), self.MAX_CHUNK_GET_SIZE, self.MAX_SINGLE_GET_SIZE, progress) def test_get_file_with_md5(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act file_content = file_client.download_file(validate_content=True) # Assert self.assertEqual(self.byte_data, file_content.readall()) def test_get_file_range_with_md5(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) file_content = file_client.download_file(offset=0, length=1024, validate_content=True) # Assert self.assertIsNone(file_content.properties.content_settings.content_md5) # Arrange props = file_client.get_file_properties() props.content_settings.content_md5 = b'MDAwMDAwMDA=' file_client.set_http_headers(props.content_settings) # Act file_content = file_client.download_file(offset=0, length=1024, validate_content=True) # Assert self.assertEqual(b'MDAwMDAwMDA=', file_content.properties.content_settings.content_md5) @record def test_get_file_server_encryption(self): #Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act file_content = file_client.download_file(offset=0, length=1024, validate_content=True) # Assert if self.is_file_encryption_enabled(): self.assertTrue(file_content.properties.server_encrypted) else: self.assertFalse(file_content.properties.server_encrypted) @record def test_get_file_properties_server_encryption(self): # Arrange file_client = ShareFileClient( self.get_file_url(), share_name=self.share_name, file_path=self.directory_name + '/' + self.byte_file, credential=self.settings.STORAGE_ACCOUNT_KEY, max_single_get_size=self.MAX_SINGLE_GET_SIZE, max_chunk_get_size=self.MAX_CHUNK_GET_SIZE) # Act props = file_client.get_file_properties() # Assert if self.is_file_encryption_enabled(): self.assertTrue(props.server_encrypted) else: self.assertFalse(props.server_encrypted)
class StorageDirectoryTest(StorageTestCase): def _setup(self, storage_account_name, storage_account_key): url = self.account_url(storage_account_name, "file") credential = storage_account_key self.fsc = ShareServiceClient(url, credential=credential) self.share_name = self.get_resource_name('utshare') if not self.is_playback(): try: 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 # --Helpers----------------------------------------------------------------- # --Test cases for directories ---------------------------------------------- @FileSharePreparer() def test_create_directories(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) # Act created = share_client.create_directory('dir1') # Assert self.assertTrue(created) @FileSharePreparer() def test_create_directories_with_metadata(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) metadata = {'hello': 'world', 'number': '42'} # Act directory = share_client.create_directory('dir1', metadata=metadata) # Assert md = directory.get_directory_properties().metadata self.assertDictEqual(md, metadata) @FileSharePreparer() def test_create_directories_fail_on_exist(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) # Act created = share_client.create_directory('dir1') with self.assertRaises(ResourceExistsError): share_client.create_directory('dir1') # Assert self.assertTrue(created) @FileSharePreparer() def test_create_subdirectories(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') # Act created = directory.create_subdirectory('dir2') # Assert self.assertTrue(created) self.assertEqual(created.directory_path, 'dir1/dir2') @FileSharePreparer() def test_create_subdirectories_with_metadata(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') metadata = {'hello': 'world', 'number': '42'} # Act created = directory.create_subdirectory('dir2', metadata=metadata) # Assert self.assertTrue(created) self.assertEqual(created.directory_path, 'dir1/dir2') sub_metadata = created.get_directory_properties().metadata self.assertEqual(sub_metadata, metadata) @FileSharePreparer() def test_create_file_in_directory(self, storage_account_name, storage_account_key): self._setup(storage_account_name, 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 = share_client.create_directory('dir1') # Act new_file = directory.upload_file(file_name, file_data) # Assert file_content = new_file.download_file().readall() self.assertEqual(file_content, file_data) @FileSharePreparer() def test_delete_file_in_directory(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) file_name = self.get_resource_name('file') share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') new_file = directory.upload_file(file_name, "hello world") # Act deleted = directory.delete_file(file_name) # Assert self.assertIsNone(deleted) with self.assertRaises(ResourceNotFoundError): new_file.get_file_properties() @FileSharePreparer() def test_share_directory_exists(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') directory2 = share_client.get_directory_client("dir2") self.assertTrue(directory.exists()) self.assertFalse(directory2.exists()) @FileSharePreparer() def test_delete_subdirectories(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') directory.create_subdirectory('dir2') # Act deleted = directory.delete_subdirectory('dir2') # Assert self.assertIsNone(deleted) subdir = directory.get_subdirectory_client('dir2') with self.assertRaises(ResourceNotFoundError): subdir.get_directory_properties() @FileSharePreparer() def test_get_directory_properties(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') # Act props = directory.get_directory_properties() # Assert self.assertIsNotNone(props) self.assertIsNotNone(props.etag) self.assertIsNotNone(props.last_modified) @FileSharePreparer() def test_get_directory_properties_with_snapshot(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) metadata = {"test1": "foo", "test2": "bar"} directory = share_client.create_directory('dir1', metadata=metadata) snapshot1 = share_client.create_snapshot() metadata2 = {"test100": "foo100", "test200": "bar200"} 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 = snap_dir.get_directory_properties() # Assert self.assertIsNotNone(props) self.assertIsNotNone(props.etag) self.assertIsNotNone(props.last_modified) self.assertDictEqual(metadata, props.metadata) @FileSharePreparer() def test_get_directory_metadata_with_snapshot(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) metadata = {"test1": "foo", "test2": "bar"} directory = share_client.create_directory('dir1', metadata=metadata) snapshot1 = share_client.create_snapshot() metadata2 = {"test100": "foo100", "test200": "bar200"} 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_metadata = snap_dir.get_directory_properties().metadata # Assert self.assertIsNotNone(snapshot_metadata) self.assertDictEqual(metadata, snapshot_metadata) @FileSharePreparer() def test_get_directory_properties_with_non_existing_directory( self, storage_account_name, storage_account_key): self._setup(storage_account_name, 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): directory.get_directory_properties() # Assert @FileSharePreparer() def test_directory_exists(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') # Act exists = directory.get_directory_properties() # Assert self.assertTrue(exists) @FileSharePreparer() def test_directory_not_exists(self, storage_account_name, storage_account_key): self._setup(storage_account_name, 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): directory.get_directory_properties() # Assert @FileSharePreparer() def test_directory_parent_not_exists(self, storage_account_name, storage_account_key): self._setup(storage_account_name, 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: directory.get_directory_properties() # Assert self.assertEqual(e.exception.error_code, StorageErrorCode.parent_not_found) @FileSharePreparer() def test_directory_exists_with_snapshot(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') snapshot = share_client.create_snapshot() 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 = snap_dir.get_directory_properties() # Assert self.assertTrue(exists) @FileSharePreparer() def test_directory_not_exists_with_snapshot(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) snapshot = share_client.create_snapshot() directory = 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): snap_dir.get_directory_properties() # Assert @FileSharePreparer() def test_get_set_directory_metadata(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') metadata = {'hello': 'world', 'number': '43'} # Act directory.set_directory_metadata(metadata) md = directory.get_directory_properties().metadata # Assert self.assertDictEqual(md, metadata) @FileSharePreparer() def test_set_directory_properties_with_empty_smb_properties( self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory_client = share_client.create_directory('dir1') directory_properties_on_creation = directory_client.get_directory_properties( ) # Act directory_client.set_http_headers() directory_properties = 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) @FileSharePreparer() def test_set_directory_properties_with_file_permission_key( self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory_client = share_client.create_directory('dir1') directory_properties_on_creation = 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 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 = 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) @FileSharePreparer() def test_list_subdirectories_and_files(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') 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 = list(directory.list_directories_and_files()) # Assert self.assertEqual(len(list_dir), 6) self.assertEqual(list_dir[0]['name'], 'subdir1') self.assertEqual(list_dir[0]['is_directory'], True) self.assertEqual(list_dir[1]['name'], 'subdir2') self.assertEqual(list_dir[1]['is_directory'], True) self.assertEqual(list_dir[2]['name'], 'subdir3') self.assertEqual(list_dir[2]['is_directory'], True) self.assertEqual(list_dir[3]['name'], 'file1') self.assertEqual(list_dir[3]['is_directory'], False) self.assertEqual(list_dir[4]['name'], 'file2') self.assertEqual(list_dir[4]['is_directory'], False) self.assertEqual(list_dir[5]['name'], 'file3') self.assertEqual(list_dir[5]['is_directory'], False) @FileSharePreparer() def test_list_subdirectories_and_files_include_other_data( self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') 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 = list( directory.list_directories_and_files( include=["timestamps", "Etag", "Attributes", "PermissionKey"])) self.assertEqual(len(list_dir), 6) self.assertIsNotNone(list_dir[0].etag) self.assertIsNotNone(list_dir[1].file_attributes) self.assertIsNotNone(list_dir[1].last_access_time) self.assertIsNotNone(list_dir[1].last_write_time) self.assertIsNotNone(list_dir[2].change_time) self.assertIsNotNone(list_dir[2].creation_time) self.assertIsNotNone(list_dir[2].file_id) try: share_client.delete_share() except: pass @FileSharePreparer() def test_list_subdirectories_and_files_include_extended_info( self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') directory.create_subdirectory("subdir1") list_dir = list( directory.list_directories_and_files(include_extended_info=True)) self.assertEqual(len(list_dir), 1) self.assertIsNotNone(list_dir[0].file_id) self.assertIsNone(list_dir[0].file_attributes) self.assertIsNone(list_dir[0].last_access_time) @FileSharePreparer() def test_list_subdirectories_and_files_with_prefix(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') 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 = list( directory.list_directories_and_files(name_starts_with="sub")) # Assert self.assertEqual(len(list_dir), 3) self.assertEqual(list_dir[0]['name'], 'subdir1') self.assertEqual(list_dir[0]['is_directory'], True) self.assertEqual(list_dir[1]['name'], 'subdir2') self.assertEqual(list_dir[1]['is_directory'], True) self.assertEqual(list_dir[2]['name'], 'subdir3') self.assertEqual(list_dir[2]['is_directory'], True) @FileSharePreparer() def test_list_subdirectories_and_files_with_snapshot( self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') directory.create_subdirectory("subdir1") directory.create_subdirectory("subdir2") directory.upload_file("file1", "data1") snapshot = share_client.create_snapshot() 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 = list(snapshot_dir.list_directories_and_files()) # Assert self.assertEqual(len(list_dir), 3) self.assertEqual(list_dir[0]['name'], 'subdir1') self.assertEqual(list_dir[0]['is_directory'], True) self.assertEqual(list_dir[1]['name'], 'subdir2') self.assertEqual(list_dir[1]['is_directory'], True) self.assertEqual(list_dir[2]['name'], 'file1') self.assertEqual(list_dir[2]['is_directory'], False) self.assertEqual(list_dir[2]['size'], 5) @FileSharePreparer() def test_list_nested_subdirectories_and_files(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') subdir = directory.create_subdirectory("subdir1") subdir.create_subdirectory("subdir2") subdir.create_subdirectory("subdir3") directory.upload_file("file1", "data1") subdir.upload_file("file2", "data2") subdir.upload_file("file3", "data3") # Act list_dir = list(directory.list_directories_and_files()) # Assert self.assertEqual(len(list_dir), 2) self.assertEqual(list_dir[0]['name'], 'subdir1') self.assertEqual(list_dir[0]['is_directory'], True) self.assertEqual(list_dir[1]['name'], 'file1') self.assertEqual(list_dir[1]['is_directory'], False) self.assertEqual(list_dir[1]['size'], 5) @FileSharePreparer() def test_delete_directory_with_existing_share(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') # Act deleted = directory.delete_directory() # Assert self.assertIsNone(deleted) with self.assertRaises(ResourceNotFoundError): directory.get_directory_properties() @FileSharePreparer() def test_delete_directory_with_non_existing_directory( self, storage_account_name, storage_account_key): self._setup(storage_account_name, 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): directory.delete_directory() # Assert @FileSharePreparer() def test_get_directory_properties_server_encryption( self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) directory = share_client.create_directory('dir1') # Act props = directory.get_directory_properties() # Assert self.assertIsNotNone(props) self.assertIsNotNone(props.etag) self.assertIsNotNone(props.last_modified) self.assertTrue(props.server_encrypted) @FileSharePreparer() def test_rename_directory(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) source_directory = share_client.create_directory('dir1') # Act new_directory = source_directory.rename_directory('dir2') # Assert props = new_directory.get_directory_properties() self.assertIsNotNone(props) @FileSharePreparer() def test_rename_directory_different_directory(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) parent_source_directory = share_client.create_directory('dir1') source_directory = parent_source_directory.create_subdirectory('sub1') dest_parent_directory = share_client.create_directory('dir2') # Act new_directory_path = dest_parent_directory.directory_path + '/sub2' new_directory = source_directory.rename_directory(new_directory_path) # Assert props = new_directory.get_directory_properties() self.assertIsNotNone(props) @FileSharePreparer() def test_rename_directory_ignore_readonly(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) source_directory = share_client.create_directory('dir1') dest_directory = share_client.create_directory('dir2') dest_file = dest_directory.get_file_client('test') file_attributes = NTFSAttributes(read_only=True) dest_file.create_file(1024, file_attributes=file_attributes) # Act new_directory = source_directory.rename_directory( dest_directory.directory_path + '/' + dest_file.file_name, overwrite=True, ignore_read_only=True) # Assert props = new_directory.get_directory_properties() self.assertIsNotNone(props) self.assertTrue(props.is_directory) @FileSharePreparer() def test_rename_directory_file_permission(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) file_permission_key = share_client.create_permission_for_share( TEST_FILE_PERMISSIONS) source_directory = share_client.create_directory('dir1') # Act new_directory = source_directory.rename_directory( 'dir2', file_permission=TEST_FILE_PERMISSIONS) # Assert props = new_directory.get_directory_properties() self.assertIsNotNone(props) self.assertEqual(file_permission_key, props.permission_key) @FileSharePreparer() def test_rename_directory_preserve_permission(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) source_directory = share_client.create_directory( 'dir1', file_permission=TEST_FILE_PERMISSIONS) source_props = source_directory.get_directory_properties() source_permission_key = source_props.permission_key # Act new_directory = source_directory.rename_directory( 'dir2', file_permission='preserve') # Assert props = new_directory.get_directory_properties() self.assertIsNotNone(props) self.assertEqual(source_permission_key, props.permission_key) @FileSharePreparer() def test_rename_directory_smb_properties(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) source_directory = share_client.create_directory('dir1') file_attributes = NTFSAttributes(read_only=True, directory=True) file_creation_time = datetime(2022, 1, 26, 10, 9, 30, 500000) file_last_write_time = datetime(2022, 1, 26, 10, 14, 30, 500000) # Act new_directory = source_directory.rename_directory( 'dir2', file_attributes=file_attributes, file_creation_time=file_creation_time, file_last_write_time=file_last_write_time) # Assert props = new_directory.get_directory_properties() self.assertIsNotNone(props) self.assertTrue(props.is_directory) self.assertEqual(str(file_attributes), props.file_attributes.replace(' ', '')) self.assertEqual(file_creation_time, props.creation_time) self.assertEqual(file_last_write_time, props.last_write_time) @FileSharePreparer() def test_rename_directory_dest_lease(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) source_directory = share_client.create_directory('dir1') dest_directory = share_client.create_directory('dir2') dest_file = dest_directory.upload_file('test', b'Hello World') lease = dest_file.acquire_lease() # Act new_directory = source_directory.rename_directory( dest_directory.directory_path + '/' + dest_file.file_name, overwrite=True, lease=lease) # Assert props = new_directory.get_directory_properties() self.assertIsNotNone(props) self.assertTrue(props.is_directory) @pytest.mark.live_test_only @FileSharePreparer() def test_rename_directory_share_sas(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self.fsc.get_share_client(self.share_name) token = generate_share_sas(share_client.account_name, share_client.share_name, share_client.credential.account_key, expiry=datetime.utcnow() + timedelta(hours=1), permission=ShareSasPermissions(read=True, write=True)) source_directory = ShareDirectoryClient(self.account_url( storage_account_name, 'file'), share_client.share_name, 'dir1', credential=token) source_directory.create_directory() # Act new_directory = source_directory.rename_directory('dir2' + '?' + token) # Assert props = new_directory.get_directory_properties() self.assertIsNotNone(props)