diff --git a/aodh/storage/__init__.py b/aodh/storage/__init__.py index 3388403a7..a3d6a0bae 100644 --- a/aodh/storage/__init__.py +++ b/aodh/storage/__init__.py @@ -77,21 +77,21 @@ class StorageBadAggregate(Exception): def get_connection_from_config(conf): retries = conf.database.max_retries + url = conf.database.connection + connection_scheme = urlparse.urlparse(url).scheme + # SQLAlchemy connections specify may specify a 'dialect' or + # 'dialect+driver'. Handle the case where driver is specified. + engine_name = connection_scheme.split('+')[0] + # NOTE: translation not applied bug #1446983 + LOG.debug('looking for %(name)r driver in %(namespace)r', + {'name': engine_name, 'namespace': _NAMESPACE}) + mgr = driver.DriverManager(_NAMESPACE, engine_name) # Convert retry_interval secs to msecs for retry decorator @retrying.retry(wait_fixed=conf.database.retry_interval * 1000, stop_max_attempt_number=retries if retries >= 0 else None) def _get_connection(): """Return an open connection to the database.""" - url = conf.database.connection - connection_scheme = urlparse.urlparse(url).scheme - # SQLAlchemy connections specify may specify a 'dialect' or - # 'dialect+driver'. Handle the case where driver is specified. - engine_name = connection_scheme.split('+')[0] - # NOTE: translation not applied bug #1446983 - LOG.debug('looking for %(name)r driver in %(namespace)r', - {'name': engine_name, 'namespace': _NAMESPACE}) - mgr = driver.DriverManager(_NAMESPACE, engine_name) return mgr.driver(conf, url) return _get_connection() diff --git a/aodh/tests/storage/test_get_connection.py b/aodh/tests/storage/test_get_connection.py index fb9ccd466..23924c147 100644 --- a/aodh/tests/storage/test_get_connection.py +++ b/aodh/tests/storage/test_get_connection.py @@ -1,5 +1,6 @@ # # Copyright 2012 New Dream Network, LLC (DreamHost) +# Copyright 2015 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -56,12 +57,20 @@ class ConnectionRetryTest(base.BaseTestCase): def test_retries(self): with mock.patch.object(retrying.time, 'sleep') as retry_sleep: - try: - self.CONF.set_override("connection", "no-such-engine://", - group="database") - storage.get_connection_from_config(self.CONF) - except RuntimeError as err: - self.assertIn('no-such-engine', six.text_type(err)) + with mock.patch.object( + storage.impl_log.Connection, '__init__') as log_init: + + class ConnectionError(Exception): + pass + + def x(a, b): + raise ConnectionError + + log_init.side_effect = x + self.CONF.set_override("connection", "log://", "database") + self.assertRaises(ConnectionError, + storage.get_connection_from_config, + self.CONF) self.assertEqual(9, retry_sleep.call_count) retry_sleep.assert_called_with(10.0)