forked from Yelp/paasta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_drain_lib.py
203 lines (172 loc) · 6.69 KB
/
test_drain_lib.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Copyright 2015-2016 Yelp Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import contextlib
import asynctest
import mock
import pytest
from paasta_tools import drain_lib
def test_register_drain_method():
with mock.patch.dict(drain_lib._drain_methods):
@drain_lib.register_drain_method("FAKEDRAINMETHOD")
class FakeDrainMethod(drain_lib.DrainMethod):
pass
assert (
type(drain_lib.get_drain_method("FAKEDRAINMETHOD", "srv", "inst", "ns"))
== FakeDrainMethod
)
@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
class TestHacheckDrainMethod:
drain_method = drain_lib.HacheckDrainMethod(
service="srv",
instance="inst",
registrations=["ns_one", "ns_two"],
hacheck_port=12345,
)
async def _async_id(self, x):
return x
def test_spool_urls(self):
fake_task = mock.Mock(host="fake_host", ports=[54321])
actual = self.drain_method.spool_urls(fake_task)
# Nerve hits /{mode}/{service}.{namespace}/{port}/status
expected = [
f"http://fake_host:12345/spool/{ns}/54321/status"
for ns in self.drain_method.registrations
]
assert actual == expected
@pytest.mark.asyncio
async def test_for_each_registration_with_no_ports(self):
fake_task = mock.Mock(host="fake_host", ports=[])
actual = await self.drain_method.for_each_registration(
task=fake_task, func=self._async_id
)
assert actual is None
@pytest.mark.asyncio
async def test_for_each_registration(self):
fake_task = mock.Mock(host="fake_host", ports=[54321])
actual = await self.drain_method.for_each_registration(
task=fake_task, func=self._async_id
)
assert actual == self.drain_method.spool_urls(fake_task)
@pytest.mark.asyncio
async def test_is_draining_yes(self):
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=mock.Mock(
return_value=asynctest.MagicMock(
__aenter__=asynctest.CoroutineMock(return_value=fake_response)
)
)
):
assert await self.drain_method.is_draining(fake_task) is True
@pytest.mark.asyncio
async def test_is_draining_no(self):
fake_response = mock.Mock(
status=200, text=asynctest.CoroutineMock(return_value="")
)
fake_task = mock.Mock(host="fake_host", ports=[54321])
with mock_ClientSession(
get=mock.Mock(
return_value=asynctest.MagicMock(
__aenter__=asynctest.CoroutineMock(return_value=fake_response)
)
)
):
assert await self.drain_method.is_draining(fake_task) is False
class TestHTTPDrainMethod:
def test_get_format_params(self):
fake_task = mock.Mock(host="fake_host", ports=[54321])
drain_method = drain_lib.HTTPDrainMethod(
"fake_service", "fake_instance", ["fake_nerve_ns"], {}, {}, {}, {}
)
assert drain_method.get_format_params(fake_task) == [
{
"host": "fake_host",
"port": 54321,
"service": "fake_service",
"instance": "fake_instance",
"nerve_ns": "fake_nerve_ns",
}
]
def test_format_url(self):
drain_method = drain_lib.HTTPDrainMethod(
"fake_service", "fake_instance", ["fake_nerve_ns"], {}, {}, {}, {}
)
url_format = "foo_{host}"
format_params = {"host": "fake_host"}
assert drain_method.format_url(url_format, format_params) == "foo_fake_host"
def test_parse_success_codes(self):
drain_method = drain_lib.HTTPDrainMethod(
"fake_service", "fake_instance", ["fake_nerve_ns"], {}, {}, {}, {}
)
assert drain_method.parse_success_codes("200") == {200}
assert drain_method.parse_success_codes("200-203") == {200, 201, 202, 203}
assert drain_method.parse_success_codes("200-202,302,305-306") == {
200,
201,
202,
302,
305,
305,
306,
}
assert drain_method.parse_success_codes(200) == {200}
def test_check_response_code(self):
drain_method = drain_lib.HTTPDrainMethod(
"fake_service", "fake_instance", ["fake_nerve_ns"], {}, {}, {}, {}
)
# Happy case
assert drain_method.check_response_code(200, "200-299") is True
# Sad case
assert drain_method.check_response_code(500, "200-299") is False
@pytest.mark.asyncio
async def test_issue_request(self):
drain_method = drain_lib.HTTPDrainMethod(
"fake_service", "fake_instance", ["fake_nerve_ns"], {}, {}, {}, {}
)
fake_task = mock.Mock(host="fake_host", ports=[54321])
url_spec = {
"url_format": "http://localhost:654321/fake/{host}",
"method": "get",
"success_codes": "1234",
}
fake_resp = mock.Mock(status=1234)
mock_request = mock.Mock(
return_value=asynctest.CoroutineMock(return_value=fake_resp)()
)
with mock_ClientSession(request=mock_request):
await drain_method.issue_request(url_spec=url_spec, task=fake_task)
mock_request.assert_called_once_with(
method="GET",
url="http://localhost:654321/fake/fake_host",
headers=mock.ANY,
timeout=15,
)