core/tests/test_bootstrap.py

305 lines
9.9 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""Test the bootstrapping."""
2015-08-11 15:20:13 +00:00
# pylint: disable=too-many-public-methods,protected-access
2015-11-15 22:28:50 +00:00
import os
2015-08-11 15:20:13 +00:00
import tempfile
2016-03-30 05:50:38 +00:00
from unittest import mock
import threading
2015-08-11 15:20:13 +00:00
import voluptuous as vol
2016-02-20 07:20:14 +00:00
from homeassistant import bootstrap, loader
from homeassistant.const import (__version__, CONF_LATITUDE, CONF_LONGITUDE,
CONF_NAME, CONF_CUSTOMIZE)
2015-08-11 15:20:13 +00:00
import homeassistant.util.dt as dt_util
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
2015-08-11 15:20:13 +00:00
2016-02-20 07:20:14 +00:00
from tests.common import get_test_home_assistant, MockModule
2016-02-14 23:08:23 +00:00
2016-03-30 05:50:38 +00:00
ORIG_TIMEZONE = dt_util.DEFAULT_TIME_ZONE
2015-08-11 15:20:13 +00:00
2016-03-30 05:50:38 +00:00
class TestBootstrap:
2016-03-09 09:25:50 +00:00
"""Test the bootstrap utils."""
2015-08-11 15:20:13 +00:00
2016-03-30 05:50:38 +00:00
def setup_method(self, method):
2016-03-09 09:25:50 +00:00
"""Setup the test."""
2016-03-30 05:50:38 +00:00
if method == self.test_from_config_file:
return
self.hass = get_test_home_assistant()
self.backup_cache = loader._COMPONENT_CACHE
2015-08-11 15:20:13 +00:00
2016-03-30 05:50:38 +00:00
def teardown_method(self, method):
2016-03-09 09:25:50 +00:00
"""Clean up."""
2016-03-30 05:50:38 +00:00
dt_util.DEFAULT_TIME_ZONE = ORIG_TIMEZONE
if method == self.test_from_config_file:
return
self.hass.stop()
loader._COMPONENT_CACHE = self.backup_cache
2015-08-11 15:20:13 +00:00
def test_from_config_file(self):
2016-03-09 09:25:50 +00:00
"""Test with configuration file."""
2015-08-11 15:20:13 +00:00
components = ['browser', 'conversation', 'script']
with tempfile.NamedTemporaryFile() as fp:
for comp in components:
fp.write('{}:\n'.format(comp).encode('utf-8'))
fp.flush()
2016-02-03 05:33:59 +00:00
hass = bootstrap.from_config_file(fp.name)
2015-08-11 15:20:13 +00:00
components.append('group')
2016-03-30 05:50:38 +00:00
assert sorted(components) == sorted(hass.config.components)
2015-11-15 22:28:50 +00:00
def test_remove_lib_on_upgrade(self):
2016-03-09 09:25:50 +00:00
"""Test removal of library on upgrade."""
2015-11-15 22:28:50 +00:00
with tempfile.TemporaryDirectory() as config_dir:
version_path = os.path.join(config_dir, '.HA_VERSION')
lib_dir = os.path.join(config_dir, 'lib')
check_file = os.path.join(lib_dir, 'check')
with open(version_path, 'wt') as outp:
outp.write('0.7.0')
os.mkdir(lib_dir)
with open(check_file, 'w'):
pass
2016-03-30 05:50:38 +00:00
self.hass.config.config_dir = config_dir
2015-11-15 22:28:50 +00:00
2016-03-30 05:50:38 +00:00
assert os.path.isfile(check_file)
bootstrap.process_ha_config_upgrade(self.hass)
assert not os.path.isfile(check_file)
2015-11-15 22:28:50 +00:00
def test_not_remove_lib_if_not_upgrade(self):
2016-03-09 09:25:50 +00:00
"""Test removal of library with no upgrade."""
2015-11-15 22:28:50 +00:00
with tempfile.TemporaryDirectory() as config_dir:
version_path = os.path.join(config_dir, '.HA_VERSION')
lib_dir = os.path.join(config_dir, 'lib')
check_file = os.path.join(lib_dir, 'check')
with open(version_path, 'wt') as outp:
outp.write(__version__)
os.mkdir(lib_dir)
with open(check_file, 'w'):
pass
2016-03-30 05:50:38 +00:00
self.hass.config.config_dir = config_dir
2015-11-15 22:28:50 +00:00
2016-03-30 05:50:38 +00:00
bootstrap.process_ha_config_upgrade(self.hass)
2015-11-15 22:28:50 +00:00
2016-03-30 05:50:38 +00:00
assert os.path.isfile(check_file)
def test_entity_customization(self):
2016-03-09 09:25:50 +00:00
"""Test entity customization through configuration."""
config = {CONF_LATITUDE: 50,
CONF_LONGITUDE: 50,
CONF_NAME: 'Test',
CONF_CUSTOMIZE: {'test.test': {'hidden': True}}}
2016-03-30 05:50:38 +00:00
bootstrap.process_ha_core_config(self.hass, config)
entity = Entity()
entity.entity_id = 'test.test'
2016-03-30 05:50:38 +00:00
entity.hass = self.hass
entity.update_ha_state()
2016-03-30 05:50:38 +00:00
state = self.hass.states.get('test.test')
2016-03-30 05:50:38 +00:00
assert state.attributes['hidden']
2016-02-20 07:20:14 +00:00
def test_handle_setup_circular_dependency(self):
2016-03-09 09:25:50 +00:00
"""Test the setup of circular dependencies."""
2016-02-20 07:20:14 +00:00
loader.set_component('comp_b', MockModule('comp_b', ['comp_a']))
def setup_a(hass, config):
2016-03-09 09:25:50 +00:00
"""Setup the another component."""
2016-02-20 07:20:14 +00:00
bootstrap.setup_component(hass, 'comp_b')
return True
loader.set_component('comp_a', MockModule('comp_a', setup=setup_a))
2016-03-30 05:50:38 +00:00
bootstrap.setup_component(self.hass, 'comp_a')
assert ['comp_a'] == self.hass.config.components
def test_validate_component_config(self):
"""Test validating component configuration."""
config_schema = vol.Schema({
'comp_conf': {
'hello': str
}
}, required=True)
loader.set_component(
'comp_conf', MockModule('comp_conf', config_schema=config_schema))
2016-03-30 05:50:38 +00:00
assert not bootstrap._setup_component(self.hass, 'comp_conf', {})
2016-03-30 05:50:38 +00:00
assert not bootstrap._setup_component(self.hass, 'comp_conf', {
'comp_conf': None
})
2016-03-30 05:50:38 +00:00
assert not bootstrap._setup_component(self.hass, 'comp_conf', {
'comp_conf': {}
})
2016-03-30 05:50:38 +00:00
assert not bootstrap._setup_component(self.hass, 'comp_conf', {
'comp_conf': {
'hello': 'world',
'invalid': 'extra',
}
})
2016-03-30 05:50:38 +00:00
assert bootstrap._setup_component(self.hass, 'comp_conf', {
'comp_conf': {
'hello': 'world',
}
})
def test_validate_platform_config(self):
"""Test validating platform configuration."""
platform_schema = PLATFORM_SCHEMA.extend({
'hello': str,
}, required=True)
loader.set_component(
'platform_conf',
MockModule('platform_conf', platform_schema=platform_schema))
2016-03-30 05:50:38 +00:00
assert not bootstrap._setup_component(self.hass, 'platform_conf', {
'platform_conf': None
})
2016-03-30 05:50:38 +00:00
assert not bootstrap._setup_component(self.hass, 'platform_conf', {
'platform_conf': {}
})
2016-03-30 05:50:38 +00:00
assert not bootstrap._setup_component(self.hass, 'platform_conf', {
'platform_conf': {
'hello': 'world',
'invalid': 'extra',
}
})
2016-03-30 05:50:38 +00:00
assert not bootstrap._setup_component(self.hass, 'platform_conf', {
'platform_conf': {
'platform': 'whatever',
'hello': 'world',
},
'platform_conf 2': {
'invalid': True
}
})
2016-03-30 05:50:38 +00:00
assert bootstrap._setup_component(self.hass, 'platform_conf', {
'platform_conf': {
'platform': 'whatever',
'hello': 'world',
}
})
2016-03-30 05:50:38 +00:00
assert bootstrap._setup_component(self.hass, 'platform_conf', {
'platform_conf': [{
'platform': 'whatever',
'hello': 'world',
}]
})
2016-03-30 05:50:38 +00:00
def test_component_not_found(self):
"""setup_component should not crash if component doesn't exist."""
assert not bootstrap.setup_component(self.hass, 'non_existing')
def test_component_not_double_initialized(self):
"""Test we do not setup a component twice."""
mock_setup = mock.MagicMock()
loader.set_component('comp', MockModule('comp', setup=mock_setup))
assert bootstrap.setup_component(self.hass, 'comp')
assert mock_setup.called
mock_setup.reset_mock()
assert bootstrap.setup_component(self.hass, 'comp')
assert not mock_setup.called
@mock.patch('homeassistant.util.package.install_package',
return_value=False)
def test_component_not_installed_if_requirement_fails(self, mock_install):
"""Component setup should fail if requirement can't install."""
loader.set_component(
'comp', MockModule('comp', requirements=['package==0.0.1']))
assert not bootstrap.setup_component(self.hass, 'comp')
assert 'comp' not in self.hass.config.components
def test_component_not_setup_twice_if_loaded_during_other_setup(self):
"""
Test component that gets setup while waiting for lock is not setup
twice.
"""
loader.set_component('comp', MockModule('comp'))
result = []
def setup_component():
result.append(bootstrap.setup_component(self.hass, 'comp'))
with bootstrap._SETUP_LOCK:
thread = threading.Thread(target=setup_component)
thread.start()
self.hass.config.components.append('comp')
thread.join()
assert len(result) == 1
assert result[0]
def test_component_not_setup_missing_dependencies(self):
"""Test we do not setup a component if not all dependencies loaded."""
deps = ['non_existing']
loader.set_component('comp', MockModule('comp', dependencies=deps))
assert not bootstrap._setup_component(self.hass, 'comp', None)
assert 'comp' not in self.hass.config.components
self.hass.config.components.append('non_existing')
assert bootstrap._setup_component(self.hass, 'comp', None)
def test_component_failing_setup(self):
"""Test component that fails setup."""
loader.set_component(
'comp', MockModule('comp', setup=lambda hass, config: False))
assert not bootstrap._setup_component(self.hass, 'comp', None)
assert 'comp' not in self.hass.config.components
def test_component_exception_setup(self):
"""Test component that raises exception during setup."""
def exception_setup(hass, config):
"""Setup that raises exception."""
raise Exception('fail!')
loader.set_component('comp', MockModule('comp', setup=exception_setup))
assert not bootstrap._setup_component(self.hass, 'comp', None)
assert 'comp' not in self.hass.config.components
@mock.patch('homeassistant.bootstrap.process_ha_core_config')
def test_home_assistant_core_config_validation(self, mock_process):
"""Test if we pass in wrong information for HA conf."""
# Extensive HA conf validation testing is done in test_config.py
assert None is bootstrap.from_config_dict({
'homeassistant': {
'latitude': 'some string'
}
})
assert not mock_process.called