Files
gandalf/tests/test_kuma.py
jaredandClaude Opus 5.5 c2ff072eb2
Lint / Python (flake8) (push) Failing after 15s
Lint / JS (eslint) (push) Successful in 1m43s
Lint / Deploy (push) Skipped
Test / Python Tests (pytest) (push) Failing after 21s
Security / Python Security (bandit) (push) Successful in 2m3s
Lint / Python (flake8) (pull_request) Failing after 15s
Lint / JS (eslint) (pull_request) Failing after 15s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 2m17s
Lint / Notify on failure (push) Successful in 3s
Test / Python Tests (pytest) (pull_request) Successful in 2m58s
Restart modem button: silence Gandalf and Uptime Kuma, restart, end both
Admins get a Restart modem button on the WAN / Fiber panel (needs
modem.access_code). After a confirmation it:

1. switches an Uptime Kuma maintenance window on (new kuma.py: logs in
   over Kuma 2's Socket.IO API and resumes/pauses one pre-made manual
   window, uptime_kuma.maintenance_id); if Kuma fails it stops and asks
   whether to go on without it,
2. starts Gandalf maintenance (global suppression, 20 minutes at most),
3. posts the gateway's Restart Device form (ModemAdmin.restart), undoing
   1 and 2 if that fails.

The monitor waits for the modem to answer with its WAN up and an uptime
showing it restarted, then switches Kuma off, ends the suppression and
records how long it took; after 30 minutes it ends both regardless. The
panel shows the restart in progress and the result for an hour.

New dependency: python-socketio[client].

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 19:27:25 -05:00

70 lines
2.4 KiB
Python

"""kuma.py: switching the Uptime Kuma maintenance window, against a fake Socket.IO client."""
import os
import sys
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
import kuma # noqa: E402
CFG = {'url': 'http://kuma.test:3001', 'username': 'gandalf', 'password': 'pw', 'maintenance_id': 7}
class FakeSocket:
def __init__(self, answers=None, connect_error=None):
self.answers = answers or {}
self.connect_error = connect_error
self.calls, self.disconnected = [], False
def connect(self, url, **kw):
if self.connect_error:
raise self.connect_error
self.url = url
def call(self, event, data=None, timeout=None):
self.calls.append((event, data))
return self.answers.get(event, {'ok': True})
def disconnect(self):
self.disconnected = True
def run(active=True, **kw):
sock = FakeSocket(**kw)
kuma.set_maintenance(CFG, active, client_factory=lambda: sock)
return sock
class TestKuma:
def test_configured_needs_every_field(self):
assert kuma.configured(CFG)
assert not kuma.configured(dict(CFG, password=''))
assert not kuma.configured(None)
def test_resume_logs_in_then_switches_the_window_on(self):
sock = run(True)
assert sock.calls == [('login', {'username': 'gandalf', 'password': 'pw', 'token': ''}),
('resumeMaintenance', 7)]
assert sock.disconnected and sock.url == CFG['url']
def test_pause(self):
assert run(False).calls[-1] == ('pauseMaintenance', 7)
def test_refused_login(self):
with pytest.raises(kuma.KumaError, match='login refused: Incorrect username or password'):
run(answers={'login': {'ok': False, 'msg': 'Incorrect username or password.'}})
def test_two_factor_account(self):
with pytest.raises(kuma.KumaError, match='two-factor'):
run(answers={'login': {'ok': False, 'tokenRequired': True}})
def test_refused_change(self):
with pytest.raises(kuma.KumaError, match='resumeMaintenance refused'):
run(answers={'resumeMaintenance': {'ok': False, 'msg': 'not found'}})
def test_unreachable(self):
sock = FakeSocket(connect_error=ConnectionError('refused'))
with pytest.raises(kuma.KumaError, match='ConnectionError'):
kuma.set_maintenance(CFG, True, client_factory=lambda: sock)
assert sock.disconnected