summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStuart McLaren <stuart.mclaren@hp.com>2016-03-30 11:56:56 +0000
committerStuart McLaren <stuart.mclaren@hp.com>2016-03-30 12:49:42 +0000
commit9fc5d56eebfefd0897dc4a0298d862ef12d3d4b2 (patch)
tree0fe2ac6f5313b32d106b11eb60882a035d1a7009
parentd0882d18c208991f7fee3eab2b3c57b9b5a49b20 (diff)
downloadpython-glanceclient-9fc5d56eebfefd0897dc4a0298d862ef12d3d4b2.tar.gz
Re-enable stacktracing when --debug is used
Commit 1f89beb6098f4f6a8d8c2912392b273bc068b2e3 introduced the behaviour that a stacktrace is printed if an exception is encountered. This helped make the client more supportable: $ glance --debug image-list . . . File "glanceclient/common/http.py", line 337, in get_http_client xxx NameError: global name 'xxx' is not defined global name 'xxx' is not defined The behaviour was lost at some point. This patch re-enables it. Change-Id: I25fc8624797909d606590747f54b9cf649ade079 Closes-bug: 1563830 (cherry picked from commit ca0989c52376a1d499cd0cc176f60b7ff8257275)
-rwxr-xr-xglanceclient/shell.py12
-rw-r--r--glanceclient/tests/unit/test_shell.py23
2 files changed, 25 insertions, 10 deletions
diff --git a/glanceclient/shell.py b/glanceclient/shell.py
index 86e6107..fba6583 100755
--- a/glanceclient/shell.py
+++ b/glanceclient/shell.py
@@ -489,8 +489,6 @@ 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.
@@ -498,8 +496,6 @@ class OpenStackImagesShell(object):
'with --debug for more info.')
raise
except Exception:
- if options.debug:
- traceback.print_exc()
raise
# Parse args once to find version
@@ -595,12 +591,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()
@@ -671,4 +661,6 @@ def main():
except KeyboardInterrupt:
utils.exit('... terminating glance client', exit_code=130)
except Exception as e:
+ if '--debug' in sys.argv[1:]:
+ 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 2a33dc0..da24e6b 100644
--- a/glanceclient/tests/unit/test_shell.py
+++ b/glanceclient/tests/unit/test_shell.py
@@ -22,6 +22,7 @@ except ImportError:
import hashlib
import os
import sys
+import traceback
import uuid
import fixtures
@@ -150,6 +151,28 @@ 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', '--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'