From 476582b6fc02d61b0040fae6febacb993b118ec2 Mon Sep 17 00:00:00 2001 From: Mike Fedosin Date: Mon, 2 Oct 2017 20:17:37 +0300 Subject: [PATCH] Don't create client for help and bash completion Currently when we need a help, client object is created and authentication is performed. This is completely useless and leads to unnecessary actions in the background. This patch: 1. Prevents creation of client object (and therefore authentication) for help or bash-completion commands. 2. Removes a workaround from keystone auth module that disables sending requests to the server if help or bash-completion commands are executing. 3. Adds related unit tests. Change-Id: Ia26d7f4e56f5ef3ae0ac5e94e8e77d1a78f8829e Closes-bug: #1720795 (cherry picked from commit d9da161c16bc524d4e1d64f09fa2d857cbcf4537) --- mistralclient/shell.py | 37 +++++++++++++++----------- mistralclient/tests/unit/test_shell.py | 11 ++++++++ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/mistralclient/shell.py b/mistralclient/shell.py index ac567740..9eab6c17 100644 --- a/mistralclient/shell.py +++ b/mistralclient/shell.py @@ -523,7 +523,13 @@ class MistralShell(app.App): self._set_shell_commands(self._get_commands(ver)) - do_help = ('help' in argv) or ('-h' in argv) or not argv + # bash-completion and help messages should not require client creation + need_client = not ( + ('bash-completion' in argv) or + ('help' in argv) or + ('-h' in argv) or + ('--help' in argv) or + not argv) # Set default for auth_url if not supplied. The default is not # set at the parser to support use cases where auth is not enabled. @@ -532,10 +538,6 @@ class MistralShell(app.App): if self.options.password or self.options.token: self.options.auth_url = 'http://localhost:35357/v3' - # bash-completion should not require authentification. - if do_help or ('bash-completion' in argv): - self.options.auth_url = None - if self.options.auth_url and not self.options.token: if not self.options.username: raise exe.IllegalArgumentException( @@ -549,6 +551,19 @@ class MistralShell(app.App): "via --os-password env[OS_PASSWORD]") ) + self.client = self._create_client() if need_client else None + + # Adding client_manager variable to make mistral client work with + # unified OpenStack client. + ClientManager = type( + 'ClientManager', + (object,), + dict(workflow_engine=self.client) + ) + + self.client_manager = ClientManager() + + def _create_client(self): kwargs = { 'cert': self.options.os_cert, 'key': self.options.os_key, @@ -556,7 +571,7 @@ class MistralShell(app.App): 'project_domain_name': self.options.project_domain_name } - self.client = client.client( + return client.client( mistral_url=self.options.mistral_url, username=self.options.username, api_key=self.options.password, @@ -585,16 +600,6 @@ class MistralShell(app.App): **kwargs ) - # Adding client_manager variable to make mistral client work with - # unified OpenStack client. - ClientManager = type( - 'ClientManager', - (object,), - dict(workflow_engine=self.client) - ) - - self.client_manager = ClientManager() - def _set_shell_commands(self, cmds_dict): for k, v in cmds_dict.items(): self.command_manager.add_command(k, v) diff --git a/mistralclient/tests/unit/test_shell.py b/mistralclient/tests/unit/test_shell.py index 24e7a36d..3bab9372 100644 --- a/mistralclient/tests/unit/test_shell.py +++ b/mistralclient/tests/unit/test_shell.py @@ -19,6 +19,17 @@ import mistralclient.tests.unit.base_shell_test as base class TestShell(base.BaseShellTests): + def test_help(self): + """Test that client is not created for help and bash complete""" + for command in ('-h', + '--help', + 'help', + 'help workbook-list', + 'bash-completion'): + with mock.patch('mistralclient.api.client.client') as client_mock: + self.shell(command) + self.assertFalse(client_mock.called) + @mock.patch('mistralclient.api.client.client') def test_command_no_mistral_url(self, client_mock): self.shell(