2016-08-23 04:42:05 +00:00
|
|
|
"""Test check_config script."""
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2016-08-23 04:42:05 +00:00
|
|
|
|
2020-07-09 14:15:14 +00:00
|
|
|
import pytest
|
|
|
|
|
2018-03-09 03:34:24 +00:00
|
|
|
from homeassistant.config import YAML_CONFIG_FILE
|
2019-12-09 15:52:24 +00:00
|
|
|
import homeassistant.scripts.check_config as check_config
|
|
|
|
|
2019-07-10 18:56:50 +00:00
|
|
|
from tests.common import get_test_config_dir, patch_yaml_files
|
2016-08-23 04:42:05 +00:00
|
|
|
|
|
|
|
BASE_CONFIG = (
|
2019-07-31 19:25:30 +00:00
|
|
|
"homeassistant:\n"
|
|
|
|
" name: Home\n"
|
|
|
|
" latitude: -26.107361\n"
|
|
|
|
" longitude: 28.054500\n"
|
|
|
|
" elevation: 1600\n"
|
|
|
|
" unit_system: metric\n"
|
|
|
|
" time_zone: GMT\n"
|
|
|
|
"\n\n"
|
2016-08-23 04:42:05 +00:00
|
|
|
)
|
|
|
|
|
2020-01-02 19:17:10 +00:00
|
|
|
BAD_CORE_CONFIG = "homeassistant:\n unit_system: bad\n\n\n"
|
2018-08-17 03:28:00 +00:00
|
|
|
|
2016-08-23 04:42:05 +00:00
|
|
|
|
2020-07-09 14:15:14 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
async def apply_stop_hass(stop_hass):
|
|
|
|
"""Make sure all hass are stopped."""
|
|
|
|
|
|
|
|
|
2021-09-03 17:15:57 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_is_file():
|
|
|
|
"""Mock is_file."""
|
|
|
|
# All files exist except for the old entity registry file
|
|
|
|
with patch(
|
|
|
|
"os.path.isfile", lambda path: not path.endswith("entity_registry.yaml")
|
|
|
|
):
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
2018-03-09 03:34:24 +00:00
|
|
|
def normalize_yaml_files(check_dict):
|
|
|
|
"""Remove configuration path from ['yaml_files']."""
|
2016-08-25 05:18:32 +00:00
|
|
|
root = get_test_config_dir()
|
2019-07-31 19:25:30 +00:00
|
|
|
return [key.replace(root, "...") for key in sorted(check_dict["yaml_files"].keys())]
|
2016-08-25 05:18:32 +00:00
|
|
|
|
|
|
|
|
2021-09-03 17:15:57 +00:00
|
|
|
def test_bad_core_config(mock_is_file, loop):
|
2019-04-30 16:20:38 +00:00
|
|
|
"""Test a bad core config setup."""
|
2019-07-31 19:25:30 +00:00
|
|
|
files = {YAML_CONFIG_FILE: BAD_CORE_CONFIG}
|
2019-04-30 16:20:38 +00:00
|
|
|
with patch_yaml_files(files):
|
|
|
|
res = check_config.check(get_test_config_dir())
|
2019-07-31 19:25:30 +00:00
|
|
|
assert res["except"].keys() == {"homeassistant"}
|
|
|
|
assert res["except"]["homeassistant"][1] == {"unit_system": "bad"}
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
2021-09-03 17:15:57 +00:00
|
|
|
def test_config_platform_valid(mock_is_file, loop):
|
2019-04-30 16:20:38 +00:00
|
|
|
"""Test a valid platform setup."""
|
2019-07-31 19:25:30 +00:00
|
|
|
files = {YAML_CONFIG_FILE: BASE_CONFIG + "light:\n platform: demo"}
|
2019-04-30 16:20:38 +00:00
|
|
|
with patch_yaml_files(files):
|
|
|
|
res = check_config.check(get_test_config_dir())
|
2019-07-31 19:25:30 +00:00
|
|
|
assert res["components"].keys() == {"homeassistant", "light"}
|
|
|
|
assert res["components"]["light"] == [{"platform": "demo"}]
|
|
|
|
assert res["except"] == {}
|
|
|
|
assert res["secret_cache"] == {}
|
|
|
|
assert res["secrets"] == {}
|
|
|
|
assert len(res["yaml_files"]) == 1
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
2021-09-03 17:15:57 +00:00
|
|
|
def test_component_platform_not_found(mock_is_file, loop):
|
2019-04-30 16:20:38 +00:00
|
|
|
"""Test errors if component or platform not found."""
|
|
|
|
# Make sure they don't exist
|
2019-07-31 19:25:30 +00:00
|
|
|
files = {YAML_CONFIG_FILE: BASE_CONFIG + "beer:"}
|
2019-04-30 16:20:38 +00:00
|
|
|
with patch_yaml_files(files):
|
|
|
|
res = check_config.check(get_test_config_dir())
|
2019-07-31 19:25:30 +00:00
|
|
|
assert res["components"].keys() == {"homeassistant"}
|
|
|
|
assert res["except"] == {
|
2019-08-07 22:35:50 +00:00
|
|
|
check_config.ERROR_STR: [
|
2021-09-18 05:25:50 +00:00
|
|
|
"Integration error: beer - Integration 'beer' not found."
|
2019-08-07 22:35:50 +00:00
|
|
|
]
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
assert res["secret_cache"] == {}
|
|
|
|
assert res["secrets"] == {}
|
|
|
|
assert len(res["yaml_files"]) == 1
|
2019-04-30 16:20:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
files = {YAML_CONFIG_FILE: BASE_CONFIG + "light:\n platform: beer"}
|
2019-04-30 16:20:38 +00:00
|
|
|
with patch_yaml_files(files):
|
|
|
|
res = check_config.check(get_test_config_dir())
|
2019-07-31 19:25:30 +00:00
|
|
|
assert res["components"].keys() == {"homeassistant", "light"}
|
|
|
|
assert res["components"]["light"] == []
|
|
|
|
assert res["except"] == {
|
2019-04-30 16:20:38 +00:00
|
|
|
check_config.ERROR_STR: [
|
2019-09-27 15:48:48 +00:00
|
|
|
"Platform error light.beer - Integration 'beer' not found."
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
assert res["secret_cache"] == {}
|
|
|
|
assert res["secrets"] == {}
|
|
|
|
assert len(res["yaml_files"]) == 1
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
2021-09-03 17:15:57 +00:00
|
|
|
def test_secrets(mock_is_file, loop):
|
2019-04-30 16:20:38 +00:00
|
|
|
"""Test secrets config checking method."""
|
2019-07-31 19:25:30 +00:00
|
|
|
secrets_path = get_test_config_dir("secrets.yaml")
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
files = {
|
2019-07-31 19:25:30 +00:00
|
|
|
get_test_config_dir(YAML_CONFIG_FILE): BASE_CONFIG
|
2020-01-02 19:17:10 +00:00
|
|
|
+ ("http:\n cors_allowed_origins: !secret http_pw"),
|
|
|
|
secrets_path: ("logger: debug\nhttp_pw: http://google.com"),
|
2019-04-30 16:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
with patch_yaml_files(files):
|
|
|
|
|
|
|
|
res = check_config.check(get_test_config_dir(), True)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert res["except"] == {}
|
|
|
|
assert res["components"].keys() == {"homeassistant", "http"}
|
|
|
|
assert res["components"]["http"] == {
|
2019-10-14 21:56:45 +00:00
|
|
|
"cors_allowed_origins": ["http://google.com"],
|
2019-07-31 19:25:30 +00:00
|
|
|
"ip_ban_enabled": True,
|
|
|
|
"login_attempts_threshold": -1,
|
|
|
|
"server_port": 8123,
|
|
|
|
"ssl_profile": "modern",
|
2018-08-17 03:28:00 +00:00
|
|
|
}
|
2019-10-14 21:56:45 +00:00
|
|
|
assert res["secret_cache"] == {secrets_path: {"http_pw": "http://google.com"}}
|
|
|
|
assert res["secrets"] == {"http_pw": "http://google.com"}
|
2019-04-30 16:20:38 +00:00
|
|
|
assert normalize_yaml_files(res) == [
|
2019-07-31 19:25:30 +00:00
|
|
|
".../configuration.yaml",
|
|
|
|
".../secrets.yaml",
|
|
|
|
]
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
2021-09-03 17:15:57 +00:00
|
|
|
def test_package_invalid(mock_is_file, loop):
|
2020-01-14 21:03:02 +00:00
|
|
|
"""Test an invalid package."""
|
2019-04-30 16:20:38 +00:00
|
|
|
files = {
|
2020-01-02 19:17:10 +00:00
|
|
|
YAML_CONFIG_FILE: BASE_CONFIG + (" packages:\n p1:\n" ' group: ["a"]')
|
2019-04-30 16:20:38 +00:00
|
|
|
}
|
|
|
|
with patch_yaml_files(files):
|
|
|
|
res = check_config.check(get_test_config_dir())
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert res["except"].keys() == {"homeassistant.packages.p1.group"}
|
|
|
|
assert res["except"]["homeassistant.packages.p1.group"][1] == {"group": ["a"]}
|
|
|
|
assert len(res["except"]) == 1
|
|
|
|
assert res["components"].keys() == {"homeassistant"}
|
|
|
|
assert len(res["components"]) == 1
|
|
|
|
assert res["secret_cache"] == {}
|
|
|
|
assert res["secrets"] == {}
|
|
|
|
assert len(res["yaml_files"]) == 1
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_bootstrap_error(loop):
|
|
|
|
"""Test a valid platform setup."""
|
2019-07-31 19:25:30 +00:00
|
|
|
files = {YAML_CONFIG_FILE: BASE_CONFIG + "automation: !include no.yaml"}
|
2019-04-30 16:20:38 +00:00
|
|
|
with patch_yaml_files(files):
|
|
|
|
res = check_config.check(get_test_config_dir(YAML_CONFIG_FILE))
|
2019-07-31 19:25:30 +00:00
|
|
|
err = res["except"].pop(check_config.ERROR_STR)
|
2019-04-30 16:20:38 +00:00
|
|
|
assert len(err) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert res["except"] == {}
|
|
|
|
assert res["components"] == {} # No components, load failed
|
|
|
|
assert res["secret_cache"] == {}
|
|
|
|
assert res["secrets"] == {}
|
|
|
|
assert res["yaml_files"] == {}
|