fix(http): pass the timeout with every request
requests ignores a timeout attribute on a Session, so open() setting one left every call without a timeout and an unresponsive gateway could hang a worker indefinitely. Each get/post/put now carries the driver's timeout. Closes #3
This commit is contained in:
1 parent
d5362fd382
commit
f7002114ae
3 files changed
+53
-3
No files matched your search
@@ -433,3 +433,47 @@ def test_a_zyxel_switch_page_adds_no_evidence_for_this_gateway(switch_model):
|
||||
if not rule.negative and rule.pattern != "zyxel" and rule.pattern in page
|
||||
]
|
||||
assert matching == []
|
||||
|
||||
|
||||
class TestEveryRequestHasATimeout:
|
||||
"""requests ignores a ``timeout`` attribute on a Session; it must go with each call."""
|
||||
|
||||
def _driver_with_session(self):
|
||||
driver = ZyxelVMGDriver("192.168.1.1", "admin", "password", timeout=7)
|
||||
session = MagicMock(spec=requests.Session)
|
||||
session.get.return_value = MockResponse(MOCK_BASIC_INFO)
|
||||
session.post.return_value = MockResponse(MOCK_LOGIN_RESPONSE)
|
||||
session.put.return_value = MockResponse({})
|
||||
return driver, session
|
||||
|
||||
@staticmethod
|
||||
def _timeouts(session):
|
||||
calls = session.get.call_args_list + session.post.call_args_list
|
||||
calls += session.put.call_args_list
|
||||
assert calls
|
||||
return {call.kwargs.get("timeout") for call in calls}
|
||||
|
||||
def test_open_login_and_logout(self):
|
||||
driver, session = self._driver_with_session()
|
||||
crypto = MagicMock()
|
||||
crypto.encrypt_json.return_value = {"content": "ENC", "iv": "IV", "key": ""}
|
||||
crypto.decrypt_response.return_value = {"result": "ZCFG_SUCCESS", "sessionkey": "1"}
|
||||
session.get.side_effect = lambda url, **kw: MockResponse(
|
||||
{"RSAPublicKey": MOCK_RSA_KEY} if "getRSAPublickKey" in url else MOCK_BASIC_INFO
|
||||
)
|
||||
with (
|
||||
patch("napalm_zyxel.zyxel_driver.ZyxelCrypto", return_value=crypto),
|
||||
patch("napalm_zyxel.zyxel_driver.requests.Session", return_value=session),
|
||||
):
|
||||
driver.open()
|
||||
driver.close()
|
||||
assert self._timeouts(session) == {7}
|
||||
|
||||
def test_dal_query_and_page_fetch(self):
|
||||
driver, session = self._driver_with_session()
|
||||
driver._session = session
|
||||
driver._sessionkey = "1"
|
||||
driver._crypto = MagicMock()
|
||||
driver._dal_query("LAN_HOSTS")
|
||||
driver._fetch_page("Status.htm")
|
||||
assert self._timeouts(session) == {7}
|
||||
Reference in new issue
Block a user