From 7edf7ff66dd16d716c7b4935738ccb15f0a83aaf Mon Sep 17 00:00:00 2001 From: jiangpch Date: Sun, 22 Oct 2017 23:17:35 -0400 Subject: [PATCH] Refactor exception handling in cmd.api Replace the known exception tuple with a map of exception classes to error codes to preserve backward compatibility of exit codes. Also change the code to handle unknown members of the Exception hierarchy without breaking. Co-authored-by: jiangpch Co-authored-by: Brian Rosmaita Closes-Bug: #1726213 Change-Id: Iabfc2ded45a576a18bdb30a6c3ada8b9799a3196 --- glance/cmd/api.py | 17 +++++---- glance/tests/unit/api/test_cmd.py | 58 +++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/glance/cmd/api.py b/glance/cmd/api.py index 4ff5071288..89fe1b730b 100644 --- a/glance/cmd/api.py +++ b/glance/cmd/api.py @@ -56,16 +56,19 @@ CONF = cfg.CONF CONF.import_group("profiler", "glance.common.wsgi") logging.register_options(CONF) -KNOWN_EXCEPTIONS = (RuntimeError, - exception.WorkerCreationFailure, - glance_store.exceptions.BadStoreConfiguration, - ValueError) +# NOTE(rosmaita): Any new exceptions added should preserve the current +# error codes for backward compatibility. The value 99 is returned +# for errors not listed in this map. +ERROR_CODE_MAP = {RuntimeError: 1, + exception.WorkerCreationFailure: 2, + glance_store.exceptions.BadStoreConfiguration: 3, + ValueError: 4, + cfg.ConfigFileValueError: 5} def fail(e): - global KNOWN_EXCEPTIONS - return_code = KNOWN_EXCEPTIONS.index(type(e)) + 1 sys.stderr.write("ERROR: %s\n" % encodeutils.exception_to_unicode(e)) + return_code = ERROR_CODE_MAP.get(type(e), 99) sys.exit(return_code) @@ -89,7 +92,7 @@ def main(): server = wsgi.Server(initialize_glance_store=True) server.start(config.load_paste_app('glance-api'), default_port=9292) server.wait() - except KNOWN_EXCEPTIONS as e: + except Exception as e: fail(e) diff --git a/glance/tests/unit/api/test_cmd.py b/glance/tests/unit/api/test_cmd.py index 3582da0fab..21da156f3d 100644 --- a/glance/tests/unit/api/test_cmd.py +++ b/glance/tests/unit/api/test_cmd.py @@ -132,3 +132,61 @@ class TestGlanceApiCmd(test_utils.BaseTestCase): self._raise(RuntimeError)) exit = self.assertRaises(SystemExit, glance.cmd.cache_pruner.main) self.assertEqual('ERROR: ', exit.code) + + def test_fail_with_value_error(self): + with mock.patch('sys.stderr.write') as mock_stderr: + with mock.patch('sys.exit') as mock_exit: + exc_msg = 'A ValueError, LOL!' + exc = ValueError(exc_msg) + glance.cmd.api.fail(exc) + mock_stderr.assert_called_once_with('ERROR: %s\n' % exc_msg) + mock_exit.assert_called_once_with(4) + + def test_fail_with_config_exception(self): + with mock.patch('sys.stderr.write') as mock_stderr: + with mock.patch('sys.exit') as mock_exit: + exc_msg = 'A ConfigError by George!' + exc = cfg.ConfigFileValueError(exc_msg) + glance.cmd.api.fail(exc) + mock_stderr.assert_called_once_with('ERROR: %s\n' % exc_msg) + mock_exit.assert_called_once_with(5) + + def test_fail_with_unknown_exception(self): + with mock.patch('sys.stderr.write') as mock_stderr: + with mock.patch('sys.exit') as mock_exit: + exc_msg = 'A Crazy Unkown Error.' + exc = CrayCray(exc_msg) + glance.cmd.api.fail(exc) + mock_stderr.assert_called_once_with('ERROR: %s\n' % exc_msg) + mock_exit.assert_called_once_with(99) + + def test_main_with_store_config_exception(self): + with mock.patch.object(glance.common.config, + 'parse_args') as mock_config: + with mock.patch('sys.exit') as mock_exit: + exc = store.exceptions.BadStoreConfiguration() + mock_config.side_effect = exc + glance.cmd.api.main() + mock_exit.assert_called_once_with(3) + + def test_main_with_runtime_error(self): + with mock.patch.object(glance.common.config, + 'parse_args') as mock_config: + with mock.patch('sys.exit') as mock_exit: + exc = RuntimeError() + mock_config.side_effect = exc + glance.cmd.api.main() + mock_exit.assert_called_once_with(1) + + def test_main_with_worker_creation_failure(self): + with mock.patch.object(glance.common.config, + 'parse_args') as mock_config: + with mock.patch('sys.exit') as mock_exit: + exx = exc.WorkerCreationFailure() + mock_config.side_effect = exx + glance.cmd.api.main() + mock_exit.assert_called_once_with(2) + + +class CrayCray(Exception): + pass