1
0
mirror of https://github.com/home-assistant/supervisor.git synced 2025-12-24 12:29:08 +00:00

Use session dbus mocks for all tests (#4198)

* Use session dbus mocks for all tests

* func instead of fn for pylint
This commit is contained in:
Mike Degatano
2023-03-21 02:30:31 -04:00
committed by GitHub
parent c6ddc8e427
commit a6caccd845
157 changed files with 685 additions and 6572 deletions

View File

@@ -1,6 +1,5 @@
"""Test OS API."""
import asyncio
from pathlib import Path
from unittest.mock import PropertyMock, patch
@@ -8,16 +7,27 @@ from aiohttp.test_utils import TestClient
import pytest
from supervisor.coresys import CoreSys
from supervisor.dbus.agent.boards import BoardManager
from supervisor.hardware.data import Device
from supervisor.os.manager import OSManager
from supervisor.resolution.const import ContextType, IssueType, SuggestionType
from supervisor.resolution.data import Issue, Suggestion
from tests.common import mock_dbus_services
from tests.dbus_service_mocks.agent_boards import Boards as BoardsService
from tests.dbus_service_mocks.agent_boards_yellow import Yellow as YellowService
from tests.dbus_service_mocks.base import DBusServiceMock
# pylint: disable=protected-access
@pytest.mark.asyncio
@pytest.fixture(name="boards_service")
async def fixture_boards_service(
os_agent_services: dict[str, DBusServiceMock]
) -> BoardsService:
"""Return mock Boards service."""
yield os_agent_services["agent_boards"]
async def test_api_os_info(api_client: TestClient):
"""Test docker info api."""
resp = await api_client.get("/os/info")
@@ -34,23 +44,16 @@ async def test_api_os_info(api_client: TestClient):
assert attr in result["data"]
@pytest.mark.asyncio
async def test_api_os_info_with_agent(api_client: TestClient, coresys: CoreSys):
"""Test docker info api."""
await coresys.dbus.agent.connect(coresys.dbus.bus)
await coresys.dbus.agent.update()
resp = await api_client.get("/os/info")
result = await resp.json()
assert result["data"]["data_disk"] == "/dev/sda"
@pytest.mark.asyncio
async def test_api_os_datadisk_move(api_client: TestClient, coresys: CoreSys):
"""Test datadisk move without exists disk."""
await coresys.dbus.agent.connect(coresys.dbus.bus)
await coresys.dbus.agent.update()
coresys.os._available = True
resp = await api_client.post("/os/datadisk/move", json={"device": "/dev/sdaaaa"})
@@ -59,12 +62,8 @@ async def test_api_os_datadisk_move(api_client: TestClient, coresys: CoreSys):
assert result["message"] == "'/dev/sdaaaa' don't exists on the host!"
@pytest.mark.asyncio
async def test_api_os_datadisk_list(api_client: TestClient, coresys: CoreSys):
"""Test datadisk list function."""
await coresys.dbus.agent.connect(coresys.dbus.bus)
await coresys.dbus.agent.update()
coresys.hardware.update_device(
Device(
"sda",
@@ -98,8 +97,6 @@ async def test_api_os_datadisk_list(api_client: TestClient, coresys: CoreSys):
async def test_api_board_yellow_info(api_client: TestClient, coresys: CoreSys):
"""Test yellow board info."""
await coresys.dbus.agent.board.connect(coresys.dbus.bus)
resp = await api_client.get("/os/boards/yellow")
assert resp.status == 200
@@ -113,25 +110,27 @@ async def test_api_board_yellow_info(api_client: TestClient, coresys: CoreSys):
async def test_api_board_yellow_options(
api_client: TestClient, coresys: CoreSys, dbus: list[str]
api_client: TestClient,
coresys: CoreSys,
os_agent_services: dict[str, DBusServiceMock | dict[str, DBusServiceMock]],
):
"""Test yellow board options."""
await coresys.dbus.agent.board.connect(coresys.dbus.bus)
yellow_service: YellowService = os_agent_services["agent_boards_yellow"]
assert coresys.dbus.agent.board.yellow.disk_led is True
assert coresys.dbus.agent.board.yellow.heartbeat_led is True
assert coresys.dbus.agent.board.yellow.power_led is True
assert len(coresys.resolution.issues) == 0
dbus.clear()
resp = await api_client.post(
"/os/boards/yellow",
json={"disk_led": False, "heartbeat_led": False, "power_led": False},
)
assert resp.status == 200
await asyncio.sleep(0)
assert dbus == [
"/io/hass/os/Boards/Yellow-io.hass.os.Boards.Yellow.DiskLED",
"/io/hass/os/Boards/Yellow-io.hass.os.Boards.Yellow.HeartbeatLED",
"/io/hass/os/Boards/Yellow-io.hass.os.Boards.Yellow.PowerLED",
]
await yellow_service.ping()
assert coresys.dbus.agent.board.yellow.disk_led is False
assert coresys.dbus.agent.board.yellow.heartbeat_led is False
assert coresys.dbus.agent.board.yellow.power_led is False
assert (
Issue(IssueType.REBOOT_REQUIRED, ContextType.SYSTEM)
@@ -143,13 +142,15 @@ async def test_api_board_yellow_options(
)
async def test_api_board_supervised_info(api_client: TestClient, coresys: CoreSys):
async def test_api_board_supervised_info(
api_client: TestClient, coresys: CoreSys, boards_service: BoardsService
):
"""Test supervised board info."""
with patch(
"supervisor.os.manager.CPE.get_product", return_value=["not-hassos"]
), patch.object(BoardManager, "board", new=PropertyMock(return_value="Supervised")):
await coresys.dbus.agent.board.connect(coresys.dbus.bus)
await coresys.dbus.hostname.connect(coresys.dbus.bus)
await mock_dbus_services({"agent_boards_supervised": None}, coresys.dbus.bus)
boards_service.board = "Supervised"
await coresys.dbus.agent.board.update()
with patch("supervisor.os.manager.CPE.get_product", return_value=["not-hassos"]):
await coresys.os.load()
assert (await api_client.get("/os/boards/supervised")).status == 200
@@ -158,13 +159,14 @@ async def test_api_board_supervised_info(api_client: TestClient, coresys: CoreSy
assert (await api_client.get("/os/boards/not-real")).status == 400
async def test_api_board_other_info(api_client: TestClient, coresys: CoreSys):
async def test_api_board_other_info(
api_client: TestClient, coresys: CoreSys, boards_service: BoardsService
):
"""Test info for other board without dbus object."""
with patch.object(
BoardManager, "board", new=PropertyMock(return_value="not-real")
), patch.object(OSManager, "board", new=PropertyMock(return_value="not-real")):
await coresys.dbus.agent.board.connect(coresys.dbus.bus)
boards_service.board = "not-real"
await coresys.dbus.agent.board.update()
with patch.object(OSManager, "board", new=PropertyMock(return_value="not-real")):
assert (await api_client.get("/os/boards/not-real")).status == 200
assert (await api_client.post("/os/boards/not-real", json={})).status == 405
assert (await api_client.get("/os/boards/yellow")).status == 400