fix: a switch or access point is read once at open
With an API key and a known MAC, opening sent no request at all: a refused key, a redirect or a device the controller does not know showed only at the first getter. Open now reads the device and closes again when that fails.
This commit is contained in:
1 parent
7c8895909a
commit
e040280ce9
2 files changed
+38
-7
No files matched your search
@@ -93,10 +93,14 @@ class UnifiDevice:
|
||||
# -- connection --------------------------------------------------------
|
||||
|
||||
def open(self) -> None:
|
||||
if self.transport == CONTROLLER:
|
||||
self.source = self._open_controller()
|
||||
else:
|
||||
self.source = self._open_ssh()
|
||||
"""Connect, and read the device once: a refused key or an unknown device fails here."""
|
||||
source = self._open_controller() if self.transport == CONTROLLER else self._open_ssh()
|
||||
try:
|
||||
source.record()
|
||||
except BaseException:
|
||||
self._close_transport()
|
||||
raise
|
||||
self.source = source
|
||||
|
||||
def _open_controller(self) -> ControllerSource:
|
||||
args = self.optional_args
|
||||
@@ -124,10 +128,13 @@ class UnifiDevice:
|
||||
return SshSource(shell)
|
||||
|
||||
def close(self) -> None:
|
||||
self._close_transport()
|
||||
self.source = None
|
||||
|
||||
def _close_transport(self) -> None:
|
||||
if self._closable is not None:
|
||||
self._closable.close()
|
||||
self._closable = None
|
||||
self.source = None
|
||||
|
||||
def is_alive(self) -> dict[str, bool]:
|
||||
return {"is_alive": self.source is not None}
|
||||
|
||||
@@ -174,13 +174,37 @@ class TestTransport:
|
||||
with pytest.raises(ConnectionException, match="controller_url"):
|
||||
Driver("h", "u", "p", optional_args={"transport": "controller"})
|
||||
|
||||
def test_a_controller_url_without_scheme_is_https(self, controller: dict[str, Any]) -> None:
|
||||
Driver("h", "", "", optional_args={"controller_url": "unifi.lan:8443", "mac": "x"}).open()
|
||||
def test_a_controller_url_without_scheme_is_https(
|
||||
self, controller: dict[str, Any], us16: dict[str, Any]
|
||||
) -> None:
|
||||
Driver(
|
||||
"h", "", "", optional_args={"controller_url": "unifi.lan:8443", "mac": us16["mac"]}
|
||||
).open()
|
||||
|
||||
assert controller["url"] == "https://unifi.lan:8443"
|
||||
|
||||
|
||||
class TestSession:
|
||||
def test_open_reads_the_device_once(self, shells: list[FakeShell]) -> None:
|
||||
driver = Driver("h", "u", "p")
|
||||
driver.open()
|
||||
driver.get_facts()
|
||||
driver.get_interfaces()
|
||||
|
||||
assert driver.is_alive() == {"is_alive": True}
|
||||
|
||||
def test_a_device_that_cannot_be_read_fails_open_and_closes(
|
||||
self, shells: list[FakeShell], monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.setattr(FakeShell, "run", lambda self, command: "not json")
|
||||
driver = Driver("h", "u", "p")
|
||||
|
||||
with pytest.raises(Exception, match="mca-dump"):
|
||||
driver.open()
|
||||
|
||||
assert shells[0].closed is True
|
||||
assert driver.is_alive() == {"is_alive": False}
|
||||
|
||||
def test_getters_before_open(self) -> None:
|
||||
with pytest.raises(ConnectionException, match="not open"):
|
||||
Driver("h", "u", "p").get_facts()
|
||||
|
||||
Reference in new issue
Block a user