From: Lajos Katona Date: Tue, 7 Jul 2015 13:04:35 +0000 (+0200) Subject: Fix locale problem in execute() X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=599977e20bd480305434168400055fa417aad8b1;p=openstack-build%2Fneutron-build.git Fix locale problem in execute() Change from new format string to old style formatting. Change-Id: Ib39de7169416c2cc053d4aa909075c68cd2d7f0b Closes-bug: #1449897 --- diff --git a/neutron/agent/linux/utils.py b/neutron/agent/linux/utils.py index 96e179b03..b3f7a0bd1 100644 --- a/neutron/agent/linux/utils.py +++ b/neutron/agent/linux/utils.py @@ -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) diff --git a/neutron/tests/unit/agent/linux/test_utils.py b/neutron/tests/unit/agent/linux/test_utils.py index 7476050c6..71a7b8959 100644 --- a/neutron/tests/unit/agent/linux/test_utils.py +++ b/neutron/tests/unit/agent/linux/test_utils.py @@ -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