Skip to content

Commit

Permalink
Improve _verify_session
Browse files Browse the repository at this point in the history
* Disconnect safely, avoid raising an exception on disconnect
* Have _verify_session raise NotConnectedError instead of ValueError
* Update session fake in tests for the new golden path
* Add explicit tests for _verify_session
  • Loading branch information
hmpf authored Jul 3, 2024
1 parent 970cc68 commit f13d7ad
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/zinolib/controllers/zino1.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,11 @@ def _verify_session(self, quiet=False):
if not getattr(self.session, 'request', None):
if quiet:
return False
raise ValueError
raise NotConnectedError("The request socket have not been set up correctly. Reconnect necessary.")
if not self.session.request.connected:
if quiet:
return False
raise NotConnectedError("Authentication necessary")
return True

@classmethod
Expand All @@ -548,8 +552,11 @@ def authenticate(self, username=None, password=None):
raise self.ManagerException(e)

def disconnect(self):
self._verify_session()
self.session = self._session_adapter.close_session(self.session)
session_ok = self._verify_session(quiet=True)
if session_ok:
self.session = self._session_adapter.close_session(self.session)
else:
self._session_adapter.close_push_channel(self.session)

def clear_flapping(self, event_or_id: EventOrId):
"""Clear flapping state of a PortStateEvent
Expand Down
26 changes: 25 additions & 1 deletion tests/test_zinolib_controllers_zino1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from datetime import datetime, timedelta, timezone

from zinolib.event_types import AdmState, Event, HistoryEntry, LogEntry
from zinolib.controllers.zino1 import EventAdapter, HistoryAdapter, LogAdapter, SessionAdapter, Zino1EventManager, UpdateHandler,RetryError
from zinolib.controllers.zino1 import EventAdapter, HistoryAdapter, LogAdapter, SessionAdapter, Zino1EventManager, UpdateHandler
from zinolib.controllers.zino1 import RetryError, NotConnectedError
from zinolib.ritz import NotifierResponse

raw_event_id = 139110
Expand Down Expand Up @@ -84,6 +85,7 @@ def _setup_config(cls, config):
def _setup_request(session, config):
class FakeSession:
authenticated = True
connected = True

session.request = FakeSession() # needs to be truthy
session.push = True # needs to be truthy
Expand All @@ -106,6 +108,28 @@ def init_manager(self):
zino1 = FakeZino1EventManager.configure(None)
return zino1

def test_verify_session_raises_notconnectederror_on_incorrect_manager(self):
zino1 = FakeZino1EventManager()
with self.assertRaises(NotConnectedError) as e:
zino1._verify_session()
self.assertIn("The request socket have not been set up correctly", e)

def test_verify_session_raises_notconnectederror_if_not_connected(self):
zino1 = FakeZino1EventManager.configure(None)
orig = zino1.session.request.connected
zino1.session.request.connected = False
try:
with self.assertRaises(NotConnectedError) as e:
zino1._verify_session()
self.assertIn("Authentication necessary", e)
finally:
zino1.session.request.connected = orig

def test_verify_session_returns_False_if_quieted_on_incorrect_manager(self):
zino1 = FakeZino1EventManager()
result = zino1._verify_session(quiet=True)
self.assertEqual(result, False)

def test_create_event_from_id_receiving_garbage_admstate_is_safely_handled(self):
global raw_attrlist
zino1 = self.init_manager()
Expand Down

0 comments on commit f13d7ad

Please sign in to comment.