-
Notifications
You must be signed in to change notification settings - Fork 26
/
test_gunicorn.py
282 lines (227 loc) · 8.46 KB
/
test_gunicorn.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
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
#
# Copyright (c) 2015-2018 Canonical, Ltd.
#
# This file is part of Talisker
# (see http://github.com/canonical-ols/talisker).
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from builtins import * # noqa
import pytest
try:
import gunicorn # noqa
except ImportError:
pytest.skip("skipping gunicorn only tests", allow_module_level=True)
else:
del gunicorn
import itertools
import json
import logging
import os
import signal
import subprocess
import sys
import time
from gunicorn.config import Config
import requests
import pytest
from talisker import gunicorn # noqa
from talisker.context import Context
from talisker import logs
from talisker.testing import GunicornProcess
import talisker.wsgi
from tests.test_metrics import counter_name
def test_talisker_entrypoint():
entrypoint = 'talisker.gunicorn'
subprocess.check_output([entrypoint, '--help'])
def test_gunicorn_logger_propagate_error_log():
cfg = Config()
logger = gunicorn.GunicornLogger(cfg)
assert logger.error_log.propagate is True
assert len(logger.error_log.handlers) == 0
def test_gunicorn_application_init(monkeypatch):
monkeypatch.setattr(sys, 'argv', ['talisker', 'wsgi:app'])
app = gunicorn.TaliskerApplication('')
assert app.cfg.logger_class == gunicorn.GunicornLogger
assert app.cfg.loglevel.lower() == 'info'
assert app.cfg.on_starting is gunicorn.gunicorn_on_starting
assert app.cfg.child_exit is gunicorn.gunicorn_child_exit
assert app.cfg.worker_exit is gunicorn.gunicorn_worker_exit
assert logs.get_talisker_handler().level == logging.NOTSET
def test_gunicorn_application_init_devel(monkeypatch):
monkeypatch.setattr(sys, 'argv', ['talisker', 'wsgi:app'])
app = gunicorn.TaliskerApplication('', devel=True)
assert app.cfg.timeout == 99999
assert app.cfg.reload
def test_gunicorn_application_init_devel_overriden(monkeypatch):
monkeypatch.setattr(
sys, 'argv',
['talisker', 'wsgi:app', '--timeout', '10'])
app = gunicorn.TaliskerApplication('', devel=True)
assert app.cfg.timeout == 10
def test_gunicorn_application_config_errorlog(monkeypatch, context):
monkeypatch.setattr(
sys, 'argv',
['talisker', 'wsgi:app', '--log-file', '/tmp/log'])
app = gunicorn.TaliskerApplication('')
context.assert_log(
msg='ignoring gunicorn errorlog',
extra={'errorlog': '/tmp/log'},
)
assert app.cfg.errorlog == '-'
def test_gunicorn_application_config_loglevel_debug_devel(monkeypatch):
monkeypatch.setattr(
sys, 'argv',
['talisker', 'wsgi:app', '--log-level', 'debug'])
app = gunicorn.TaliskerApplication('', devel=True)
assert app.cfg.loglevel.lower() == 'debug'
assert logs.get_talisker_handler().level == logging.DEBUG
def test_gunicorn_application_config_statsd(monkeypatch, context):
monkeypatch.setattr(
sys, 'argv',
['talisker', 'wsgi:app', '--statsd-host', 'localhost:8125'])
app = gunicorn.TaliskerApplication('')
context.assert_log(
msg='ignoring gunicorn statsd',
extra={'statsd_host': ('localhost', 8125)},
)
assert app.cfg.statsd_host is None
assert app.cfg.statsd_prefix is None
def test_gunicorn_application_config_logger_class(monkeypatch, context):
monkeypatch.setattr(
sys, 'argv',
['talisker', 'wsgi:app', '--logger-class', 'gunicorn.glogging.Logger'])
from gunicorn.glogging import Logger
app = gunicorn.TaliskerApplication('')
context.assert_log(
msg='using custom gunicorn logger class',
extra={'logger_class': Logger},
)
assert app.cfg.logger_class is Logger
def wsgi(environ, start_response):
start_response('200 OK', [])
return ''
def test_gunicorn_application_load(monkeypatch):
monkeypatch.setattr(
sys, 'argv', ['', __name__ + ':wsgi', '--bind=0.0.0.0:0'])
app = gunicorn.TaliskerApplication('')
wsgiapp = app.load_wsgiapp()
assert wsgiapp._talisker_wrapped
assert wsgiapp._talisker_original_app == wsgi
counter = itertools.count()
next(counter)
def counter_app(environ, start_response, accumulator=[]):
start_response('200 OK', [('Content-Type', 'application/json')])
num = str(next(counter))
Context.logging.push({num: num})
return [json.dumps(Context.logging.flat).encode('utf8')]
def test_gunicorn_clears_context():
app = __name__ + ':counter_app'
pr = GunicornProcess(app, args=['--worker-class=sync'])
with pr:
r1 = requests.get(pr.url('/')).json()
r2 = requests.get(pr.url('/')).json()
r3 = requests.get(pr.url('/')).json()
assert r1['1'] == '1'
assert r2['2'] == '2'
assert '1' not in r2
assert r3['3'] == '3'
assert '2' not in r3
@pytest.mark.timeout(120)
@pytest.mark.flaky
def test_gunicorn_prometheus_cleanup(caplog):
caplog.set_level(logging.INFO)
app = __name__ + ':counter_app'
workers = 8
server = GunicornProcess(
app, args=['--worker-class=sync', '-w', str(workers)])
def increment(n):
for i in range(n):
requests.get(server.url('/_status/test/prometheus'))
def files(pid):
pid = str(pid)
pid_files = set()
archives = set()
for path in os.listdir(os.environ['prometheus_multiproc_dir']):
# ignore master pids
if pid in path:
continue
if '_archive.db' in path:
archives.add(path)
else:
pid_files.add(path)
return archives, pid_files
def stats():
return requests.get(server.url('/_status/metrics')).text
name = counter_name('test_total')
valid_archives = set(['counter_archive.db', 'histogram_archive.db'])
sleep_factor = 1
if os.environ.get('CI') == 'true':
# travis is slow
sleep_factor = 20
with server:
# forking can be really slow on travis, so make sure *all* the workers
# have had time to spin up before running the test
time.sleep(1 * sleep_factor)
increment(2000)
archives, pid_files_1 = files(server.ps.pid)
assert len(archives) == 0
# different number of files depending on prometheus_client version
# so we assert against 1x or 2x workers
assert len(pid_files_1) in (workers, 2 * workers)
assert name + ' 2000.0' in stats()
os.kill(server.ps.pid, signal.SIGHUP)
time.sleep(2 * sleep_factor)
archives, pid_files_2 = files(server.ps.pid)
assert archives == valid_archives
assert pid_files_1.isdisjoint(pid_files_2)
assert name + ' 2000.0' in stats()
increment(2000)
assert name + ' 4000.0' in stats()
archives, pid_files_3 = files(server.ps.pid)
assert archives == valid_archives
assert len(pid_files_3) in (workers, 2 * workers)
os.kill(server.ps.pid, signal.SIGHUP)
time.sleep(2 * sleep_factor)
archives, pid_files_4 = files(server.ps.pid)
assert archives == valid_archives
assert pid_files_3.isdisjoint(pid_files_4)
assert name + ' 4000.0' in stats()
def test_gunicorn_worker_exit(wsgi_env, context):
wsgi_env['start_time'] = time.time()
wsgi_env['REQUEST_ID'] = 'ID'
Context.new()
Context.request_id = 'ID'
request = talisker.wsgi.TaliskerWSGIRequest(wsgi_env, None, [])
talisker.wsgi.REQUESTS['ID'] = request
class worker:
pid = 100
gunicorn.gunicorn_worker_exit(None, worker())
context.assert_log(
name='talisker.wsgi',
msg='GET /',
extra={
'request_id': 'ID',
'timeout': True,
},
)
assert len(context.sentry) == 1