From 7c79e2115ad5fb6b7b3fa9412a5b7f34527bbad5 Mon Sep 17 00:00:00 2001 From: Gorka Eguileor Date: Tue, 7 Feb 2023 13:05:23 +0100 Subject: [PATCH] Fix Migrations UTs using wrong DB Our current migrations unit tests are NOT doing the migrations in the DB they should. Migrations are being run in whatever was last used for the normal cinder UTs, which is SQLite. This creates some issues such as: - Migrations can not be run independently, because no "normal cinder UT" has been run before, so there is no DB that the migration can use. This is easy to test: $ . .tox/py310/bin/activate $ stestr run -n cinder.tests.unit.db.test_migrations That will fail with oslo_db.exception.DBNonExistentDatabase because no previous test has created the sqlite DB. If we were to run any other UT before them they would work. - Migrations that run conditional code based on the DB Engine will fail, even when running things conditionally. For example a migration that uses `index_exists` that only works on MySQL will fail even when doing it conditionally and only running it on MySQL, because it will actually be run in SQLite: is_mysql = engine.dialect.name == 'mysql' idx_name = f'{table}_deleted_project_id_idx' + if is_mysql and utils.index_exists(engine, table, idx_name): This patch fixes this by making sure the get_engine method in Cinder returns the same engine that the migration code is using. Change-Id: I15f6e4bd180e9a5af82c76d61658a3cb1eac22c8 --- cinder/tests/unit/db/test_migrations.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cinder/tests/unit/db/test_migrations.py b/cinder/tests/unit/db/test_migrations.py index bf9a1038b66..612b9caef0c 100644 --- a/cinder/tests/unit/db/test_migrations.py +++ b/cinder/tests/unit/db/test_migrations.py @@ -34,6 +34,7 @@ from sqlalchemy.engine import reflection import cinder.db.legacy_migrations from cinder.db import migration +from cinder.db.sqlalchemy import api from cinder.db.sqlalchemy import models from cinder.tests import fixtures as cinder_fixtures from cinder.tests.unit import utils as test_utils @@ -58,6 +59,7 @@ class CinderModelsMigrationsSync(test_migrations.ModelsMigrationsSync): self.useFixture(cinder_fixtures.StandardLogging()) self.engine = enginefacade.writer.get_engine() + self.patch(api, 'get_engine', self.get_engine) def db_sync(self, engine): migration.db_sync(engine=self.engine) @@ -140,6 +142,7 @@ class MigrationsWalk( def setUp(self): super().setUp() self.engine = enginefacade.writer.get_engine() + self.patch(api, 'get_engine', lambda: self.engine) self.config = migration._find_alembic_conf() self.init_version = migration.ALEMBIC_INIT_VERSION