]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix locale problem in execute()
authorLajos Katona <lajos.katona@ericsson.com>
Tue, 7 Jul 2015 13:04:35 +0000 (15:04 +0200)
committerLajos Katona <lajos.katona@ericsson.com>
Mon, 31 Aug 2015 10:08:09 +0000 (12:08 +0200)
Change from new format string to old style formatting.

Change-Id: Ib39de7169416c2cc053d4aa909075c68cd2d7f0b
Closes-bug: #1449897

neutron/agent/linux/utils.py
neutron/tests/unit/agent/linux/test_utils.py

index 96e179b03bf7c8bede5b470f03c1121d64c1a9ea..b3f7a0bd1ec55f6d5aa6d8f4c2186821d30bd437 100644 (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)
index 7476050c66baa5fbfe14f945198fb65f6ca07eee..71a7b8959a0473370484d0d7f56013afc512c0ef 100644 (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