-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Add system health to Portainer #169698
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
Open
erwindouna
wants to merge
2
commits into
home-assistant:dev
Choose a base branch
from
erwindouna:portainer-add-system-health
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+92
−0
Open
Add system health to Portainer #169698
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """Provide info to system health.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from homeassistant.components import system_health | ||
| from homeassistant.const import CONF_URL | ||
| from homeassistant.core import HomeAssistant, callback | ||
|
|
||
| from .const import DOMAIN | ||
|
|
||
|
|
||
| @callback | ||
| def async_register( | ||
| hass: HomeAssistant, register: system_health.SystemHealthRegistration | ||
| ) -> None: | ||
| """Register system health callbacks.""" | ||
| register.async_register_info(system_health_info) | ||
|
|
||
|
|
||
| async def system_health_info(hass: HomeAssistant) -> dict[str, Any]: | ||
| """Get info for the info page.""" | ||
| config_entry = hass.config_entries.async_entries(DOMAIN)[0] | ||
|
|
||
| return { | ||
| "can_reach_server": system_health.async_check_can_reach_url( | ||
| hass, f"{config_entry.data[CONF_URL].rstrip('/')}/api/system/status" | ||
| ), | ||
|
Comment on lines
+24
to
+27
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """Test Portainer system health.""" | ||
|
|
||
| import asyncio | ||
|
|
||
| from aiohttp import ClientError | ||
|
|
||
| from homeassistant.components.portainer.const import DOMAIN | ||
| from homeassistant.config_entries import ConfigEntryState | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.setup import async_setup_component | ||
|
|
||
| from .conftest import MOCK_TEST_CONFIG, TEST_ENTRY, TEST_INSTANCE_ID | ||
|
|
||
| from tests.common import MockConfigEntry, get_system_health_info | ||
| from tests.test_util.aiohttp import AiohttpClientMocker | ||
|
|
||
| MOCK_HEALTH_URL = "https://127.0.0.1:9000/api/system/status" | ||
|
|
||
|
|
||
| async def test_system_health( | ||
| hass: HomeAssistant, | ||
| aioclient_mock: AiohttpClientMocker, | ||
| ) -> None: | ||
| """Test system health when server is reachable.""" | ||
| aioclient_mock.get(MOCK_HEALTH_URL, text="ok") | ||
|
|
||
| hass.config.components.add(DOMAIN) | ||
| assert await async_setup_component(hass, "system_health", {}) | ||
| await hass.async_block_till_done() | ||
|
erwindouna marked this conversation as resolved.
Outdated
|
||
|
|
||
| entry = MockConfigEntry( | ||
| domain=DOMAIN, | ||
| data=MOCK_TEST_CONFIG, | ||
| unique_id=TEST_INSTANCE_ID, | ||
| entry_id=TEST_ENTRY, | ||
| state=ConfigEntryState.LOADED, | ||
| ) | ||
|
erwindouna marked this conversation as resolved.
Outdated
|
||
| entry.add_to_hass(hass) | ||
|
|
||
| info = await get_system_health_info(hass, DOMAIN) | ||
|
|
||
| for key, val in info.items(): | ||
| if asyncio.iscoroutine(val): | ||
| info[key] = await val | ||
|
|
||
| assert info["can_reach_server"] == "ok" | ||
|
|
||
|
|
||
| async def test_system_health_failed_connect( | ||
| hass: HomeAssistant, | ||
| aioclient_mock: AiohttpClientMocker, | ||
| ) -> None: | ||
| """Test system health when server is unreachable.""" | ||
| aioclient_mock.get(MOCK_HEALTH_URL, exc=ClientError) | ||
|
|
||
| hass.config.components.add(DOMAIN) | ||
| assert await async_setup_component(hass, "system_health", {}) | ||
| await hass.async_block_till_done() | ||
|
erwindouna marked this conversation as resolved.
Outdated
|
||
|
|
||
| entry = MockConfigEntry( | ||
| domain=DOMAIN, | ||
| data=MOCK_TEST_CONFIG, | ||
| unique_id=TEST_INSTANCE_ID, | ||
| entry_id=TEST_ENTRY, | ||
| state=ConfigEntryState.LOADED, | ||
| ) | ||
|
|
||
| entry.add_to_hass(hass) | ||
|
|
||
| info = await get_system_health_info(hass, DOMAIN) | ||
|
|
||
| for key, val in info.items(): | ||
| if asyncio.iscoroutine(val): | ||
| info[key] = await val | ||
|
|
||
| assert info["can_reach_server"] == {"error": "unreachable", "type": "failed"} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.