Use repr in debug output of SSH and subprocess

Use repr in debug output of SSH and subprocess
This will prevent decode errors, while non unicode
 non ascii data will present in input
 (Example: binary coded cyrillic environment value set)

Change-Id: I6c20e43444df150ff7fd7902421f6f44dc20fe12
This commit is contained in:
Alexey Stepanov 2016-09-06 11:27:48 +03:00
parent bb3e7b0586
commit 060ab8eaeb
6 changed files with 30 additions and 30 deletions

View File

@ -309,7 +309,7 @@ class ExecResult(object):
def __repr__(self):
return (
'{cls}(cmd={cmd}, stdout={stdout}, stderr={stderr}, '
'{cls}(cmd={cmd!r}, stdout={stdout}, stderr={stderr}, '
'exit_code={exit_code!s})'.format(
cls=self.__class__.__name__,
cmd=self.cmd,
@ -320,7 +320,7 @@ class ExecResult(object):
def __str__(self):
return (
"{cls}(\n\tcmd={cmd},"
"{cls}(\n\tcmd={cmd!r},"
"\n\t stdout=\n'{stdout_brief}',"
"\n\tstderr=\n'{stderr_brief}', "
'\n\texit_code={exit_code!s}\n)'.format(

View File

@ -615,7 +615,7 @@ class SSHClient(six.with_metaclass(_MemorizedSSH, object)):
ret = self.execute(command, verbose, timeout, **kwargs)
if ret['exit_code'] not in expected:
message = (
"{append}Command '{cmd}' returned exit code {code!s} while "
"{append}Command '{cmd!r}' returned exit code {code!s} while "
"expected {expected!s}\n"
"\tSTDOUT:\n"
"{stdout}"
@ -657,7 +657,7 @@ class SSHClient(six.with_metaclass(_MemorizedSSH, object)):
error_info=error_info, raise_on_err=raise_on_err, **kwargs)
if ret['stderr']:
message = (
"{append}Command '{cmd}' STDERR while not expected\n"
"{append}Command '{cmd!r}' STDERR while not expected\n"
"\texit code: {code!s}\n"
"\tSTDOUT:\n"
"{stdout}"
@ -730,7 +730,7 @@ class SSHClient(six.with_metaclass(_MemorizedSSH, object)):
channel.close()
status_tmpl = (
'Wait for {0} during {1}s: no return code!\n'
'Wait for {0!r} during {1}s: no return code!\n'
'\tSTDOUT:\n'
'{2}\n'
'\tSTDERR"\n'
@ -764,7 +764,7 @@ class SSHClient(six.with_metaclass(_MemorizedSSH, object)):
if verbose:
logger.info(
'{cmd} execution results:\n'
'{cmd!r} execution results:\n'
'Exit code: {code!s}\n'
'STDOUT:\n'
'{stdout}\n'
@ -777,7 +777,7 @@ class SSHClient(six.with_metaclass(_MemorizedSSH, object)):
))
else:
logger.debug(
'{cmd} execution results: Exit code: {code}'.format(
'{cmd!r} execution results: Exit code: {code}'.format(
cmd=command,
code=result.exit_code
)
@ -792,7 +792,7 @@ class SSHClient(six.with_metaclass(_MemorizedSSH, object)):
:type timeout: int
:rtype: tuple
"""
logger.debug("Executing command: '{}'".format(command.rstrip()))
logger.debug("Executing command: {!r}".format(command.rstrip()))
chan = self._ssh.get_transport().open_session(timeout=timeout)

View File

@ -135,11 +135,11 @@ class Subprocess(with_metaclass(SingletonMeta, object)):
except OSError:
# Nothing to kill
logger.warning(
"{} has been completed just after timeout: "
"{!r} has been completed just after timeout: "
"please validate timeout.".format(command))
status_tmpl = (
'Wait for {0} during {1}s: no return code!\n'
'Wait for {0!r} during {1}s: no return code!\n'
'\tSTDOUT:\n'
'{2}\n'
'\tSTDERR"\n'
@ -170,12 +170,12 @@ class Subprocess(with_metaclass(SingletonMeta, object)):
:rtype: ExecResult
:raises: TimeoutError
"""
logger.debug("Executing command: '{}'".format(command.rstrip()))
logger.debug("Executing command: {!r}".format(command.rstrip()))
result = cls.__exec_command(command=command, timeout=timeout,
verbose=verbose, **kwargs)
if verbose:
logger.debug(
'{cmd} execution results:\n'
'{cmd!r} execution results:\n'
'Exit code: {code!s}\n'
'STDOUT:\n'
'{stdout}\n'
@ -188,7 +188,7 @@ class Subprocess(with_metaclass(SingletonMeta, object)):
))
else:
logger.debug(
'{cmd} execution results: Exit code: {code}'.format(
'{cmd!r} execution results: Exit code: {code}'.format(
cmd=command,
code=result.exit_code
)
@ -230,7 +230,7 @@ class Subprocess(with_metaclass(SingletonMeta, object)):
ret = cls.execute(command, verbose, timeout, **kwargs)
if ret['exit_code'] not in expected:
message = (
"{append}Command '{cmd}' returned exit code {code!s} while "
"{append}Command '{cmd!r}' returned exit code {code!s} while "
"expected {expected!s}\n"
"\tSTDOUT:\n"
"{stdout}"
@ -275,7 +275,7 @@ class Subprocess(with_metaclass(SingletonMeta, object)):
error_info=error_info, raise_on_err=raise_on_err, **kwargs)
if ret['stderr']:
message = (
"{append}Command '{cmd}' STDERR while not expected\n"
"{append}Command '{cmd!r}' STDERR while not expected\n"
"\texit code: {code!s}\n"
"\tSTDOUT:\n"
"{stdout}"

View File

@ -53,7 +53,7 @@ class TestExecResult(TestCase):
self.assertEqual(exec_result.exit_code, exec_result['exit_code'])
self.assertEqual(
repr(exec_result),
'{cls}(cmd={cmd}, stdout={stdout}, stderr={stderr}, '
'{cls}(cmd={cmd!r}, stdout={stdout}, stderr={stderr}, '
'exit_code={exit_code!s})'.format(
cls=ExecResult.__name__,
cmd=cmd,
@ -64,7 +64,7 @@ class TestExecResult(TestCase):
)
self.assertEqual(
str(exec_result),
"{cls}(\n\tcmd={cmd},"
"{cls}(\n\tcmd={cmd!r},"
"\n\t stdout=\n'{stdout_brief}',"
"\n\tstderr=\n'{stderr_brief}', "
'\n\texit_code={exit_code!s}\n)'.format(

View File

@ -936,7 +936,7 @@ class TestExecute(TestCase):
))
self.assertIn(
mock.call.debug(
"Executing command: '{}'".format(command.rstrip())),
"Executing command: {!r}".format(command.rstrip())),
logger.mock_calls
)
@ -971,7 +971,7 @@ class TestExecute(TestCase):
))
self.assertIn(
mock.call.debug(
"Executing command: '{}'".format(command.rstrip())),
"Executing command: {!r}".format(command.rstrip())),
logger.mock_calls
)
@ -1004,7 +1004,7 @@ class TestExecute(TestCase):
))
self.assertIn(
mock.call.debug(
"Executing command: '{}'".format(command.rstrip())),
"Executing command: {!r}".format(command.rstrip())),
logger.mock_calls
)
@ -1040,7 +1040,7 @@ class TestExecute(TestCase):
))
self.assertIn(
mock.call.debug(
"Executing command: '{}'".format(command.rstrip())),
"Executing command: {!r}".format(command.rstrip())),
logger.mock_calls
)
@ -1072,7 +1072,7 @@ class TestExecute(TestCase):
))
self.assertIn(
mock.call.debug(
"Executing command: '{}'".format(command.rstrip())),
"Executing command: {!r}".format(command.rstrip())),
logger.mock_calls
)
@ -1104,7 +1104,7 @@ class TestExecute(TestCase):
))
self.assertIn(
mock.call.debug(
"Executing command: '{}'".format(command.rstrip())),
"Executing command: {!r}".format(command.rstrip())),
logger.mock_calls
)
@ -1149,7 +1149,7 @@ class TestExecute(TestCase):
))
self.assertIn(
mock.call.debug(
"Executing command: '{}'".format(command.rstrip())),
"Executing command: {!r}".format(command.rstrip())),
logger.mock_calls
)
@ -1218,7 +1218,7 @@ class TestExecute(TestCase):
mock.call.close()))
logger.assert_has_calls((
mock.call.info(
'{cmd} execution results:\n'
'{cmd!r} execution results:\n'
'Exit code: {code!s}\n'
'STDOUT:\n'
'{stdout}\n'
@ -1271,7 +1271,7 @@ class TestExecute(TestCase):
mock.call.close()))
logger.assert_has_calls((
mock.call.info(
'{cmd} execution results:\n'
'{cmd!r} execution results:\n'
'Exit code: {code!s}\n'
'STDOUT:\n'
'{stdout}\n'

View File

@ -95,9 +95,9 @@ class TestSubprocessRunner(TestCase):
stdin=PIPE, stdout=PIPE, universal_newlines=False),
))
logger.assert_has_calls((
call.debug("Executing command: '{}'".format(command.rstrip())),
call.debug("Executing command: {!r}".format(command.rstrip())),
call.debug(
'{cmd} execution results: Exit code: {code}'.format(
'{cmd!r} execution results: Exit code: {code}'.format(
cmd=command,
code=result.exit_code
)),
@ -115,9 +115,9 @@ class TestSubprocessRunner(TestCase):
result = runner.execute(command, verbose=True)
logger.assert_has_calls((
call.debug("Executing command: '{}'".format(command.rstrip())),
call.debug("Executing command: {!r}".format(command.rstrip())),
call.debug(
'{cmd} execution results:\n'
'{cmd!r} execution results:\n'
'Exit code: {code!s}\n'
'STDOUT:\n'
'{stdout}\n'