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)
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)
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 StorageShareTest(StorageTestCase): def _setup(self, storage_account_name, storage_account_key): file_url = self.account_url(storage_account_name, "file") credentials = storage_account_key self.fsc = ShareServiceClient(account_url=file_url, credential=credentials) self.test_shares = [] def _teardown(self, FILE_PATH): if os.path.isfile(FILE_PATH): try: os.remove(FILE_PATH) except: pass # --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_name) return share def _create_share(self, prefix=TEST_SHARE_PREFIX, **kwargs): share_client = self._get_share_reference(prefix) try: share_client.create_share(**kwargs) except: pass return share_client def _delete_shares(self, prefix=TEST_SHARE_PREFIX): for l in self.fsc.list_shares(include_snapshots=True): try: self.fsc.delete_share(l.name, delete_snapshots=True) except: pass # --Test cases for shares ----------------------------------------- def test_create_share_client(self): share_client = ShareClient.from_share_url( "http://127.0.0.1:11002/account/customized/path/share?snapshot=baz&", credential={ "account_name": "myaccount", "account_key": "key" }) self.assertEqual(share_client.share_name, "share") self.assertEqual(share_client.snapshot, "baz") share_client = ShareClient.from_share_url( "http://127.0.0.1:11002/account/share?snapshot=baz&", credential="credential") self.assertEqual(share_client.share_name, "share") self.assertEqual(share_client.snapshot, "baz") @FileSharePreparer() def test_create_share(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() # Act created = self._create_share() # Assert self.assertTrue(created) self._delete_shares(share.share_name) @FileSharePreparer() def test_create_share_snapshot(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() # Act created = share.create_share() snapshot = share.create_snapshot() # Assert self.assertTrue(created) self.assertIsNotNone(snapshot['snapshot']) self.assertIsNotNone(snapshot['etag']) self.assertIsNotNone(snapshot['last_modified']) self._delete_shares(share.share_name) @FileSharePreparer() def test_create_snapshot_with_metadata(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() metadata = {"test1": "foo", "test2": "bar"} metadata2 = {"test100": "foo100", "test200": "bar200"} # Act created = share.create_share(metadata=metadata) snapshot = share.create_snapshot(metadata=metadata2) share_props = share.get_share_properties() snapshot_client = ShareClient(self.account_url(storage_account_name, "file"), share_name=share.share_name, snapshot=snapshot, credential=storage_account_key) snapshot_props = 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) self._delete_shares(share.share_name) @FileSharePreparer() def test_delete_share_with_snapshots(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() share.create_share() snapshot = share.create_snapshot() # Act with self.assertRaises(HttpResponseError): share.delete_share() deleted = share.delete_share(delete_snapshots=True) self.assertIsNone(deleted) self._delete_shares() @pytest.mark.playback_test_only @FileSharePreparer() def test_undelete_share(self, storage_account_name, storage_account_key): # share soft delete should enabled by SRP call or use armclient, so make this test as playback only. self._setup(storage_account_name, storage_account_key) share_client = self._create_share(prefix="sharerestore") # Act share_client.delete_share() # to make sure the share deleted with self.assertRaises(ResourceNotFoundError): share_client.get_share_properties() share_list = list( self.fsc.list_shares(include_deleted=True, include_snapshots=True, include_metadata=True)) self.assertTrue(len(share_list) >= 1) for share in share_list: # find the deleted share and restore it if share.deleted and share.name == share_client.share_name: if self.is_live: time.sleep(60) restored_share_client = self.fsc.undelete_share( share.name, share.version) # to make sure the deleted share is restored props = restored_share_client.get_share_properties() self.assertIsNotNone(props) @FileSharePreparer() def test_lease_share_acquire_and_release(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self._create_share('test') # Act lease = share_client.acquire_lease() lease.release() # Assert @FileSharePreparer() def test_acquire_lease_on_sharesnapshot(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference("testshare1") # Act share.create_share() snapshot = share.create_snapshot() snapshot_client = ShareClient(self.account_url(storage_account_name, "file"), share_name=share.share_name, snapshot=snapshot, credential=storage_account_key) share_lease = share.acquire_lease() share_snapshot_lease = snapshot_client.acquire_lease() # Assert with self.assertRaises(HttpResponseError): share.get_share_properties(lease=share_snapshot_lease) with self.assertRaises(HttpResponseError): snapshot_client.get_share_properties(lease=share_lease) self.assertIsNotNone(snapshot['snapshot']) self.assertIsNotNone(snapshot['etag']) self.assertIsNotNone(snapshot['last_modified']) self.assertIsNotNone(share_lease) self.assertIsNotNone(share_snapshot_lease) self.assertNotEqual(share_lease, share_snapshot_lease) share_snapshot_lease.release() share_lease.release() self._delete_shares(share.share_name) @FileSharePreparer() def test_lease_share_renew(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self._create_share('test') lease = share_client.acquire_lease(lease_duration=15) self.sleep(10) lease_id_start = lease.id # Act lease.renew() # Assert self.assertEqual(lease.id, lease_id_start) self.sleep(5) with self.assertRaises(HttpResponseError): share_client.delete_share() self.sleep(10) share_client.delete_share() @FileSharePreparer() def test_lease_share_with_duration(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self._create_share('test') # Act lease = share_client.acquire_lease(lease_duration=15) # Assert with self.assertRaises(HttpResponseError): share_client.acquire_lease() self.sleep(15) share_client.acquire_lease() @FileSharePreparer() def test_lease_share_twice(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self._create_share('test') # Act lease = share_client.acquire_lease(lease_duration=15) # Assert lease2 = share_client.acquire_lease(lease_id=lease.id) self.assertEqual(lease.id, lease2.id) @FileSharePreparer() def test_lease_share_with_proposed_lease_id(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self._create_share('test') # Act proposed_lease_id = '55e97f64-73e8-4390-838d-d9e84a374321' lease = share_client.acquire_lease(lease_id=proposed_lease_id) # Assert self.assertEqual(proposed_lease_id, lease.id) @FileSharePreparer() def test_lease_share_change_lease_id(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self._create_share('test') # Act lease_id = '29e0b239-ecda-4f69-bfa3-95f6af91464c' lease = share_client.acquire_lease() lease_id1 = lease.id lease.change(proposed_lease_id=lease_id) lease.renew() lease_id2 = lease.id # Assert self.assertIsNotNone(lease_id1) self.assertIsNotNone(lease_id2) self.assertNotEqual(lease_id1, lease_id) self.assertEqual(lease_id2, lease_id) @FileSharePreparer() def test_set_share_metadata_with_lease_id(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self._create_share('test') metadata = {'hello': 'world', 'number': '43'} lease_id = share_client.acquire_lease() # Act share_client.set_share_metadata(metadata, lease=lease_id) # Assert md = share_client.get_share_properties().metadata self.assertDictEqual(md, metadata) @FileSharePreparer() def test_get_share_metadata_with_lease_id(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self._create_share('test') metadata = {'hello': 'world', 'number': '43'} share_client.set_share_metadata(metadata) lease_id = share_client.acquire_lease() # Act md = share_client.get_share_properties(lease=lease_id).metadata # Assert self.assertDictEqual(md, metadata) @FileSharePreparer() def test_get_share_properties_with_lease_id(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self._create_share('test') metadata = {'hello': 'world', 'number': '43'} share_client.set_share_metadata(metadata) lease_id = share_client.acquire_lease() # Act props = share_client.get_share_properties(lease=lease_id) lease_id.break_lease() # Assert self.assertIsNotNone(props) self.assertDictEqual(props.metadata, metadata) self.assertEqual(props.lease.duration, 'infinite') self.assertEqual(props.lease.state, 'leased') self.assertEqual(props.lease.status, 'locked') @FileSharePreparer() def test_get_share_acl_with_lease_id(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self._create_share('test') lease_id = share_client.acquire_lease() # Act acl = share_client.get_share_access_policy(lease=lease_id) # Assert self.assertIsNotNone(acl) self.assertIsNone(acl.get('public_access')) @FileSharePreparer() def test_set_share_acl_with_lease_id(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self._create_share('test') lease_id = share_client.acquire_lease() # Act access_policy = AccessPolicy(permission=ShareSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow()) signed_identifiers = {'testid': access_policy} share_client.set_share_access_policy(signed_identifiers, lease=lease_id) # Assert acl = share_client.get_share_access_policy() self.assertIsNotNone(acl) self.assertIsNone(acl.get('public_access')) @FileSharePreparer() def test_lease_share_break_period(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self._create_share('test') # Act lease = share_client.acquire_lease(lease_duration=15) # Assert lease.break_lease(lease_break_period=5) self.sleep(6) with self.assertRaises(HttpResponseError): share_client.delete_share(lease=lease) @FileSharePreparer() def test_delete_share_with_lease_id(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_client = self._create_share('test') lease = share_client.acquire_lease(lease_duration=15) # Assert with self.assertRaises(HttpResponseError): share_client.delete_share() # Act deleted = share_client.delete_share(lease=lease) # Assert self.assertIsNone(deleted) with self.assertRaises(ResourceNotFoundError): share_client.get_share_properties() @pytest.mark.playback_test_only @FileSharePreparer() def test_restore_to_existing_share(self, storage_account_name, storage_account_key): # share soft delete should enabled by SRP call or use armclient, so make this test as playback only. self._setup(storage_account_name, storage_account_key) # Act share_client = self._create_share() share_client.delete_share() # to make sure the share deleted with self.assertRaises(ResourceNotFoundError): share_client.get_share_properties() # create a share with the same name as the deleted one if self.is_live: time.sleep(30) share_client.create_share() share_list = list(self.fsc.list_shares(include_deleted=True)) self.assertTrue(len(share_list) >= 1) for share in share_list: # find the deleted share and restore it if share.deleted and share.name == share_client.share_name: with self.assertRaises(HttpResponseError): self.fsc.undelete_share(share.name, share.version) @FileSharePreparer() def test_delete_snapshot(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() share.create_share() snapshot = share.create_snapshot() # Act with self.assertRaises(HttpResponseError): share.delete_share() snapshot_client = ShareClient(self.account_url(storage_account_name, "file"), share_name=share.share_name, snapshot=snapshot, credential=storage_account_key) deleted = snapshot_client.delete_share() self.assertIsNone(deleted) self._delete_shares() @FileSharePreparer() def test_create_share_fail_on_exist(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() # Act created = share.create_share() # Assert self.assertTrue(created) self._delete_shares() @FileSharePreparer() def test_create_share_with_already_existing_share_fail_on_exist( self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() # Act created = share.create_share() with self.assertRaises(HttpResponseError): share.create_share() # Assert self.assertTrue(created) self._delete_shares() @FileSharePreparer() def test_create_share_with_metadata(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) metadata = {'hello': 'world', 'number': '42'} # Act client = self._get_share_reference() created = client.create_share(metadata=metadata) # Assert self.assertTrue(created) md = client.get_share_properties().metadata self.assertDictEqual(md, metadata) self._delete_shares() @FileSharePreparer() def test_create_share_with_quota(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) # Act client = self._get_share_reference() created = client.create_share(quota=1) # Assert props = client.get_share_properties() self.assertTrue(created) self.assertEqual(props.quota, 1) self._delete_shares() @FileSharePreparer() def test_create_share_with_access_tier(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) # Act client = self._get_share_reference() created = client.create_share(access_tier="Hot") # Assert props = client.get_share_properties() self.assertTrue(created) self.assertEqual(props.access_tier, "Hot") self._delete_shares() @FileSharePreparer() def test_share_exists(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._create_share() # Act exists = share.get_share_properties() # Assert self.assertTrue(exists) self._delete_shares() @FileSharePreparer() def test_share_not_exists(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() # Act with self.assertRaises(ResourceNotFoundError): share.get_share_properties() # Assert self._delete_shares() @FileSharePreparer() def test_share_snapshot_exists(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._create_share() snapshot = share.create_snapshot() # Act snapshot_client = self.fsc.get_share_client(share.share_name, snapshot=snapshot) exists = snapshot_client.get_share_properties() # Assert self.assertTrue(exists) self._delete_shares() @FileSharePreparer() def test_share_snapshot_not_exists(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = 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): snapshot_client.get_share_properties() # Assert self._delete_shares() @FileSharePreparer() def test_unicode_create_share_unicode_name(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_name = u'啊齄丂狛狜' # Act with self.assertRaises(HttpResponseError): # not supported - share name must be alphanumeric, lowercase client = self.fsc.get_share_client(share_name) client.create_share() # Assert self._delete_shares() @FileSharePreparer() def test_list_shares_no_options(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._create_share() # Act shares = list(self.fsc.list_shares()) # Assert self.assertIsNotNone(shares) self.assertGreaterEqual(len(shares), 1) self.assertIsNotNone(shares[0]) self.assertNamedItemInContainer(shares, share.share_name) self._delete_shares() @FileSharePreparer() def test_list_shares_no_options_for_premium_account( self, premium_storage_file_account_name, premium_storage_file_account_key): self._setup(premium_storage_file_account_name, premium_storage_file_account_key) share = self._create_share() # Act shares = list(self.fsc.list_shares()) # Assert self.assertIsNotNone(shares) self.assertGreaterEqual(len(shares), 1) self.assertIsNotNone(shares[0]) self.assertIsNotNone(shares[0].provisioned_iops) self.assertIsNotNone(shares[0].provisioned_ingress_mbps) self.assertIsNotNone(shares[0].provisioned_egress_mbps) self.assertIsNotNone(shares[0].next_allowed_quota_downgrade_time) self._delete_shares() @FileSharePreparer() def test_list_shares_leased_share(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._create_share("test1") # Act lease = share.acquire_lease() resp = list(self.fsc.list_shares()) # Assert self.assertIsNotNone(resp) self.assertGreaterEqual(len(resp), 1) self.assertIsNotNone(resp[0]) self.assertEqual(resp[0].lease.duration, 'infinite') self.assertEqual(resp[0].lease.status, 'locked') self.assertEqual(resp[0].lease.state, 'leased') lease.release() self._delete_shares() @pytest.mark.playback_test_only @FileSharePreparer() def test_list_shares_with_snapshot(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) #share = self._get_share_reference() share = self._create_share('random') snapshot1 = share.create_snapshot() snapshot2 = share.create_snapshot() # Act shares = self.fsc.list_shares(include_snapshots=True) # Assert self.assertIsNotNone(shares) all_shares = list(shares) 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']) share.delete_share(delete_snapshots=True) self._delete_shares() @pytest.mark.playback_test_only @FileSharePreparer() def test_list_shares_with_prefix(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() share.create_share() # Act shares = list(self.fsc.list_shares(name_starts_with=share.share_name)) # Assert self.assertEqual(len(shares), 1) self.assertIsNotNone(shares[0]) self.assertEqual(shares[0].name, share.share_name) self.assertIsNone(shares[0].metadata) self._delete_shares() @FileSharePreparer() def test_list_shares_with_include_metadata(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) metadata = {'hello': 'world', 'number': '42'} share = self._get_share_reference() share.create_share(metadata=metadata) # Act shares = list( self.fsc.list_shares(share.share_name, include_metadata=True)) # 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) self._delete_shares() @FileSharePreparer() def test_list_shares_with_num_results_and_marker(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) prefix = 'listshare' share_names = [] for i in range(0, 4): share_names.append(self._create_share(prefix + str(i)).share_name) #share_names.sort() # Act generator1 = self.fsc.list_shares(prefix, results_per_page=2).by_page() shares1 = list(next(generator1)) generator2 = self.fsc.list_shares(prefix, results_per_page=2).by_page( continuation_token=generator1.continuation_token) shares2 = list(next(generator2)) # 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]) self._delete_shares() @FileSharePreparer() def test_set_share_metadata(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._create_share() metadata = {'hello': 'world', 'number': '42'} # Act share.set_share_metadata(metadata) # Assert md = share.get_share_properties().metadata self.assertDictEqual(md, metadata) self._delete_shares() @FileSharePreparer() def test_get_share_metadata(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) metadata = {'hello': 'world', 'number': '42'} # Act client = self._get_share_reference() created = client.create_share(metadata=metadata) # Assert self.assertTrue(created) md = client.get_share_properties().metadata self.assertDictEqual(md, metadata) self._delete_shares() @FileSharePreparer() def test_get_share_metadata_with_snapshot(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) metadata = {'hello': 'world', 'number': '42'} # Act client = self._get_share_reference() created = client.create_share(metadata=metadata) snapshot = client.create_snapshot() snapshot_client = self.fsc.get_share_client(client.share_name, snapshot=snapshot) # Assert self.assertTrue(created) md = snapshot_client.get_share_properties().metadata self.assertDictEqual(md, metadata) self._delete_shares() @FileSharePreparer() def test_set_share_properties(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share1 = self._create_share("share1") share2 = self._create_share("share2") share1.set_share_quota(3) share1.set_share_properties(access_tier="Hot") share2.set_share_properties(access_tier=ShareAccessTier("Cool"), quota=2) # Act props1 = share1.get_share_properties() props2 = share2.get_share_properties() share1_quota = props1.quota share1_tier = props1.access_tier share2_quota = props2.quota share2_tier = props2.access_tier # Assert self.assertEqual(share1_quota, 3) self.assertEqual(share1_tier, "Hot") self.assertEqual(share2_quota, 2) self.assertEqual(share2_tier, "Cool") self._delete_shares() @pytest.mark.playback_test_only @FileSharePreparer() def test_create_share_with_protocol(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) # Act share_client = self._get_share_reference("testshare2") with self.assertRaises(ValueError): share_client.create_share(protocols="SMB", root_squash=ShareRootSquash.all_squash) share_client.create_share(protocols="NFS", root_squash=ShareRootSquash.root_squash) share_enabled_protocol = share_client.get_share_properties().protocols share_root_squash = share_client.get_share_properties().root_squash # Assert self.assertEqual(share_enabled_protocol, ["NFS"]) self.assertEqual(share_root_squash, ShareRootSquash.root_squash) share_client.delete_share() @pytest.mark.playback_test_only @FileSharePreparer() def test_set_share_properties_with_root_squash(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share1 = self._create_share("share1", protocols=ShareProtocols.NFS) share2 = self._create_share("share2", protocols=ShareProtocols.NFS) share1.set_share_properties(root_squash="NoRootSquash") share2.set_share_properties(root_squash=ShareRootSquash.root_squash) # Act share1_props = share1.get_share_properties() share2_props = share2.get_share_properties() # # Assert self.assertEqual(share1_props.root_squash, ShareRootSquash.no_root_squash) self.assertEqual(share1_props.protocols, ['NFS']) self.assertEqual(share2_props.root_squash, ShareRootSquash.root_squash) self.assertEqual(share2_props.protocols, ['NFS']) @pytest.mark.playback_test_only @FileSharePreparer() def test_list_shares_with_root_squash_and_protocols( self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) self._create_share(prefix="testshare1", protocols="NFS", root_squash=ShareRootSquash.all_squash) self._create_share(prefix="testshare2", protocols=ShareProtocols.SMB) # Act shares = list(self.fsc.list_shares()) share1_props = shares[0] share2_props = shares[1] # Assert self.assertIsNotNone(shares) self.assertGreaterEqual(len(shares), 2) self.assertEqual(share1_props.root_squash, ShareRootSquash.all_squash) self.assertEqual(share1_props.protocols, ["NFS"]) self.assertEqual(share2_props.root_squash, None) self.assertEqual(share2_props.protocols, ["SMB"]) self._delete_shares() @FileSharePreparer() def test_get_share_properties_for_premium_account( self, premium_storage_file_account_name, premium_storage_file_account_key): self._setup(premium_storage_file_account_name, premium_storage_file_account_key) share = self._create_share() # Act props = share.get_share_properties() # Assert self.assertIsNotNone(props) self.assertIsNotNone(props.quota) self.assertIsNotNone(props.provisioned_iops) self.assertIsNotNone(props.provisioned_ingress_mbps) self.assertIsNotNone(props.provisioned_egress_mbps) self.assertIsNotNone(props.next_allowed_quota_downgrade_time) self._delete_shares() @FileSharePreparer() def test_delete_share_with_existing_share(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() share.create_share() # Act deleted = share.delete_share() # Assert self.assertIsNone(deleted) self._delete_shares() @FileSharePreparer() def test_delete_share_with_existing_share_fail_not_exist( self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) client = self._get_share_reference() # Act with LogCaptured(self) as log_captured: with self.assertRaises(HttpResponseError): client.delete_share() log_as_str = log_captured.getvalue() self._delete_shares() @FileSharePreparer() def test_delete_share_with_non_existing_share(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) client = self._get_share_reference() # Act with LogCaptured(self) as log_captured: with self.assertRaises(HttpResponseError): deleted = client.delete_share() log_as_str = log_captured.getvalue() self.assertTrue('ERROR' not in log_as_str) self._delete_shares() @FileSharePreparer() def test_delete_share_with_non_existing_share_fail_not_exist( self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) client = self._get_share_reference() # Act with LogCaptured(self) as log_captured: with self.assertRaises(HttpResponseError): client.delete_share() log_as_str = log_captured.getvalue() self._delete_shares() @FileSharePreparer() def test_get_share_stats(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() share.create_share() # Act share_usage = share.get_share_stats() # Assert self.assertEqual(share_usage, 0) self._delete_shares() @FileSharePreparer() def test_set_share_acl(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() share.create_share() # Act resp = share.set_share_access_policy(signed_identifiers=dict()) # Assert acl = share.get_share_access_policy() self.assertIsNotNone(acl) self._delete_shares() @FileSharePreparer() def test_set_share_acl_with_empty_signed_identifiers( self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() share.create_share() # Act resp = share.set_share_access_policy(dict()) # Assert acl = share.get_share_access_policy() self.assertIsNotNone(acl) self.assertEqual(len(acl.get('signed_identifiers')), 0) self._delete_shares() @FileSharePreparer() def test_set_share_acl_with_signed_identifiers(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() 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 = share.set_share_access_policy(identifiers) # Assert acl = share.get_share_access_policy() self.assertIsNotNone(acl) self.assertEqual(len(acl['signed_identifiers']), 1) self.assertEqual(acl['signed_identifiers'][0].id, 'testid') self._delete_shares() @FileSharePreparer() def test_set_share_acl_too_many_ids(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() share.create_share() # Act identifiers = dict() for i in range(0, 6): identifiers['id{}'.format(i)] = AccessPolicy() # Assert with self.assertRaises(ValueError) as e: 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.' ) self._delete_shares() @FileSharePreparer() def test_list_directories_and_files(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._create_share() dir0 = share.get_directory_client() dir0.upload_file('file1', 'data1') dir1 = share.get_directory_client('dir1') dir1.create_directory() dir1.upload_file('file2', 'data2') dir2 = share.get_directory_client('dir2') dir2.create_directory() # Act resp = list(share.list_directories_and_files()) # 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') self._delete_shares() @FileSharePreparer() def test_list_directories_and_files_with_snapshot(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_name = self._create_share() dir1 = share_name.get_directory_client('dir1') dir1.create_directory() dir2 = share_name.get_directory_client('dir2') dir2.create_directory() snapshot1 = share_name.create_snapshot() dir3 = share_name.get_directory_client('dir3') dir3.create_directory() file1 = share_name.get_file_client('file1') file1.upload_file('data') # Act snapshot_client = self.fsc.get_share_client(share_name.share_name, snapshot=snapshot1) resp = list(snapshot_client.list_directories_and_files()) # Assert self.assertIsNotNone(resp) self.assertEqual(len(resp), 2) self.assertIsNotNone(resp[0]) self.assertNamedItemInContainer(resp, 'dir1') self.assertNamedItemInContainer(resp, 'dir2') self._delete_shares() @FileSharePreparer() def test_list_directories_and_files_with_num_results( self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_name = self._create_share() dir1 = share_name.create_directory('dir1') root = share_name.get_directory_client() root.upload_file('filea1', '1024') root.upload_file('filea2', '1024') root.upload_file('filea3', '1024') root.upload_file('fileb1', '1024') # Act result = share_name.list_directories_and_files( results_per_page=2).by_page() result = list(next(result)) # Assert self.assertIsNotNone(result) self.assertEqual(len(result), 2) self.assertNamedItemInContainer(result, 'dir1') self.assertNamedItemInContainer(result, 'filea1') self._delete_shares() @FileSharePreparer() def test_list_directories_and_files_with_num_results_and_marker( self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share_name = self._create_share() dir1 = share_name.get_directory_client('dir1') dir1.create_directory() dir1.upload_file('filea1', '1024') dir1.upload_file('filea2', '1024') dir1.upload_file('filea3', '1024') dir1.upload_file('fileb1', '1024') # Act generator1 = share_name.list_directories_and_files( 'dir1', results_per_page=2).by_page() result1 = list(next(generator1)) generator2 = share_name.list_directories_and_files( 'dir1', results_per_page=2).by_page( continuation_token=generator1.continuation_token) result2 = list(next(generator2)) # 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) self._delete_shares() @FileSharePreparer() def test_list_directories_and_files_with_prefix(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._create_share() dir1 = share.create_directory('dir1') share.create_directory('dir1/pref_dir3') share.create_directory('dir2') root = share.get_directory_client() root.upload_file('file1', '1024') dir1.upload_file('pref_file2', '1025') dir1.upload_file('file3', '1025') # Act resp = list( share.list_directories_and_files('dir1', name_starts_with='pref')) # Assert self.assertIsNotNone(resp) self.assertEqual(len(resp), 2) self.assertIsNotNone(resp[0]) self.assertNamedItemInContainer(resp, 'pref_file2') self.assertNamedItemInContainer(resp, 'pref_dir3') self._delete_shares() @FileSharePreparer() def test_shared_access_share(self, storage_account_name, storage_account_key): # SAS URL is calculated from storage key, so this test runs live only if not self.is_live: return self._setup(storage_account_name, storage_account_key) file_name = 'file1' dir_name = 'dir1' data = b'hello world' share = self._create_share() dir1 = share.create_directory(dir_name) 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.account_url(storage_account_name, "file"), share_name=share.share_name, file_path=dir_name + '/' + file_name, credential=token, ) # Act print(sas_client.url) response = requests.get(sas_client.url) # Assert self.assertTrue(response.ok) self.assertEqual(data, response.content) self._delete_shares() @FileSharePreparer() def test_create_permission_for_share(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) 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 = self._create_share() permission_key = share_client.create_permission_for_share( user_given_permission) self.assertIsNotNone(permission_key) server_returned_permission = share_client.get_permission_for_share( permission_key) self.assertIsNotNone(server_returned_permission) permission_key2 = 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) @FileSharePreparer() def test_transport_closed_only_once(self, storage_account_name, storage_account_key): if not self.is_live: return self._setup(storage_account_name, storage_account_key) transport = RequestsTransport() url = self.account_url(storage_account_name, "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 @FileSharePreparer() def test_delete_directory_from_share(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._create_share() dir1 = share.create_directory('dir1') share.create_directory('dir2') share.create_directory('dir3') # Act resp = list(share.list_directories_and_files()) self.assertEqual(len(resp), 3) share.delete_directory('dir3') # Assert resp = list(share.list_directories_and_files()) self.assertEqual(len(resp), 2) self._delete_shares()
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) self.share_name = self.get_resource_name('utshare') if not self.is_playback(): self.fsc.create_share(self.share_name) def tearDown(self): if not self.is_playback(): try: self.fsc.delete_share(self.share_name, delete_snapshots='include') except: pass return super(StorageDirectoryTest, self).tearDown() # --Helpers----------------------------------------------------------------- # --Test cases for directories ---------------------------------------------- @record def test_create_directories(self): # Arrange share_client = self.fsc.get_share_client(self.share_name) # Act created = share_client.create_directory('dir1') # Assert self.assertTrue(created) @record def test_create_directories_with_metadata(self): # Arrange 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) @record def test_create_directories_fail_on_exist(self): # Arrange 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) @record def test_create_subdirectories(self): # Arrange 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') @record def test_create_subdirectories_with_metadata(self): # Arrange 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) @record def test_create_file_in_directory(self): # Arrange 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) @record def test_delete_file_in_directory(self): # Arrange 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() @record def test_delete_subdirectories(self): # Arrange 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() @record def test_get_directory_properties(self): # Arrange 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) @record def test_get_directory_properties_with_snapshot(self): # Arrange 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) @record def test_get_directory_metadata_with_snapshot(self): # Arrange 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) @record def test_get_directory_properties_with_non_existing_directory(self): # Arrange 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 @record def test_directory_exists(self): # Arrange 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) @record def test_directory_not_exists(self): # Arrange 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 @record def test_directory_parent_not_exists(self): # Arrange 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) @record def test_directory_exists_with_snapshot(self): # Arrange 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) @record def test_directory_not_exists_with_snapshot(self): # Arrange 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 @record def test_get_set_directory_metadata(self): # Arrange 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) @record def test_set_directory_properties_with_empty_smb_properties(self): # Arrange 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) @record def test_set_directory_properties_with_file_permission_key(self): # Arrange 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) @record def test_list_subdirectories_and_files(self): # Arrange 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 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_with_prefix(self): # Arrange 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 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_snapshot(self): # Arrange 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 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_nested_subdirectories_and_files(self): # Arrange 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 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_delete_directory_with_existing_share(self): # Arrange 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() @record def test_delete_directory_with_non_existing_directory(self): # Arrange 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 @record def test_get_directory_properties_server_encryption(self): # Arrange 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) if self.is_file_encryption_enabled(): self.assertTrue(props.server_encrypted) else: self.assertFalse(props.server_encrypted)
class StorageShareTest(StorageTestCase): def _setup(self, storage_account, storage_account_key): file_url = self.account_url(storage_account, "file") credentials = storage_account_key self.fsc = ShareServiceClient(account_url=file_url, credential=credentials) self.test_shares = [] def _teardown(self, FILE_PATH): if os.path.isfile(FILE_PATH): try: os.remove(FILE_PATH) except: pass # --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_name) 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 _delete_shares(self, prefix=TEST_SHARE_PREFIX): for l in self.fsc.list_shares(include_snapshots=True): try: self.fsc.delete_share(l.name, delete_snapshots=True) except: pass # --Test cases for shares ----------------------------------------- @GlobalStorageAccountPreparer() def test_create_share(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() # Act created = self._create_share() # Assert self.assertTrue(created) self._delete_shares(share.share_name) @GlobalStorageAccountPreparer() def test_create_share_snapshot(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() # Act created = share.create_share() snapshot = share.create_snapshot() # Assert self.assertTrue(created) self.assertIsNotNone(snapshot['snapshot']) self.assertIsNotNone(snapshot['etag']) self.assertIsNotNone(snapshot['last_modified']) self._delete_shares(share.share_name) @GlobalStorageAccountPreparer() def test_create_snapshot_with_metadata(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() metadata = {"test1": "foo", "test2": "bar"} metadata2 = {"test100": "foo100", "test200": "bar200"} # Act created = share.create_share(metadata=metadata) snapshot = share.create_snapshot(metadata=metadata2) share_props = share.get_share_properties() snapshot_client = ShareClient(self.account_url(storage_account, "file"), share_name=share.share_name, snapshot=snapshot, credential=storage_account_key) snapshot_props = 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) self._delete_shares(share.share_name) @GlobalStorageAccountPreparer() def test_delete_share_with_snapshots(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() share.create_share() snapshot = share.create_snapshot() # Act with self.assertRaises(HttpResponseError): share.delete_share() deleted = share.delete_share(delete_snapshots=True) self.assertIsNone(deleted) self._delete_shares() @GlobalStorageAccountPreparer() def test_delete_snapshot(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() share.create_share() snapshot = share.create_snapshot() # Act with self.assertRaises(HttpResponseError): share.delete_share() snapshot_client = ShareClient(self.account_url(storage_account, "file"), share_name=share.share_name, snapshot=snapshot, credential=storage_account_key) deleted = snapshot_client.delete_share() self.assertIsNone(deleted) self._delete_shares() @GlobalStorageAccountPreparer() def test_create_share_fail_on_exist(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() # Act created = share.create_share() # Assert self.assertTrue(created) self._delete_shares() @GlobalStorageAccountPreparer() def test_create_share_with_already_existing_share_fail_on_exist( self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() # Act created = share.create_share() with self.assertRaises(HttpResponseError): share.create_share() # Assert self.assertTrue(created) self._delete_shares() @GlobalStorageAccountPreparer() def test_create_share_with_metadata(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) metadata = {'hello': 'world', 'number': '42'} # Act client = self._get_share_reference() created = client.create_share(metadata=metadata) # Assert self.assertTrue(created) md = client.get_share_properties().metadata self.assertDictEqual(md, metadata) self._delete_shares() @GlobalStorageAccountPreparer() def test_create_share_with_quota(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) # Act client = self._get_share_reference() created = client.create_share(quota=1) # Assert props = client.get_share_properties() self.assertTrue(created) self.assertEqual(props.quota, 1) self._delete_shares() @GlobalStorageAccountPreparer() def test_share_exists(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._create_share() # Act exists = share.get_share_properties() # Assert self.assertTrue(exists) self._delete_shares() @GlobalStorageAccountPreparer() def test_share_not_exists(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() # Act with self.assertRaises(ResourceNotFoundError): share.get_share_properties() # Assert self._delete_shares() @GlobalStorageAccountPreparer() def test_share_snapshot_exists(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._create_share() snapshot = share.create_snapshot() # Act snapshot_client = self.fsc.get_share_client(share.share_name, snapshot=snapshot) exists = snapshot_client.get_share_properties() # Assert self.assertTrue(exists) self._delete_shares() @GlobalStorageAccountPreparer() def test_share_snapshot_not_exists(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = 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): snapshot_client.get_share_properties() # Assert self._delete_shares() @GlobalStorageAccountPreparer() def test_unicode_create_share_unicode_name(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share_name = u'啊齄丂狛狜' # Act with self.assertRaises(HttpResponseError): # not supported - share name must be alphanumeric, lowercase client = self.fsc.get_share_client(share_name) client.create_share() # Assert self._delete_shares() @GlobalStorageAccountPreparer() def test_list_shares_no_options(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._create_share() # Act shares = list(self.fsc.list_shares()) # Assert self.assertIsNotNone(shares) self.assertGreaterEqual(len(shares), 1) self.assertIsNotNone(shares[0]) self.assertNamedItemInContainer(shares, share.share_name) self._delete_shares() @GlobalResourceGroupPreparer() @StorageAccountPreparer(random_name_enabled=True, sku='premium_LRS', name_prefix='pyacrstorage', kind='FileStorage') def test_list_shares_no_options_for_premium_account( self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._create_share() # Act shares = list(self.fsc.list_shares()) # Assert self.assertIsNotNone(shares) self.assertGreaterEqual(len(shares), 1) self.assertIsNotNone(shares[0]) self.assertIsNotNone(shares[0].provisioned_iops) self.assertIsNotNone(shares[0].provisioned_ingress_mbps) self.assertIsNotNone(shares[0].provisioned_egress_mbps) self.assertIsNotNone(shares[0].next_allowed_quota_downgrade_time) self._delete_shares() @GlobalStorageAccountPreparer() def test_list_shares_with_snapshot(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) #share = self._get_share_reference() share = self._create_share('random') snapshot1 = share.create_snapshot() snapshot2 = share.create_snapshot() # Act shares = self.fsc.list_shares(include_snapshots=True) # Assert self.assertIsNotNone(shares) all_shares = list(shares) 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']) share.delete_share(delete_snapshots=True) self._delete_shares() @GlobalStorageAccountPreparer() def test_list_shares_with_prefix(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() share.create_share() # Act shares = list(self.fsc.list_shares(name_starts_with=share.share_name)) # Assert self.assertEqual(len(shares), 1) self.assertIsNotNone(shares[0]) self.assertEqual(shares[0].name, share.share_name) self.assertIsNone(shares[0].metadata) self._delete_shares() @GlobalStorageAccountPreparer() def test_list_shares_with_include_metadata(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) metadata = {'hello': 'world', 'number': '42'} share = self._get_share_reference() share.create_share(metadata=metadata) # Act shares = list( self.fsc.list_shares(share.share_name, include_metadata=True)) # 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) self._delete_shares() @GlobalStorageAccountPreparer() def test_list_shares_with_num_results_and_marker(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) prefix = 'listshare' share_names = [] for i in range(0, 4): share_names.append(self._create_share(prefix + str(i)).share_name) #share_names.sort() # Act generator1 = self.fsc.list_shares(prefix, results_per_page=2).by_page() shares1 = list(next(generator1)) generator2 = self.fsc.list_shares(prefix, results_per_page=2).by_page( continuation_token=generator1.continuation_token) shares2 = list(next(generator2)) # 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]) self._delete_shares() @GlobalStorageAccountPreparer() def test_set_share_metadata(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._create_share() metadata = {'hello': 'world', 'number': '42'} # Act share.set_share_metadata(metadata) # Assert md = share.get_share_properties().metadata self.assertDictEqual(md, metadata) self._delete_shares() @GlobalStorageAccountPreparer() def test_get_share_metadata(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) metadata = {'hello': 'world', 'number': '42'} # Act client = self._get_share_reference() created = client.create_share(metadata=metadata) # Assert self.assertTrue(created) md = client.get_share_properties().metadata self.assertDictEqual(md, metadata) self._delete_shares() @GlobalStorageAccountPreparer() def test_get_share_metadata_with_snapshot(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) metadata = {'hello': 'world', 'number': '42'} # Act client = self._get_share_reference() created = client.create_share(metadata=metadata) snapshot = client.create_snapshot() snapshot_client = self.fsc.get_share_client(client.share_name, snapshot=snapshot) # Assert self.assertTrue(created) md = snapshot_client.get_share_properties().metadata self.assertDictEqual(md, metadata) self._delete_shares() @GlobalStorageAccountPreparer() def test_set_share_properties(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._create_share() share.set_share_quota(1) # Act props = share.get_share_properties() # Assert self.assertIsNotNone(props) self.assertEqual(props.quota, 1) self._delete_shares() @GlobalResourceGroupPreparer() @StorageAccountPreparer(random_name_enabled=True, sku='premium_LRS', name_prefix='pyacrstorage', kind='FileStorage') def test_get_share_properties_for_premium_account(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._create_share() # Act props = share.get_share_properties() # Assert self.assertIsNotNone(props) self.assertIsNotNone(props.quota) self.assertIsNotNone(props.provisioned_iops) self.assertIsNotNone(props.provisioned_ingress_mbps) self.assertIsNotNone(props.provisioned_egress_mbps) self.assertIsNotNone(props.next_allowed_quota_downgrade_time) self._delete_shares() @GlobalStorageAccountPreparer() def test_delete_share_with_existing_share(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() share.create_share() # Act deleted = share.delete_share() # Assert self.assertIsNone(deleted) self._delete_shares() @GlobalStorageAccountPreparer() def test_delete_share_with_existing_share_fail_not_exist( self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) client = self._get_share_reference() # Act with LogCaptured(self) as log_captured: with self.assertRaises(HttpResponseError): client.delete_share() log_as_str = log_captured.getvalue() self._delete_shares() @GlobalStorageAccountPreparer() def test_delete_share_with_non_existing_share(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) client = self._get_share_reference() # Act with LogCaptured(self) as log_captured: with self.assertRaises(HttpResponseError): deleted = client.delete_share() log_as_str = log_captured.getvalue() self.assertTrue('ERROR' not in log_as_str) self._delete_shares() @GlobalStorageAccountPreparer() def test_delete_share_with_non_existing_share_fail_not_exist( self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) client = self._get_share_reference() # Act with LogCaptured(self) as log_captured: with self.assertRaises(HttpResponseError): client.delete_share() log_as_str = log_captured.getvalue() self._delete_shares() @GlobalStorageAccountPreparer() def test_get_share_stats(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() share.create_share() # Act share_usage = share.get_share_stats() # Assert self.assertEqual(share_usage, 0) self._delete_shares() @GlobalStorageAccountPreparer() def test_set_share_acl(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() share.create_share() # Act resp = share.set_share_access_policy(signed_identifiers=dict()) # Assert acl = share.get_share_access_policy() self.assertIsNotNone(acl) self._delete_shares() @GlobalStorageAccountPreparer() def test_set_share_acl_with_empty_signed_identifiers( self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() share.create_share() # Act resp = share.set_share_access_policy(dict()) # Assert acl = share.get_share_access_policy() self.assertIsNotNone(acl) self.assertEqual(len(acl.get('signed_identifiers')), 0) self._delete_shares() @GlobalStorageAccountPreparer() def test_set_share_acl_with_signed_identifiers(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() 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 = share.set_share_access_policy(identifiers) # Assert acl = share.get_share_access_policy() self.assertIsNotNone(acl) self.assertEqual(len(acl['signed_identifiers']), 1) self.assertEqual(acl['signed_identifiers'][0].id, 'testid') self._delete_shares() @GlobalStorageAccountPreparer() def test_set_share_acl_too_many_ids(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._get_share_reference() share.create_share() # Act identifiers = dict() for i in range(0, 6): identifiers['id{}'.format(i)] = AccessPolicy() # Assert with self.assertRaises(ValueError) as e: 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.' ) self._delete_shares() @GlobalStorageAccountPreparer() def test_list_directories_and_files(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._create_share() dir0 = share.get_directory_client() dir0.upload_file('file1', 'data1') dir1 = share.get_directory_client('dir1') dir1.create_directory() dir1.upload_file('file2', 'data2') dir2 = share.get_directory_client('dir2') dir2.create_directory() # Act resp = list(share.list_directories_and_files()) # 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') self._delete_shares() @GlobalStorageAccountPreparer() def test_list_directories_and_files_with_snapshot(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share_name = self._create_share() dir1 = share_name.get_directory_client('dir1') dir1.create_directory() dir2 = share_name.get_directory_client('dir2') dir2.create_directory() snapshot1 = share_name.create_snapshot() dir3 = share_name.get_directory_client('dir3') dir3.create_directory() file1 = share_name.get_file_client('file1') file1.upload_file('data') # Act snapshot_client = self.fsc.get_share_client(share_name.share_name, snapshot=snapshot1) resp = list(snapshot_client.list_directories_and_files()) # Assert self.assertIsNotNone(resp) self.assertEqual(len(resp), 2) self.assertIsNotNone(resp[0]) self.assertNamedItemInContainer(resp, 'dir1') self.assertNamedItemInContainer(resp, 'dir2') self._delete_shares() @GlobalStorageAccountPreparer() def test_list_directories_and_files_with_num_results( self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share_name = self._create_share() dir1 = share_name.create_directory('dir1') root = share_name.get_directory_client() root.upload_file('filea1', '1024') root.upload_file('filea2', '1024') root.upload_file('filea3', '1024') root.upload_file('fileb1', '1024') # Act result = share_name.list_directories_and_files( results_per_page=2).by_page() result = list(next(result)) # Assert self.assertIsNotNone(result) self.assertEqual(len(result), 2) self.assertNamedItemInContainer(result, 'dir1') self.assertNamedItemInContainer(result, 'filea1') self._delete_shares() @GlobalStorageAccountPreparer() def test_list_directories_and_files_with_num_results_and_marker( self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share_name = self._create_share() dir1 = share_name.get_directory_client('dir1') dir1.create_directory() dir1.upload_file('filea1', '1024') dir1.upload_file('filea2', '1024') dir1.upload_file('filea3', '1024') dir1.upload_file('fileb1', '1024') # Act generator1 = share_name.list_directories_and_files( 'dir1', results_per_page=2).by_page() result1 = list(next(generator1)) generator2 = share_name.list_directories_and_files( 'dir1', results_per_page=2).by_page( continuation_token=generator1.continuation_token) result2 = list(next(generator2)) # 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) self._delete_shares() @GlobalStorageAccountPreparer() def test_list_directories_and_files_with_prefix(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._create_share() dir1 = share.create_directory('dir1') share.create_directory('dir1/pref_dir3') share.create_directory('dir2') root = share.get_directory_client() root.upload_file('file1', '1024') dir1.upload_file('pref_file2', '1025') dir1.upload_file('file3', '1025') # Act resp = list( share.list_directories_and_files('dir1', name_starts_with='pref')) # Assert self.assertIsNotNone(resp) self.assertEqual(len(resp), 2) self.assertIsNotNone(resp[0]) self.assertNamedItemInContainer(resp, 'pref_file2') self.assertNamedItemInContainer(resp, 'pref_dir3') self._delete_shares() @GlobalStorageAccountPreparer() def test_shared_access_share(self, resource_group, location, storage_account, storage_account_key): # SAS URL is calculated from storage key, so this test runs live only if not self.is_live: return self._setup(storage_account, storage_account_key) file_name = 'file1' dir_name = 'dir1' data = b'hello world' share = self._create_share() dir1 = share.create_directory(dir_name) 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.account_url(storage_account, "file"), share_name=share.share_name, file_path=dir_name + '/' + file_name, credential=token, ) # Act print(sas_client.url) response = requests.get(sas_client.url) # Assert self.assertTrue(response.ok) self.assertEqual(data, response.content) self._delete_shares() @GlobalStorageAccountPreparer() def test_create_permission_for_share(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) 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 = self._create_share() permission_key = share_client.create_permission_for_share( user_given_permission) self.assertIsNotNone(permission_key) server_returned_permission = share_client.get_permission_for_share( permission_key) self.assertIsNotNone(server_returned_permission) permission_key2 = 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) @GlobalStorageAccountPreparer() 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 @GlobalStorageAccountPreparer() def test_delete_directory_from_share(self, resource_group, location, storage_account, storage_account_key): self._setup(storage_account, storage_account_key) share = self._create_share() dir1 = share.create_directory('dir1') share.create_directory('dir2') share.create_directory('dir3') # Act resp = list(share.list_directories_and_files()) self.assertEqual(len(resp), 3) share.delete_directory('dir3') # Assert resp = list(share.list_directories_and_files()) self.assertEqual(len(resp), 2) self._delete_shares()