Add stdout and stderr to debug output for shell_command (#14465)

This commit is contained in:
Nathan Henrie 2018-05-16 05:47:41 -06:00 committed by Pascal Vizeli
parent 25dcddfeef
commit 1053473111
2 changed files with 51 additions and 5 deletions

View file

@ -68,8 +68,9 @@ def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
cmd,
loop=hass.loop,
stdin=None,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.DEVNULL)
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
else:
# Template used. Break into list and use create_subprocess_exec
# (which uses shell=False) for security
@ -80,12 +81,19 @@ def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
*shlexed_cmd,
loop=hass.loop,
stdin=None,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.DEVNULL)
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
process = yield from create_process
yield from process.communicate()
stdout_data, stderr_data = yield from process.communicate()
if stdout_data:
_LOGGER.debug("Stdout of command: `%s`, return code: %s:\n%s",
cmd, process.returncode, stdout_data)
if stderr_data:
_LOGGER.debug("Stderr of command: `%s`, return code: %s:\n%s",
cmd, process.returncode, stderr_data)
if process.returncode != 0:
_LOGGER.exception("Error running command: `%s`, return code: %s",
cmd, process.returncode)

View file

@ -148,3 +148,41 @@ class TestShellCommand(unittest.TestCase):
self.assertEqual(1, mock_call.call_count)
self.assertEqual(1, mock_error.call_count)
self.assertFalse(os.path.isfile(path))
@patch('homeassistant.components.shell_command._LOGGER.debug')
def test_stdout_captured(self, mock_output):
"""Test subprocess that has stdout."""
test_phrase = "I have output"
self.assertTrue(
setup_component(self.hass, shell_command.DOMAIN, {
shell_command.DOMAIN: {
'test_service': "echo {}".format(test_phrase)
}
}))
self.hass.services.call('shell_command', 'test_service',
blocking=True)
self.hass.block_till_done()
self.assertEqual(1, mock_output.call_count)
self.assertEqual(test_phrase.encode() + b'\n',
mock_output.call_args_list[0][0][-1])
@patch('homeassistant.components.shell_command._LOGGER.debug')
def test_stderr_captured(self, mock_output):
"""Test subprocess that has stderr."""
test_phrase = "I have error"
self.assertTrue(
setup_component(self.hass, shell_command.DOMAIN, {
shell_command.DOMAIN: {
'test_service': ">&2 echo {}".format(test_phrase)
}
}))
self.hass.services.call('shell_command', 'test_service',
blocking=True)
self.hass.block_till_done()
self.assertEqual(1, mock_output.call_count)
self.assertEqual(test_phrase.encode() + b'\n',
mock_output.call_args_list[0][0][-1])