driver / standard (pull_request) Failing after 19s
driver / lint (pull_request) Successful in 16s
driver / test (3.12) (pull_request) Failing after 17s
driver / test (3.13) (pull_request) Failing after 15s
driver / types (pull_request) Failing after 32s
driver / lowest (pull_request) Failing after 22s
driver / build (pull_request) Successful in 39s
driver / security (pull_request) Failing after 45s
CI / driver (pull_request) Failing after 1m27s
driver / publish (pull_request) Skipped
An adopted switch or access point carries driver (unifi_switch, unifi_ap) and driver_args (controller transport, URL, site, MAC; no secret), so netOrk can add it with the controller's credential. A device waiting for adoption and a gateway get none. The end-to-end test opens an access point with exactly what the controller reported.
213 lines
8.3 KiB
Python
213 lines
8.3 KiB
Python
"""The drivers against the simulator: real TLS, real SSH, real HTTP, nothing faked inside.
|
|
|
|
The simulator (``sim/unifi_sim``) plays a controller and its devices from the
|
|
fixtures; these tests are what a first real controller will be held against.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Iterator
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from napalm.base.exceptions import ConnectionException
|
|
from napalm_device_types.ssh_host_keys import SshHostKeyMismatch
|
|
from napalm_device_types.tls_certificates import TlsCertificateMismatch
|
|
from unifi_sim import SimController, SimDevice, serve_controller, serve_device
|
|
|
|
from napalm_ubiquiti import UnifiAccessPointDriver, UnifiNetworkDriver, UnifiSwitchDriver
|
|
from napalm_ubiquiti.client import RateLimited, UnifiError
|
|
|
|
US16 = "fc:ec:da:11:22:33"
|
|
UAP = "80:2a:a8:00:01:02"
|
|
|
|
|
|
@pytest.fixture
|
|
def make_controller() -> Iterator[Any]:
|
|
servers: list[Any] = []
|
|
|
|
def make(**kwargs: Any) -> Any:
|
|
server = serve_controller(SimController.from_fixtures(**kwargs))
|
|
servers.append(server)
|
|
return server
|
|
|
|
yield make
|
|
for server in servers:
|
|
server.stop()
|
|
|
|
|
|
@pytest.fixture
|
|
def unifi_os(make_controller: Any) -> Any:
|
|
return make_controller()
|
|
|
|
|
|
def _device_args(server: Any, **extra: Any) -> dict[str, Any]:
|
|
return {"controller_url": server.url, "token_value": server.sim.api_key, **extra}
|
|
|
|
|
|
class TestThroughAUnifiOsConsole:
|
|
def test_a_switch_by_its_api_key(self, unifi_os: Any) -> None:
|
|
seen: list[dict[str, Any]] = []
|
|
args = _device_args(unifi_os, tls_certificate_observer=seen.append)
|
|
|
|
with UnifiSwitchDriver("192.168.0.57", "", "", optional_args=args) as switch:
|
|
facts = switch.get_facts()
|
|
vlans = switch.get_vlans_detail()
|
|
|
|
assert facts["hostname"] == "Switch 16"
|
|
assert "Port 1" in vlans["1"]["untagged"]
|
|
assert seen[0]["seen"]["fingerprint"] == unifi_os.fingerprint
|
|
|
|
def test_an_access_point_with_its_clients(self, unifi_os: Any) -> None:
|
|
with UnifiAccessPointDriver(
|
|
"ap", "", "", optional_args=_device_args(unifi_os, mac=UAP)
|
|
) as ap:
|
|
ssids = ap.get_ssids()
|
|
clients = ap.get_wireless_clients()
|
|
|
|
assert ssids
|
|
assert [c["mac"] for c in clients] == ["00:00:00:00:00:01"]
|
|
|
|
def test_a_local_admin_login(self, unifi_os: Any) -> None:
|
|
user, password = unifi_os.sim.admin
|
|
args = {"controller_url": unifi_os.url, "mac": US16}
|
|
|
|
with UnifiSwitchDriver("x", user, password, optional_args=args) as switch:
|
|
assert switch.get_facts()["model"] == "US16P150"
|
|
|
|
def test_a_wrong_key(self, unifi_os: Any) -> None:
|
|
args = {"controller_url": unifi_os.url, "token_value": "wrong", "mac": US16}
|
|
|
|
with pytest.raises(ConnectionException, match="API key"):
|
|
UnifiSwitchDriver("x", "", "", optional_args=args).open()
|
|
|
|
def test_a_changed_certificate_is_refused(self, unifi_os: Any) -> None:
|
|
args = _device_args(unifi_os, mac=US16, tls_fingerprints={str(unifi_os.port): "AB" * 32})
|
|
|
|
with pytest.raises(TlsCertificateMismatch):
|
|
UnifiSwitchDriver("x", "", "", optional_args=args).open()
|
|
|
|
def test_a_cloud_account_with_mfa(self, unifi_os: Any) -> None:
|
|
args = {"controller_url": unifi_os.url, "mac": US16}
|
|
|
|
with pytest.raises(ConnectionException, match="local admin"):
|
|
UnifiSwitchDriver("x", "cloud@example.com", "pw", optional_args=args).open()
|
|
|
|
def test_too_many_failed_logins(self, unifi_os: Any) -> None:
|
|
args = {"controller_url": unifi_os.url, "mac": US16}
|
|
for _ in range(unifi_os.sim.max_failed_logins):
|
|
with pytest.raises(ConnectionException):
|
|
UnifiSwitchDriver("x", "admin", "wrong", optional_args=args).open()
|
|
|
|
with pytest.raises(RateLimited) as caught:
|
|
UnifiSwitchDriver("x", "admin", "wrong", optional_args=args).open()
|
|
|
|
assert caught.value.retry_after == 300
|
|
|
|
def test_a_wrong_site(self, unifi_os: Any) -> None:
|
|
with pytest.raises(UnifiError, match="no site"):
|
|
UnifiSwitchDriver(
|
|
"x", "", "", optional_args=_device_args(unifi_os, site="nosuch")
|
|
).open()
|
|
|
|
def test_a_redirect_is_not_followed(self, make_controller: Any) -> None:
|
|
server = make_controller(redirect=True)
|
|
|
|
with pytest.raises(UnifiError, match="redirect"):
|
|
UnifiSwitchDriver("x", "", "", optional_args=_device_args(server, mac=US16)).open()
|
|
|
|
|
|
class TestTheController:
|
|
def test_its_devices_with_the_pending_one_on_10_6(self, unifi_os: Any) -> None:
|
|
args = {"token_value": unifi_os.sim.api_key, "port": unifi_os.port}
|
|
|
|
with UnifiNetworkDriver("127.0.0.1", "", "", optional_args=args) as controller:
|
|
facts = controller.get_facts()
|
|
devices = controller.get_managed_devices()
|
|
|
|
assert facts["os_version"] == "10.6.106"
|
|
assert {(d["site"], d["state"]) for d in devices} >= {("default", "pending")}
|
|
assert {d["site"] for d in devices} == {"default", "branch"}
|
|
assert UAP in {d["mac"] for d in devices}
|
|
|
|
def test_a_managed_device_opens_with_what_the_controller_says(self, unifi_os: Any) -> None:
|
|
key = {"token_value": unifi_os.sim.api_key}
|
|
with UnifiNetworkDriver(
|
|
"127.0.0.1", "", "", optional_args={**key, "port": unifi_os.port}
|
|
) as c:
|
|
ap = next(d for d in c.get_managed_devices() if d.get("driver") == "unifi_ap")
|
|
|
|
with UnifiAccessPointDriver(
|
|
ap["ip"], "", "", optional_args={**ap["driver_args"], **key}
|
|
) as d:
|
|
assert d.get_facts()["hostname"] == ap["name"]
|
|
|
|
def test_no_pending_devices_before_10(self, make_controller: Any) -> None:
|
|
server = make_controller(version="9.1.120")
|
|
args = {"token_value": server.sim.api_key, "port": server.port}
|
|
|
|
with UnifiNetworkDriver("127.0.0.1", "", "", optional_args=args) as controller:
|
|
devices = controller.get_managed_devices()
|
|
|
|
assert devices
|
|
assert all(d["state"] != "pending" for d in devices)
|
|
|
|
def test_a_standalone_controller(self, make_controller: Any) -> None:
|
|
server = make_controller(unifi_os=False, version="8.0.24")
|
|
user, password = server.sim.admin
|
|
|
|
with UnifiNetworkDriver(
|
|
"127.0.0.1", user, password, optional_args={"port": server.port}
|
|
) as c:
|
|
facts = c.get_facts()
|
|
devices = c.get_managed_devices()
|
|
|
|
assert facts["os_version"] == "8.0.24"
|
|
assert US16 in {d["mac"] for d in devices}
|
|
assert any(d.get("serial") for d in devices)
|
|
|
|
def test_one_site(self, unifi_os: Any) -> None:
|
|
args = {"token_value": unifi_os.sim.api_key, "port": unifi_os.port, "site": "branch"}
|
|
|
|
with UnifiNetworkDriver("127.0.0.1", "", "", optional_args=args) as controller:
|
|
assert {d["site"] for d in controller.get_managed_devices()} == {"branch"}
|
|
|
|
|
|
class TestASwitchWithoutAController:
|
|
@pytest.fixture
|
|
def switch(self) -> Iterator[Any]:
|
|
server = serve_device(SimDevice.us24())
|
|
yield server
|
|
server.stop()
|
|
|
|
def test_read_over_ssh(self, switch: Any) -> None:
|
|
seen: list[dict[str, Any]] = []
|
|
args = {"ssh_port": switch.port, "ssh_host_key_observer": seen.append}
|
|
|
|
with UnifiSwitchDriver("127.0.0.1", "ubnt", "ubnt", optional_args=args) as device:
|
|
facts = device.get_facts()
|
|
vlans = device.get_vlans()
|
|
neighbours = device.get_lldp_neighbors()
|
|
|
|
assert facts["hostname"] == "usw-1"
|
|
assert "Port 2" in vlans[11]["interfaces"]
|
|
assert neighbours
|
|
assert seen[0]["accepted"] is True
|
|
|
|
def test_a_wrong_password(self, switch: Any) -> None:
|
|
with pytest.raises(ConnectionException):
|
|
UnifiSwitchDriver(
|
|
"127.0.0.1", "ubnt", "no", optional_args={"ssh_port": switch.port}
|
|
).open()
|
|
|
|
def test_a_changed_host_key_is_refused(self, switch: Any) -> None:
|
|
other = serve_device(SimDevice.us24())
|
|
try:
|
|
known = {str(switch.port): other.host_key}
|
|
args = {"ssh_port": switch.port, "ssh_host_keys": known}
|
|
|
|
with pytest.raises(SshHostKeyMismatch):
|
|
UnifiSwitchDriver("127.0.0.1", "ubnt", "ubnt", optional_args=args).open()
|
|
finally:
|
|
other.stop()
|