test_handler.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from kafkaesk.app import Application
  2. from kafkaesk.ext.logging import handler
  3. from unittest.mock import MagicMock
  4. from unittest.mock import patch
  5. import pytest
  6. pytestmark = pytest.mark.asyncio
  7. def test_close_log_handler():
  8. mock = MagicMock()
  9. h = handler.PydanticKafkaeskHandler(MagicMock(), "stream", mock)
  10. h.close()
  11. mock.close.assert_called_once()
  12. def test_format_log_exc():
  13. h = handler.PydanticKafkaeskHandler(MagicMock(), "stream", MagicMock())
  14. record = MagicMock()
  15. record.exc_text = None
  16. record.exc_info = (Exception(), None, None)
  17. data = h._format_base_log(record)
  18. assert data["stack"]
  19. def test_swallows_schema_conflict():
  20. app = Application()
  21. handler.PydanticKafkaeskHandler(app, "stream", MagicMock())
  22. handler.PydanticKafkaeskHandler(app, "stream", MagicMock())
  23. def test_get_k8s_ns():
  24. with patch("kafkaesk.ext.logging.handler._K8S_NS", handler._not_set), patch(
  25. "kafkaesk.ext.logging.handler.os.path.exists", return_value=True
  26. ), patch("kafkaesk.ext.logging.handler.open") as open_file:
  27. fi = MagicMock()
  28. cm = MagicMock()
  29. cm.__enter__.return_value = fi
  30. open_file.return_value = cm
  31. fi.read.return_value = "foobar\n"
  32. assert handler.get_k8s_ns() == "foobar"
  33. class TestQueue:
  34. def test_not_running(self):
  35. qq = handler.KafkaeskQueue(MagicMock())
  36. assert not qq.running
  37. def test_not_running_task_done(self):
  38. qq = handler.KafkaeskQueue(MagicMock())
  39. qq._task = MagicMock()
  40. qq._task.done.return_value = True
  41. assert not qq.running
  42. def test_running(self):
  43. qq = handler.KafkaeskQueue(MagicMock())
  44. qq._task = MagicMock()
  45. qq._task.done.return_value = False
  46. assert qq.running
  47. async def test_runtime_error_not_running(self):
  48. qq = handler.KafkaeskQueue(MagicMock())
  49. with pytest.raises(RuntimeError):
  50. assert await qq._run()