From 3d2a3bc23fc360c0532030ded68c008b21e5e4fd Mon Sep 17 00:00:00 2001 From: Ben Nemec Date: Thu, 13 Sep 2018 17:24:57 +0000 Subject: [PATCH] Handle exceptions from checks The original check code in Nova behaves this way, so for consistency do it here too. --- oslo_upgradecheck/tests/test_upgradecheck.py | 8 ++++++++ oslo_upgradecheck/upgradecheck.py | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/oslo_upgradecheck/tests/test_upgradecheck.py b/oslo_upgradecheck/tests/test_upgradecheck.py index a89ace9..7de1c5e 100644 --- a/oslo_upgradecheck/tests/test_upgradecheck.py +++ b/oslo_upgradecheck/tests/test_upgradecheck.py @@ -79,3 +79,11 @@ class TestMain(base.BaseTestCase): inst = TestCommands() result = upgradecheck.main(inst.check) self.assertEqual(upgradecheck.UpgradeCheckCode.FAILURE, result) + + def test_main_exception(self): + def raises(): + raise Exception('test exception') + mock_argv = ['test-status', 'upgrade', 'check'] + with mock.patch.object(sys, 'argv', mock_argv, create=True): + result = upgradecheck.main(raises) + self.assertEqual(255, result) diff --git a/oslo_upgradecheck/upgradecheck.py b/oslo_upgradecheck/upgradecheck.py index 4bc271d..f28bce1 100644 --- a/oslo_upgradecheck/upgradecheck.py +++ b/oslo_upgradecheck/upgradecheck.py @@ -16,6 +16,7 @@ import functools import sys import textwrap +import traceback import enum from oslo_config import cfg @@ -148,4 +149,9 @@ def main(check_callback): conf.register_cli_opt(opt) conf(sys.argv[1:]) - return conf.category.action_fn() + try: + return conf.category.action_fn() + except Exception: + print(_('Error:\n%s') % traceback.format_exc()) + # This is 255 so it's not confused with the upgrade check exit codes. + return 255