style: ruff format and the lint rules of the driver standard
Mechanical: ruff format, and ruff check --fix with the rule set every NAPALM driver uses (E, F, I, UP, B at line length 100, Python 3.10). What ruff cannot fix comes in the next commit.
This commit is contained in:
1 parent
7f2ae6ac58
commit
a212bc8715
14 files changed
+322
-239
No files matched your search
@@ -86,7 +86,9 @@ class TestRebootHost:
|
||||
|
||||
def test_detached_so_the_answer_comes_back_first(self):
|
||||
drv = _driver()
|
||||
with patch("napalm_openwrt.openwrt.run_on_transport", return_value=CommandResult("", "", 0)) as run:
|
||||
with patch(
|
||||
"napalm_openwrt.openwrt.run_on_transport", return_value=CommandResult("", "", 0)
|
||||
) as run:
|
||||
drv.reboot_host()
|
||||
|
||||
command = run.call_args.args[1]
|
||||
@@ -96,8 +98,9 @@ class TestRebootHost:
|
||||
def test_a_refusal_is_raised(self):
|
||||
drv = _driver()
|
||||
failed = CommandResult("", "reboot: Operation not permitted", 1)
|
||||
with patch("napalm_openwrt.openwrt.run_on_transport", return_value=failed), pytest.raises(
|
||||
RuntimeError, match="not permitted"
|
||||
with (
|
||||
patch("napalm_openwrt.openwrt.run_on_transport", return_value=failed),
|
||||
pytest.raises(RuntimeError, match="not permitted"),
|
||||
):
|
||||
drv.reboot_host()
|
||||
|
||||
|
||||
+87
-36
@@ -1,15 +1,16 @@
|
||||
"""Unit tests for OpenWrtDriver — no real device required."""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from napalm_openwrt.openwrt import OpenWrtDriver
|
||||
import pytest
|
||||
|
||||
from napalm_openwrt.openwrt import OpenWrtDriver
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Fixtures
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def driver():
|
||||
"""Return a driver instance with a mocked Netmiko connection."""
|
||||
@@ -130,9 +131,11 @@ HOSTNAME = "OpenWrt"
|
||||
# Tests
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestGetFacts:
|
||||
def _make_send(self):
|
||||
"""Return a _send_command mock that handles both str and list args."""
|
||||
|
||||
def _send(cmd, **kw):
|
||||
key = cmd[0] if isinstance(cmd, list) else cmd
|
||||
if "openwrt_release" in key:
|
||||
@@ -146,6 +149,7 @@ class TestGetFacts:
|
||||
if "ip link" in key:
|
||||
return IP_LINK_SHOW
|
||||
return ""
|
||||
|
||||
return _send
|
||||
|
||||
def test_returns_required_keys(self, driver):
|
||||
@@ -153,8 +157,15 @@ class TestGetFacts:
|
||||
|
||||
facts = driver.get_facts()
|
||||
assert set(facts.keys()) == {
|
||||
"vendor", "model", "hostname", "fqdn", "os_version",
|
||||
"serial_number", "uptime", "interface_list", "number_of_interfaces",
|
||||
"vendor",
|
||||
"model",
|
||||
"hostname",
|
||||
"fqdn",
|
||||
"os_version",
|
||||
"serial_number",
|
||||
"uptime",
|
||||
"interface_list",
|
||||
"number_of_interfaces",
|
||||
}
|
||||
|
||||
def test_number_of_interfaces_matches_list(self, driver):
|
||||
@@ -213,8 +224,13 @@ class TestGetInterfaces:
|
||||
result = driver.get_interfaces()
|
||||
for iface_data in result.values():
|
||||
assert set(iface_data.keys()) >= {
|
||||
"is_up", "is_enabled", "description",
|
||||
"last_flapped", "speed", "mtu", "mac_address",
|
||||
"is_up",
|
||||
"is_enabled",
|
||||
"description",
|
||||
"last_flapped",
|
||||
"speed",
|
||||
"mtu",
|
||||
"mac_address",
|
||||
}
|
||||
|
||||
|
||||
@@ -658,6 +674,7 @@ mgmt (id: 2)
|
||||
# Tests for new methods
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestCli:
|
||||
def test_returns_dict_keyed_by_command(self, driver):
|
||||
driver._send_command = lambda cmd, **kw: f"output of {cmd}"
|
||||
@@ -758,8 +775,17 @@ class TestGetNtpStats:
|
||||
result = driver.get_ntp_stats()
|
||||
for entry in result:
|
||||
assert set(entry.keys()) >= {
|
||||
"remote", "referenceid", "synchronized", "stratum",
|
||||
"type", "when", "hostpoll", "reachability", "delay", "offset", "jitter",
|
||||
"remote",
|
||||
"referenceid",
|
||||
"synchronized",
|
||||
"stratum",
|
||||
"type",
|
||||
"when",
|
||||
"hostpoll",
|
||||
"reachability",
|
||||
"delay",
|
||||
"offset",
|
||||
"jitter",
|
||||
}
|
||||
|
||||
def test_empty_on_no_tool(self, driver):
|
||||
@@ -888,8 +914,12 @@ class TestGetRouteTo:
|
||||
for prefix, routes in driver.get_route_to().items():
|
||||
for route in routes:
|
||||
assert set(route.keys()) >= {
|
||||
"protocol", "current_active", "next_hop",
|
||||
"outgoing_interface", "preference", "routing_table",
|
||||
"protocol",
|
||||
"current_active",
|
||||
"next_hop",
|
||||
"outgoing_interface",
|
||||
"preference",
|
||||
"routing_table",
|
||||
}
|
||||
|
||||
|
||||
@@ -925,23 +955,17 @@ class TestTraceroute:
|
||||
|
||||
class TestGetNetworkInstances:
|
||||
def test_default_instance_always_present(self, driver):
|
||||
driver._send_command = lambda cmd, **kw: (
|
||||
IP_LINK_SHOW if "ip link" in cmd else ""
|
||||
)
|
||||
driver._send_command = lambda cmd, **kw: IP_LINK_SHOW if "ip link" in cmd else ""
|
||||
result = driver.get_network_instances()
|
||||
assert "default" in result
|
||||
|
||||
def test_default_instance_type(self, driver):
|
||||
driver._send_command = lambda cmd, **kw: (
|
||||
IP_LINK_SHOW if "ip link" in cmd else ""
|
||||
)
|
||||
driver._send_command = lambda cmd, **kw: IP_LINK_SHOW if "ip link" in cmd else ""
|
||||
result = driver.get_network_instances()
|
||||
assert result["default"]["type"] == "DEFAULT_INSTANCE"
|
||||
|
||||
def test_default_interfaces_populated(self, driver):
|
||||
driver._send_command = lambda cmd, **kw: (
|
||||
IP_LINK_SHOW if "ip link" in cmd else ""
|
||||
)
|
||||
driver._send_command = lambda cmd, **kw: IP_LINK_SHOW if "ip link" in cmd else ""
|
||||
result = driver.get_network_instances()
|
||||
ifaces = result["default"]["interfaces"]["interface"]
|
||||
assert "eth0" in ifaces
|
||||
@@ -955,6 +979,7 @@ class TestGetNetworkInstances:
|
||||
if "ip link" in cmd:
|
||||
return IP_LINK_SHOW
|
||||
return ""
|
||||
|
||||
driver._send_command = _send
|
||||
result = driver.get_network_instances()
|
||||
assert "vpn" in result
|
||||
@@ -962,16 +987,12 @@ class TestGetNetworkInstances:
|
||||
assert result["vpn"]["type"] == "L3VRF"
|
||||
|
||||
def test_name_filter(self, driver):
|
||||
driver._send_command = lambda cmd, **kw: (
|
||||
IP_LINK_SHOW if "ip link" in cmd else ""
|
||||
)
|
||||
driver._send_command = lambda cmd, **kw: IP_LINK_SHOW if "ip link" in cmd else ""
|
||||
result = driver.get_network_instances(name="default")
|
||||
assert list(result.keys()) == ["default"]
|
||||
|
||||
def test_required_keys(self, driver):
|
||||
driver._send_command = lambda cmd, **kw: (
|
||||
IP_LINK_SHOW if "ip link" in cmd else ""
|
||||
)
|
||||
driver._send_command = lambda cmd, **kw: IP_LINK_SHOW if "ip link" in cmd else ""
|
||||
for inst in driver.get_network_instances().values():
|
||||
assert set(inst.keys()) >= {"name", "type", "state", "interfaces"}
|
||||
|
||||
@@ -1057,6 +1078,7 @@ class TestGetChannelScan:
|
||||
|
||||
def _make_send(self, mode: str = "active"):
|
||||
"""Return a _send_command mock routing iw dev / iw scan output."""
|
||||
|
||||
def _send(cmd, **kw):
|
||||
if cmd.strip().startswith("iw dev") and "scan" not in cmd:
|
||||
return IW_DEV_OUT
|
||||
@@ -1065,6 +1087,7 @@ class TestGetChannelScan:
|
||||
if "wlan1" in cmd and "scan" in cmd:
|
||||
return IW_SCAN_WLAN1
|
||||
return ""
|
||||
|
||||
return _send
|
||||
|
||||
def test_returns_dict(self, driver):
|
||||
@@ -1089,8 +1112,13 @@ class TestGetChannelScan:
|
||||
driver._send_command = self._make_send()
|
||||
entry = driver.get_channel_scan()["wlan0"][0]
|
||||
assert set(entry.keys()) >= {
|
||||
"bssid", "ssid", "frequency", "channel", "signal_dbm",
|
||||
"channel_width", "band",
|
||||
"bssid",
|
||||
"ssid",
|
||||
"frequency",
|
||||
"channel",
|
||||
"signal_dbm",
|
||||
"channel_width",
|
||||
"band",
|
||||
}
|
||||
|
||||
def test_wlan0_first_entry_values(self, driver):
|
||||
@@ -1300,7 +1328,9 @@ class TestGetSsidsAcl:
|
||||
|
||||
def test_deny_maps_to_blacklist(self, driver):
|
||||
deny_uci = UCI_WIRELESS_ACL.replace("macfilter='allow'", "macfilter='deny'")
|
||||
driver._send_command = lambda cmd, **kw: deny_uci if cmd.strip() == "uci show wireless" else ""
|
||||
driver._send_command = lambda cmd, **kw: (
|
||||
deny_uci if cmd.strip() == "uci show wireless" else ""
|
||||
)
|
||||
result = driver.get_ssids()
|
||||
assert result["CorpWiFi"]["acl_mode"] == "blacklist"
|
||||
|
||||
@@ -1308,7 +1338,9 @@ class TestGetSsidsAcl:
|
||||
class TestPushMacAcl:
|
||||
"""Tests for OpenWrtWirelessMixin.push_mac_acl()."""
|
||||
|
||||
def _make_send(self, sections="wireless.@wifi-iface[0]\nwireless.@wifi-iface[1]", ssid_by_section=None):
|
||||
def _make_send(
|
||||
self, sections="wireless.@wifi-iface[0]\nwireless.@wifi-iface[1]", ssid_by_section=None
|
||||
):
|
||||
ssid_by_section = ssid_by_section or {
|
||||
"wireless.@wifi-iface[0]": "CorpWiFi",
|
||||
"wireless.@wifi-iface[1]": "GuestNet",
|
||||
@@ -1370,16 +1402,24 @@ class TestPushMacAcl:
|
||||
send, issued = self._make_send()
|
||||
driver._send_command = send
|
||||
driver.push_mac_acl("CorpWiFi", "whitelist", ["AA:BB:CC:DD:EE:01", "AA:BB:CC:DD:EE:02"])
|
||||
assert any("add_list wireless.@wifi-iface[0].maclist='AA:BB:CC:DD:EE:01'" in c for c in issued)
|
||||
assert any("add_list wireless.@wifi-iface[0].maclist='AA:BB:CC:DD:EE:02'" in c for c in issued)
|
||||
assert any(
|
||||
"add_list wireless.@wifi-iface[0].maclist='AA:BB:CC:DD:EE:01'" in c for c in issued
|
||||
)
|
||||
assert any(
|
||||
"add_list wireless.@wifi-iface[0].maclist='AA:BB:CC:DD:EE:02'" in c for c in issued
|
||||
)
|
||||
|
||||
def test_maclist_cleared_before_readd(self, driver):
|
||||
"""Full-rebuild: existing maclist must be deleted before new entries are added."""
|
||||
send, issued = self._make_send()
|
||||
driver._send_command = send
|
||||
driver.push_mac_acl("CorpWiFi", "whitelist", ["AA:BB:CC:DD:EE:01"])
|
||||
delete_idx = next(i for i, c in enumerate(issued) if "delete wireless.@wifi-iface[0].maclist" in c)
|
||||
add_idx = next(i for i, c in enumerate(issued) if "add_list wireless.@wifi-iface[0].maclist" in c)
|
||||
delete_idx = next(
|
||||
i for i, c in enumerate(issued) if "delete wireless.@wifi-iface[0].maclist" in c
|
||||
)
|
||||
add_idx = next(
|
||||
i for i, c in enumerate(issued) if "add_list wireless.@wifi-iface[0].maclist" in c
|
||||
)
|
||||
assert delete_idx < add_idx
|
||||
|
||||
def test_only_matching_ssid_sections_touched(self, driver):
|
||||
@@ -1474,8 +1514,17 @@ IP_ADDR_BRLAN = """\
|
||||
class _FakeShell:
|
||||
"""Collects issued commands and answers them from a canned config."""
|
||||
|
||||
def __init__(self, firewall="", network="", ip_addr="", ssh_connection="",
|
||||
reload_out="", nft_hits="1", listen_hits="1", snmpd="running"):
|
||||
def __init__(
|
||||
self,
|
||||
firewall="",
|
||||
network="",
|
||||
ip_addr="",
|
||||
ssh_connection="",
|
||||
reload_out="",
|
||||
nft_hits="1",
|
||||
listen_hits="1",
|
||||
snmpd="running",
|
||||
):
|
||||
self.firewall = firewall
|
||||
self.network = network
|
||||
self.ip_addr = ip_addr
|
||||
@@ -1633,7 +1682,9 @@ class TestFixSnmpRuleWriting:
|
||||
)
|
||||
driver._send_command = shell
|
||||
driver.configure_snmp_agent(_SETTINGS)
|
||||
delete_idx = next(i for i, c in enumerate(shell.issued) if "snmp_netork" in c and "delete" in c)
|
||||
delete_idx = next(
|
||||
i for i, c in enumerate(shell.issued) if "snmp_netork" in c and "delete" in c
|
||||
)
|
||||
assert any("uci commit firewall" in c for c in shell.issued[delete_idx:])
|
||||
|
||||
def test_legacy_snmp_netork_rule_is_recognised_as_stale(self, driver):
|
||||
|
||||
@@ -24,7 +24,9 @@ HANDLER = "napalm_openwrt.openwrt.ConnectHandler"
|
||||
|
||||
def _device() -> MagicMock:
|
||||
device = MagicMock()
|
||||
device.remote_conn_pre.get_transport.return_value.get_remote_server_key.return_value = SERVER_KEY
|
||||
device.remote_conn_pre.get_transport.return_value.get_remote_server_key.return_value = (
|
||||
SERVER_KEY
|
||||
)
|
||||
return device
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user