RFC: Add system health component (#20436)

* Add system health component

* Remove stale comment

* Fix confusing syntax

* Update test_init.py

* Address comments

* Lint

* Move distro check to updater

* Convert to websocket

* Lint

* Make info callback async

* Fix tests

* Fix tests

* Lint

* Catch exceptions
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2019-01-30 10:57:53 -08:00
1 parent 91aa874c0c
commit cb07ea0d60
10 files changed
+283 -65

No files matched your search

@@ -0,0 +1 @@
"""Tests for the system health component."""
+105
View File
@@ -0,0 +1,105 @@
"""Tests for the system health component init."""
import asyncio
from unittest.mock import Mock
import pytest
from homeassistant.setup import async_setup_component
from tests.common import mock_coro
@pytest.fixture
def mock_system_info(hass):
"""Mock system info."""
hass.helpers.system_info.async_get_system_info = Mock(
return_value=mock_coro({'hello': True})
)
async def test_info_endpoint_return_info(hass, hass_ws_client,
mock_system_info):
"""Test that the info endpoint works."""
assert await async_setup_component(hass, 'system_health', {})
client = await hass_ws_client(hass)
resp = await client.send_json({
'id': 6,
'type': 'system_health/info',
})
resp = await client.receive_json()
assert resp['success']
data = resp['result']
assert len(data) == 1
data = data['homeassistant']
assert data == {'hello': True}
async def test_info_endpoint_register_callback(hass, hass_ws_client,
mock_system_info):
"""Test that the info endpoint allows registering callbacks."""
async def mock_info(hass):
return {'storage': 'YAML'}
hass.components.system_health.async_register_info('lovelace', mock_info)
assert await async_setup_component(hass, 'system_health', {})
client = await hass_ws_client(hass)
resp = await client.send_json({
'id': 6,
'type': 'system_health/info',
})
resp = await client.receive_json()
assert resp['success']
data = resp['result']
assert len(data) == 2
data = data['lovelace']
assert data == {'storage': 'YAML'}
async def test_info_endpoint_register_callback_timeout(hass, hass_ws_client,
mock_system_info):
"""Test that the info endpoint timing out."""
async def mock_info(hass):
raise asyncio.TimeoutError
hass.components.system_health.async_register_info('lovelace', mock_info)
assert await async_setup_component(hass, 'system_health', {})
client = await hass_ws_client(hass)
resp = await client.send_json({
'id': 6,
'type': 'system_health/info',
})
resp = await client.receive_json()
assert resp['success']
data = resp['result']
assert len(data) == 2
data = data['lovelace']
assert data == {'error': 'Fetching info timed out'}
async def test_info_endpoint_register_callback_exc(hass, hass_ws_client,
mock_system_info):
"""Test that the info endpoint requires auth."""
async def mock_info(hass):
raise Exception("TEST ERROR")
hass.components.system_health.async_register_info('lovelace', mock_info)
assert await async_setup_component(hass, 'system_health', {})
client = await hass_ws_client(hass)
resp = await client.send_json({
'id': 6,
'type': 'system_health/info',
})
resp = await client.receive_json()
assert resp['success']
data = resp['result']
assert len(data) == 2
data = data['lovelace']
assert data == {'error': 'TEST ERROR'}
+14 -24
View File
@@ -8,7 +8,8 @@ import pytest
from homeassistant.setup import async_setup_component
from homeassistant.components import updater
import homeassistant.util.dt as dt_util
from tests.common import async_fire_time_changed, mock_coro, mock_component
from tests.common import (
async_fire_time_changed, mock_coro, mock_component, MockDependency)
NEW_VERSION = '10000.0'
MOCK_VERSION = '10.0'
@@ -23,6 +24,13 @@ MOCK_CONFIG = {updater.DOMAIN: {
}}
@pytest.fixture(autouse=True)
def mock_distro():
"""Mock distro dep."""
with MockDependency('distro'):
yield
@pytest.fixture
def mock_get_newest_version():
"""Fixture to mock get_newest_version."""
@@ -99,30 +107,12 @@ def test_disable_reporting(hass, mock_get_uuid, mock_get_newest_version):
assert call[1] is None
@asyncio.coroutine
def test_enabled_component_info(hass, mock_get_uuid):
"""Test if new entity is created if new version is available."""
with patch('homeassistant.components.updater.platform.system',
Mock(return_value="junk")):
res = yield from updater.get_system_info(hass, True)
assert 'components' in res, 'Updater failed to generate component list'
@asyncio.coroutine
def test_disable_component_info(hass, mock_get_uuid):
"""Test if new entity is created if new version is available."""
with patch('homeassistant.components.updater.platform.system',
Mock(return_value="junk")):
res = yield from updater.get_system_info(hass, False)
assert 'components' not in res, 'Updater failed, components generate'
@asyncio.coroutine
def test_get_newest_version_no_analytics_when_no_huuid(hass, aioclient_mock):
"""Test we do not gather analytics when no huuid is passed in."""
aioclient_mock.post(updater.UPDATER_URL, json=MOCK_RESPONSE)
with patch('homeassistant.components.updater.get_system_info',
with patch('homeassistant.helpers.system_info.async_get_system_info',
side_effect=Exception):
res = yield from updater.get_newest_version(hass, None, False)
assert res == (MOCK_RESPONSE['version'],
@@ -134,7 +124,7 @@ def test_get_newest_version_analytics_when_huuid(hass, aioclient_mock):
"""Test we do not gather analytics when no huuid is passed in."""
aioclient_mock.post(updater.UPDATER_URL, json=MOCK_RESPONSE)
with patch('homeassistant.components.updater.get_system_info',
with patch('homeassistant.helpers.system_info.async_get_system_info',
Mock(return_value=mock_coro({'fake': 'bla'}))):
res = yield from updater.get_newest_version(hass, MOCK_HUUID, False)
assert res == (MOCK_RESPONSE['version'],
@@ -144,7 +134,7 @@ def test_get_newest_version_analytics_when_huuid(hass, aioclient_mock):
@asyncio.coroutine
def test_error_fetching_new_version_timeout(hass):
"""Test we do not gather analytics when no huuid is passed in."""
with patch('homeassistant.components.updater.get_system_info',
with patch('homeassistant.helpers.system_info.async_get_system_info',
Mock(return_value=mock_coro({'fake': 'bla'}))), \
patch('async_timeout.timeout', side_effect=asyncio.TimeoutError):
res = yield from updater.get_newest_version(hass, MOCK_HUUID, False)
@@ -156,7 +146,7 @@ def test_error_fetching_new_version_bad_json(hass, aioclient_mock):
"""Test we do not gather analytics when no huuid is passed in."""
aioclient_mock.post(updater.UPDATER_URL, text='not json')
with patch('homeassistant.components.updater.get_system_info',
with patch('homeassistant.helpers.system_info.async_get_system_info',
Mock(return_value=mock_coro({'fake': 'bla'}))):
res = yield from updater.get_newest_version(hass, MOCK_HUUID, False)
assert res is None
@@ -170,7 +160,7 @@ def test_error_fetching_new_version_invalid_response(hass, aioclient_mock):
# 'release-notes' is missing
})
with patch('homeassistant.components.updater.get_system_info',
with patch('homeassistant.helpers.system_info.async_get_system_info',
Mock(return_value=mock_coro({'fake': 'bla'}))):
res = yield from updater.get_newest_version(hass, MOCK_HUUID, False)
assert res is None