From 1d8a06da78ed16ed29cdc3b153d67ec1961b57bf Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Mon, 12 Dec 2022 17:28:12 +0000 Subject: 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 --- novaclient/tests/unit/test_shell.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/novaclient/tests/unit/test_shell.py b/novaclient/tests/unit/test_shell.py index a997b23f..f46d0ad6 100644 --- a/novaclient/tests/unit/test_shell.py +++ b/novaclient/tests/unit/test_shell.py @@ -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='') 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='') + 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='') @@ -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='') + 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"), -- cgit v1.2.1