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_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_old_api_copy_file_succeeds(self, resource_group, location, storage_account, storage_account_key): fsc = ShareServiceClient(self.account_url(storage_account, "file"), credential=storage_account_key, max_range_size=4 * 1024, api_version=self.api_version_1) share = self._create_share(fsc) file_name = self._get_file_reference() source_client = share.get_file_client(file_name) source_client.upload_file(self.short_byte_data) source_prop = source_client.get_file_properties() file_client = ShareFileClient(self.account_url(storage_account, "file"), share_name=share.share_name, file_path='file1copy', credential=storage_account_key, api_version=self.api_version_1) # Act copy = file_client.start_copy_from_url(source_client.url) # Assert dest_prop = file_client.get_file_properties() # to make sure the acl is copied from source self.assertEqual(source_prop['permission_key'], dest_prop['permission_key']) self.assertIsNotNone(copy) self.assertEqual(copy['copy_status'], 'success') self.assertIsNotNone(copy['copy_id']) copy_file = file_client.download_file().readall() self.assertEqual(copy_file, self.short_byte_data)
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_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)
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)
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_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)
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_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)
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_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)
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)
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)
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)
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)