From 5000a851d8f08c2bc6e1f8db1576a9d51abfebf7 Mon Sep 17 00:00:00 2001 From: Rakesh H S Date: Fri, 12 Sep 2014 15:46:28 +0530 Subject: [PATCH] handles keyboard interrupt When an user intentionally provides an keyboard interrupt, keystoneclient throws the entire traceback on to the terminal instead of handling it. keystoneclient will now handle the keyboard interrrupt and provides an crisp message on to the terminal. Change-Id: I1026d259fd0688dd2b950b1bdb135d219da2a60a Closes-Bug: #1367283 --- keystoneclient/shell.py | 4 +++- keystoneclient/tests/test_shell.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/keystoneclient/shell.py b/keystoneclient/shell.py index be7330ce0..16e3a2c91 100644 --- a/keystoneclient/shell.py +++ b/keystoneclient/shell.py @@ -461,7 +461,9 @@ class OpenStackHelpFormatter(argparse.HelpFormatter): def main(): try: OpenStackIdentityShell().main(sys.argv[1:]) - + except KeyboardInterrupt: + print("... terminating keystone client", file=sys.stderr) + sys.exit(130) except Exception as e: print(encodeutils.safe_encode(six.text_type(e)), file=sys.stderr) sys.exit(1) diff --git a/keystoneclient/tests/test_shell.py b/keystoneclient/tests/test_shell.py index 65cba9dbd..842aa4e67 100644 --- a/keystoneclient/tests/test_shell.py +++ b/keystoneclient/tests/test_shell.py @@ -520,3 +520,13 @@ class ShellTest(utils.TestCase): 'http://example.com:4321/go', 'http://example.com:9876/adm') self.assertTrue(all([x == y for x, y in zip(actual, expect)])) + + def test_shell_keyboard_interrupt(self): + shell_mock = mock.MagicMock() + with mock.patch('keystoneclient.shell.OpenStackIdentityShell.main', + shell_mock): + try: + shell_mock.side_effect = KeyboardInterrupt() + openstack_shell.main() + except SystemExit as ex: + self.assertEqual(130, ex.code)