Modem access code: NAT sessions, and a ping from the modem when slow
Lint / JS (eslint) (push) Successful in 1m11s
Lint / Python (flake8) (push) Successful in 1m55s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 54s
Lint / Python (flake8) (pull_request) Successful in 53s
Test / Python Tests (pytest) (push) Successful in 1m5s
Lint / JS (eslint) (pull_request) Successful in 11s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 55s
Test / Python Tests (pytest) (pull_request) Successful in 1m3s
Lint / JS (eslint) (push) Successful in 1m11s
Lint / Python (flake8) (push) Successful in 1m55s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 54s
Lint / Python (flake8) (pull_request) Successful in 53s
Test / Python Tests (pytest) (push) Successful in 1m5s
Lint / JS (eslint) (pull_request) Successful in 11s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 55s
Test / Python Tests (pytest) (pull_request) Successful in 1m3s
With modem.access_code in config.json, the monitor logs in to the BGW320 (md5 of code + nonce, as its own page does) for two read-only checks, stored in monitor_state.modem_checks: - NAT sessions in use every 30 minutes; Needs attention warns at 80%. - While UniFi says the internet is slow, a ping from the modem itself to 1.1.1.1 (at most every 10 minutes). Compared with UniFi's latency through the modem, it says whether the delay is past the modem (the provider), in the modem, or gone. The Internet line in Needs attention and the WAN / Fiber panel show the result. A refused code is reported and not retried for 30 minutes; other failures pause the checks for 10. The unauthenticated status polling is unchanged. Tested against the real gateway and a fake one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2f4cb05f67
commit
de75f71bf8
10 files changed
+645
-8
No files matched your search
@@ -301,3 +301,157 @@ class TestWanQuality:
|
||||
def test_counts_since(self):
|
||||
periods = [{'ts': 0, 'state': 'latency'}, {'ts': 300, 'state': 'ok'}, {'ts': 600, 'state': 'loss'}]
|
||||
assert modem.quality_counts(periods, 300) == {'ok': 1, 'latency': 0, 'loss': 1, 'down': 0}
|
||||
|
||||
|
||||
# Trimmed from a BGW320-505 (firmware 6.40.4)
|
||||
NAT_PAGE = '''<table class="table60" summary="This table displays a summary of session information.">
|
||||
<tr>
|
||||
<th scope="row" nowrap="nowrap">Total sessions available</th>
|
||||
<td>32767
|
||||
</td></tr>
|
||||
<tr>
|
||||
<th scope="row">Total sessions in use</th>
|
||||
<td>2858
|
||||
</td></tr></table>'''
|
||||
LOGIN_FORM = ('<h1>Access Code Required</h1><form>'
|
||||
'<input type="hidden" name="nonce" value="0a1b2c3d4e5f60718293a4b5c6d7e8f9" />')
|
||||
LOGIN_NO_NONCE = '<h1>Access Code Required</h1><strong>Cookies must be enabled</strong>'
|
||||
DIAG_FORM = ('<form method="post" action="/cgi-bin/diag.ha">'
|
||||
'<input type="hidden" name="nonce" value="ffee00112233445566778899aabbccdd" />')
|
||||
PING_DONE = '''PING 1.1.1.1 (1.1.1.1): 56 data bytes
|
||||
64 bytes from 1.1.1.1: seq=0 ttl=59 time=24.697 ms
|
||||
64 bytes from 1.1.1.1: seq=4 ttl=59 time=12.392 ms
|
||||
|
||||
--- 1.1.1.1 ping statistics ---
|
||||
5 packets transmitted, 5 packets received, 0% packet loss
|
||||
round-trip min/avg/max = 12.392/20.501/32.697 ms
|
||||
test done'''
|
||||
|
||||
|
||||
def _diag(window):
|
||||
return DIAG_FORM + f'<textarea id="progress" name="ProgressWindow" rows="15">{window}</textarea></form>'
|
||||
|
||||
|
||||
class TestModemPages:
|
||||
def test_login_hash_matches_the_page_script(self):
|
||||
# The page posts hex_md5(password + nonce)
|
||||
assert modem.login_hash('abc', '123') == 'e99a18c428cb38d5f260853678922e03'
|
||||
|
||||
def test_login_page_is_recognised(self):
|
||||
assert modem.is_login_page(LOGIN_FORM) and modem.is_login_page(LOGIN_NO_NONCE)
|
||||
assert not modem.is_login_page(NAT_PAGE)
|
||||
|
||||
def test_nat_summary(self):
|
||||
assert modem.parse_nat_summary(NAT_PAGE) == {'in_use': 2858, 'available': 32767}
|
||||
assert modem.parse_nat_summary('<table></table>') is None
|
||||
|
||||
def test_ping_output(self):
|
||||
assert modem.parse_ping_output(PING_DONE) == {
|
||||
'sent': 5, 'received': 5, 'loss_pct': 0, 'min_ms': 12.392, 'avg_ms': 20.501, 'max_ms': 32.697}
|
||||
|
||||
def test_ping_still_running_is_none(self):
|
||||
assert modem.parse_ping_output('PING 1.1.1.1 (1.1.1.1): 56 data bytes\n64 bytes from') is None
|
||||
|
||||
def test_ping_all_lost(self):
|
||||
r = modem.parse_ping_output('5 packets transmitted, 0 packets received, 100% packet loss')
|
||||
assert r['loss_pct'] == 100 and r['avg_ms'] is None
|
||||
|
||||
@pytest.mark.parametrize('gateway,ours,expected', [
|
||||
({'avg_ms': 900.0, 'loss_pct': 0}, 1000, 'upstream'),
|
||||
({'avg_ms': 20.0, 'loss_pct': 40}, 1000, 'upstream'),
|
||||
({'avg_ms': None, 'loss_pct': 100}, None, 'upstream'),
|
||||
({'avg_ms': 20.0, 'loss_pct': 0}, 1000, 'gateway'),
|
||||
({'avg_ms': 20.0, 'loss_pct': 0}, 30, 'clear'),
|
||||
({'avg_ms': 20.0, 'loss_pct': 0}, None, 'clear'),
|
||||
])
|
||||
def test_verdict(self, gateway, ours, expected):
|
||||
assert modem.ping_verdict(gateway, ours) == expected
|
||||
|
||||
|
||||
class FakeGateway:
|
||||
"""Answers ModemAdmin's requests like the BGW320 does."""
|
||||
|
||||
def __init__(self, code='secret', ping_pages=None):
|
||||
self.code, self.logged_in, self.calls = code, False, []
|
||||
self.cookie = self.pinged = False
|
||||
self.ping_pages = list(ping_pages or [_diag(PING_DONE)])
|
||||
|
||||
def __call__(self, method, url, **kw):
|
||||
path = url.split('192.0.2.254', 1)[1]
|
||||
self.calls.append((method, path))
|
||||
if path == '/cgi-bin/login.ha' and method == 'GET':
|
||||
first, self.cookie = not self.cookie, True
|
||||
return MagicMock(status_code=200, text=LOGIN_NO_NONCE if first else LOGIN_FORM)
|
||||
if path == '/cgi-bin/login.ha':
|
||||
ok = kw['data']['hashpassword'] == modem.login_hash(self.code, '0a1b2c3d4e5f60718293a4b5c6d7e8f9')
|
||||
assert set(kw['data']['password']) == {'*'} # the code itself is never sent
|
||||
self.logged_in = ok
|
||||
return MagicMock(status_code=302 if ok else 200, text='' if ok else LOGIN_FORM)
|
||||
if not self.logged_in:
|
||||
return MagicMock(status_code=200, text=LOGIN_FORM)
|
||||
if path == '/cgi-bin/nattable.ha':
|
||||
return MagicMock(status_code=200, text=NAT_PAGE)
|
||||
if path == '/cgi-bin/diag.ha' and method == 'POST':
|
||||
assert kw['data']['WebAddress'] == '1.1.1.1' and kw['data']['Ping'] == 'Ping'
|
||||
self.pinged = True
|
||||
return MagicMock(status_code=200, text=_diag(''))
|
||||
if path == '/cgi-bin/diag.ha':
|
||||
# Before the test the window is empty; after it, each refresh shows more output
|
||||
page = self.ping_pages.pop(0) if self.pinged else _diag('')
|
||||
return MagicMock(status_code=200, text=page)
|
||||
raise AssertionError(path)
|
||||
|
||||
|
||||
class TestModemAdmin:
|
||||
def _admin(self, gw, now=None):
|
||||
clock = MagicMock(return_value=now or 1000.0)
|
||||
a = modem.ModemAdmin({'url': 'http://192.0.2.254', 'access_code': 'secret'},
|
||||
clock=clock, sleep=lambda s: None)
|
||||
a.session.request = MagicMock(side_effect=gw)
|
||||
return a, clock
|
||||
|
||||
def test_logs_in_when_needed_and_reads_nat(self):
|
||||
gw = FakeGateway()
|
||||
a, _ = self._admin(gw)
|
||||
assert a.nat_sessions() == {'in_use': 2858, 'available': 32767}
|
||||
assert ('POST', '/cgi-bin/login.ha') in gw.calls
|
||||
gw.calls.clear()
|
||||
a.nat_sessions() # session still good: no second login
|
||||
assert ('POST', '/cgi-bin/login.ha') not in gw.calls
|
||||
|
||||
def test_expired_session_logs_in_again(self):
|
||||
gw = FakeGateway()
|
||||
a, _ = self._admin(gw)
|
||||
a.nat_sessions()
|
||||
gw.logged_in = False
|
||||
assert a.nat_sessions()['in_use'] == 2858
|
||||
|
||||
def test_refused_code_backs_off(self):
|
||||
gw = FakeGateway(code='other')
|
||||
a, clock = self._admin(gw)
|
||||
with pytest.raises(modem.ModemLoginFailed):
|
||||
a.nat_sessions()
|
||||
gw.calls.clear()
|
||||
clock.return_value = 1000.0 + modem.LOGIN_BACKOFF_S - 1
|
||||
with pytest.raises(modem.ModemLoginFailed):
|
||||
a.nat_sessions()
|
||||
assert ('POST', '/cgi-bin/login.ha') not in gw.calls # didn't even try
|
||||
|
||||
def test_ping_from_the_gateway_waits_for_the_result(self):
|
||||
gw = FakeGateway(ping_pages=[_diag('PING 1.1.1.1 (1.1.1.1): 56 data bytes'), _diag(PING_DONE)])
|
||||
a, _ = self._admin(gw)
|
||||
r = a.ping('1.1.1.1')
|
||||
assert r['avg_ms'] == 20.501 and r['target'] == '1.1.1.1'
|
||||
|
||||
def test_ping_that_never_finishes(self):
|
||||
gw = FakeGateway(ping_pages=[_diag('PING ...')] * 50)
|
||||
a, clock = self._admin(gw)
|
||||
clock.side_effect = [1000.0] + [2000.0] * 100
|
||||
with pytest.raises(modem.ModemDataInvalid, match='did not finish'):
|
||||
a.ping('1.1.1.1')
|
||||
|
||||
def test_unreachable(self):
|
||||
a, _ = self._admin(FakeGateway())
|
||||
a.session.request = MagicMock(side_effect=requests.ConnectionError('boom'))
|
||||
with pytest.raises(modem.ModemUnreachable):
|
||||
a.nat_sessions()
|
||||
Reference in new issue
Block a user