68 lines
2.7 KiB
Python
68 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import importlib.util
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
spec = importlib.util.spec_from_file_location("socks5_app", ROOT / "app.py")
|
|
assert spec is not None and spec.loader is not None
|
|
app = importlib.util.module_from_spec(spec)
|
|
sys.modules["socks5_app"] = app
|
|
spec.loader.exec_module(app)
|
|
|
|
|
|
class LogRetentionTest(unittest.TestCase):
|
|
def test_check_records_are_pruned_per_proxy(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
store = app.Store(Path(td) / "monitor.db")
|
|
|
|
async def run():
|
|
for i in range(5):
|
|
await store.add_check("node-a", True, i, None, f"203.0.113.{i}", max_per_proxy=2)
|
|
await store.add_check("node-b", True, 99, None, "198.51.100.1", max_per_proxy=2)
|
|
|
|
asyncio.run(run())
|
|
node_a = store.recent_checks("node-a", 10)
|
|
node_b = store.recent_checks("node-b", 10)
|
|
self.assertEqual(len(node_a), 2)
|
|
self.assertEqual([r["exit_ip"] for r in node_a], ["203.0.113.4", "203.0.113.3"])
|
|
self.assertEqual(len(node_b), 1)
|
|
|
|
def test_clear_logs_removes_checks_events_and_notification_logs(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
store = app.Store(Path(td) / "monitor.db")
|
|
|
|
async def seed_and_clear():
|
|
await store.add_check("node-a", True, 1, None, "203.0.113.1")
|
|
await store.add_event("node-a", "unknown", "up", "ok")
|
|
await store.add_notification({"id": "ch-1", "name": "test"}, True, "title", "body", "ok")
|
|
removed = await store.clear_logs()
|
|
return removed
|
|
|
|
removed = asyncio.run(seed_and_clear())
|
|
self.assertEqual(removed["checks"], 1)
|
|
self.assertEqual(removed["events"], 1)
|
|
self.assertEqual(removed["notification_logs"], 1)
|
|
self.assertEqual(store.recent_checks("node-a", 10), [])
|
|
self.assertEqual(store.recent_events(10), [])
|
|
self.assertEqual(store.recent_notifications(10), [])
|
|
|
|
def test_settings_accept_max_checks_per_proxy(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
cfg = Path(td) / "config.json"
|
|
cfg.write_text('{"proxies": [], "notifications": {"channels": []}}', encoding="utf-8")
|
|
application = app.App(cfg)
|
|
settings = asyncio.run(application.update_settings({"max_checks_per_proxy": 7}))
|
|
self.assertEqual(settings["max_checks_per_proxy"], 7)
|
|
saved = app.load_json(cfg)
|
|
self.assertEqual(saved["max_checks_per_proxy"], 7)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|