From 96ff6e46c4e5c476b42399d9df016a468b2e1b6d Mon Sep 17 00:00:00 2001 From: Rakesh H S Date: Thu, 25 Sep 2014 11:19:31 +0530 Subject: [PATCH] Return 130 for keyboard interrupt When keyboard interrupt is received by glanceclient, the return code as of now is 1. But since the client was terminated by an keyboard interrupt, the return code should be 130. (http://tldp.org/LDP/abs/html/exitcodes.html) It is useful when people are writing automation test cases and want to validate based on the return code. Change-Id: Ia70116ab6f0708a0ce6eeaed07c1e7a56e68c9f4 Closes-Bug: #1373231 --- glanceclient/common/utils.py | 4 ++-- glanceclient/shell.py | 2 +- tests/test_shell.py | 9 +++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py index 5841ffe..6d4529d 100644 --- a/glanceclient/common/utils.py +++ b/glanceclient/common/utils.py @@ -234,10 +234,10 @@ def import_versioned_module(version, submodule=None): return importutils.import_module(module) -def exit(msg=''): +def exit(msg='', exit_code=1): if msg: print(encodeutils.safe_decode(msg), file=sys.stderr) - sys.exit(1) + sys.exit(exit_code) def save_image(data, path): diff --git a/glanceclient/shell.py b/glanceclient/shell.py index b52361d..53b1c3a 100644 --- a/glanceclient/shell.py +++ b/glanceclient/shell.py @@ -697,6 +697,6 @@ def main(): try: OpenStackImagesShell().main(map(encodeutils.safe_decode, sys.argv[1:])) except KeyboardInterrupt: - utils.exit('... terminating glance client') + utils.exit('... terminating glance client', exit_code=130) except Exception as e: utils.exit(utils.exception_to_str(e)) diff --git a/tests/test_shell.py b/tests/test_shell.py index 0327db7..2721655 100644 --- a/tests/test_shell.py +++ b/tests/test_shell.py @@ -289,6 +289,15 @@ class ShellTest(utils.TestCase): self.assertEqual('mydomain', kwargs['project_domain_name']) self.assertEqual('myid', kwargs['project_domain_id']) + @mock.patch.object(openstack_shell.OpenStackImagesShell, 'main') + def test_shell_keyboard_interrupt(self, mock_glance_shell): + # Ensure that exit code is 130 for KeyboardInterrupt + try: + mock_glance_shell.side_effect = KeyboardInterrupt() + openstack_shell.main() + except SystemExit as ex: + self.assertEqual(130, ex.code) + class ShellTestWithKeystoneV3Auth(ShellTest): # auth environment to use