This commit is contained in:
Paulus Schoutsen 2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View file

@ -15,6 +15,7 @@ from tests.common import get_test_home_assistant
@asyncio.coroutine
def mock_process_creator(error: bool = False) -> asyncio.coroutine:
"""Mock a coroutine that creates a process when yielded."""
@asyncio.coroutine
def communicate() -> Tuple[bytes, bytes]:
"""Mock a coroutine that runs a process when yielded.
@ -48,134 +49,132 @@ class TestShellCommand(unittest.TestCase):
def test_executing_service(self):
"""Test if able to call a configured service."""
with tempfile.TemporaryDirectory() as tempdirname:
path = os.path.join(tempdirname, 'called.txt')
path = os.path.join(tempdirname, "called.txt")
assert setup_component(
self.hass,
shell_command.DOMAIN, {
shell_command.DOMAIN: {
'test_service': "date > {}".format(path)
}
}
)
self.hass,
shell_command.DOMAIN,
{shell_command.DOMAIN: {"test_service": "date > {}".format(path)}},
)
self.hass.services.call('shell_command', 'test_service',
blocking=True)
self.hass.services.call("shell_command", "test_service", blocking=True)
self.hass.block_till_done()
assert os.path.isfile(path)
def test_config_not_dict(self):
"""Test that setup fails if config is not a dict."""
assert not setup_component(self.hass, shell_command.DOMAIN, {
shell_command.DOMAIN: ['some', 'weird', 'list']
})
assert not setup_component(
self.hass,
shell_command.DOMAIN,
{shell_command.DOMAIN: ["some", "weird", "list"]},
)
def test_config_not_valid_service_names(self):
"""Test that setup fails if config contains invalid service names."""
assert not setup_component(self.hass, shell_command.DOMAIN, {
shell_command.DOMAIN: {
'this is invalid because space': 'touch bla.txt'
}
})
assert not setup_component(
self.hass,
shell_command.DOMAIN,
{shell_command.DOMAIN: {"this is invalid because space": "touch bla.txt"}},
)
@patch('homeassistant.components.shell_command.asyncio.subprocess'
'.create_subprocess_shell')
@patch(
"homeassistant.components.shell_command.asyncio.subprocess"
".create_subprocess_shell"
)
def test_template_render_no_template(self, mock_call):
"""Ensure shell_commands without templates get rendered properly."""
mock_call.return_value = mock_process_creator(error=False)
assert setup_component(
self.hass,
shell_command.DOMAIN, {
shell_command.DOMAIN: {
'test_service': "ls /bin"
}
})
self.hass,
shell_command.DOMAIN,
{shell_command.DOMAIN: {"test_service": "ls /bin"}},
)
self.hass.services.call('shell_command', 'test_service',
blocking=True)
self.hass.services.call("shell_command", "test_service", blocking=True)
self.hass.block_till_done()
cmd = mock_call.mock_calls[0][1][0]
assert 1 == mock_call.call_count
assert 'ls /bin' == cmd
assert "ls /bin" == cmd
@patch('homeassistant.components.shell_command.asyncio.subprocess'
'.create_subprocess_exec')
@patch(
"homeassistant.components.shell_command.asyncio.subprocess"
".create_subprocess_exec"
)
def test_template_render(self, mock_call):
"""Ensure shell_commands with templates get rendered properly."""
self.hass.states.set('sensor.test_state', 'Works')
self.hass.states.set("sensor.test_state", "Works")
mock_call.return_value = mock_process_creator(error=False)
assert setup_component(self.hass, shell_command.DOMAIN, {
shell_command.DOMAIN: {
'test_service': ("ls /bin {{ states.sensor"
".test_state.state }}")
}
})
assert setup_component(
self.hass,
shell_command.DOMAIN,
{
shell_command.DOMAIN: {
"test_service": ("ls /bin {{ states.sensor" ".test_state.state }}")
}
},
)
self.hass.services.call('shell_command', 'test_service',
blocking=True)
self.hass.services.call("shell_command", "test_service", blocking=True)
self.hass.block_till_done()
cmd = mock_call.mock_calls[0][1]
assert 1 == mock_call.call_count
assert ('ls', '/bin', 'Works') == cmd
assert ("ls", "/bin", "Works") == cmd
@patch('homeassistant.components.shell_command.asyncio.subprocess'
'.create_subprocess_shell')
@patch('homeassistant.components.shell_command._LOGGER.error')
@patch(
"homeassistant.components.shell_command.asyncio.subprocess"
".create_subprocess_shell"
)
@patch("homeassistant.components.shell_command._LOGGER.error")
def test_subprocess_error(self, mock_error, mock_call):
"""Test subprocess that returns an error."""
mock_call.return_value = mock_process_creator(error=True)
with tempfile.TemporaryDirectory() as tempdirname:
path = os.path.join(tempdirname, 'called.txt')
assert setup_component(self.hass, shell_command.DOMAIN, {
shell_command.DOMAIN: {
'test_service': "touch {}".format(path)
}
})
path = os.path.join(tempdirname, "called.txt")
assert setup_component(
self.hass,
shell_command.DOMAIN,
{shell_command.DOMAIN: {"test_service": "touch {}".format(path)}},
)
self.hass.services.call('shell_command', 'test_service',
blocking=True)
self.hass.services.call("shell_command", "test_service", blocking=True)
self.hass.block_till_done()
assert 1 == mock_call.call_count
assert 1 == mock_error.call_count
assert not os.path.isfile(path)
@patch('homeassistant.components.shell_command._LOGGER.debug')
@patch("homeassistant.components.shell_command._LOGGER.debug")
def test_stdout_captured(self, mock_output):
"""Test subprocess that has stdout."""
test_phrase = "I have output"
assert setup_component(self.hass, shell_command.DOMAIN, {
shell_command.DOMAIN: {
'test_service': "echo {}".format(test_phrase)
}
})
assert 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.services.call("shell_command", "test_service", blocking=True)
self.hass.block_till_done()
assert 1 == mock_output.call_count
assert test_phrase.encode() + b'\n' == \
mock_output.call_args_list[0][0][-1]
assert test_phrase.encode() + b"\n" == mock_output.call_args_list[0][0][-1]
@patch('homeassistant.components.shell_command._LOGGER.debug')
@patch("homeassistant.components.shell_command._LOGGER.debug")
def test_stderr_captured(self, mock_output):
"""Test subprocess that has stderr."""
test_phrase = "I have error"
assert setup_component(self.hass, shell_command.DOMAIN, {
shell_command.DOMAIN: {
'test_service': ">&2 echo {}".format(test_phrase)
}
})
assert 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.services.call("shell_command", "test_service", blocking=True)
self.hass.block_till_done()
assert 1 == mock_output.call_count
assert test_phrase.encode() + b'\n' == \
mock_output.call_args_list[0][0][-1]
assert test_phrase.encode() + b"\n" == mock_output.call_args_list[0][0][-1]