Skip to content

Commit

Permalink
Add system health tests (#3604)
Browse files Browse the repository at this point in the history
* Add system health tests

* Update snapshots
  • Loading branch information
emontnemery authored Apr 11, 2024
1 parent 338aaab commit 5de7986
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
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"},
),
)

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)

0 comments on commit 5de7986

Please sign in to comment.