hass-core/tests/components/test_shell_command.py
Paulus Schoutsen 609d7ebea5 Migrate core from threads to async awesomeness (#3248)
* Add event loop to the core

* Add block_till_done to HA core object

* Fix some tests

* Linting core

* Fix statemachine tests

* Core test fixes

* fix block_till_done to wait for loop and queue to empty

* fix test_core for passing, and correct start/stop/block_till_done

* Fix remote tests

* Fix tests: block_till_done

* Fix linting

* Fix more tests

* Fix final linting

* Fix remote test

* remove unnecessary import

* reduce sleep to avoid slowing down the tests excessively

* fix remaining tests to wait for non-threadsafe operations

* Add async_ doc strings for event loop / coroutine info

* Fix command line test to block for the right timeout

* Fix py3.4.2 loop var access

* Fix SERVICE_CALL_LIMIT being in effect for other tests

* Fix lint errors

* Fix lint error with proper placement

* Fix slave start to not start a timer

* Add asyncio compatible listeners.

* Increase min Python version to 3.4.2

* Move async backports to util

* Add backported async tests

* Fix linting

* Simplify Python version check

* Fix lint

* Remove unneeded try/except and queue listener appproriately.

* Fix tuple vs. list unorderable error on version compare.

* Fix version tests
2016-09-12 19:16:14 -07:00

96 lines
3.6 KiB
Python

"""The tests for the Shell command component."""
import os
import tempfile
import unittest
from unittest.mock import patch
from subprocess import SubprocessError
from homeassistant.bootstrap import _setup_component
from homeassistant.components import shell_command
from tests.common import get_test_home_assistant
class TestShellCommand(unittest.TestCase):
"""Test the Shell command component."""
def setUp(self): # pylint: disable=invalid-name
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started."""
self.hass.stop()
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')
assert _setup_component(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.block_till_done()
self.assertTrue(os.path.isfile(path))
def test_config_not_dict(self):
"""Test if config is not a dict."""
assert not _setup_component(self.hass, shell_command.DOMAIN, {
shell_command.DOMAIN: ['some', 'weird', 'list']
})
def test_config_not_valid_service_names(self):
"""Test 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'
}
})
def test_template_render_no_template(self):
"""Ensure shell_commands without templates get rendered properly."""
cmd, shell = shell_command._parse_command(self.hass, 'ls /bin', {})
self.assertTrue(shell)
self.assertEqual(cmd, 'ls /bin')
def test_template_render(self):
"""Ensure shell_commands with templates get rendered properly."""
self.hass.states.set('sensor.test_state', 'Works')
cmd, shell = shell_command._parse_command(
self.hass,
'ls /bin {{ states.sensor.test_state.state }}', {}
)
self.assertFalse(shell, False)
self.assertEqual(cmd[-1], 'Works')
def test_invalid_template_fails(self):
"""Test that shell_commands with invalid templates fail."""
cmd, _shell = shell_command._parse_command(
self.hass,
'ls /bin {{ states. .test_state.state }}', {}
)
self.assertEqual(cmd, None)
@patch('homeassistant.components.shell_command.subprocess.call',
side_effect=SubprocessError)
@patch('homeassistant.components.shell_command._LOGGER.error')
def test_subprocess_raising_error(self, mock_call, mock_error):
"""Test subprocess."""
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)
}
})
self.hass.services.call('shell_command', 'test_service',
blocking=True)
self.assertFalse(os.path.isfile(path))
self.assertEqual(1, mock_error.call_count)