Switch to using enginefacade
Closes-Bug: #2067345 Change-Id: If9a2c96628cfcb819fee5e19f872ea015979b30f
This commit is contained in:
parent
8d09169a8a
commit
0ce2c41404
@ -12,6 +12,7 @@
|
||||
|
||||
from eventlet.green import threading
|
||||
from oslo_context import context
|
||||
from oslo_db.sqlalchemy import enginefacade
|
||||
|
||||
from magnum.common import policy
|
||||
|
||||
@ -20,6 +21,7 @@ import magnum.conf
|
||||
CONF = magnum.conf.CONF
|
||||
|
||||
|
||||
@enginefacade.transaction_context_provider
|
||||
class RequestContext(context.RequestContext):
|
||||
"""Extends security contexts from the OpenStack common library."""
|
||||
|
||||
|
@ -13,8 +13,8 @@
|
||||
from logging import config as log_config
|
||||
|
||||
from alembic import context
|
||||
from oslo_db.sqlalchemy import enginefacade
|
||||
|
||||
from magnum.db.sqlalchemy import api as sqla_api
|
||||
from magnum.db.sqlalchemy import models
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
@ -43,7 +43,7 @@ def run_migrations_online():
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
engine = sqla_api.get_engine()
|
||||
engine = enginefacade.writer.get_engine()
|
||||
with engine.connect() as connection:
|
||||
context.configure(connection=connection,
|
||||
target_metadata=target_metadata)
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -87,15 +87,6 @@ class MagnumBase(models.TimestampMixin,
|
||||
d[c.name] = self[c.name]
|
||||
return d
|
||||
|
||||
def save(self, session=None):
|
||||
import magnum.db.sqlalchemy.api as db_api
|
||||
|
||||
if session is None:
|
||||
session = db_api.get_session()
|
||||
|
||||
with session.begin():
|
||||
super(MagnumBase, self).save(session)
|
||||
|
||||
|
||||
Base = declarative_base(cls=MagnumBase)
|
||||
|
||||
|
@ -16,10 +16,10 @@
|
||||
"""Magnum DB test base class."""
|
||||
|
||||
import fixtures
|
||||
from oslo_db.sqlalchemy import enginefacade
|
||||
|
||||
import magnum.conf
|
||||
from magnum.db import api as dbapi
|
||||
from magnum.db.sqlalchemy import api as sqla_api
|
||||
from magnum.db.sqlalchemy import migration
|
||||
from magnum.db.sqlalchemy import models
|
||||
from magnum.tests import base
|
||||
@ -32,16 +32,15 @@ _DB_CACHE = None
|
||||
|
||||
class Database(fixtures.Fixture):
|
||||
|
||||
def __init__(self, db_api, db_migrate, sql_connection):
|
||||
def __init__(self, engine, db_migrate, sql_connection):
|
||||
self.sql_connection = sql_connection
|
||||
|
||||
self.engine = db_api.get_engine()
|
||||
self.engine = engine
|
||||
self.engine.dispose()
|
||||
conn = self.engine.connect()
|
||||
self.setup_sqlite(db_migrate)
|
||||
self.post_migrations()
|
||||
|
||||
self._DB = "".join(line for line in conn.connection.iterdump())
|
||||
with self.engine.connect() as conn:
|
||||
self.setup_sqlite(db_migrate)
|
||||
self.post_migrations()
|
||||
self._DB = "".join(line for line in conn.connection.iterdump())
|
||||
self.engine.dispose()
|
||||
|
||||
def setup_sqlite(self, db_migrate):
|
||||
@ -50,9 +49,10 @@ class Database(fixtures.Fixture):
|
||||
models.Base.metadata.create_all(self.engine)
|
||||
db_migrate.stamp('head')
|
||||
|
||||
def _setUp(self):
|
||||
conn = self.engine.connect()
|
||||
conn.connection.executescript(self._DB)
|
||||
def setUp(self):
|
||||
super(Database, self).setUp()
|
||||
with self.engine.connect() as conn:
|
||||
conn.connection.executescript(self._DB)
|
||||
self.addCleanup(self.engine.dispose)
|
||||
|
||||
def post_migrations(self):
|
||||
@ -68,6 +68,8 @@ class DbTestCase(base.TestCase):
|
||||
|
||||
global _DB_CACHE
|
||||
if not _DB_CACHE:
|
||||
_DB_CACHE = Database(sqla_api, migration,
|
||||
engine = enginefacade.writer.get_engine()
|
||||
_DB_CACHE = Database(engine, migration,
|
||||
sql_connection=CONF.database.connection)
|
||||
engine.dispose()
|
||||
self.useFixture(_DB_CACHE)
|
||||
|
@ -26,16 +26,22 @@ class SqlAlchemyCustomTypesTestCase(base.DbTestCase):
|
||||
# Create ClusterTemplate w/o labels
|
||||
cluster_template1_id = uuidutils.generate_uuid()
|
||||
self.dbapi.create_cluster_template({'uuid': cluster_template1_id})
|
||||
cluster_template1 = sa_api.model_query(
|
||||
models.ClusterTemplate).filter_by(uuid=cluster_template1_id).one()
|
||||
with sa_api._session_for_read() as session:
|
||||
cluster_template1 = (session.query(
|
||||
models.ClusterTemplate)
|
||||
.filter_by(uuid=cluster_template1_id)
|
||||
.one())
|
||||
self.assertEqual({}, cluster_template1.labels)
|
||||
|
||||
# Create ClusterTemplate with labels
|
||||
cluster_template2_id = uuidutils.generate_uuid()
|
||||
self.dbapi.create_cluster_template(
|
||||
{'uuid': cluster_template2_id, 'labels': {'bar': 'foo'}})
|
||||
cluster_template2 = sa_api.model_query(
|
||||
models.ClusterTemplate).filter_by(uuid=cluster_template2_id).one()
|
||||
with sa_api._session_for_read() as session:
|
||||
cluster_template2 = (session.query(
|
||||
models.ClusterTemplate)
|
||||
.filter_by(uuid=cluster_template2_id)
|
||||
.one())
|
||||
self.assertEqual('foo', cluster_template2.labels['bar'])
|
||||
|
||||
def test_JSONEncodedDict_type_check(self):
|
||||
@ -48,8 +54,9 @@ class SqlAlchemyCustomTypesTestCase(base.DbTestCase):
|
||||
# Create nodegroup w/o node_addresses
|
||||
nodegroup1_id = uuidutils.generate_uuid()
|
||||
self.dbapi.create_nodegroup({'uuid': nodegroup1_id})
|
||||
nodegroup1 = sa_api.model_query(
|
||||
models.NodeGroup).filter_by(uuid=nodegroup1_id).one()
|
||||
with sa_api._session_for_read() as session:
|
||||
nodegroup1 = session.query(
|
||||
models.NodeGroup).filter_by(uuid=nodegroup1_id).one()
|
||||
self.assertEqual([], nodegroup1.node_addresses)
|
||||
|
||||
# Create nodegroup with node_addresses
|
||||
@ -59,8 +66,9 @@ class SqlAlchemyCustomTypesTestCase(base.DbTestCase):
|
||||
'node_addresses': ['mynode_address1',
|
||||
'mynode_address2']
|
||||
})
|
||||
nodegroup2 = sa_api.model_query(
|
||||
models.NodeGroup).filter_by(uuid=nodegroup2_id).one()
|
||||
with sa_api._session_for_read() as session:
|
||||
nodegroup2 = session.query(
|
||||
models.NodeGroup).filter_by(uuid=nodegroup2_id).one()
|
||||
self.assertEqual(['mynode_address1', 'mynode_address2'],
|
||||
nodegroup2.node_addresses)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user