hass-core/tests/components/test_shell_command.py

70 lines
2.5 KiB
Python
Raw Normal View History

2016-03-09 10:25:50 +01:00
"""The tests for the Shell command component."""
2015-10-11 21:30:17 -07:00
import os
import tempfile
import unittest
2015-10-11 21:41:44 -07:00
from unittest.mock import patch
from subprocess import SubprocessError
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
class TestShellCommand(unittest.TestCase):
2016-03-09 10:25:50 +01:00
"""Test the Shell command component."""
2015-10-11 21:30:17 -07:00
def setUp(self): # pylint: disable=invalid-name
2016-03-09 10:25:50 +01:00
"""Setup things to be run when tests are started."""
2016-02-14 15:08:23 -08:00
self.hass = get_test_home_assistant()
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')
2015-10-11 21:41:44 -07:00
self.assertTrue(shell_command.setup(self.hass, {
2015-10-11 21:30:17 -07:00
'shell_command': {
'test_service': "date > {}".format(path)
2015-10-11 21:30:17 -07:00
}
2015-10-11 21:41:44 -07:00
}))
2015-10-11 21:30:17 -07:00
self.hass.services.call('shell_command', 'test_service',
blocking=True)
self.assertTrue(os.path.isfile(path))
2015-10-11 21:41:44 -07:00
def test_config_not_dict(self):
2016-03-09 10:25:50 +01:00
"""Test if config is not a dict."""
2015-10-11 21:41:44 -07:00
self.assertFalse(shell_command.setup(self.hass, {
'shell_command': ['some', 'weird', 'list']
}))
def test_config_not_valid_service_names(self):
2016-03-09 10:25:50 +01:00
"""Test if config contains invalid service names."""
2015-10-11 21:41:44 -07:00
self.assertFalse(shell_command.setup(self.hass, {
'shell_command': {
'this is invalid because space': 'touch bla.txt'
}}))
@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 10:25:50 +01:00
"""Test subprocess."""
2015-10-11 21:41:44 -07:00
with tempfile.TemporaryDirectory() as tempdirname:
path = os.path.join(tempdirname, 'called.txt')
self.assertTrue(shell_command.setup(self.hass, {
'shell_command': {
'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)