-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test: Exception handling in PingDataSubProcess.async_ping
Tests that expected exceptions are handled, but unexpected exceptions are still raised. Patches out asyncio.create_subprocess_exec to generate exceptions, which also means that the external ping command is not called during these tests.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,55 @@ | ||
"""Test the exception handling in subprocess version of async_ping.""" | ||
|
||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from homeassistant.components.ping.helpers import PingDataSubProcess | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers import entity_registry as er | ||
|
||
from tests.common import MockConfigEntry | ||
|
||
|
||
class MockAsyncSubprocess: | ||
"""Minimal mock implementation of asyncio.subprocess.Process for exception testing.""" | ||
|
||
def __init__(self, killsig=ProcessLookupError, **kwargs) -> None: | ||
"""Store provided exception type for later.""" | ||
self.killsig = killsig | ||
|
||
async def communicate(self) -> None: | ||
"""Fails immediately with a timeout.""" | ||
raise TimeoutError | ||
|
||
async def kill(self) -> None: | ||
"""Raise preset exception when called.""" | ||
raise self.killsig | ||
|
||
|
||
async def test_async_ping_expected_exceptions( | ||
hass: HomeAssistant, | ||
config_entry: MockConfigEntry, | ||
entity_registry: er.EntityRegistry, | ||
) -> None: | ||
"""Test PingDataSubProcess.async_ping handles expected exceptions.""" | ||
with patch("asyncio.create_subprocess_exec", return_value=MockAsyncSubprocess()): | ||
# Actual parameters irrelevant, as subprocess will not be created | ||
ping = PingDataSubProcess(hass, host="10.10.10.10", count=3, privileged=False) | ||
assert await ping.async_ping() is None | ||
|
||
|
||
async def test_async_ping_unexpected_exceptions( | ||
hass: HomeAssistant, | ||
config_entry: MockConfigEntry, | ||
entity_registry: er.EntityRegistry, | ||
) -> None: | ||
"""Test PingDataSubProcess.async_ping does not suppress unexpected exceptions.""" | ||
with patch( | ||
"asyncio.create_subprocess_exec", | ||
return_value=MockAsyncSubprocess(killsig=KeyboardInterrupt), | ||
): | ||
# Actual parameters irrelevant, as subprocess will not be created | ||
ping = PingDataSubProcess(hass, host="10.10.10.10", count=3, privileged=False) | ||
with pytest.raises(KeyboardInterrupt): | ||
assert await ping.async_ping() is None |