core/tests/components/command_line/test_notify.py

124 lines
3.7 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the command line notification platform."""
from __future__ import annotations
import os
2021-03-01 16:27:04 +00:00
import subprocess
import tempfile
from typing import Any
2021-01-01 21:31:56 +00:00
from unittest.mock import patch
from pytest import LogCaptureFixture
2021-03-01 16:27:04 +00:00
from homeassistant import setup
from homeassistant.components.notify import DOMAIN
from homeassistant.core import HomeAssistant
async def setup_test_service(hass: HomeAssistant, config_dict: dict[str, Any]) -> None:
2021-03-01 16:27:04 +00:00
"""Set up a test command line notify service."""
assert await setup.async_setup_component(
hass,
2021-03-01 16:27:04 +00:00
DOMAIN,
{
2021-03-01 16:27:04 +00:00
DOMAIN: [
{"platform": "command_line", "name": "Test", **config_dict},
]
},
)
await hass.async_block_till_done()
2021-03-01 16:27:04 +00:00
async def test_setup(hass: HomeAssistant) -> None:
2021-03-01 16:27:04 +00:00
"""Test sensor setup."""
await setup_test_service(hass, {"command": "exit 0"})
assert hass.services.has_service(DOMAIN, "test")
async def test_bad_config(hass: HomeAssistant) -> None:
2021-03-01 16:27:04 +00:00
"""Test set up the platform with bad/missing configuration."""
await setup_test_service(hass, {})
assert not hass.services.has_service(DOMAIN, "test")
async def test_command_line_output(hass: HomeAssistant) -> None:
2021-03-01 16:27:04 +00:00
"""Test the command line output."""
with tempfile.TemporaryDirectory() as tempdirname:
filename = os.path.join(tempdirname, "message.txt")
message = "one, two, testing, testing"
await setup_test_service(
hass,
{
"command": f"cat > {filename}",
},
)
assert hass.services.has_service(DOMAIN, "test")
assert await hass.services.async_call(
DOMAIN, "test", {"message": message}, blocking=True
)
with open(filename) as handle:
# the echo command adds a line break
assert message == handle.read()
async def test_error_for_none_zero_exit_code(
caplog: LogCaptureFixture, hass: HomeAssistant
) -> None:
2021-03-01 16:27:04 +00:00
"""Test if an error is logged for non zero exit codes."""
await setup_test_service(
hass,
{
"command": "exit 1",
},
)
assert await hass.services.async_call(
2021-03-01 16:27:04 +00:00
DOMAIN, "test", {"message": "error"}, blocking=True
)
assert "Command failed" in caplog.text
async def test_timeout(caplog: LogCaptureFixture, hass: HomeAssistant) -> None:
2021-03-01 16:27:04 +00:00
"""Test blocking is not forever."""
await setup_test_service(
hass,
{
"command": "sleep 10000",
"command_timeout": 0.0000001,
},
)
assert await hass.services.async_call(
DOMAIN, "test", {"message": "error"}, blocking=True
)
assert "Timeout" in caplog.text
2021-03-01 16:27:04 +00:00
async def test_subprocess_exceptions(
caplog: LogCaptureFixture, hass: HomeAssistant
) -> None:
2021-03-01 16:27:04 +00:00
"""Test that notify subprocess exceptions are handled correctly."""
with patch(
2021-04-25 00:39:24 +00:00
"homeassistant.components.command_line.notify.subprocess.Popen"
) as check_output:
check_output.return_value.__enter__ = check_output
check_output.return_value.communicate.side_effect = [
2021-03-01 16:27:04 +00:00
subprocess.TimeoutExpired("cmd", 10),
2021-04-25 00:39:24 +00:00
None,
2021-03-01 16:27:04 +00:00
subprocess.SubprocessError(),
2021-04-25 00:39:24 +00:00
]
2021-03-01 16:27:04 +00:00
await setup_test_service(hass, {"command": "exit 0"})
assert await hass.services.async_call(
DOMAIN, "test", {"message": "error"}, blocking=True
)
2021-04-25 00:39:24 +00:00
assert check_output.call_count == 2
2021-03-01 16:27:04 +00:00
assert "Timeout for command" in caplog.text
assert await hass.services.async_call(
DOMAIN, "test", {"message": "error"}, blocking=True
)
2021-04-25 00:39:24 +00:00
assert check_output.call_count == 4
2021-03-01 16:27:04 +00:00
assert "Error trying to exec command" in caplog.text