Merge "Fix locale problem in execute()"

This commit is contained in:
Jenkins 2015-09-05 16:03:46 +00:00 committed by Gerrit Code Review
commit bad4bd96ce
2 changed files with 31 additions and 8 deletions

View File

@ -83,6 +83,7 @@ def create_process(cmd, run_as_root=False, addl_env=None):
cmd = list(map(str, addl_env_args(addl_env) + cmd))
if run_as_root:
cmd = shlex.split(config.get_root_helper(cfg.CONF)) + cmd
LOG.debug("Running command: %s", cmd)
obj = utils.subprocess_popen(cmd, shell=False,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
@ -98,6 +99,7 @@ def execute_rootwrap_daemon(cmd, process_input, addl_env):
# In practice, no neutron code should be trying to execute something that
# would throw those errors, and if it does it should be fixed as opposed to
# just logging the execution error.
LOG.debug("Running command (rootwrap daemon): %s", cmd)
client = RootwrapDaemonHelper.get_client()
return client.execute(cmd, process_input)
@ -132,20 +134,24 @@ def execute(cmd, process_input=None, addl_env=None,
except UnicodeError:
pass
m = _("\nCommand: {cmd}\nExit code: {code}\n").format(
cmd=cmd,
code=returncode)
command_str = {
'cmd': cmd,
'code': returncode
}
m = _("\nCommand: %(cmd)s"
"\nExit code: %(code)d\n") % command_str
extra_ok_codes = extra_ok_codes or []
if returncode and returncode in extra_ok_codes:
returncode = None
if returncode and log_fail_as_error:
m += ("Stdin: {stdin}\n"
"Stdout: {stdout}\nStderr: {stderr}").format(
stdin=process_input or '',
stdout=_stdout,
stderr=_stderr)
command_str['stdin'] = process_input or ''
command_str['stdout'] = _stdout
command_str['stderr'] = _stderr
m += _("Stdin: %(stdin)s\n"
"Stdout: %(stdout)s\n"
"Stderr: %(stderr)s") % command_str
LOG.error(m)
else:
LOG.debug(m)

View File

@ -18,6 +18,8 @@ import mock
import six
import testtools
import oslo_i18n
from neutron.agent.linux import utils
from neutron.tests import base
@ -100,6 +102,21 @@ class AgentUtilsExecuteTest(base.BaseTestCase):
utils.execute(['ls'])
self.assertTrue(log.debug.called)
def test_return_code_log_error_change_locale(self):
ja_output = 'std_out in Japanese'
ja_error = 'std_err in Japanese'
ja_message_out = oslo_i18n._message.Message(ja_output)
ja_message_err = oslo_i18n._message.Message(ja_error)
ja_translate_out = oslo_i18n._translate.translate(ja_message_out, 'ja')
ja_translate_err = oslo_i18n._translate.translate(ja_message_err, 'ja')
self.mock_popen.return_value = (ja_translate_out, ja_translate_err)
self.process.return_value.returncode = 1
with mock.patch.object(utils, 'LOG') as log:
utils.execute(['ls'], check_exit_code=False)
self.assertIn(ja_translate_out, str(log.error.call_args_list))
self.assertIn(ja_translate_err, str(log.error.call_args_list))
def test_return_code_raise_runtime_do_not_log_fail_as_error(self):
self.mock_popen.return_value = ('', '')
self.process.return_value.returncode = 1