From 99db3c83e09b8c1df25ec06fe99530aa088c325f Mon Sep 17 00:00:00 2001 From: Richard Avelar Date: Fri, 10 Feb 2017 16:33:33 +0000 Subject: [PATCH] Address db_sync check against new install This patch fixes a bug and causes a log message along with an exit code to be returned when a DBMigration error is raised. Change-Id: Iba7aff606937561ad98e2ef551ca4005bd4f337d Closes-Bug: #1663627 --- doc/source/upgrading.rst | 7 ++++--- keystone/cmd/cli.py | 16 ++++++++++++---- keystone/tests/unit/test_cli.py | 12 ++++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/doc/source/upgrading.rst b/doc/source/upgrading.rst index a6a3d61fd2..48831e772d 100644 --- a/doc/source/upgrading.rst +++ b/doc/source/upgrading.rst @@ -225,7 +225,7 @@ authenticate requests normally. the previous release. Using db_sync check -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~~~~ In order to check the current state of your rolling upgrades, you may run the command ``keystone-manage db_sync --check``. This will inform you of any @@ -239,8 +239,9 @@ can make from your current version. Here are a list of possible return codes. database and operator intervention will be required. * A return code of ``2`` means that an upgrade from your current database - version is available and your first step is to run ``keystone-manage - db_sync --expand``. + version is available, your database is not currently under version control, + or the database is already under control. Your first step is to run + ``keystone-manage db_sync --expand``. * A return code of ``3`` means that the expansion stage is complete, and the next step is to run ``keystone-manage db_sync --migrate``. diff --git a/keystone/cmd/cli.py b/keystone/cmd/cli.py index bfd04e8281..f6f3126769 100644 --- a/keystone/cmd/cli.py +++ b/keystone/cmd/cli.py @@ -21,6 +21,7 @@ import uuid import migrate from oslo_config import cfg +from oslo_db.sqlalchemy import migration from oslo_log import log from oslo_log import versionutils from oslo_serialization import jsonutils @@ -457,10 +458,17 @@ class DbSync(BaseApp): @classmethod def check_db_sync_status(self): status = 0 - expand_version = upgrades.get_db_version(repo='expand_repo') - migrate_version = upgrades.get_db_version( - repo='data_migration_repo') - contract_version = upgrades.get_db_version(repo='contract_repo') + try: + expand_version = upgrades.get_db_version(repo='expand_repo') + migrate_version = upgrades.get_db_version( + repo='data_migration_repo') + contract_version = upgrades.get_db_version(repo='contract_repo') + except migration.exception.DbMigrationError: + LOG.info(_LI('Your database is not currently under version ' + 'control or the database is already controlled. Your ' + 'first step is to run `keystone-manage db_sync ' + '--expand`.')) + return 2 repo = migrate.versioning.repository.Repository( upgrades.find_repo('expand_repo')) diff --git a/keystone/tests/unit/test_cli.py b/keystone/tests/unit/test_cli.py index 8dcfa2db36..86b8efaf2d 100644 --- a/keystone/tests/unit/test_cli.py +++ b/keystone/tests/unit/test_cli.py @@ -19,6 +19,7 @@ import uuid import fixtures import mock import oslo_config.fixture +from oslo_db.sqlalchemy import migration from oslo_log import log from oslotest import mockpatch from six.moves import configparser @@ -680,6 +681,17 @@ class CliDBSyncTestCase(unit.BaseTestCase): cli.DbSync.main() self._assert_correct_call(upgrades.contract_schema) + @mock.patch('keystone.cmd.cli.upgrades.get_db_version') + def test_db_sync_check_when_database_is_empty(self, mocked_get_db_version): + e = migration.exception.DbMigrationError("Invalid version") + mocked_get_db_version.side_effect = e + checker = cli.DbSync() + + log_info = self.useFixture(fixtures.FakeLogger(level=log.INFO)) + status = checker.check_db_sync_status() + self.assertIn("not currently under version control", log_info.output) + self.assertEqual(status, 2) + class TestMappingPopulate(unit.SQLDriverOverrides, unit.TestCase):