Files
core/tests/components/shell_command/test_init.py
T

182 lines
6.8 KiB
Python
Raw Normal View History

2016-03-09 10:25:50 +01:00
"""The tests for the Shell command component."""
2017-11-23 18:28:31 -07:00
import asyncio
2015-10-11 21:30:17 -07:00
import os
import tempfile
import unittest
2017-11-23 18:28:31 -07:00
from typing import Tuple
from unittest.mock import Mock, patch
2015-10-11 21:30:17 -07:00
2017-03-05 01:41:54 -08:00
from homeassistant.setup import setup_component
2015-10-11 21:30:17 -07:00
from homeassistant.components import shell_command
2016-02-14 15:08:23 -08:00
from tests.common import get_test_home_assistant
2015-10-11 21:30:17 -07:00
2017-11-23 18:28:31 -07:00
@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.
Returns a tuple of (stdout, stderr).
2017-11-23 18:28:31 -07:00
"""
return b"I am stdout", b"I am stderr"
mock_process = Mock()
mock_process.communicate = communicate
mock_process.returncode = int(error)
return mock_process
2015-10-11 21:30:17 -07:00
class TestShellCommand(unittest.TestCase):
2017-11-23 18:28:31 -07:00
"""Test the shell_command component."""
2015-10-11 21:30:17 -07:00
def setUp(self): # pylint: disable=invalid-name
2018-08-19 22:29:08 +02:00
"""Set up things to be run when tests are started.
2017-11-23 18:28:31 -07:00
Also seems to require a child watcher attached to the loop when run
from pytest.
"""
2016-02-14 15:08:23 -08:00
self.hass = get_test_home_assistant()
2017-11-23 18:28:31 -07:00
asyncio.get_child_watcher().attach_loop(self.hass.loop)
2015-10-11 21:30:17 -07:00
def tearDown(self): # pylint: disable=invalid-name
2016-03-09 10:25:50 +01:00
"""Stop everything that was started."""
2015-10-11 21:30:17 -07:00
self.hass.stop()
def test_executing_service(self):
2016-03-09 10:25:50 +01:00
"""Test if able to call a configured service."""
2015-10-11 21:30:17 -07:00
with tempfile.TemporaryDirectory() as tempdirname:
path = os.path.join(tempdirname, 'called.txt')
2017-11-23 18:28:31 -07:00
assert setup_component(
self.hass,
shell_command.DOMAIN, {
shell_command.DOMAIN: {
'test_service': "date > {}".format(path)
}
}
)
2015-10-11 21:30:17 -07:00
self.hass.services.call('shell_command', 'test_service',
blocking=True)
self.hass.block_till_done()
2018-10-24 12:10:05 +02:00
assert os.path.isfile(path)
2015-10-11 21:41:44 -07:00
def test_config_not_dict(self):
2017-11-23 18:28:31 -07:00
"""Test that setup fails if config is not a dict."""
2018-10-24 12:10:05 +02:00
assert not setup_component(self.hass, shell_command.DOMAIN, {
2017-11-23 18:28:31 -07:00
shell_command.DOMAIN: ['some', 'weird', 'list']
2018-10-24 12:10:05 +02:00
})
2015-10-11 21:41:44 -07:00
def test_config_not_valid_service_names(self):
2017-11-23 18:28:31 -07:00
"""Test that setup fails if config contains invalid service names."""
2018-10-24 12:10:05 +02:00
assert not setup_component(self.hass, shell_command.DOMAIN, {
2017-11-23 18:28:31 -07:00
shell_command.DOMAIN: {
'this is invalid because space': 'touch bla.txt'
}
2018-10-24 12:10:05 +02:00
})
2015-10-11 21:41:44 -07:00
2017-11-23 18:28:31 -07:00
@patch('homeassistant.components.shell_command.asyncio.subprocess'
'.create_subprocess_shell')
2016-09-27 21:29:55 -07:00
def test_template_render_no_template(self, mock_call):
"""Ensure shell_commands without templates get rendered properly."""
2017-11-23 18:28:31 -07:00
mock_call.return_value = mock_process_creator(error=False)
2018-10-24 12:10:05 +02:00
assert setup_component(
2017-11-23 18:28:31 -07:00
self.hass,
shell_command.DOMAIN, {
shell_command.DOMAIN: {
'test_service': "ls /bin"
}
2018-10-24 12:10:05 +02:00
})
2016-09-27 21:29:55 -07:00
self.hass.services.call('shell_command', 'test_service',
blocking=True)
2017-11-23 18:28:31 -07:00
self.hass.block_till_done()
2016-09-27 21:29:55 -07:00
cmd = mock_call.mock_calls[0][1][0]
2018-10-24 12:10:05 +02:00
assert 1 == mock_call.call_count
assert 'ls /bin' == cmd
2016-09-27 21:29:55 -07:00
2017-11-23 18:28:31 -07:00
@patch('homeassistant.components.shell_command.asyncio.subprocess'
'.create_subprocess_exec')
2016-09-27 21:29:55 -07:00
def test_template_render(self, mock_call):
2017-11-23 18:28:31 -07:00
"""Ensure shell_commands with templates get rendered properly."""
self.hass.states.set('sensor.test_state', 'Works')
2018-01-01 14:30:09 -08:00
mock_call.return_value = mock_process_creator(error=False)
2018-10-24 12:10:05 +02:00
assert setup_component(self.hass, shell_command.DOMAIN, {
2017-11-23 18:28:31 -07:00
shell_command.DOMAIN: {
'test_service': ("ls /bin {{ states.sensor"
".test_state.state }}")
}
2018-10-24 12:10:05 +02:00
})
2016-09-27 21:29:55 -07:00
self.hass.services.call('shell_command', 'test_service',
blocking=True)
2017-11-23 18:28:31 -07:00
self.hass.block_till_done()
cmd = mock_call.mock_calls[0][1]
2016-09-27 21:29:55 -07:00
2018-10-24 12:10:05 +02:00
assert 1 == mock_call.call_count
assert ('ls', '/bin', 'Works') == cmd
2017-11-23 18:28:31 -07:00
@patch('homeassistant.components.shell_command.asyncio.subprocess'
'.create_subprocess_shell')
2015-10-11 21:41:44 -07:00
@patch('homeassistant.components.shell_command._LOGGER.error')
2017-11-23 18:28:31 -07:00
def test_subprocess_error(self, mock_error, mock_call):
"""Test subprocess that returns an error."""
mock_call.return_value = mock_process_creator(error=True)
2015-10-11 21:41:44 -07:00
with tempfile.TemporaryDirectory() as tempdirname:
path = os.path.join(tempdirname, 'called.txt')
2018-10-24 12:10:05 +02:00
assert setup_component(self.hass, shell_command.DOMAIN, {
2017-11-23 18:28:31 -07:00
shell_command.DOMAIN: {
'test_service': "touch {}".format(path)
}
2018-10-24 12:10:05 +02:00
})
2015-10-11 21:41:44 -07:00
self.hass.services.call('shell_command', 'test_service',
blocking=True)
2017-11-23 18:28:31 -07:00
self.hass.block_till_done()
2018-10-24 12:10:05 +02:00
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')
def test_stdout_captured(self, mock_output):
"""Test subprocess that has stdout."""
test_phrase = "I have output"
2018-10-24 12:10:05 +02:00
assert setup_component(self.hass, shell_command.DOMAIN, {
shell_command.DOMAIN: {
'test_service': "echo {}".format(test_phrase)
}
2018-10-24 12:10:05 +02:00
})
self.hass.services.call('shell_command', 'test_service',
blocking=True)
self.hass.block_till_done()
2018-10-24 12:10:05 +02:00
assert 1 == mock_output.call_count
assert 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"
2018-10-24 12:10:05 +02:00
assert setup_component(self.hass, shell_command.DOMAIN, {
shell_command.DOMAIN: {
'test_service': ">&2 echo {}".format(test_phrase)
}
2018-10-24 12:10:05 +02:00
})
self.hass.services.call('shell_command', 'test_service',
blocking=True)
self.hass.block_till_done()
2018-10-24 12:10:05 +02:00
assert 1 == mock_output.call_count
assert test_phrase.encode() + b'\n' == \
mock_output.call_args_list[0][0][-1]