diff options
author | Akihiro Motoki <amotoki@gmail.com> | 2017-07-27 10:32:23 +0000 |
---|---|---|
committer | Akihiro Motoki <amotoki@gmail.com> | 2017-07-27 13:46:09 +0000 |
commit | bbdea85ba4681ad1a7fdee4ec4f544e75f0d1202 (patch) | |
tree | 97f970c41aaf77db0ea5201de2cbc7cecbd973b1 | |
parent | b14e842b4ff8cda21ae573fc4240e5e5200b9b27 (diff) | |
download | python-neutronclient-bbdea85ba4681ad1a7fdee4ec4f544e75f0d1202.tar.gz |
Populate shell.COMMANDS dict not to break CLI extension UT
shell.COMMANDS is not intended to be public.
However, 8 projects using neutron CLI extenions access this variable
in their UT. I am honestly surprised on this, but it is the real.
Neutron CLI is now deprecated, so it is better not to break them.
This commit populates shell.COMMANDS dict based on registered commands.
The following is my testting results with 'neutron net-list'.
I ran this 10 times and the average user time is as follows:
before 1.096 sec
after 1.112 sec
More dynamic loading can be explored but I think
this overhead can be acceptable.
Closes-Bug: #1706573
Change-Id: Idcbc64e6f709a666829e0c2985a03aa4517ccf77
-rw-r--r-- | neutronclient/shell.py | 10 | ||||
-rw-r--r-- | neutronclient/tests/unit/test_shell.py | 16 |
2 files changed, 26 insertions, 0 deletions
diff --git a/neutronclient/shell.py b/neutronclient/shell.py index 51b06d3..4045b8b 100644 --- a/neutronclient/shell.py +++ b/neutronclient/shell.py @@ -103,6 +103,14 @@ def check_non_negative_int(value): return value +# NOTE(amotoki): This is only to provide compatibility +# to existing neutron CLI extensions. See bug 1706573 for detail. +def _set_commands_dict_for_compat(apiversion, command_manager): + global COMMANDS + COMMANDS = {apiversion: dict((cmd, command_manager.find_command([cmd])[0]) + for cmd in command_manager.commands)} + + class BashCompletionCommand(command.Command): """Prints all of the commands and options for bash-completion.""" @@ -166,6 +174,8 @@ class NeutronShell(app.App): self.auth_client = None self.api_version = apiversion + _set_commands_dict_for_compat(apiversion, self.command_manager) + def build_option_parser(self, description, version): """Return an argparse option parser for this application. diff --git a/neutronclient/tests/unit/test_shell.py b/neutronclient/tests/unit/test_shell.py index 2ae8bdc..2a6c4ff 100644 --- a/neutronclient/tests/unit/test_shell.py +++ b/neutronclient/tests/unit/test_shell.py @@ -27,6 +27,7 @@ import testtools from testtools import matchers from neutronclient.common import clientmanager +from neutronclient.neutron.v2_0 import network from neutronclient import shell as openstack_shell @@ -348,3 +349,18 @@ class ShellTest(testtools.TestCase): os_token='token', insecure=True, cacert='cacert', expect_verify=False, expect_insecure=True) + + def test_commands_dict_populated(self): + # neutron.shell.COMMANDS is populated once NeutronShell is initialized. + # To check COMMANDS during NeutronShell initialization, + # reset COMMANDS to some dummy value before calling NeutronShell(). + self.useFixture(fixtures.MockPatchObject(openstack_shell, + 'COMMANDS', None)) + openstack_shell.NeutronShell('2.0') + self.assertDictContainsSubset( + {'net-create': network.CreateNetwork, + 'net-delete': network.DeleteNetwork, + 'net-list': network.ListNetwork, + 'net-show': network.ShowNetwork, + 'net-update': network.UpdateNetwork}, + openstack_shell.COMMANDS['2.0']) |