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>
458 lines
20 KiB
Python
458 lines
20 KiB
Python
"""Tests for modem.py parsing. Fixtures copy the BGW320-505 (fw 6.35.8) markup with sanitized values."""
|
|
import os
|
|
import sys
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
import requests
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
|
|
|
import modem # noqa: E402
|
|
|
|
|
|
def dom_table(heading, current, flags=(0, 0, 0, 0), limits=(-292, -90, -279, -100)):
|
|
return f'''
|
|
<h1>{heading} Currently {current}</h1>
|
|
<table class="grid table100" summary="{heading} table">
|
|
<thead><tr><th scope="col" width="30%"> </th><th scope="col">Low</th><th scope="col">High</th></tr></thead>
|
|
<tbody>
|
|
<tr class="a"><td class="heading" scope="row">Alarm</td>
|
|
<td>{flags[0]} (Threshold {limits[0]})</td>
|
|
<td>{flags[1]} (Threshold {limits[1]})</td>
|
|
</tr>
|
|
<tr class="b"><td class="heading" scope="row">Warning</td>
|
|
<td>{flags[2]} (Threshold {limits[2]})</td>
|
|
<td>{flags[3]} (Threshold {limits[3]})</td>
|
|
</tr>
|
|
</tbody></table>'''
|
|
|
|
|
|
def fiber_page(optical='Up', rx=-169, rx_flags=(0, 0, 0, 0), include_rx=True):
|
|
rx_table = dom_table('Rx Power', rx, rx_flags) if include_rx else ''
|
|
return f'''<html><title>Fiber Status</title>
|
|
<tr><th scope="row" width="47%">Optical WAN Operational Status</th>
|
|
<td class="col2">{optical} </td></tr>
|
|
<tr><th scope="row">Wave Length</th><td class="col2">1270 nm </td></tr>
|
|
<tr><th scope="row">Rx LOS State</th><td class="col2">1</td></tr>
|
|
<tr><th scope="row">Vendor PN</th><td class="col2">TESTPN</td></tr>
|
|
{dom_table('Temperature', 34, limits=(-50, 95, -45, 90))}
|
|
{dom_table('Vcc', 3, limits=(3, 3, 3, 3))}
|
|
{dom_table('Tx Bias', 6, limits=(0, 500, 0, 400))}
|
|
{dom_table('Tx Power', 33, limits=(-10, 59, 0, 49))}
|
|
{rx_table}</html>'''
|
|
|
|
|
|
def broadband_page(connection='Up', pon='OPERATION (O5)'):
|
|
return f'''<html><title>Broadband Status</title>
|
|
<tr><th scope="row" width="47%">Broadband Connection Source</th><td class="col2">FIBER</td></tr>
|
|
<tr><th scope="row" width="47%">Broadband Connection</th>
|
|
<td class="col2">
|
|
{connection}
|
|
</td></tr>
|
|
<tr><th scope="row">Broadband IPv4 Address</th><td class="col2">203.0.113.10</td></tr>
|
|
<tr class="a"><th scope="row" class="heading a">Current Speed (Mbps)</th><td>10000</td></tr>
|
|
<tr><th scope="row">Transmit Bytes</th><td>200</td></tr>
|
|
<tr><th scope="row">Receive Bytes</th><td>100</td></tr>
|
|
<tr><th scope="row">Receive Errors</th><td>1</td></tr>
|
|
<tr><th scope="row">Transmit Errors</th><td>2</td></tr>
|
|
<tr><th scope="row">Receive Drops</th><td>3</td></tr>
|
|
<tr><th scope="row">Transmit Drops</th><td>4</td></tr>
|
|
<tr><th scope="row">Transmit Packets</th><td>7</td></tr>
|
|
<tr><th scope="row">Transmit Packets</th><td>999</td></tr>
|
|
<tr><th scope="row">PON Link Status</th><td>{pon}</td></tr>
|
|
<tr><th scope="row">UNI Status</th><td>up</td></tr></html>'''
|
|
|
|
|
|
SYSINFO = '''<tr><th scope="row">Model Number</th><td class="col2">BGW320-505</td></tr>
|
|
<tr><th scope="row">Software Version</th><td class="col2">6.35.8</td></tr>
|
|
<tr><th scope="row">Time Since Last Reboot</th><td class="col2">28951</td></tr>'''
|
|
|
|
|
|
class TestBuildStatus:
|
|
def test_scales_optical_power_from_tenths_of_dbm(self):
|
|
d = modem.build_status(fiber_page(), broadband_page(), SYSINFO)
|
|
assert d['optics']['rx_power']['value'] == -16.9
|
|
assert d['optics']['tx_power']['value'] == 3.3
|
|
assert d['optics']['temperature']['value'] == 34.0
|
|
|
|
def test_scales_thresholds(self):
|
|
rx = modem.build_status(fiber_page(), broadband_page())['optics']['rx_power']
|
|
assert (rx['alarm_low'], rx['alarm_high'], rx['warn_low'], rx['warn_high']) == (-29.2, -9.0, -27.9, -10.0)
|
|
|
|
def test_unit_ambiguous_readings_are_dropped_but_flags_kept(self):
|
|
d = modem.build_status(fiber_page(), broadband_page())
|
|
assert d['optics']['tx_bias']['value'] is None
|
|
assert d['optics']['vcc']['alarm_low'] is None
|
|
assert d['optics']['vcc']['alarm_flag'] is False
|
|
|
|
def test_system_and_wan_fields(self):
|
|
d = modem.build_status(fiber_page(), broadband_page(), SYSINFO)
|
|
assert d['model'] == 'BGW320-505'
|
|
assert d['firmware'] == '6.35.8'
|
|
assert d['uptime_s'] == 28951
|
|
assert d['broadband_state'] == 'Up'
|
|
assert d['line_speed_mbps'] == 10000
|
|
assert d['counters'] == {
|
|
'rx_bytes': 100, 'tx_bytes': 200, 'rx_errors': 1,
|
|
'tx_errors': 2, 'rx_drops': 3, 'tx_drops': 4,
|
|
}
|
|
|
|
def test_repeated_label_keeps_first_value(self):
|
|
assert modem.parse_rows(broadband_page())['Transmit Packets'] == '7'
|
|
|
|
def test_sysinfo_is_optional(self):
|
|
d = modem.build_status(fiber_page(), broadband_page(), '')
|
|
assert d['model'] == '' and d['uptime_s'] is None
|
|
|
|
def test_unknown_page_is_invalid(self):
|
|
with pytest.raises(modem.ModemDataInvalid):
|
|
modem.build_status('<html>nope</html>', broadband_page())
|
|
with pytest.raises(modem.ModemDataInvalid):
|
|
modem.build_status(fiber_page(), '<html>nope</html>')
|
|
|
|
def test_up_link_without_rx_power_is_invalid(self):
|
|
with pytest.raises(modem.ModemDataInvalid):
|
|
modem.build_status(fiber_page(include_rx=False), broadband_page())
|
|
|
|
def test_cut_fiber_without_rx_power_is_not_invalid(self):
|
|
d = modem.build_status(fiber_page(optical='Down', include_rx=False), broadband_page(connection='Down'))
|
|
assert 'rx_power' not in d['optics']
|
|
assert modem.wan_problems(d)
|
|
|
|
|
|
class TestWanProblems:
|
|
def test_healthy_link_has_none(self):
|
|
assert modem.wan_problems(modem.build_status(fiber_page(), broadband_page())) == []
|
|
|
|
@pytest.mark.parametrize('field,value,expected', [
|
|
('broadband_state', 'Down', 'broadband connection is Down'),
|
|
('optical_state', 'Down', 'optical WAN is Down'),
|
|
('pon_state', 'INITIAL (O1)', 'PON link state is INITIAL (O1)'),
|
|
])
|
|
def test_each_failure_is_reported(self, field, value, expected):
|
|
d = {'broadband_state': 'Up', 'optical_state': 'Up', 'pon_state': 'OPERATION (O5)', field: value}
|
|
assert modem.wan_problems(d) == [expected]
|
|
|
|
def test_missing_pon_field_is_not_a_problem(self):
|
|
assert modem.wan_problems({'broadband_state': 'Up', 'optical_state': 'Up', 'pon_state': ''}) == []
|
|
|
|
def test_rx_los_pin_is_ignored(self):
|
|
# Real gateways report Rx LOS State=1 on a healthy link (see fixture).
|
|
assert modem.wan_problems(modem.build_status(fiber_page(), broadband_page())) == []
|
|
|
|
|
|
class TestMetricStatus:
|
|
@pytest.mark.parametrize('rx,expected', [
|
|
(-169, 'ok'),
|
|
(-279, 'warning'),
|
|
(-285, 'warning'),
|
|
(-292, 'alarm'),
|
|
(-400, 'alarm'),
|
|
(-100, 'warning'),
|
|
(-90, 'alarm'),
|
|
])
|
|
def test_rx_thresholds(self, rx, expected):
|
|
rx_metric = modem.parse_dom_metrics(fiber_page(rx=rx))['rx_power']
|
|
assert modem.metric_status(rx_metric) == expected
|
|
|
|
def test_module_flags_win_over_reading(self):
|
|
alarm = modem.parse_dom_metrics(fiber_page(rx_flags=(1, 0, 0, 0)))['rx_power']
|
|
warn = modem.parse_dom_metrics(fiber_page(rx_flags=(0, 0, 1, 0)))['rx_power']
|
|
assert modem.metric_status(alarm) == 'alarm'
|
|
assert modem.metric_status(warn) == 'warning'
|
|
|
|
def test_flag_only_metric(self):
|
|
assert modem.metric_status(modem.parse_dom_metrics(fiber_page())['vcc']) == 'ok'
|
|
|
|
|
|
class TestCounterRates:
|
|
def test_rate_from_delta(self):
|
|
assert modem.counter_rates({'rx_bytes': 300}, {'rx_bytes': 100}, 100)['rx_bytes'] == 2.0
|
|
|
|
def test_first_poll_is_none(self):
|
|
assert modem.counter_rates({'rx_bytes': 300}, None, 100)['rx_bytes'] is None
|
|
|
|
def test_reboot_counter_reset_is_none(self):
|
|
assert modem.counter_rates({'rx_bytes': 5}, {'rx_bytes': 100}, 100)['rx_bytes'] is None
|
|
|
|
|
|
class TestClient:
|
|
def _client(self, **response):
|
|
c = modem.ModemClient({'url': 'http://modem.test/'})
|
|
resp = MagicMock(status_code=response.get('status', 200), text=response.get('text', ''))
|
|
c.session.get = MagicMock(return_value=resp)
|
|
return c
|
|
|
|
def test_network_failure_is_unreachable(self):
|
|
c = self._client()
|
|
c.session.get = MagicMock(side_effect=requests.ConnectionError('boom'))
|
|
with pytest.raises(modem.ModemUnreachable):
|
|
c.poll()
|
|
|
|
def test_redirect_is_invalid_and_not_followed(self):
|
|
c = self._client(status=302)
|
|
with pytest.raises(modem.ModemDataInvalid):
|
|
c.poll()
|
|
assert c.session.get.call_args.kwargs['allow_redirects'] is False
|
|
|
|
def test_login_page_is_invalid(self):
|
|
with pytest.raises(modem.ModemDataInvalid, match='login'):
|
|
self._client(text='<html><title>Login</title></html>').poll()
|
|
|
|
|
|
class TestWanHistory:
|
|
def ev(self, key, ts, device='RPDU'):
|
|
return {'key': key, 'ts': ts, 'device': device, 'wan': 'WAN1'}
|
|
|
|
def test_outage_opens_and_closes(self):
|
|
h = {}
|
|
modem.apply_wan_events(h, [self.ev(modem.LOG_WAN_FAILED, 100), self.ev(modem.LOG_WAN_RESTORED, 200)])
|
|
assert h['outages'] == [{'failed_at': 100, 'restored_at': 200, 'wan': 'WAN1'}]
|
|
|
|
def test_events_are_handled_in_time_order(self):
|
|
h = {}
|
|
modem.apply_wan_events(h, [self.ev(modem.LOG_WAN_RESTORED, 200), self.ev(modem.LOG_WAN_FAILED, 100)])
|
|
assert h['outages'][0]['restored_at'] == 200
|
|
|
|
def test_outage_spanning_two_batches(self):
|
|
h = {}
|
|
modem.apply_wan_events(h, [self.ev(modem.LOG_WAN_FAILED, 100)])
|
|
assert h['outages'][0]['restored_at'] is None
|
|
modem.apply_wan_events(h, [self.ev(modem.LOG_WAN_RESTORED, 200)])
|
|
assert h['outages'][0]['restored_at'] == 200
|
|
|
|
def test_repeat_failure_does_not_open_a_second_outage(self):
|
|
h = {}
|
|
modem.apply_wan_events(h, [self.ev(modem.LOG_WAN_FAILED, 100), self.ev(modem.LOG_WAN_FAILED, 150)])
|
|
assert len(h['outages']) == 1
|
|
|
|
def test_restored_without_failure_is_ignored(self):
|
|
h = {}
|
|
modem.apply_wan_events(h, [self.ev(modem.LOG_WAN_RESTORED, 200)])
|
|
assert h['outages'] == []
|
|
|
|
def test_blip_is_not_actionable_but_restart_and_instability_are(self):
|
|
keys = [modem.LOG_WAN_FAILED, modem.LOG_WAN_RESTORED, modem.LOG_MODEM_RESTARTED, modem.LOG_WAN_UNSTABLE]
|
|
got = modem.apply_wan_events({}, [self.ev(k, i) for i, k in enumerate(keys)])
|
|
assert [e['key'] for e in got] == [modem.LOG_MODEM_RESTARTED, modem.LOG_WAN_UNSTABLE]
|
|
|
|
def test_history_is_bounded(self):
|
|
h = {}
|
|
for i in range(60):
|
|
modem.apply_wan_events(h, [
|
|
self.ev(modem.LOG_WAN_FAILED, i * 2), self.ev(modem.LOG_WAN_RESTORED, i * 2 + 1),
|
|
])
|
|
assert len(h['outages']) == 50
|
|
|
|
def test_uptime_reset_confirms_logged_restart(self):
|
|
h = {}
|
|
modem.apply_wan_events(h, [self.ev(modem.LOG_MODEM_RESTARTED, 1000)])
|
|
modem.record_uptime_reset(h, 1023)
|
|
assert h['restarts'] == [{'at': 1000, 'by': 'RPDU', 'confirmed': True}]
|
|
|
|
def test_unexplained_uptime_reset_is_recorded(self):
|
|
h = {}
|
|
modem.record_uptime_reset(h, 5000)
|
|
assert h['restarts'] == [{'at': 5000, 'by': None, 'confirmed': True}]
|
|
|
|
|
|
def _dashboard(health, downtime=(), latencies=None, group='WAN'):
|
|
return {
|
|
'wan_history': {'wan_history_details': [{
|
|
'network_group': group,
|
|
'health_history': [dict({'timestamp': ts * 1000, 'high_latency': False, 'packet_loss': False,
|
|
'wan_downtime': False}, **flags) for ts, flags in health],
|
|
'downtime_history': [{'timestamp': ts * 1000, 'downtime_in_seconds': s} for ts, s in downtime],
|
|
}]},
|
|
'wan': {'wan_details': [{'network_group': group, 'stats': {'service_latencies': [
|
|
{'service_name': k, 'latency': v} for k, v in (latencies or {}).items()]}}]},
|
|
}
|
|
|
|
|
|
class TestWanQuality:
|
|
def test_worst_flag_wins_and_periods_are_in_time_order(self):
|
|
q = modem.wan_quality(_dashboard([
|
|
(600, {'high_latency': True, 'packet_loss': True}),
|
|
(0, {}),
|
|
(300, {'high_latency': True}),
|
|
(900, {'wan_downtime': True, 'packet_loss': True}),
|
|
]), now=1000)
|
|
assert [p['state'] for p in q['periods']] == ['ok', 'latency', 'loss', 'down']
|
|
assert [p['ts'] for p in q['periods']] == [0, 300, 600, 900]
|
|
assert q['updated'] == 1000 and q['wan'] == 'WAN'
|
|
|
|
def test_downtime_and_latencies(self):
|
|
q = modem.wan_quality(_dashboard([(0, {})], downtime=[(500, 90)],
|
|
latencies={'cloudflare': 12, 'google': 26}), now=1)
|
|
assert q['downtime'] == [{'ts': 500, 'seconds': 90}]
|
|
assert q['latencies'] == {'cloudflare': 12, 'google': 26}
|
|
|
|
def test_prefers_the_primary_wan(self):
|
|
d = _dashboard([(0, {'high_latency': True})], group='WAN2')
|
|
d['wan_history']['wan_history_details'].append(
|
|
_dashboard([(0, {})])['wan_history']['wan_history_details'][0])
|
|
assert modem.wan_quality(d, 0)['periods'][0]['state'] == 'ok'
|
|
|
|
@pytest.mark.parametrize('dashboard', [None, {}, {'wan_history': {}}, {'wan_history': {'wan_history_details': []}}])
|
|
def test_missing_history_is_none(self, dashboard):
|
|
assert modem.wan_quality(dashboard, 0) is None
|
|
|
|
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()
|