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

Add system health tests #3604

Merged
merged 2 commits into from
Apr 11, 2024
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
19 changes: 19 additions & 0 deletions tests/output/proxy_calls.json
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,25 @@
"https://api.github.com/repos/hacs/integration/git/trees/main": 1,
"https://api.github.com/repos/hacs/integration/releases": 1
},
"tests/test_system_health.py::test_system_health": {
"https://api.github.com": 1,
"https://api.github.com/rate_limit": 1,
"https://api.github.com/repos/hacs/integration": 1,
"https://api.github.com/repos/hacs/integration/contents/custom_components/hacs/manifest.json": 1,
"https://api.github.com/repos/hacs/integration/contents/hacs.json": 1,
"https://api.github.com/repos/hacs/integration/git/trees/main": 1,
"https://api.github.com/repos/hacs/integration/releases": 1,
"https://data-v2.hacs.xyz/data.json": 1,
"https://github.com/": 1,
"https://raw.githubusercontent.com/hacs/integration/main/hacs.json": 1
},
"tests/test_system_health.py::test_system_health_after_unload": {
"https://api.github.com/repos/hacs/integration": 1,
"https://api.github.com/repos/hacs/integration/contents/custom_components/hacs/manifest.json": 1,
"https://api.github.com/repos/hacs/integration/contents/hacs.json": 1,
"https://api.github.com/repos/hacs/integration/git/trees/main": 1,
"https://api.github.com/repos/hacs/integration/releases": 1
},
"tests/utils/test_path.py::test_is_safe": {
"https://api.github.com/repos/hacs/integration": 1,
"https://api.github.com/repos/hacs/integration/contents/custom_components/hacs/manifest.json": 1,
Expand Down
11 changes: 11 additions & 0 deletions tests/snapshots/system_health/system_health.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Available Repositories": 7,
"Downloaded Repositories": 1,
"GitHub API": "ok",
"GitHub API Calls Remaining": 4999,
"GitHub Content": "ok",
"GitHub Web": "ok",
"HACS Data": "ok",
"Installed Version": "0.0.0",
"Stage": "running"
}
87 changes: 87 additions & 0 deletions tests/test_system_health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""Test system health."""

import asyncio
from typing import Any, Generator

from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
import pytest

from custom_components.hacs.base import HacsBase

from tests.common import MockedResponse, ResponseMocker, safe_json_dumps
from tests.conftest import SnapshotFixture


HACS_SYSTEM_HEALTH_DOMAIN = "Home Assistant Community Store"


async def get_system_health_info(hass: HomeAssistant, domain: str) -> dict[str, Any]:
"""Get system health info."""
return await hass.data["system_health"][domain].info_callback(hass)


async def test_system_health(
setup_integration: Generator,
hass: HomeAssistant,
response_mocker: ResponseMocker,
snapshots: SnapshotFixture,
) -> None:
"""Test HACS system health."""
response_mocker.add(
url="https://api.github.com",
response=MockedResponse(
content={},
headers={"Content-Type": "application/json"},
),
)
response_mocker.add(
url="https://raw.githubusercontent.com/hacs/integration/main/hacs.json",
response=MockedResponse(
content={},
headers={"Content-Type": "application/json"},
),
)
response_mocker.add(
url="https://data-v2.hacs.xyz/data.json",
response=MockedResponse(
content={},
headers={"Content-Type": "application/json"},
),
)
ludeeus marked this conversation as resolved.
Show resolved Hide resolved

assert await async_setup_component(hass, "system_health", {})
await hass.async_block_till_done()

info = await get_system_health_info(hass, HACS_SYSTEM_HEALTH_DOMAIN)

for key, val in info.items():
if asyncio.iscoroutine(val):
info[key] = await val

snapshots.assert_match(safe_json_dumps(info), "system_health/system_health.json")


async def test_system_health_after_unload(
hacs: HacsBase,
hass: HomeAssistant,
) -> None:
"""Test HACS system health."""
await hass.config_entries.async_unload(hacs.configuration.config_entry.entry_id)

assert await async_setup_component(hass, "system_health", {})
await hass.async_block_till_done()

with pytest.raises(KeyError, match="hacs"):
await get_system_health_info(hass, HACS_SYSTEM_HEALTH_DOMAIN)


async def test_system_health_no_hacs(
hass: HomeAssistant,
) -> None:
"""Test HACS system health."""
assert await async_setup_component(hass, "system_health", {})
await hass.async_block_till_done()

with pytest.raises(KeyError, match=HACS_SYSTEM_HEALTH_DOMAIN):
await get_system_health_info(hass, HACS_SYSTEM_HEALTH_DOMAIN)
Loading