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
This commit is contained in:
@@ -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.
|
||||
|
||||
|
@@ -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'])
|
||||
|
Reference in New Issue
Block a user