Catch IOError in cliutils

Catch always IOError in cliutils main method.
This simplifes output for end user and make it simpler to undrestand
that user specified wrong file.

Change-Id: I2e3bc7a9b5daff00c62ddf645fc2aaccae20f27b
This commit is contained in:
Boris Pavlovic 2014-09-23 08:48:53 +04:00
parent 265dfd9728
commit f171cfe236
2 changed files with 68 additions and 54 deletions

View File

@ -183,6 +183,11 @@ def run(argv, categories):
try: try:
ret = fn(*fn_args, **fn_kwargs) ret = fn(*fn_args, **fn_kwargs)
return(ret) return(ret)
except IOError as e:
if CONF.debug:
raise
print(e)
return 1
except Exception: except Exception:
print(_("Command failed, please check log for more info")) print(_("Command failed, please check log for more info"))
raise raise

View File

@ -31,6 +31,10 @@ CONF = cfg.CONF
class CliUtilsTestCase(test.TestCase): class CliUtilsTestCase(test.TestCase):
def tearDown(self):
self._unregister_opts()
super(CliUtilsTestCase, self).tearDown()
def test_pretty_float_formatter_rounding(self): def test_pretty_float_formatter_rounding(self):
test_table_rows = {"test_header": 6.56565} test_table_rows = {"test_header": 6.56565}
self.__dict__.update(**test_table_rows) self.__dict__.update(**test_table_rows)
@ -92,86 +96,91 @@ class CliUtilsTestCase(test.TestCase):
def _unregister_opts(self): def _unregister_opts(self):
CONF.reset() CONF.reset()
category_opt = cfg.SubCommandOpt('category', category_opt = cfg.SubCommandOpt("category",
title='Command categories', title="Command categories",
help='Available categories' help="Available categories"
) )
CONF.unregister_opt(category_opt) CONF.unregister_opt(category_opt)
@mock.patch('oslo.config.cfg.CONF', @mock.patch("oslo.config.cfg.CONF",
side_effect=cfg.ConfigFilesNotFoundError("config_file")) side_effect=cfg.ConfigFilesNotFoundError("config_file"))
@mock.patch('rally.cmd.cliutils.CONF', config_file=None) @mock.patch("rally.cmd.cliutils.CONF", config_file=None)
def test_run_fails(self, mock_cmd_cliutils_conf, mock_cliutils_conf): def test_run_fails(self, mock_cmd_cliutils_conf, mock_cliutils_conf):
categories = { categories = {
'deployment': deployment.DeploymentCommands, "deployment": deployment.DeploymentCommands,
'info': info.InfoCommands, "info": info.InfoCommands,
'show': show.ShowCommands, "show": show.ShowCommands,
'task': task.TaskCommands, "task": task.TaskCommands,
'use': use.UseCommands, "use": use.UseCommands,
'verify': verify.VerifyCommands} "verify": verify.VerifyCommands}
ret = cliutils.run(['rally', 'show', 'flavors'], categories) ret = cliutils.run(["rally", "show", "flavors"], categories)
self._unregister_opts()
self.assertEqual(ret, 2) self.assertEqual(ret, 2)
def test_run_version(self): def test_run_version(self):
categories = { categories = {
'deployment': deployment.DeploymentCommands, "deployment": deployment.DeploymentCommands,
'info': info.InfoCommands, "info": info.InfoCommands,
'show': show.ShowCommands, "show": show.ShowCommands,
'task': task.TaskCommands, "task": task.TaskCommands,
'use': use.UseCommands, "use": use.UseCommands,
'verify': verify.VerifyCommands} "verify": verify.VerifyCommands}
ret = cliutils.run(['rally', 'version'], categories) ret = cliutils.run(["rally", "version"], categories)
self._unregister_opts()
self.assertEqual(ret, 0) self.assertEqual(ret, 0)
def test_run_bash_completion(self): def test_run_bash_completion(self):
categories = { categories = {
'deployment': deployment.DeploymentCommands, "deployment": deployment.DeploymentCommands,
'info': info.InfoCommands, "info": info.InfoCommands,
'show': show.ShowCommands, "show": show.ShowCommands,
'task': task.TaskCommands, "task": task.TaskCommands,
'use': use.UseCommands, "use": use.UseCommands,
'verify': verify.VerifyCommands} "verify": verify.VerifyCommands}
ret = cliutils.run(['rally', 'bash-completion'], categories) ret = cliutils.run(["rally", "bash-completion"], categories)
self._unregister_opts()
self.assertEqual(ret, 0) self.assertEqual(ret, 0)
def test_run_bash_completion_with_query_category(self): def test_run_bash_completion_with_query_category(self):
categories = { categories = {
'deployment': deployment.DeploymentCommands, "deployment": deployment.DeploymentCommands,
'info': info.InfoCommands, "info": info.InfoCommands,
'show': show.ShowCommands, "show": show.ShowCommands,
'task': task.TaskCommands, "task": task.TaskCommands,
'use': use.UseCommands, "use": use.UseCommands,
'verify': verify.VerifyCommands} "verify": verify.VerifyCommands}
ret = cliutils.run(['rally', 'bash-completion', 'info'], categories) ret = cliutils.run(["rally", "bash-completion", "info"], categories)
self._unregister_opts()
self.assertEqual(ret, 0) self.assertEqual(ret, 0)
def test_run_show(self): def test_run_show(self):
categories = { categories = {
'deployment': deployment.DeploymentCommands, "deployment": deployment.DeploymentCommands,
'info': info.InfoCommands, "info": info.InfoCommands,
'show': show.ShowCommands, "show": show.ShowCommands,
'task': task.TaskCommands, "task": task.TaskCommands,
'use': use.UseCommands, "use": use.UseCommands,
'verify': verify.VerifyCommands} "verify": verify.VerifyCommands}
ret = cliutils.run(['rally', 'show', 'keypairs'], categories) ret = cliutils.run(["rally", "show", "keypairs"], categories)
self._unregister_opts()
self.assertEqual(ret, 1) self.assertEqual(ret, 1)
@mock.patch('rally.openstack.common.cliutils.validate_args', @mock.patch("rally.openstack.common.cliutils.validate_args",
side_effect=exceptions.MissingArgs("missing")) side_effect=exceptions.MissingArgs("missing"))
def test_run_show_fails(self, mock_validate_args): def test_run_show_fails(self, mock_validate_args):
categories = { categories = {
'deployment': deployment.DeploymentCommands, "deployment": deployment.DeploymentCommands,
'info': info.InfoCommands, "info": info.InfoCommands,
'show': show.ShowCommands, "show": show.ShowCommands,
'task': task.TaskCommands, "task": task.TaskCommands,
'use': use.UseCommands, "use": use.UseCommands,
'verify': verify.VerifyCommands} "verify": verify.VerifyCommands}
ret = cliutils.run(['rally', 'show', 'keypairs'], categories) ret = cliutils.run(["rally", "show", "keypairs"], categories)
self.assertTrue(mock_validate_args.called) self.assertTrue(mock_validate_args.called)
self._unregister_opts()
self.assertEqual(ret, 1) self.assertEqual(ret, 1)
def test_run_failed_to_open_file(self):
class FailuresCommands(object):
def failed_to_open_file(self):
raise IOError("No such file")
ret = cliutils.run(["rally", "failure", "failed_to_open_file"],
{"failure": FailuresCommands})
self.assertEqual(1, ret)