Python3.11: Fix argparse-related test failures

In Python3.11, the following code crashes:

$ cat test.py
import argparse

parser = argparse.ArgumentParser(description='Short sample app')
subparsers = parser.add_subparsers()
subparsers.add_parser('foo')
subparsers.add_parser('foo')

$ python3.11 test.py
Traceback (most recent call last):
  File "/tmp/arg.py", line 6, in <module>
    subparsers.add_parser('foo')
  File "/usr/lib/python3.11/argparse.py", line 1197, in add_parser
    raise ArgumentError(self, _('conflicting subparser: %s') % name)
argparse.ArgumentError: argument {foo}: conflicting subparser: foo

It is now forbidden to use add_parser() multiple times with the same
arguments, which is exactly what we do in the following tests:

- cinderclient.tests.unit.test_shell.TestLoadVersionedActions.test_load_versioned_actions
- cinderclient.tests.unit.test_shell.TestLoadVersionedActions.test_load_actions_with_versioned_args

In order to fix the tests failures, we make sure to reset the parser and
subparsers before calling add_parser().

While not strictly necessary, we split those tests into two functions,
for readability purposes.

Closes-Bug: #1983054
Change-Id: I2d4cb81a394f3c10e5f01945d894746a904fb5df
This commit is contained in:
Cyril Roelandt 2022-07-28 20:48:58 +02:00
parent 1f3b663485
commit 90eb9d2be6
1 changed files with 13 additions and 4 deletions

View File

@ -376,7 +376,7 @@ class TestLoadVersionedActions(utils.TestCase):
self.mock_completion()
def test_load_versioned_actions(self):
def test_load_versioned_actions_v3_0(self):
parser = cinderclient.shell.CinderClientArgumentParser()
subparsers = parser.add_subparsers(metavar='<subcommand>')
shell = cinderclient.shell.OpenStackCinderShell()
@ -388,6 +388,10 @@ class TestLoadVersionedActions(utils.TestCase):
"fake_action 3.0 to 3.1",
shell.subcommands['fake-action'].get_default('func')())
def test_load_versioned_actions_v3_2(self):
parser = cinderclient.shell.CinderClientArgumentParser()
subparsers = parser.add_subparsers(metavar='<subcommand>')
shell = cinderclient.shell.OpenStackCinderShell()
shell.subcommands = {}
shell._find_actions(subparsers, fake_actions_module,
api_versions.APIVersion("3.2"), False, [])
@ -521,7 +525,7 @@ class TestLoadVersionedActions(utils.TestCase):
@mock.patch.object(cinderclient.shell.CinderClientArgumentParser,
'add_argument')
def test_load_actions_with_versioned_args(self, mock_add_arg):
def test_load_actions_with_versioned_args_v36(self, mock_add_arg):
parser = cinderclient.shell.CinderClientArgumentParser(add_help=False)
subparsers = parser.add_subparsers(metavar='<subcommand>')
shell = cinderclient.shell.OpenStackCinderShell()
@ -533,8 +537,13 @@ class TestLoadVersionedActions(utils.TestCase):
self.assertNotIn(mock.call('--foo', help="second foo"),
mock_add_arg.call_args_list)
mock_add_arg.reset_mock()
@mock.patch.object(cinderclient.shell.CinderClientArgumentParser,
'add_argument')
def test_load_actions_with_versioned_args_v39(self, mock_add_arg):
parser = cinderclient.shell.CinderClientArgumentParser(add_help=False)
subparsers = parser.add_subparsers(metavar='<subcommand>')
shell = cinderclient.shell.OpenStackCinderShell()
shell.subcommands = {}
shell._find_actions(subparsers, fake_actions_module,
api_versions.APIVersion("3.9"), False, [])
self.assertNotIn(mock.call('--foo', help="first foo"),