test_quota_scheduler_runtime.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """Regression tests for quota scheduler/runtime integration."""
  2. from __future__ import annotations
  3. from contextlib import contextmanager
  4. from services.quota import quota_scheduler
  5. class _Scheduler:
  6. def __init__(self) -> None:
  7. self.tasks = {}
  8. def task(self, *_args, **kwargs):
  9. def register(function):
  10. self.tasks[kwargs["id"]] = function
  11. return function
  12. return register
  13. class _ConfigManager:
  14. is_valid = True
  15. config_path = "/selected/squid.conf"
  16. def test_periodic_quota_reload_uses_the_selected_squid_runtime(monkeypatch):
  17. """The scheduler must not reload systemd/default Docker by accident."""
  18. scheduler = _Scheduler()
  19. selected_runtime = object()
  20. calls = []
  21. events = []
  22. @contextmanager
  23. def config_lock(config_path):
  24. events.append(("lock", "enter", config_path))
  25. try:
  26. yield
  27. finally:
  28. events.append(("lock", "exit", config_path))
  29. def reconfigure(config_path, runtime):
  30. calls.append((config_path, runtime))
  31. events.append(("reload", config_path, runtime))
  32. return True, "ok"
  33. monkeypatch.setattr(quota_scheduler, "SquidConfigManager", _ConfigManager)
  34. monkeypatch.setattr(
  35. quota_scheduler, "_find_squid_runtime", lambda: selected_runtime
  36. )
  37. monkeypatch.setattr(quota_scheduler, "squid_config_write_lock", config_lock)
  38. monkeypatch.setattr(
  39. quota_scheduler,
  40. "reconfigure_squid",
  41. reconfigure,
  42. )
  43. monkeypatch.setattr(quota_scheduler.Path, "exists", lambda _path: False)
  44. quota_scheduler.register_quota_scheduler_tasks(scheduler)
  45. scheduler.tasks["reload_squid_if_quota_enabled"]()
  46. assert calls == [("/selected/squid.conf", selected_runtime)]
  47. assert events == [
  48. ("lock", "enter", "/selected/squid.conf"),
  49. ("reload", "/selected/squid.conf", selected_runtime),
  50. ("lock", "exit", "/selected/squid.conf"),
  51. ]