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
|
|
|
|
2016-04-12 04:52:39 +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')
|
2016-04-12 04:52:39 +00:00
|
|
|
assert _setup_component(self.hass, shell_command.DOMAIN, {
|
|
|
|
shell_command.DOMAIN: {
|
2015-10-24 19:40:36 +00:00
|
|
|
'test_service': "date > {}".format(path)
|
2015-10-12 04:30:17 +00:00
|
|
|
}
|
2016-04-12 04:52:39 +00:00
|
|
|
})
|
2015-10-12 04:30:17 +00:00
|
|
|
|
|
|
|
self.hass.services.call('shell_command', 'test_service',
|
|
|
|
blocking=True)
|
2016-09-13 02:16:14 +00:00
|
|
|
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."""
|
2016-04-12 04:52:39 +00:00
|
|
|
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."""
|
2016-04-12 04:52:39 +00:00
|
|
|
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'
|
2016-04-12 04:52:39 +00:00
|
|
|
}
|
|
|
|
})
|
2015-10-12 04:41:44 +00:00
|
|
|
|
2016-06-18 16:57:18 +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')
|
2016-04-12 04:52:39 +00:00
|
|
|
assert _setup_component(self.hass, shell_command.DOMAIN, {
|
|
|
|
shell_command.DOMAIN: {
|
2015-10-12 04:41:44 +00:00
|
|
|
'test_service': "touch {}".format(path)
|
|
|
|
}
|
2016-04-12 04:52:39 +00:00
|
|
|
})
|
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)
|