Fix cinder-manage <category> traceback

Running the cinder-manage command with a category but without an action
will throw a Traceback saying "oslo_config.cfg.NoSuchOptError: no such
option action_fn in group [DEFAULT]"

Example command that triggers this: cinder-manage backup

This patch fixes this by simulating that the user had used the --help
action while still exitting with an error (after all the call was
missing arguments).

New output:

  $ cinder-manage backup

  usage: cinder-manage backup [-h] {list,update_backup_host} ...

  positional arguments:
    {list,update_backup_host}

    optional arguments:
      -h, --help            show this help message and exit

Closes-Bug: #1902852
Change-Id: I6fca54d95b41d189545789c775e23223abb37c1b
This commit is contained in:
Gorka Eguileor 2020-11-04 11:32:11 +01:00
parent bb312b6141
commit fdc3711c95
3 changed files with 28 additions and 0 deletions

View File

@ -691,12 +691,20 @@ def methods_of(obj):
return result
def missing_action(help_func):
def wrapped():
help_func()
exit(2)
return wrapped
def add_command_parsers(subparsers):
for category in sorted(CATEGORIES):
command_object = CATEGORIES[category]()
parser = subparsers.add_parser(category)
parser.set_defaults(command_object=command_object)
parser.set_defaults(action_fn=missing_action(parser.print_help))
category_subparsers = parser.add_subparsers(dest='action')

View File

@ -1155,6 +1155,19 @@ class TestCinderManageCmd(test.TestCase):
self.assertTrue(register_cli_opt.called)
self.assertEqual(2, exit.code)
def test_main_missing_action(self):
sys.argv = ['cinder-manage', 'backup']
cinder_manage.CONF = cfg.ConfigOpts()
stdout = io.StringIO()
with mock.patch('sys.stdout', new=stdout):
exit = self.assertRaises(SystemExit, cinder_manage.main)
self.assertEqual(2, exit.code)
stdout.seek(0)
output = stdout.read()
self.assertTrue(output.startswith('usage: '))
@mock.patch('oslo_config.cfg.ConfigOpts.__call__')
@mock.patch('oslo_log.log.setup')
@mock.patch('oslo_config.cfg.ConfigOpts.register_cli_opt')

View File

@ -0,0 +1,7 @@
---
fixes:
- |
`Bug #1902852
<https://bugs.launchpad.net/python-cinderclient/+bug/1902852>`_:
Fixed throwing Python traceback message when using ``cinder-manage
<category>`` without an action for the category.