]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix test_external_tables_not_changed
authorAnn Kamyshnikova <akamyshnikova@mirantis.com>
Tue, 1 Sep 2015 12:15:53 +0000 (15:15 +0300)
committerAnn Kamyshnikova <akamyshnikova@mirantis.com>
Wed, 9 Sep 2015 11:53:44 +0000 (14:53 +0300)
test_external_tables_not_changed was not
executed properly as new engine was created in env.py.

Related-bug: #1466704

Change-Id: If02415d7abd17024946f7aee8fb6abc374a7aefe

neutron/db/migration/alembic_migrations/env.py
neutron/tests/functional/db/test_migrations.py

index 834b9ac5f80856ff05721c7a5bcebc4469bb42c9..87dfa9eabb8dd16914aae7c0719563d9be5f459a 100644 (file)
@@ -103,9 +103,11 @@ def run_migrations_online():
 
     """
     set_mysql_engine()
-    engine = session.create_engine(neutron_config.database.connection)
-
-    connection = engine.connect()
+    connection = config.attributes.get('connection')
+    new_engine = connection is None
+    if new_engine:
+        engine = session.create_engine(neutron_config.database.connection)
+        connection = engine.connect()
     context.configure(
         connection=connection,
         target_metadata=target_metadata,
@@ -116,8 +118,9 @@ def run_migrations_online():
         with context.begin_transaction():
             context.run_migrations()
     finally:
-        connection.close()
-        engine.dispose()
+        if new_engine:
+            connection.close()
+            engine.dispose()
 
 
 if context.is_offline_mode():
index b8e476073fd03beee91c2ed73e6b4d8978f80b3f..959c5c115055e929db4004ed525b16dcf15d4590 100644 (file)
@@ -20,6 +20,7 @@ import alembic
 import alembic.autogenerate
 import alembic.migration
 from alembic import script as alembic_script
+from contextlib import contextmanager
 import mock
 from oslo_config import cfg
 from oslo_config import fixture as config_fixture
@@ -207,6 +208,14 @@ class _TestModelsMigrations(test_migrations.ModelsMigrationsSync):
 
 class TestModelsMigrationsMysql(_TestModelsMigrations,
                                 base.MySQLTestCase):
+    @contextmanager
+    def _listener(self, engine, listener_func):
+        try:
+            event.listen(engine, 'before_execute', listener_func)
+            yield
+        finally:
+            event.remove(engine, 'before_execute',
+                         listener_func)
 
     # There is no use to run this against both dialects, so add this test just
     # for MySQL tests
@@ -222,20 +231,30 @@ class TestModelsMigrationsMysql(_TestModelsMigrations,
                           "migration.")
 
             if hasattr(clauseelement, 'element'):
-                if (clauseelement.element.name in external.TABLES or
+                element = clauseelement.element
+                if (element.name in external.TABLES or
                         (hasattr(clauseelement, 'table') and
-                         clauseelement.element.table.name in external.TABLES)):
+                            element.table.name in external.TABLES)):
+                    # Table 'nsxv_vdr_dhcp_bindings' was created in liberty,
+                    # before NSXV has moved to separate repo.
+                    if ((isinstance(clauseelement,
+                                    sqlalchemy.sql.ddl.CreateTable) and
+                            element.name == 'nsxv_vdr_dhcp_bindings')):
+                        return
                     self.fail("External table referenced by neutron core "
                               "migration.")
 
         engine = self.get_engine()
         cfg.CONF.set_override('connection', engine.url, group='database')
-        migration.do_alembic_command(self.alembic_config, 'upgrade', 'kilo')
-
-        event.listen(engine, 'before_execute', block_external_tables)
-        migration.do_alembic_command(self.alembic_config, 'upgrade', 'heads')
-
-        event.remove(engine, 'before_execute', block_external_tables)
+        with engine.begin() as connection:
+            self.alembic_config.attributes['connection'] = connection
+            migration.do_alembic_command(self.alembic_config, 'upgrade',
+                                         'kilo')
+
+            with self._listener(engine,
+                                block_external_tables):
+                migration.do_alembic_command(self.alembic_config, 'upgrade',
+                                             'heads')
 
 
 class TestModelsMigrationsPsql(_TestModelsMigrations,