Add stdout and stderr to debug output for shell_command (#14465)
This commit is contained in:
parent
25dcddfeef
commit
1053473111
2 changed files with 51 additions and 5 deletions
|
@ -68,8 +68,9 @@ def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
|
||||||
cmd,
|
cmd,
|
||||||
loop=hass.loop,
|
loop=hass.loop,
|
||||||
stdin=None,
|
stdin=None,
|
||||||
stdout=asyncio.subprocess.DEVNULL,
|
stdout=asyncio.subprocess.PIPE,
|
||||||
stderr=asyncio.subprocess.DEVNULL)
|
stderr=asyncio.subprocess.PIPE,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
# Template used. Break into list and use create_subprocess_exec
|
# Template used. Break into list and use create_subprocess_exec
|
||||||
# (which uses shell=False) for security
|
# (which uses shell=False) for security
|
||||||
|
@ -80,12 +81,19 @@ def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
|
||||||
*shlexed_cmd,
|
*shlexed_cmd,
|
||||||
loop=hass.loop,
|
loop=hass.loop,
|
||||||
stdin=None,
|
stdin=None,
|
||||||
stdout=asyncio.subprocess.DEVNULL,
|
stdout=asyncio.subprocess.PIPE,
|
||||||
stderr=asyncio.subprocess.DEVNULL)
|
stderr=asyncio.subprocess.PIPE,
|
||||||
|
)
|
||||||
|
|
||||||
process = yield from create_process
|
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:
|
if process.returncode != 0:
|
||||||
_LOGGER.exception("Error running command: `%s`, return code: %s",
|
_LOGGER.exception("Error running command: `%s`, return code: %s",
|
||||||
cmd, process.returncode)
|
cmd, process.returncode)
|
||||||
|
|
|
@ -148,3 +148,41 @@ class TestShellCommand(unittest.TestCase):
|
||||||
self.assertEqual(1, mock_call.call_count)
|
self.assertEqual(1, mock_call.call_count)
|
||||||
self.assertEqual(1, mock_error.call_count)
|
self.assertEqual(1, mock_error.call_count)
|
||||||
self.assertFalse(os.path.isfile(path))
|
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])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue