core/tests/components/test_shell_command.py

97 lines
3.6 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the Shell command component."""
2015-10-12 04:30:17 +00:00
import os
import tempfile
import unittest
2015-10-12 04:41:44 +00:00
from unittest.mock import patch
from subprocess import SubprocessError
2015-10-12 04:30:17 +00:00
from homeassistant.bootstrap import _setup_component
2015-10-12 04:30:17 +00:00
from homeassistant.components import shell_command
2016-02-14 23:08:23 +00:00
from tests.common import get_test_home_assistant
2015-10-12 04:30:17 +00:00
class TestShellCommand(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the Shell command component."""
2015-10-12 04:30:17 +00:00
def setUp(self): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
"""Setup things to be run when tests are started."""
2016-02-14 23:08:23 +00:00
self.hass = get_test_home_assistant()
2015-10-12 04:30:17 +00:00
def tearDown(self): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
2015-10-12 04:30:17 +00:00
self.hass.stop()
def test_executing_service(self):
2016-03-09 09:25:50 +00:00
"""Test if able to call a configured service."""
2015-10-12 04:30:17 +00:00
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)
2015-10-12 04:30:17 +00:00
}
})
2015-10-12 04:30:17 +00:00
self.hass.services.call('shell_command', 'test_service',
blocking=True)
self.hass.block_till_done()
2015-10-12 04:30:17 +00:00
self.assertTrue(os.path.isfile(path))
2015-10-12 04:41:44 +00:00
def test_config_not_dict(self):
2016-03-09 09:25:50 +00:00
"""Test if config is not a dict."""
assert not _setup_component(self.hass, shell_command.DOMAIN, {
shell_command.DOMAIN: ['some', 'weird', 'list']
})
2015-10-12 04:41:44 +00:00
def test_config_not_valid_service_names(self):
2016-03-09 09:25:50 +00:00
"""Test if config contains invalid service names."""
assert not _setup_component(self.hass, shell_command.DOMAIN, {
shell_command.DOMAIN: {
2015-10-12 04:41:44 +00:00
'this is invalid because space': 'touch bla.txt'
}
})
2015-10-12 04:41:44 +00:00
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)
2015-10-12 04:41:44 +00:00
@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):
2016-03-09 09:25:50 +00:00
"""Test subprocess."""
2015-10-12 04:41:44 +00:00
with tempfile.TemporaryDirectory() as tempdirname:
path = os.path.join(tempdirname, 'called.txt')
assert _setup_component(self.hass, shell_command.DOMAIN, {
shell_command.DOMAIN: {
2015-10-12 04:41:44 +00:00
'test_service': "touch {}".format(path)
}
})
2015-10-12 04:41:44 +00:00
self.hass.services.call('shell_command', 'test_service',
blocking=True)
self.assertFalse(os.path.isfile(path))
self.assertEqual(1, mock_error.call_count)