mirror of
https://github.com/home-assistant/core.git
synced 2026-10-06 22:38:02 -04:00
Use freezer in Portainer tests (#183301)
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
"""Tests for the Portainer integration."""
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
@@ -10,17 +9,7 @@ async def setup_integration(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Set up the Portainer integration for testing and enable all entities."""
|
||||
"""Set up the Portainer integration for testing."""
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_registry = er.async_get(hass)
|
||||
|
||||
for entry in er.async_entries_for_config_entry(
|
||||
entity_registry, config_entry.entry_id
|
||||
):
|
||||
if entry.disabled_by is not None:
|
||||
entity_registry.async_update_entity(entry.entity_id, disabled_by=None)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -139,6 +139,11 @@ def mock_portainer_client(mock_portainer_watcher: MagicMock) -> Generator[AsyncM
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def enable_all_entities(entity_registry_enabled_by_default: None) -> None:
|
||||
"""Enable all entities when they are registered."""
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_portainer_event_listeners() -> Generator[dict[int, MagicMock]]:
|
||||
"""Mock PortainerEventListener; one MagicMock instance per endpoint_id.
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from typing import Any, cast
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from pyportainer.exceptions import (
|
||||
PortainerAuthenticationError,
|
||||
PortainerConnectionError,
|
||||
@@ -15,6 +16,7 @@ import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.portainer.const import DOMAIN
|
||||
from homeassistant.components.portainer.coordinator import DEFAULT_SCAN_INTERVAL
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY,
|
||||
@@ -33,7 +35,11 @@ from homeassistant.setup import async_setup_component
|
||||
from . import setup_integration
|
||||
from .conftest import MOCK_TEST_CONFIG, TEST_INSTANCE_ID
|
||||
|
||||
from tests.common import MockConfigEntry, async_load_json_array_fixture
|
||||
from tests.common import (
|
||||
MockConfigEntry,
|
||||
async_fire_time_changed,
|
||||
async_load_json_array_fixture,
|
||||
)
|
||||
from tests.typing import WebSocketGenerator
|
||||
|
||||
|
||||
@@ -413,6 +419,7 @@ async def test_removed_endpoint_stops_event_listener(
|
||||
mock_portainer_client: AsyncMock,
|
||||
mock_portainer_event_listeners: dict[int, MagicMock],
|
||||
mock_config_entry: MockConfigEntry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test a removed endpoint's Docker event listener is stopped and dropped."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
@@ -420,8 +427,9 @@ async def test_removed_endpoint_stops_event_listener(
|
||||
assert 1 in coordinator._event_listeners
|
||||
|
||||
mock_portainer_client.get_endpoints.return_value = []
|
||||
await coordinator.async_refresh()
|
||||
await hass.async_block_till_done()
|
||||
freezer.tick(DEFAULT_SCAN_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
mock_portainer_event_listeners[1].stop.assert_called_once()
|
||||
assert 1 not in coordinator._event_listeners
|
||||
@@ -432,6 +440,7 @@ async def test_new_container_callback(
|
||||
mock_portainer_client: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test new container creates entities after refresh."""
|
||||
mock_portainer_client.get_containers.return_value = []
|
||||
@@ -449,9 +458,9 @@ async def test_new_container_callback(
|
||||
if "/focused_einstein" in container["Names"]
|
||||
]
|
||||
|
||||
coordinator = mock_config_entry.runtime_data
|
||||
await coordinator.async_refresh()
|
||||
await hass.async_block_till_done()
|
||||
freezer.tick(DEFAULT_SCAN_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert len(
|
||||
er.async_entries_for_config_entry(entity_registry, mock_config_entry.entry_id)
|
||||
@@ -478,6 +487,7 @@ async def test_new_stack_callback(
|
||||
mock_portainer_client: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test new stack creates entities after refresh."""
|
||||
mock_portainer_client.get_stacks.return_value = []
|
||||
@@ -495,9 +505,9 @@ async def test_new_stack_callback(
|
||||
if stack["Name"] == "webstack"
|
||||
]
|
||||
|
||||
coordinator = mock_config_entry.runtime_data
|
||||
await coordinator.async_refresh()
|
||||
await hass.async_block_till_done()
|
||||
freezer.tick(DEFAULT_SCAN_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert len(
|
||||
er.async_entries_for_config_entry(entity_registry, mock_config_entry.entry_id)
|
||||
@@ -509,6 +519,7 @@ async def test_stack_recreated_with_new_id(
|
||||
mock_portainer_client: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test a stack recreated with the same name but a new ID re-registers its device.
|
||||
|
||||
@@ -558,8 +569,9 @@ async def test_stack_recreated_with_new_id(
|
||||
]
|
||||
|
||||
coordinator = mock_config_entry.runtime_data
|
||||
await coordinator.async_refresh()
|
||||
await hass.async_block_till_done()
|
||||
freezer.tick(DEFAULT_SCAN_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert coordinator.last_update_success
|
||||
|
||||
|
||||
Reference in New Issue
Block a user