mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 08:47:10 +00:00
Improve test creating mqtt certificate files (#102380)
* Improve test creating mqtt certificate files * Split tests * Cleanup and de-duplicate * Update tests/components/mqtt/test_util.py
This commit is contained in:
parent
712c061ac0
commit
fe8fb8928c
@ -3,6 +3,7 @@
|
|||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from random import getrandbits
|
from random import getrandbits
|
||||||
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
@ -16,62 +17,94 @@ from tests.common import MockConfigEntry
|
|||||||
from tests.typing import MqttMockHAClient, MqttMockPahoClient
|
from tests.typing import MqttMockHAClient, MqttMockPahoClient
|
||||||
|
|
||||||
|
|
||||||
|
async def help_create_test_certificate_file(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_temp_dir: str,
|
||||||
|
option: str,
|
||||||
|
content: bytes = b"old content",
|
||||||
|
) -> None:
|
||||||
|
"""Help creating a certificate test file."""
|
||||||
|
temp_dir = Path(tempfile.gettempdir()) / mock_temp_dir
|
||||||
|
|
||||||
|
def _create_file() -> None:
|
||||||
|
if not temp_dir.exists():
|
||||||
|
temp_dir.mkdir(0o700)
|
||||||
|
temp_file = temp_dir / option
|
||||||
|
with open(temp_file, "wb") as old_file:
|
||||||
|
old_file.write(content)
|
||||||
|
old_file.close()
|
||||||
|
|
||||||
|
await hass.async_add_executor_job(_create_file)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("option", "content", "file_created"),
|
("option", "content"),
|
||||||
[
|
[
|
||||||
(mqtt.CONF_CERTIFICATE, "auto", False),
|
(mqtt.CONF_CERTIFICATE, "### CA CERTIFICATE ###"),
|
||||||
(mqtt.CONF_CERTIFICATE, "### CA CERTIFICATE ###", True),
|
(mqtt.CONF_CLIENT_CERT, "### CLIENT CERTIFICATE ###"),
|
||||||
(mqtt.CONF_CLIENT_CERT, "### CLIENT CERTIFICATE ###", True),
|
(mqtt.CONF_CLIENT_KEY, "### PRIVATE KEY ###"),
|
||||||
(mqtt.CONF_CLIENT_KEY, "### PRIVATE KEY ###", True),
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@pytest.mark.parametrize("temp_dir_prefix", ["create-test"])
|
@pytest.mark.parametrize("temp_dir_prefix", ["create-test1"])
|
||||||
async def test_async_create_certificate_temp_files(
|
async def test_async_create_certificate_temp_files(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_temp_dir: str,
|
mock_temp_dir: str,
|
||||||
option: str,
|
option: str,
|
||||||
content: str,
|
content: str,
|
||||||
file_created: bool,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test creating and reading and recovery certificate files."""
|
"""Test creating and reading and recovery certificate files."""
|
||||||
config = {option: content}
|
config = {option: content}
|
||||||
|
|
||||||
temp_dir = Path(tempfile.gettempdir()) / mock_temp_dir
|
# Create old file to be able to assert it is replaced and recovered
|
||||||
|
await help_create_test_certificate_file(hass, mock_temp_dir, option)
|
||||||
# Create old file to be able to assert it is removed with auto option
|
|
||||||
def _ensure_old_file_exists() -> None:
|
|
||||||
if not temp_dir.exists():
|
|
||||||
temp_dir.mkdir(0o700)
|
|
||||||
temp_file = temp_dir / option
|
|
||||||
with open(temp_file, "wb") as old_file:
|
|
||||||
old_file.write(b"old content")
|
|
||||||
old_file.close()
|
|
||||||
|
|
||||||
await hass.async_add_executor_job(_ensure_old_file_exists)
|
|
||||||
await mqtt.util.async_create_certificate_temp_files(hass, config)
|
await mqtt.util.async_create_certificate_temp_files(hass, config)
|
||||||
file_path = await hass.async_add_executor_job(mqtt.util.get_file_path, option)
|
file_path = await hass.async_add_executor_job(mqtt.util.get_file_path, option)
|
||||||
assert bool(file_path) is file_created
|
assert file_path is not None
|
||||||
assert (
|
assert (
|
||||||
await hass.async_add_executor_job(
|
await hass.async_add_executor_job(
|
||||||
mqtt.util.migrate_certificate_file_to_content, file_path or content
|
mqtt.util.migrate_certificate_file_to_content, file_path
|
||||||
)
|
)
|
||||||
== content
|
== content
|
||||||
)
|
)
|
||||||
|
|
||||||
# Make sure certificate temp files are recovered
|
# Make sure old files are removed to test certificate and dir creation
|
||||||
await hass.async_add_executor_job(_ensure_old_file_exists)
|
def _remove_old_files() -> None:
|
||||||
|
temp_dir = Path(tempfile.gettempdir()) / mock_temp_dir
|
||||||
|
shutil.rmtree(temp_dir)
|
||||||
|
|
||||||
|
await hass.async_add_executor_job(_remove_old_files)
|
||||||
|
|
||||||
|
# Test a new dir and file is created correctly
|
||||||
await mqtt.util.async_create_certificate_temp_files(hass, config)
|
await mqtt.util.async_create_certificate_temp_files(hass, config)
|
||||||
file_path2 = await hass.async_add_executor_job(mqtt.util.get_file_path, option)
|
file_path = await hass.async_add_executor_job(mqtt.util.get_file_path, option)
|
||||||
assert bool(file_path2) is file_created
|
assert file_path is not None
|
||||||
assert (
|
assert (
|
||||||
await hass.async_add_executor_job(
|
await hass.async_add_executor_job(
|
||||||
mqtt.util.migrate_certificate_file_to_content, file_path2 or content
|
mqtt.util.migrate_certificate_file_to_content, file_path
|
||||||
)
|
)
|
||||||
== content
|
== content
|
||||||
)
|
)
|
||||||
|
|
||||||
assert file_path == file_path2
|
|
||||||
|
@pytest.mark.parametrize("temp_dir_prefix", ["create-test2"])
|
||||||
|
async def test_certificate_temp_files_with_auto_mode(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_temp_dir: str,
|
||||||
|
) -> None:
|
||||||
|
"""Test creating and reading and recovery certificate files with auto mode."""
|
||||||
|
config = {mqtt.CONF_CERTIFICATE: "auto"}
|
||||||
|
|
||||||
|
# Create old file to be able to assert it is removed with auto option
|
||||||
|
await help_create_test_certificate_file(hass, mock_temp_dir, mqtt.CONF_CERTIFICATE)
|
||||||
|
await mqtt.util.async_create_certificate_temp_files(hass, config)
|
||||||
|
file_path = await hass.async_add_executor_job(mqtt.util.get_file_path, "auto")
|
||||||
|
assert file_path is None
|
||||||
|
assert (
|
||||||
|
await hass.async_add_executor_job(
|
||||||
|
mqtt.util.migrate_certificate_file_to_content, "auto"
|
||||||
|
)
|
||||||
|
== "auto"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_reading_non_exitisting_certificate_file() -> None:
|
async def test_reading_non_exitisting_certificate_file() -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user