From 433f3d98e88631bc20bc1fe954e9db524853cb6b Mon Sep 17 00:00:00 2001 From: Ruby Loo Date: Tue, 28 Jun 2016 16:02:56 -0400 Subject: [PATCH] Add keyword arg 'log_stdout' to utils.execute() This adds the keyword argument 'log_stdout' (Boolean) to utils.execute(). If set to True, it will log the stdout returned by executing the command. By default, it is True. This argument is used in ipa's (ironic-python-agent) copy of execute(), and is being added here in preparation for changing ipa to use this library's execute(). The docstrings associated with utils.execute() were updated to reflect the copy in ipa. Change-Id: I44148c6e9150c7afd3de7b0b59bedb4983d9a6a7 Partial-Bug: 1587199 --- ironic_lib/tests/test_utils.py | 26 ++++++++++++++++++++++++++ ironic_lib/utils.py | 21 ++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/ironic_lib/tests/test_utils.py b/ironic_lib/tests/test_utils.py index 3116ddb..0938224 100644 --- a/ironic_lib/tests/test_utils.py +++ b/ironic_lib/tests/test_utils.py @@ -188,6 +188,32 @@ grep foo 'foo', run_as_root=True, root_helper=CONF.ironic_lib.root_helper) + @mock.patch.object(utils, 'LOG', autospec=True) + def _test_execute_with_log_stdout(self, log_mock, log_stdout=None): + with mock.patch.object(processutils, 'execute') as execute_mock: + execute_mock.return_value = ('stdout', 'stderr') + if log_stdout is not None: + utils.execute('foo', log_stdout=log_stdout) + else: + utils.execute('foo') + execute_mock.assert_called_once_with('foo') + name, args, kwargs = log_mock.debug.mock_calls[1] + if log_stdout is False: + self.assertEqual(2, log_mock.debug.call_count) + self.assertNotIn('stdout', args[0]) + else: + self.assertEqual(3, log_mock.debug.call_count) + self.assertIn('stdout', args[0]) + + def test_execute_with_log_stdout_default(self): + self._test_execute_with_log_stdout() + + def test_execute_with_log_stdout_true(self): + self._test_execute_with_log_stdout(log_stdout=True) + + def test_execute_with_log_stdout_false(self): + self._test_execute_with_log_stdout(log_stdout=False) + class MkfsTestCase(test_base.BaseTestCase): diff --git a/ironic_lib/utils.py b/ironic_lib/utils.py index 596cb8a..d731d22 100644 --- a/ironic_lib/utils.py +++ b/ironic_lib/utils.py @@ -46,13 +46,21 @@ LOG = logging.getLogger(__name__) def execute(*cmd, **kwargs): """Convenience wrapper around oslo's execute() method. - :param cmd: Passed to processutils.execute. - :param use_standard_locale: True | False. Defaults to False. If set to - True, execute command with standard locale + Executes and logs results from a system command. See docs for + oslo_concurrency.processutils.execute for usage. + + :param \*cmd: positional arguments to pass to processutils.execute() + :param use_standard_locale: keyword-only argument. True | False. + Defaults to False. If set to True, + execute command with standard locale added to environment variables. + :param log_stdout: keyword-only argument. True | False. Defaults + to True. If set to True, logs the output. + :param \*\*kwargs: keyword arguments to pass to processutils.execute() :returns: (stdout, stderr) from process execution - :raises: UnknownArgumentError + :raises: UnknownArgumentError on receiving unknown arguments :raises: ProcessExecutionError + :raises: OSError """ use_standard_locale = kwargs.pop('use_standard_locale', False) @@ -61,6 +69,8 @@ def execute(*cmd, **kwargs): env['LC_ALL'] = 'C' kwargs['env_variables'] = env + log_stdout = kwargs.pop('log_stdout', True) + # If root_helper config is not specified, no commands are run as root. run_as_root = kwargs.get('run_as_root', False) if run_as_root: @@ -72,7 +82,8 @@ def execute(*cmd, **kwargs): result = processutils.execute(*cmd, **kwargs) LOG.debug('Execution completed, command line is "%s"', ' '.join(map(str, cmd))) - LOG.debug('Command stdout is: "%s"' % result[0]) + if log_stdout: + LOG.debug('Command stdout is: "%s"' % result[0]) LOG.debug('Command stderr is: "%s"' % result[1]) return result