def create_api_app(global_conf, **local_conf): """Creates MainAPI application""" controllers = {} api_version = global_conf.get('api_version') if api_version == 'v2.0': controllers.update({ '/log/single': v2_logs.Logs() }) elif api_version == 'v3.0': controllers.update({ '/logs': v3_logs.Logs() }) wsgi_app = falcon.API( request_type=request.Request ) for route, ctrl in controllers.items(): wsgi_app.add_route(route, ctrl) error_handlers.register_error_handlers(wsgi_app) return wsgi_app
def test_should_return_v3_as_version(self, _): logs_resource = logs.Logs() self.assertEqual('v3.0', logs_resource.version)
def test_send_identical_messages(self, _): # mocks only log publisher, so the last component that actually # sends data to kafka # case is to verify if publisher was called with same arguments # for both cases v2 = v2_logs.Logs() v3 = v3_logs.Logs() publish_mock = mock.Mock() v2._kafka_publisher._kafka_publisher.publish = publish_mock v3._processor._kafka_publisher.publish = publish_mock component = 'monasca-log-api' service = 'laas' hostname = 'kornik' tenant_id = 'ironMan' roles = 'admin' v2_dimensions = 'hostname:%s,service:%s' % (hostname, service) v3_dimensions = { 'hostname': hostname, 'component': component, 'service': service } v2_body = { 'message': 'test' } v3_body = { 'logs': [ { 'message': 'test', 'dimensions': v3_dimensions } ] } self.api.add_route('/v2.0', v2) self.api.add_route('/v3.0', v3) self.simulate_request( '/v2.0', method='POST', headers={ headers.X_ROLES.name: roles, headers.X_DIMENSIONS.name: v2_dimensions, headers.X_APPLICATION_TYPE.name: component, headers.X_TENANT_ID.name: tenant_id, 'Content-Type': 'application/json', 'Content-Length': '100' }, body=json.dumps(v2_body) ) self.simulate_request( '/v3.0', method='POST', headers={ headers.X_ROLES.name: roles, headers.X_TENANT_ID.name: tenant_id, 'Content-Type': 'application/json', 'Content-Length': '100' }, body=json.dumps(v3_body) ) self.assertEqual(2, publish_mock.call_count) # in v2 send_messages is called with single envelope v2_send_msg_arg = publish_mock.mock_calls[0][1][1] # in v3 it is always called with list of envelopes v3_send_msg_arg = publish_mock.mock_calls[1][1][1] self.maxDiff = None # at this point we know that both args should be identical self.assertEqual(type(v2_send_msg_arg), type(v3_send_msg_arg)) self.assertIsInstance(v3_send_msg_arg, list) self.assertEqual(len(v2_send_msg_arg), len(v3_send_msg_arg)) self.assertEqual(1, len(v2_send_msg_arg)) v2_msg_as_dict = json.loads(v2_send_msg_arg[0]) v3_msg_as_dict = json.loads(v3_send_msg_arg[0]) self.assertDictEqual(v2_msg_as_dict, v3_msg_as_dict)
def _init_resource(test): resource = logs.Logs() test.api.add_route(ENDPOINT, resource) return resource