tests: Fix Python 3.11 compatibility

The argparse lib in Python 3.11 will not allow you to register a
subparser more than once with the same name. We were inadvertently doing
this in two of our unit tests as part of our check for version handling.
There's no need for this. Stop doing it and simply create a new parser
each time.

An unnecessary check is removed from one of the tests since it confuses
matters.

Change-Id: I93827f84c456c9f6960e30e2424b67947254752c
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2022-12-12 17:28:12 +00:00
parent 2af1d0c514
commit 1d8a06da78
1 changed files with 12 additions and 4 deletions

View File

@ -777,6 +777,8 @@ class ShellTest(utils.TestCase):
class TestLoadVersionedActions(utils.TestCase):
def test_load_versioned_actions(self):
# first load with API version 2.15, ensuring we use the 2.15 version of
# the underlying function (which returns 1)
parser = novaclient.shell.NovaClientArgumentParser()
subparsers = parser.add_subparsers(metavar='<subcommand>')
shell = novaclient.shell.OpenStackComputeShell()
@ -787,6 +789,12 @@ class TestLoadVersionedActions(utils.TestCase):
self.assertEqual(
1, shell.subcommands['fake-action'].get_default('func')())
# now load with API version 2.25, ensuring we now use the
# correspponding version of the underlying function (which now returns
# 2)
parser = novaclient.shell.NovaClientArgumentParser()
subparsers = parser.add_subparsers(metavar='<subcommand>')
shell = novaclient.shell.OpenStackComputeShell()
shell.subcommands = {}
shell._find_actions(subparsers, fake_actions_module,
api_versions.APIVersion("2.25"), False)
@ -794,10 +802,6 @@ class TestLoadVersionedActions(utils.TestCase):
self.assertEqual(
2, shell.subcommands['fake-action'].get_default('func')())
self.assertIn('fake-action2', shell.subcommands.keys())
self.assertEqual(
3, shell.subcommands['fake-action2'].get_default('func')())
def test_load_versioned_actions_not_in_version_range(self):
parser = novaclient.shell.NovaClientArgumentParser()
subparsers = parser.add_subparsers(metavar='<subcommand>')
@ -908,6 +912,10 @@ class TestLoadVersionedActions(utils.TestCase):
mock_add_arg.reset_mock()
parser = novaclient.shell.NovaClientArgumentParser(add_help=False)
subparsers = parser.add_subparsers(metavar='<subcommand>')
shell = novaclient.shell.OpenStackComputeShell()
shell.subcommands = {}
shell._find_actions(subparsers, fake_actions_module,
api_versions.APIVersion("2.21"), False)
self.assertNotIn(mock.call('--foo', help="first foo"),