def create_queue(self, queue_name, metadata=None, fail_on_exist=False, timeout=None): ''' Creates a queue under the given account. :param str queue_name: The name of the queue to create. A queue name must be from 3 through 63 characters long and may only contain lowercase letters, numbers, and the dash (-) character. The first and last letters in the queue must be alphanumeric. The dash (-) character cannot be the first or last character. Consecutive dash characters are not permitted in the queue name. :param metadata: A dict containing name-value pairs to associate with the queue as metadata. Note that metadata names preserve the case with which they were created, but are case-insensitive when set or read. :type metadata: dict(str, str) :param bool fail_on_exist: Specifies whether to throw an exception if the queue already exists. :param int timeout: The server timeout, expressed in seconds. :return: A boolean indicating whether the queue was created. If fail_on_exist was set to True, this will throw instead of returning false. :rtype: bool ''' _validate_not_none('queue_name', queue_name) request = HTTPRequest() request.method = 'PUT' request.host_locations = self._get_host_locations() request.path = _get_path(queue_name) request.query = {'timeout': _int_to_str(timeout)} _add_metadata_headers(metadata, request) def _return_request(request): return request if not fail_on_exist: try: response = self._perform_request( request, parser=_return_request, expected_errors=[_QUEUE_ALREADY_EXISTS_ERROR_CODE]) if response.status == _HTTP_RESPONSE_NO_CONTENT: return False return True except AzureHttpError as ex: _dont_fail_on_exist(ex) return False else: response = self._perform_request(request, parser=_return_request) if response.status == _HTTP_RESPONSE_NO_CONTENT: raise AzureConflictHttpError( _ERROR_CONFLICT.format(response.message), response.status) return True
def test_retry(self, mocker): # Arrange settings = setup_settings_for_test() mocked_blob_service = mocker.patch('azure.storage.blob.BlockBlobService') mocked_blob_service.return_value.exists.return_value = True mocked_blob_service.return_value.get_blob_to_text.return_value.content = '{}' acquire_lease_spy = mocker.spy(mocked_blob_service.return_value, 'acquire_blob_lease') release_lease_spy = mocker.spy(mocked_blob_service.return_value, 'release_blob_lease') instance = mocked_blob_service.return_value instance.acquire_blob_lease.side_effect = [AzureConflictHttpError(message="message", status_code=409), 13] save_image_metadata(settings, 'container_name', 'blob_name.jpg', settings.image_sizes) assert acquire_lease_spy.call_count == 2 assert acquire_lease_spy.call_args_list[0].args[0] == 'data' assert acquire_lease_spy.call_args_list[0].args[1] == 'srcsets.json' assert acquire_lease_spy.call_args_list[1].args[0] == 'data' assert acquire_lease_spy.call_args_list[1].args[1] == 'srcsets.json' release_lease_spy.assert_called_once_with('data', 'srcsets.json', lease_id=13)
def test_create_container(self): # tests if container was created with proper params self.azure_handler.create_container('test') self.mock_blob_service.create_container.assert_called_with( 'test', fail_on_exist=True, timeout=300, public_access=None) self.azure_handler.create_container('test', set_public=True) self.mock_blob_service.create_container.assert_called_with( 'test', fail_on_exist=True, timeout=300, public_access=PublicAccess.Container) self.mock_azure.reset_mock() # tests if it was muted when an error occured at creating containers self.mock_blob_service.create_container = mock.Mock( side_effect=AzureConflictHttpError('a', 'b')) self.assertEqual(self.azure_handler.create_container('test'), False) self.assertEqual( self.azure_handler.create_container('test', set_public=True), False)
def create_queue(self, queue_name, x_ms_meta_name_values=None, fail_on_exist=False): ''' Creates a queue under the given account. queue_name: name of the queue. x_ms_meta_name_values: Optional. A dict containing name-value pairs to associate with the queue as metadata. fail_on_exist: Specify whether throw exception when queue exists. ''' _validate_not_none('queue_name', queue_name) request = HTTPRequest() request.method = 'PUT' request.host = self._get_host() request.path = '/' + _str(queue_name) + '' request.headers = [('x-ms-meta-name-values', x_ms_meta_name_values)] request.path, request.query = _update_request_uri_query_local_storage( request, self.use_local_storage) request.headers = _update_storage_queue_header(request, self.authentication) if not fail_on_exist: try: response = self._perform_request(request) if response.status == _HTTP_RESPONSE_NO_CONTENT: return False return True except AzureHttpError as ex: _dont_fail_on_exist(ex) return False else: response = self._perform_request(request) if response.status == _HTTP_RESPONSE_NO_CONTENT: raise AzureConflictHttpError( _ERROR_CONFLICT.format(response.message), response.status) return True