def rabbit_config(request, rabbit_manager): from kombu import pools from nameko.testing.utils import ( reset_rabbit_vhost, reset_rabbit_connections, get_rabbit_connections, get_rabbit_config, ) amqp_uri = request.config.getoption("AMQP_URI") conf = get_rabbit_config(amqp_uri) reset_rabbit_connections(conf["vhost"], rabbit_manager) reset_rabbit_vhost(conf["vhost"], conf["username"], rabbit_manager) yield conf pools.reset() # close connections in pools # raise a runtime error if the test leaves any connections lying around connections = get_rabbit_connections(conf["vhost"], rabbit_manager) open_connections = [conn for conn in connections if conn["state"] != "closed"] if open_connections: count = len(open_connections) raise RuntimeError("{} rabbit connection(s) left open.".format(count))
def rabbit_config(request, rabbit_manager): from kombu import pools from nameko.testing.utils import (reset_rabbit_vhost, reset_rabbit_connections, get_rabbit_connections, get_rabbit_config) amqp_uri = request.config.getoption('AMQP_URI') conf = get_rabbit_config(amqp_uri) reset_rabbit_connections(conf['vhost'], rabbit_manager) reset_rabbit_vhost(conf['vhost'], conf['username'], rabbit_manager) yield conf pools.reset() # close connections in pools # raise a runtime error if the test leaves any connections lying around connections = get_rabbit_connections(conf['vhost'], rabbit_manager) open_connections = [ conn for conn in connections if conn['state'] != "closed" ] if open_connections: count = len(open_connections) raise RuntimeError("{} rabbit connection(s) left open.".format(count))
def test_reset_rabbit_vhost(rabbit_config, rabbit_manager): vhost = rabbit_config['vhost'] username = rabbit_config['username'] def get_active_vhosts(): return [vhost_data['name'] for vhost_data in rabbit_manager.get_all_vhosts()] reset_rabbit_vhost(vhost, username, rabbit_manager) assert vhost in get_active_vhosts() rabbit_manager.delete_vhost(vhost) assert vhost not in get_active_vhosts() reset_rabbit_vhost(vhost, username, rabbit_manager) assert vhost in get_active_vhosts()
def rabbit_config(request, rabbit_manager): amqp_uri = request.config.getoption('AMQP_URI') conf = get_rabbit_config(amqp_uri) reset_rabbit_connections(conf['vhost'], rabbit_manager) reset_rabbit_vhost(conf['vhost'], conf['username'], rabbit_manager) yield conf pools.reset() # close connections in pools # raise a runtime error if the test leaves any connections lying around connections = get_rabbit_connections(conf['vhost'], rabbit_manager) if connections: count = len(connections) raise RuntimeError("{} rabbit connection(s) left open.".format(count))
def test_reset_rabbit_vhost_errors(): rabbit_manager = Mock() # 500 error error_500 = HTTPError({'reason': 'error'}, status=500) rabbit_manager.delete_vhost.side_effect = error_500 with pytest.raises(HTTPError): reset_rabbit_vhost("vhost", "username", rabbit_manager) # 404 error error_404 = HTTPError({'reason': 'error'}, status=404) rabbit_manager.delete_vhost.side_effect = error_404 # does not raise reset_rabbit_vhost("vhost", "username", rabbit_manager) assert rabbit_manager.create_vhost.call_args == call("vhost") assert rabbit_manager.set_vhost_permissions.call_args == call( "vhost", "username", '.*', '.*', '.*')
def test_reset_rabbit_vhost_errors(): rabbit_manager = Mock() # 500 error response_500 = Response() response_500.status_code = 50 error_500 = HTTPError(response=response_500) rabbit_manager.delete_vhost.side_effect = error_500 with pytest.raises(HTTPError): reset_rabbit_vhost("vhost", "username", rabbit_manager) # 404 error response_404 = Response() response_404.status_code = 404 error_404 = HTTPError(response=response_404) rabbit_manager.delete_vhost.side_effect = error_404 # does not raise reset_rabbit_vhost("vhost", "username", rabbit_manager) assert rabbit_manager.create_vhost.call_args == call("vhost") assert rabbit_manager.set_vhost_permissions.call_args == call( "vhost", "username", '.*', '.*', '.*')
def rabbit_config(request, rabbit_manager): amqp_uri = request.config.getoption('AMQP_URI') conf = {'AMQP_URI': amqp_uri} uri = urlparse(amqp_uri) vhost = uri.path[1:].replace('/', '%2F') username = uri.username conf['vhost'] = vhost conf['username'] = username reset_rabbit_connections(vhost, rabbit_manager) reset_rabbit_vhost(vhost, username, rabbit_manager) yield conf pools.reset() # close connections in pools # raise a runtime error if the test leaves any connections lying around connections = get_rabbit_connections(vhost, rabbit_manager) if connections: count = len(connections) raise RuntimeError("{} rabbit connection(s) left open.".format(count))