def dispatch(*args): # test json serialization for a in args: json.dumps(a) # return a dispatch report r = DispatchReport() return r.dict()
def _request(self, method, path, queries=(), body=None, ensure_encoding=True): """ make a HTTP request to the pulp server and return the response :param method: name of an HTTP method such as GET, POST, PUT, HEAD or DELETE :type method: basestring :param path: URL for this request :type path: basestring :param queries: mapping object or a sequence of 2-element tuples, in either case representing key-value pairs to be used as query parameters on the URL. :type queries: mapping object or sequence of 2-element tuples :param body: Data structure that will be JSON serialized and send as the request's body. :type body: Anything that is JSON-serializable. :param ensure_encoding: toggle proper string encoding for the body :type ensure_encoding: bool :return: Response object :rtype: pulp.bindings.responses.Response :raises: ConnectionException or one of the RequestExceptions (depending on response codes) in case of unsuccessful request """ url = self._build_url(path, queries) if ensure_encoding: body = self._process_body(body) if not isinstance(body, (NoneType, basestring)): body = json.dumps(body) self.log.debug('sending %s request to %s' % (method, url)) response_code, response_body = self.server_wrapper.request(method, url, body) if self.api_responses_logger: self.api_responses_logger.info( '%s request to %s with parameters %s' % (method, url, body)) self.api_responses_logger.info("Response status : %s \n" % response_code) self.api_responses_logger.info( "Response body :\n %s\n" % json.dumps(response_body, indent=2)) if response_code >= 300: self._handle_exceptions(response_code, response_body) elif response_code == 200 or response_code == 201: body = response_body elif response_code == 202: if isinstance(response_body, list): body = [Task(t) for t in response_body] else: body = Task(response_body) return Response(response_code, body)
def _request(self, method, path, queries=(), body=None, ensure_encoding=True): """ make a HTTP request to the pulp server and return the response :param method: name of an HTTP method such as GET, POST, PUT, HEAD or DELETE :type method: basestring :param path: URL for this request :type path: basestring :param queries: mapping object or a sequence of 2-element tuples, in either case representing key-value pairs to be used as query parameters on the URL. :type queries: mapping object or sequence of 2-element tuples :param body: Data structure that will be JSON serialized and send as the request's body. :type body: Anything that is JSON-serializable. :param ensure_encoding: toggle proper string encoding for the body :type ensure_encoding: bool :return: Response object :rtype: pulp.bindings.responses.Response :raises: ConnectionException or one of the RequestExceptions (depending on response codes) in case of unsuccessful request """ url = self._build_url(path, queries) if ensure_encoding: body = self._process_body(body) if not isinstance(body, (NoneType, basestring)): body = json.dumps(body) self.log.debug('sending %s request to %s' % (method, url)) response_code, response_body = self.server_wrapper.request(method, url, body) if self.api_responses_logger: self.api_responses_logger.info('%s request to %s with parameters %s' % (method, url, body)) self.api_responses_logger.info("Response status : %s \n" % response_code) self.api_responses_logger.info("Response body :\n %s\n" % json.dumps(response_body, indent=2)) if response_code >= 300: self._handle_exceptions(response_code, response_body) elif response_code == 200 or response_code == 201: body = response_body elif response_code == 202: if isinstance(response_body, list): body = [Task(t) for t in response_body] else: body = Task(response_body) return Response(response_code, body)
def publish(cls, event, exchange=None): """ Publish an event to a remote exchange. This sends the JSON-serialized version of the output from the event's "data" method as the content of the message. Failures to publish a message will be logged but will not prevent execution from continuing. :param event: the event that should be published to a remote exchange :type event: pulp.server.event.data.Event :param exchange: optional name of an exchange to use. This will override whatever is setup in the server config. :type exchange: str """ connection = cls.connection() if connection: subject = '%s.%s' % (cls.BASE_SUBJECT, event.event_type) destination = '%s/%s; {create:always, node:{type:topic}, link:{x-declare:{auto-delete:True}}}' % ( exchange or cls.EXCHANGE, subject) data = json.dumps(event.data()) try: cls.connection().session().sender(destination).send(data) except MessagingError, e: # log the error, but allow life to go on. cls.logger.exception('could not publish message: %s' % str(e))
def publish(cls, event, exchange=None): """ Publish an event to a remote exchange. This sends the JSON-serialized version of the output from the event's "data" method as the content of the message. Failures to publish a message will be logged but will not prevent execution from continuing. :param event: the event that should be published to a remote exchange :type event: pulp.server.event.data.Event :param exchange: optional name of an exchange to use. This will override whatever is setup in the server config. :type exchange: str """ connection = cls.connection() if connection: subject = '%s.%s' % (cls.BASE_SUBJECT, event.event_type) destination = ( '%s/%s; {create:always, node:{type:topic}, link:{x-declare:{auto-delete:True}}}' % (exchange or cls.EXCHANGE, subject)) data = json.dumps(event.data(), default=json_util.default) try: cls.connection().session().sender(destination).send(data) except MessagingError, e: # log the error, but allow life to go on. cls._logger.exception('could not publish message: %s' % str(e))
def _do_request(self, request_type, uri, params, additional_headers, serialize_json=True): """ Override the base class controller to allow for less deterministic responses due to integration with the dispatch package. """ # Use the default headers established at setup and override/add any headers = dict(PulpWebserviceTests.HEADERS) if additional_headers is not None: headers.update(additional_headers) # Serialize the parameters if any are specified if params is None: params = {} if serialize_json: params = json.dumps(params) # Invoke the API f = getattr(PulpWebserviceTests.TEST_APP, request_type) response = f('http://localhost' + uri, params=params, headers=headers, expect_errors=True) # Collect return information and deserialize it status = response.status try: body = json.loads(response.body) except ValueError: body = None return status, body
def test_add_unit_metadata(self): unit = DockerImage('foo_image', 'foo_parent', 2048) test_result = {'id': 'foo_image'} result_json = json.dumps(test_result) self.context.add_unit_metadata(unit) self.context.metadata_file_handle.write.assert_called_once_with( result_json)
def test_add_unit_metadata_with_tag(self): unit = DockerImage('foo_image', 'foo_parent', 2048) test_result = {'id': 'foo_image'} result_json = json.dumps(test_result) self.context.tags = {'bar': 'foo_image'} self.context.redirect_url = 'http://www.pulpproject.org/foo/' self.context.add_unit_metadata(unit) self.context.metadata_file_handle.write.assert_called_once_with(result_json)
def test_add_unit_metadata_with_tag(self): unit = models.Image(image_id='foo_image', parent_id='foo_parent', size=2048) test_result = {'id': 'foo_image'} result_json = json.dumps(test_result) self.context.tags = {'bar': 'foo_image'} self.context.redirect_url = 'http://www.pulpproject.org/foo/' self.context.add_unit_metadata(unit) self.context.metadata_file_handle.write.assert_called_once_with(result_json)
def test_success(self): specs = [{'content_type_id': 'foo', 'unit_id': 'bar'}] ret = self.api.remove_bulk(specs) body = json.dumps(specs) self.api.server.POST.assert_called_once_with(self.api.DELETE_BULK_PATH, body) self.assertEqual(ret, self.api.server.POST.return_value)
def test_write_file_footer(self): self.context._write_file_footer() calls = [ call('],"tags":'), call(json.dumps({u'latest': u'image_id'})), call('}') ] self.context.metadata_file_handle.write.assert_has_calls(calls)
def test_publish(self, mock_connection): mock_event = mock.MagicMock() mock_event.data.return_value = {} mock_event.event_type = data.TYPE_REPO_PUBLISH_FINISHED self.manager.publish(mock_event) expected_topic = "pulp.server." + data.TYPE_REPO_PUBLISH_FINISHED expected_destination = "%s/%s" % (config.get("messaging", "topic_exchange"), expected_topic) sender = mock_connection.return_value.session.return_value.sender self.assertEqual(sender.call_count, 1) self.assertTrue(sender.call_args[0][0].startswith(expected_destination)) sender.return_value.send.assert_called_once_with(json.dumps(mock_event.data.return_value))
def test_publish(self, mock_connection): mock_event = mock.MagicMock() mock_event.data.return_value = {} mock_event.event_type = data.TYPE_REPO_PUBLISH_FINISHED self.manager.publish(mock_event) expected_topic = 'pulp.server.' + data.TYPE_REPO_PUBLISH_FINISHED expected_destination = '%s/%s' % ( config.get('messaging', 'topic_exchange'), expected_topic) sender = mock_connection.return_value.session.return_value.sender self.assertEqual(sender.call_count, 1) self.assertTrue(sender.call_args[0][0].startswith(expected_destination)) sender.return_value.send.assert_called_once_with( json.dumps(mock_event.data.return_value))
def to_json(self): """ Return the repository metadata as a JSON representation. :return: The repository metadata as json. :rtype: str """ repo_metadata_dict = [] for module in self.modules: module_metadata = {'name': module.name, 'author': module.author, 'version': module.version, 'tag_list': module.tag_list} repo_metadata_dict.append(module_metadata) # Serialize metadata of all modules in the repo into a single JSON document return json.dumps(repo_metadata_dict)
def remove_bulk(self, specs): """ Remove several orphaned content units. :param specs: list of dicts that include keys "content_type_id" and "unit_id". Each dict matches one unit to be removed. :type specs: list of dicts """ expected_keys = set(('content_type_id', 'unit_id')) for spec in tuple(specs): if not isinstance(spec, dict): raise TypeError('members of "spec" must be dicts') if expected_keys != set(spec.keys()): raise ValueError('dict must include 2 keys: "content_type_id" and "unit_id"') body = json.dumps(specs) return self.server.POST(self.DELETE_BULK_PATH, body)
def remove_bulk(self, specs): """ Remove several orphaned content units. :param specs: list of dicts that include keys "content_type_id" and "unit_id". Each dict matches one unit to be removed. :type specs: list of dicts """ expected_keys = set(('content_type_id', 'unit_id')) for spec in tuple(specs): if not isinstance(spec, dict): raise TypeError('members of "spec" must be dicts') if expected_keys != set(spec.keys()): raise ValueError( 'dict must include 2 keys: "content_type_id" and "unit_id"' ) body = json.dumps(specs) return self.server.POST(self.DELETE_BULK_PATH, body)
def to_json(self): """ Serializes the repository metadata into its JSON representation. """ # Assemble all of the modules in dictionary form module_dicts = [m.to_dict() for m in self.modules] # For each module, we only want a small subset of data that goes in the # repo metadata document limited_module_dicts = [] included_fields = ('name', 'author', 'version', 'tag_list') for m in module_dicts: clean_module = dict([(k, v) for k, v in m.items() if k in included_fields]) limited_module_dicts.append(clean_module) # Serialize the dictionaries into a single JSON document serialized = json.dumps(limited_module_dicts) return serialized
def test_add_unit_metadata(self): unit = models.Image(image_id='foo_image', parent_id='foo_parent', size=2048) test_result = {'id': 'foo_image'} result_json = json.dumps(test_result) self.context.add_unit_metadata(unit) self.context.metadata_file_handle.write.assert_called_once_with(result_json)
def to_json(self, **kw): return json.dumps(self.serialize())
def test_write_file_footer(self): self.context._write_file_footer() calls = [call('],"tags":'), call(json.dumps({u'latest': u'image_id'})), call('}')] self.context.metadata_file_handle.write.assert_has_calls(calls)
def test_add_unit_metadata(self): unit = DockerImage('foo_image', 'foo_parent', 2048) test_result = {'id': 'foo_image'} result_json = json.dumps(test_result) self.context.add_unit_metadata(unit) self.context.metadata_file_handle.write.assert_called_once_with(result_json)