Skip to content

Commit

Permalink
Improve AsusWRT integration tests (home-assistant#102810)
Browse files Browse the repository at this point in the history
  • Loading branch information
ollo69 authored Nov 9, 2023
1 parent 78add0f commit 143e114
Show file tree
Hide file tree
Showing 6 changed files with 435 additions and 380 deletions.
53 changes: 53 additions & 0 deletions tests/components/asuswrt/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Test code shared between test files."""

from aioasuswrt.asuswrt import Device as LegacyDevice

from homeassistant.components.asuswrt.const import (
CONF_SSH_KEY,
MODE_ROUTER,
PROTOCOL_SSH,
PROTOCOL_TELNET,
)
from homeassistant.const import (
CONF_HOST,
CONF_MODE,
CONF_PASSWORD,
CONF_PORT,
CONF_PROTOCOL,
CONF_USERNAME,
)

ASUSWRT_BASE = "homeassistant.components.asuswrt"

HOST = "myrouter.asuswrt.com"
ROUTER_MAC_ADDR = "a1:b2:c3:d4:e5:f6"

CONFIG_DATA_TELNET = {
CONF_HOST: HOST,
CONF_PORT: 23,
CONF_PROTOCOL: PROTOCOL_TELNET,
CONF_USERNAME: "user",
CONF_PASSWORD: "pwd",
CONF_MODE: MODE_ROUTER,
}

CONFIG_DATA_SSH = {
CONF_HOST: HOST,
CONF_PORT: 22,
CONF_PROTOCOL: PROTOCOL_SSH,
CONF_USERNAME: "user",
CONF_SSH_KEY: "aaaaa",
CONF_MODE: MODE_ROUTER,
}

MOCK_MACS = [
"A1:B1:C1:D1:E1:F1",
"A2:B2:C2:D2:E2:F2",
"A3:B3:C3:D3:E3:F3",
"A4:B4:C4:D4:E4:F4",
]


def new_device(mac, ip, name):
"""Return a new device for specific protocol."""
return LegacyDevice(mac, ip, name)
83 changes: 83 additions & 0 deletions tests/components/asuswrt/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""Fixtures for Asuswrt component."""

from unittest.mock import Mock, patch

from aioasuswrt.asuswrt import AsusWrt as AsusWrtLegacy
from aioasuswrt.connection import TelnetConnection
import pytest

from .common import ASUSWRT_BASE, MOCK_MACS, ROUTER_MAC_ADDR, new_device

ASUSWRT_LEGACY_LIB = f"{ASUSWRT_BASE}.bridge.AsusWrtLegacy"

MOCK_BYTES_TOTAL = [60000000000, 50000000000]
MOCK_CURRENT_TRANSFER_RATES = [20000000, 10000000]
MOCK_LOAD_AVG = [1.1, 1.2, 1.3]
MOCK_TEMPERATURES = {"2.4GHz": 40.2, "CPU": 71.2}


@pytest.fixture(name="patch_setup_entry")
def mock_controller_patch_setup_entry():
"""Mock setting up a config entry."""
with patch(
f"{ASUSWRT_BASE}.async_setup_entry", return_value=True
) as setup_entry_mock:
yield setup_entry_mock


@pytest.fixture(name="mock_devices_legacy")
def mock_devices_legacy_fixture():
"""Mock a list of devices."""
return {
MOCK_MACS[0]: new_device(MOCK_MACS[0], "192.168.1.2", "Test"),
MOCK_MACS[1]: new_device(MOCK_MACS[1], "192.168.1.3", "TestTwo"),
}


@pytest.fixture(name="mock_available_temps")
def mock_available_temps_fixture():
"""Mock a list of available temperature sensors."""
return [True, False, True]


@pytest.fixture(name="connect_legacy")
def mock_controller_connect_legacy(mock_devices_legacy, mock_available_temps):
"""Mock a successful connection with legacy library."""
with patch(ASUSWRT_LEGACY_LIB, spec=AsusWrtLegacy) as service_mock:
service_mock.return_value.connection = Mock(spec=TelnetConnection)
service_mock.return_value.is_connected = True
service_mock.return_value.async_get_nvram.return_value = {
"label_mac": ROUTER_MAC_ADDR,
"model": "abcd",
"firmver": "efg",
"buildno": "123",
}
service_mock.return_value.async_get_connected_devices.return_value = (
mock_devices_legacy
)
service_mock.return_value.async_get_bytes_total.return_value = MOCK_BYTES_TOTAL
service_mock.return_value.async_get_current_transfer_rates.return_value = (
MOCK_CURRENT_TRANSFER_RATES
)
service_mock.return_value.async_get_loadavg.return_value = MOCK_LOAD_AVG
service_mock.return_value.async_get_temperature.return_value = MOCK_TEMPERATURES
service_mock.return_value.async_find_temperature_commands.return_value = (
mock_available_temps
)
yield service_mock


@pytest.fixture(name="connect_legacy_sens_fail")
def mock_controller_connect_legacy_sens_fail(connect_legacy):
"""Mock a successful connection using legacy library with sensors fail."""
connect_legacy.return_value.async_get_nvram.side_effect = OSError
connect_legacy.return_value.async_get_connected_devices.side_effect = OSError
connect_legacy.return_value.async_get_bytes_total.side_effect = OSError
connect_legacy.return_value.async_get_current_transfer_rates.side_effect = OSError
connect_legacy.return_value.async_get_loadavg.side_effect = OSError
connect_legacy.return_value.async_get_temperature.side_effect = OSError
connect_legacy.return_value.async_find_temperature_commands.return_value = [
True,
True,
True,
]
Loading

0 comments on commit 143e114

Please sign in to comment.