Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform check_config service in current process #13017

Merged
merged 2 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions homeassistant/config.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""Module to help with parsing and generating configuration files."""
import asyncio
from collections import OrderedDict
# pylint: disable=no-name-in-module
from distutils.version import LooseVersion # pylint: disable=import-error
import logging
import os
import re
import shutil
import sys
# pylint: disable=unused-import
from typing import Any, List, Tuple # NOQA

Expand Down Expand Up @@ -665,22 +663,14 @@ async def async_check_ha_config_file(hass):

This method is a coroutine.
"""
proc = await asyncio.create_subprocess_exec(
sys.executable, '-m', 'homeassistant', '--script',
'check_config', '--config', hass.config.config_dir,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT, loop=hass.loop)

# Wait for the subprocess exit
log, _ = await proc.communicate()
exit_code = await proc.wait()

# Convert to ASCII
log = RE_ASCII.sub('', log.decode())

if exit_code != 0 or RE_YAML_ERROR.search(log):
return log
return None
from homeassistant.scripts.check_config import check_ha_config_file

res = await hass.async_add_job(
check_ha_config_file, hass.config.config_dir)

if not res.errors:
return None
return '\n'.join([err.message for err in res.errors])


@callback
Expand Down
34 changes: 12 additions & 22 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
CONFIG_PATH as SCRIPTS_CONFIG_PATH)
from homeassistant.components.config.customize import (
CONFIG_PATH as CUSTOMIZE_CONFIG_PATH)
import homeassistant.scripts.check_config as check_config

from tests.common import (
get_test_config_dir, get_test_home_assistant, mock_coro)
from tests.common import get_test_config_dir, get_test_home_assistant

CONFIG_DIR = get_test_config_dir()
YAML_PATH = os.path.join(CONFIG_DIR, config_util.YAML_CONFIG_FILE)
Expand Down Expand Up @@ -514,35 +514,25 @@ def test_discovering_configuration_auto_detect_fails(self, mock_detect,
assert len(self.hass.config.whitelist_external_dirs) == 1
assert "/test/config/www" in self.hass.config.whitelist_external_dirs

@mock.patch('asyncio.create_subprocess_exec')
def test_check_ha_config_file_correct(self, mock_create):
@mock.patch('homeassistant.scripts.check_config.check_ha_config_file')
def test_check_ha_config_file_correct(self, mock_check):
"""Check that restart propagates to stop."""
process_mock = mock.MagicMock()
attrs = {
'communicate.return_value': mock_coro((b'output', None)),
'wait.return_value': mock_coro(0)}
process_mock.configure_mock(**attrs)
mock_create.return_value = mock_coro(process_mock)

mock_check.return_value = check_config.HomeAssistantConfig()
assert run_coroutine_threadsafe(
config_util.async_check_ha_config_file(self.hass), self.hass.loop
config_util.async_check_ha_config_file(self.hass),
self.hass.loop
).result() is None

@mock.patch('asyncio.create_subprocess_exec')
def test_check_ha_config_file_wrong(self, mock_create):
@mock.patch('homeassistant.scripts.check_config.check_ha_config_file')
def test_check_ha_config_file_wrong(self, mock_check):
"""Check that restart with a bad config doesn't propagate to stop."""
process_mock = mock.MagicMock()
attrs = {
'communicate.return_value':
mock_coro(('\033[34mhello'.encode('utf-8'), None)),
'wait.return_value': mock_coro(1)}
process_mock.configure_mock(**attrs)
mock_create.return_value = mock_coro(process_mock)
mock_check.return_value = check_config.HomeAssistantConfig()
mock_check.return_value.add_error("bad")

assert run_coroutine_threadsafe(
config_util.async_check_ha_config_file(self.hass),
self.hass.loop
).result() == 'hello'
).result() == 'bad'


# pylint: disable=redefined-outer-name
Expand Down