forked from Yelp/paasta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_hacheck.py
61 lines (47 loc) · 1.51 KB
/
test_hacheck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import contextlib
import asynctest
import mock
import pytest
from paasta_tools import hacheck
@contextlib.contextmanager
def mock_ClientSession(**fake_session_kwargs):
fake_session = asynctest.MagicMock(name="session", **fake_session_kwargs)
class FakeClientSession:
def __init__(self, *args, **kwargs):
...
async def __aenter__(*args):
return fake_session
async def __aexit__(*args):
pass
with mock.patch("aiohttp.ClientSession", new=FakeClientSession, autospec=False):
yield
@pytest.mark.asyncio
async def test_get_spool():
fake_response = mock.Mock(
status=503,
text=asynctest.CoroutineMock(
return_value="Service service in down state since 1435694078.778886 "
"until 1435694178.780000: Drained by Paasta"
),
)
fake_task = mock.Mock(host="fake_host", ports=[54321])
with mock_ClientSession(
get=asynctest.Mock(
return_value=asynctest.MagicMock(
__aenter__=asynctest.CoroutineMock(return_value=fake_response)
)
)
):
actual = await hacheck.get_spool(fake_task)
expected = {
"service": "service",
"state": "down",
"reason": "Drained by Paasta",
"since": 1435694078.778886,
"until": 1435694178.780000,
}
assert actual == expected
@pytest.mark.asyncio
async def test_get_spool_handles_no_ports():
actual = await hacheck.get_spool(None)
assert actual is None