diff options
author | Jenkins <jenkins@review.openstack.org> | 2016-04-18 17:04:57 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2016-04-18 17:04:57 +0000 |
commit | 12e92558e50d0aa200d2ca0f98a7110e0adce922 (patch) | |
tree | e1a20f59370af9de196a362ac0cee70cf6d209e9 | |
parent | cad0245885edc63de74b122541bd287d9c74af08 (diff) | |
parent | d0ec3a7ebb6add2e96c4a06231939baf56e2139a (diff) | |
download | python-glanceclient-12e92558e50d0aa200d2ca0f98a7110e0adce922.tar.gz |
Merge "Re-enable stacktracing when --debug is used"
-rw-r--r-- | glanceclient/common/utils.py | 8 | ||||
-rwxr-xr-x | glanceclient/shell.py | 14 | ||||
-rw-r--r-- | glanceclient/tests/unit/test_shell.py | 39 |
3 files changed, 49 insertions, 12 deletions
diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py index 7c88ff9..c7dd4dd 100644 --- a/glanceclient/common/utils.py +++ b/glanceclient/common/utils.py @@ -467,6 +467,14 @@ def endpoint_version_from_url(endpoint, default_version=None): return None, default_version +def debug_enabled(argv): + if bool(env('GLANCECLIENT_DEBUG')) is True: + return True + if '--debug' in argv or '-d' in argv: + return True + return False + + class IterableWithLength(object): def __init__(self, iterable, length): self.iterable = iterable diff --git a/glanceclient/shell.py b/glanceclient/shell.py index 1a5f20f..2a77b69 100755 --- a/glanceclient/shell.py +++ b/glanceclient/shell.py @@ -490,18 +490,12 @@ class OpenStackImagesShell(object): try: return self.get_subcommand_parser(api_version) except ImportError as e: - if options.debug: - traceback.print_exc() if not str(e): # Add a generic import error message if the raised # ImportError has none. raise ImportError('Unable to import module. Re-run ' 'with --debug for more info.') raise - except Exception: - if options.debug: - traceback.print_exc() - raise # Parse args once to find version @@ -603,12 +597,6 @@ class OpenStackImagesShell(object): args.func(client, args) except exc.Unauthorized: raise exc.CommandError("Invalid OpenStack Identity credentials.") - except Exception: - # NOTE(kragniz) Print any exceptions raised to stderr if the - # --debug flag is set - if args.debug: - traceback.print_exc() - raise finally: if profile: trace_id = osprofiler_profiler.get().get_base_id() @@ -679,4 +667,6 @@ def main(): except KeyboardInterrupt: utils.exit('... terminating glance client', exit_code=130) except Exception as e: + if utils.debug_enabled(argv) is True: + traceback.print_exc() utils.exit(encodeutils.exception_to_unicode(e)) diff --git a/glanceclient/tests/unit/test_shell.py b/glanceclient/tests/unit/test_shell.py index 3f6b095..d78eaca 100644 --- a/glanceclient/tests/unit/test_shell.py +++ b/glanceclient/tests/unit/test_shell.py @@ -23,6 +23,7 @@ import hashlib import logging import os import sys +import traceback import uuid import fixtures @@ -151,6 +152,44 @@ class ShellTest(testutils.TestCase): argstr = '--os-image-api-version 2 help foofoo' self.assertRaises(exc.CommandError, shell.main, argstr.split()) + @mock.patch('sys.stdout', six.StringIO()) + @mock.patch('sys.stderr', six.StringIO()) + @mock.patch('sys.argv', ['glance', 'help', 'foofoo']) + def test_no_stacktrace_when_debug_disabled(self): + with mock.patch.object(traceback, 'print_exc') as mock_print_exc: + try: + openstack_shell.main() + except SystemExit: + pass + self.assertFalse(mock_print_exc.called) + + @mock.patch('sys.stdout', six.StringIO()) + @mock.patch('sys.stderr', six.StringIO()) + @mock.patch('sys.argv', ['glance', 'help', 'foofoo']) + def test_stacktrace_when_debug_enabled_by_env(self): + old_environment = os.environ.copy() + os.environ = {'GLANCECLIENT_DEBUG': '1'} + try: + with mock.patch.object(traceback, 'print_exc') as mock_print_exc: + try: + openstack_shell.main() + except SystemExit: + pass + self.assertTrue(mock_print_exc.called) + finally: + os.environ = old_environment + + @mock.patch('sys.stdout', six.StringIO()) + @mock.patch('sys.stderr', six.StringIO()) + @mock.patch('sys.argv', ['glance', '--debug', 'help', 'foofoo']) + def test_stacktrace_when_debug_enabled(self): + with mock.patch.object(traceback, 'print_exc') as mock_print_exc: + try: + openstack_shell.main() + except SystemExit: + pass + self.assertTrue(mock_print_exc.called) + def test_help(self): shell = openstack_shell.OpenStackImagesShell() argstr = '--os-image-api-version 2 help' |