def test_raise_connection_error_decorated(self): self.dbapi = api.DBAPI('sqlalchemy', {'sqlalchemy': __name__}) self.test_db_api.error_counter = 5 self.assertRaises(exception.DBConnectionError, self.dbapi.api_raise_enable_retry) self.assertEqual(4, self.test_db_api.error_counter, 'Unexpected retry')
def _db_api(self): if not self.__db_api: ec2_db_api = db_api.DBAPI(CONF.database.backend, backend_mapping=_BACKEND_MAPPING) if CONF.database.use_tpool: self.__db_api = tpool.Proxy(ec2_db_api) else: self.__db_api = ec2_db_api return self.__db_api
def test_retry_one(self): self.dbapi = api.DBAPI('sqlalchemy', {'sqlalchemy': __name__}, use_db_reconnect=True, retry_interval=1) try: func = self.dbapi.api_raise_enable_retry self.test_db_api.error_counter = 1 self.assertTrue(func(), 'Single retry did not succeed.') except Exception: self.fail('Single retry raised an un-wrapped error.') self.assertEqual( 0, self.test_db_api.error_counter, 'Counter not decremented, retry logic probably failed.')
def test_retry_until_failure(self): self.dbapi = api.DBAPI('sqlalchemy', {'sqlalchemy': __name__}, use_db_reconnect=True, retry_interval=1, inc_retry_interval=False, max_retries=3) func = self.dbapi.api_raise_enable_retry self.test_db_api.error_counter = 5 self.assertRaises( exception.DBError, func, 'Retry of permanent failure did not throw DBError exception.') self.assertNotEqual( 0, self.test_db_api.error_counter, 'Retry did not stop after sql_max_retries iterations.')
def test_retry_two(self, p_time_sleep): self.dbapi = api.DBAPI('sqlalchemy', {'sqlalchemy': __name__}, use_db_reconnect=True, retry_interval=1, inc_retry_interval=False) try: func = self.dbapi.api_raise_enable_retry self.test_db_api.error_counter = 2 self.assertTrue(func(), 'Multiple retry did not succeed.') except Exception: self.fail('Multiple retry raised an un-wrapped error.') p_time_sleep.assert_called_with(1) self.assertEqual( 0, self.test_db_api.error_counter, 'Counter not decremented, retry logic probably failed.')
`sqlalchemy` is the only supported backend right now. :sql_connection: string specifying the sqlalchemy connection to use, like: `sqlite:///var/lib/blazar/blazar.sqlite`. """ from oslo_config import cfg from oslo_db import api as db_api from oslo_log import log as logging _BACKEND_MAPPING = { 'sqlalchemy': 'blazar.db.sqlalchemy.utils', } IMPL = db_api.DBAPI(cfg.CONF.database.backend, backend_mapping=_BACKEND_MAPPING) LOG = logging.getLogger(__name__) def setup_db(): """Set up database, create tables, etc. Return True on success, False otherwise """ return IMPL.setup_db() def drop_db(): """Drop database. Return True on success, False otherwise
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import cachetools import contextlib import threading from oslo_config import cfg from oslo_db import api as db_api _BACKEND_MAPPING = { 'sqlalchemy': 'mistral.db.v2.sqlalchemy.api', } IMPL = db_api.DBAPI('sqlalchemy', backend_mapping=_BACKEND_MAPPING) CONF = cfg.CONF _ACTION_DEF_CACHE = cachetools.TTLCache( maxsize=1000, ttl=CONF.engine.action_definition_cache_time # 60 seconds by default ) _ACTION_DEF_CACHE_LOCK = threading.RLock() def setup_db(): IMPL.setup_db()
def test_dbapi_lazy_loading(self): dbapi = api.DBAPI('oslo_db.tests.test_api', lazy=True) self.assertIsNone(dbapi._backend) dbapi.api_class_call1(1, 'abc') self.assertIsNotNone(dbapi._backend)
def test_dbapi_full_path_module_method(self): dbapi = api.DBAPI('oslo_db.tests.test_api') result = dbapi.api_class_call1(1, 2, kwarg1='meow') expected = ((1, 2), {'kwarg1': 'meow'}) self.assertEqual(expected, result)
def test_mocked_methods_are_not_wrapped(self, mocked_wrap, mocked_method): dbapi = api.DBAPI('oslo_db.tests.test_api') dbapi.api_class_call1() self.assertFalse(mocked_wrap.called)
def test_raise_connection_error(self): self.dbapi = api.DBAPI('sqlalchemy', {'sqlalchemy': __name__}) self.test_db_api.error_counter = 5 self.assertRaises(exception.DBConnectionError, self.dbapi._api_raise)
""" from oslo_config import cfg from oslo_db import api as db_api db_opts = [ cfg.StrOpt('db_backend', default='sqlalchemy', help='The backend to use for database.'), ] CONF = cfg.CONF CONF.register_opts(db_opts, "database") _BACKEND_MAPPING = {'sqlalchemy': 'delfin.db.sqlalchemy.api'} IMPL = db_api.DBAPI(CONF.database.db_backend, backend_mapping=_BACKEND_MAPPING, lazy=True) def register_db(): """Create database and tables.""" IMPL.register_db() def storage_get(context, storage_id): """Retrieve a storage device.""" return IMPL.storage_get(context, storage_id) def storage_get_all(context, marker=None,