2020-08-06 05:11:26 +03:00
|
|
|
"""Tests module."""
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import dataclasses
|
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
from .containers import Container
|
2020-08-06 05:11:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
class RequestStub:
|
|
|
|
status: int
|
|
|
|
content_length: int
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def container():
|
2021-11-01 03:25:13 +03:00
|
|
|
return Container(
|
|
|
|
config={
|
|
|
|
"log": {
|
|
|
|
"level": "INFO",
|
|
|
|
"formant": "[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s",
|
2020-08-06 05:11:26 +03:00
|
|
|
},
|
2021-11-01 03:25:13 +03:00
|
|
|
"monitors": {
|
|
|
|
"example": {
|
|
|
|
"method": "GET",
|
|
|
|
"url": "http://fake-example.com",
|
|
|
|
"timeout": 1,
|
|
|
|
"check_every": 1,
|
|
|
|
},
|
|
|
|
"httpbin": {
|
|
|
|
"method": "GET",
|
|
|
|
"url": "https://fake-httpbin.org/get",
|
|
|
|
"timeout": 1,
|
|
|
|
"check_every": 1,
|
|
|
|
},
|
2020-08-06 05:11:26 +03:00
|
|
|
},
|
2021-11-01 03:25:13 +03:00
|
|
|
}
|
|
|
|
)
|
2020-08-06 05:11:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_example_monitor(container, caplog):
|
2021-09-30 22:03:19 +03:00
|
|
|
caplog.set_level("INFO")
|
2020-08-06 05:11:26 +03:00
|
|
|
|
|
|
|
http_client_mock = mock.AsyncMock()
|
|
|
|
http_client_mock.request.return_value = RequestStub(
|
|
|
|
status=200,
|
|
|
|
content_length=635,
|
|
|
|
)
|
|
|
|
|
|
|
|
with container.http_client.override(http_client_mock):
|
|
|
|
example_monitor = container.example_monitor()
|
|
|
|
await example_monitor.check()
|
|
|
|
|
2021-09-30 22:03:19 +03:00
|
|
|
assert "http://fake-example.com" in caplog.text
|
|
|
|
assert "response code: 200" in caplog.text
|
|
|
|
assert "content length: 635" in caplog.text
|
2020-08-06 05:11:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_dispatcher(container, caplog, event_loop):
|
2021-09-30 22:03:19 +03:00
|
|
|
caplog.set_level("INFO")
|
2020-08-06 05:11:26 +03:00
|
|
|
|
|
|
|
example_monitor_mock = mock.AsyncMock()
|
|
|
|
httpbin_monitor_mock = mock.AsyncMock()
|
|
|
|
|
2021-11-01 03:25:13 +03:00
|
|
|
with container.override_providers(
|
|
|
|
example_monitor=example_monitor_mock,
|
|
|
|
httpbin_monitor=httpbin_monitor_mock,
|
|
|
|
):
|
2020-08-06 05:11:26 +03:00
|
|
|
dispatcher = container.dispatcher()
|
|
|
|
event_loop.create_task(dispatcher.start())
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
dispatcher.stop()
|
|
|
|
|
|
|
|
assert example_monitor_mock.check.called
|
|
|
|
assert httpbin_monitor_mock.check.called
|