-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtest_sanic.py
More file actions
323 lines (242 loc) · 10 KB
/
test_sanic.py
File metadata and controls
323 lines (242 loc) · 10 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, you can obtain one at http://mozilla.org/MPL/2.0/.
import functools
import logging
import uuid
import pytest
import redis
import sanic_redis.core
from sanic import Sanic, response
from sanic_redis import SanicRedis
from sanic_testing.testing import SanicTestClient
from dockerflow import checks, health
from dockerflow.sanic import Dockerflow
class FakeRedis:
def __init__(self, *args, error=None, **kw):
self.error = error
def __await__(self):
return self
yield
def __enter__(self):
return self
def __exit__(self, *exc_info):
pass
async def close(self):
pass
async def wait_closed(self):
pass
async def ping(self):
if self.error == "connection":
raise redis.ConnectionError("fake")
elif self.error == "redis":
raise redis.RedisError("fake")
elif self.error == "malformed":
return b"PING"
else:
return b"PONG"
async def fake_redis(*args, **kw):
return FakeRedis(*args, **kw)
@pytest.fixture()
def app():
app = Sanic(f"dockerflow-{uuid.uuid4().hex}")
@app.route("/")
async def root(request):
if request.body:
raise ValueError(request.body.decode())
return response.raw(b"")
return app
@pytest.fixture()
def dockerflow(app):
return Dockerflow(app)
@pytest.fixture()
def _setup_request_summary_logger(dockerflow):
dockerflow.summary_logger.addHandler(logging.NullHandler())
dockerflow.summary_logger.setLevel(logging.INFO)
@pytest.fixture()
def dockerflow_redis(app):
app.config["REDIS"] = {"address": "redis://:password@localhost:6379/0"}
return Dockerflow(app, redis=SanicRedis(app))
@pytest.fixture()
def test_client(app):
return SanicTestClient(app)
def test_instantiating(app):
Dockerflow()
assert ("__heartbeat__",) not in app.router.routes_all
Dockerflow(app)
assert ("__heartbeat__",) in app.router.routes_all
def test_version_exists(dockerflow, mocker, test_client, version_content):
mocker.patch.object(dockerflow, "_version_callback", return_value=version_content)
_, response = test_client.get("/__version__")
assert response.status == 200
assert response.json == version_content
def test_version_path(app, mocker, test_client, version_content):
custom_version_path = "/something/extra/ordinary"
dockerflow = Dockerflow(app, version_path=custom_version_path)
version_callback = mocker.patch.object(
dockerflow, "_version_callback", return_value=version_content
)
_, response = test_client.get("/__version__")
assert response.status == 200
assert response.json == version_content
version_callback.assert_called_with(custom_version_path)
def test_version_missing(dockerflow, mocker, test_client):
mocker.patch.object(dockerflow, "_version_callback", return_value=None)
_, response = test_client.get("/__version__")
assert response.status == 404
def test_version_callback(dockerflow, test_client):
callback_version = {"version": "1.0"}
@dockerflow.version_callback
async def version_callback(path):
return callback_version
_, response = test_client.get("/__version__")
assert response.status == 200
assert response.json == callback_version
def test_lbheartbeat(dockerflow, test_client):
_, response = test_client.get("/__lbheartbeat__")
assert response.status == 200
assert response.body == b""
def test_error_returns_500_and_logs_error(dockerflow, test_client, caplog):
with caplog.at_level(logging.INFO, logger="dockerflow.sanic"):
_, response = test_client.get("/__error__")
assert response.status_code == 500
assert len(caplog.records) >= 1
record = caplog.records[0]
assert record.getMessage() == "The __error__ endpoint was called"
assert record.levelno == logging.ERROR
def test_heartbeat(dockerflow, test_client):
_, response = test_client.get("/__heartbeat__")
assert response.status == 200
def test_heartbeat_checks(dockerflow, test_client):
@checks.register
def error_check():
return [checks.Error("some error", id="tests.checks.E001")]
@checks.register()
def warning_check():
return [checks.Warning("some warning", id="tests.checks.W001")]
@checks.register(name="warning-check-two")
async def warning_check2():
return [checks.Warning("some other warning", id="tests.checks.W002")]
_, response = test_client.get("/__heartbeat__")
assert response.status == 500
payload = response.json
assert payload["status"] == "error"
details = payload["details"]
assert "error_check" in details
assert "warning_check" in details
assert "warning-check-two" in details
def test_heartbeat_silenced_checks(app, test_client):
app = Dockerflow(app, silenced_checks=["tests.checks.E001"])
@checks.register
def error_check():
return [checks.Error("some error", id="tests.checks.E001")]
@checks.register()
def warning_check():
return [checks.Warning("some warning", id="tests.checks.W001")]
_, response = test_client.get("/__heartbeat__")
assert response.status == 200
payload = response.json
assert payload["status"] == "warning"
details = payload["details"]
assert "error_check" not in details
assert "warning_check" in details
def test_heartbeat_logging(dockerflow, test_client, caplog):
@checks.register
def error_check():
return [checks.Error("some error", id="tests.checks.E001")]
@checks.register()
def warning_check():
return [checks.Warning("some warning", id="tests.checks.W001")]
with caplog.at_level(logging.INFO, logger="dockerflow.checks.registry"):
_, response = test_client.get("/__heartbeat__")
logged = [(record.levelname, record.message) for record in caplog.records]
assert ("ERROR", "tests.checks.E001: some error") in logged
assert ("WARNING", "tests.checks.W001: some warning") in logged
def test_redis_check(dockerflow_redis, mocker, test_client):
assert "check_redis_connected" in checks.get_checks()
mocker.patch.object(sanic_redis.core, "from_url", fake_redis)
_, response = test_client.get("/__heartbeat__")
assert response.status == 200
assert response.json["status"] == "ok"
@pytest.mark.parametrize(
("error", "messages"),
[
(
"connection",
{health.ERROR_CANNOT_CONNECT_REDIS: "Could not connect to redis: fake"},
),
("redis", {health.ERROR_REDIS_EXCEPTION: 'Redis error: "fake"'}),
("malformed", {health.ERROR_REDIS_PING_FAILED: "Redis ping failed"}),
],
)
def test_redis_check_error(dockerflow_redis, mocker, test_client, error, messages):
assert "check_redis_connected" in checks.get_checks()
fake_redis_error = functools.partial(fake_redis, error=error)
mocker.patch.object(sanic_redis.core, "from_url", fake_redis_error)
_, response = test_client.get("/__heartbeat__")
assert response.status == 500
assert response.json["status"] == "error"
assert response.json["details"]["check_redis_connected"]["messages"] == messages
def assert_log_record(caplog, errno=0, level=logging.INFO, rid=None, t=int, path="/"):
records = [r for r in caplog.records if r.name == "request.summary"]
assert len(records) == 1
record = records.pop()
assert record.agent == "dockerflow/tests"
assert record.lang == "tlh"
assert record.method == "GET"
assert record.path == path
assert record.errno == errno
assert record.levelno == level
assert getattr(record, "rid", None) == rid
if t is None:
assert getattr(record, "t", None) is None
else:
assert isinstance(record.t, t)
return record
headers = {"User-Agent": "dockerflow/tests", "Accept-Language": "tlh"}
@pytest.mark.usefixtures("_setup_request_summary_logger")
def test_request_summary(caplog, test_client):
request, _ = test_client.get(headers=headers)
assert isinstance(request.ctx.start_timestamp, float)
assert request.ctx.id is not None
assert_log_record(caplog, rid=request.ctx.id)
@pytest.mark.usefixtures("_setup_request_summary_logger")
def test_request_summary_querystring(app, caplog, test_client):
app.config["DOCKERFLOW_SUMMARY_LOG_QUERYSTRING"] = True
_, _ = test_client.get("/?x=شكر", headers=headers)
records = [r for r in caplog.records if r.name == "request.summary"]
assert len(records) == 1
record = caplog.records[0]
assert record.querystring == "x=شكر"
def test_request_summary_exception(app, caplog, dockerflow, test_client):
@app.route("/exception")
def exception_raiser(request):
raise ValueError("exception message")
request, _ = test_client.get("/exception", headers=headers)
record = assert_log_record(
caplog, 500, logging.ERROR, request.ctx.id, path="/exception"
)
assert record.getMessage() == "exception message"
@pytest.mark.usefixtures("_setup_request_summary_logger")
def test_request_summary_failed_request(app, caplog, test_client):
@app.middleware
def hostile_callback(request):
del request.ctx.id
# simulating resetting request changes
del request.ctx.start_timestamp
test_client.get(headers={"X-Request-ID": "tracked", **headers})
assert_log_record(caplog, rid="tracked", t=None)
def test_heartbeat_checks_legacy(dockerflow, test_client):
dockerflow.checks.clear()
@dockerflow.check
def error_check():
return [checks.Error("some error", id="tests.checks.E001")]
def error_check_partial(obj):
return [checks.Error(repr(obj), id="tests.checks.E001")]
dockerflow.init_check(error_check_partial, ("foo", "bar"))
_, response = test_client.get("/__heartbeat__")
assert response.status == 500
payload = response.json
assert payload["status"] == "error"
assert "error_check" in payload["details"]
assert "('foo', 'bar')" in str(payload["details"]["error_check_partial"])