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
|
# Tests for aiohttp/worker.py
import asyncio
import os
import socket
import ssl
from unittest import mock
import pytest
from aiohttp import web
base_worker = pytest.importorskip("aiohttp.worker")
try:
import uvloop
except ImportError:
uvloop = None
WRONG_LOG_FORMAT = '%a "%{Referrer}i" %(h)s %(l)s %s'
ACCEPTABLE_LOG_FORMAT = '%a "%{Referrer}i" %s'
# tokio event loop does not allow to override attributes
def skip_if_no_dict(loop):
if not hasattr(loop, "__dict__"):
pytest.skip("can not override loop attributes")
class BaseTestWorker:
def __init__(self):
self.servers = {}
self.exit_code = 0
self._notify_waiter = None
self.cfg = mock.Mock()
self.cfg.graceful_timeout = 100
self.pid = "pid"
self.wsgi = web.Application()
class AsyncioWorker(BaseTestWorker, base_worker.GunicornWebWorker): # type: ignore
pass
PARAMS = [AsyncioWorker]
if uvloop is not None:
class UvloopWorker(
BaseTestWorker, base_worker.GunicornUVLoopWebWorker # type: ignore
):
pass
PARAMS.append(UvloopWorker)
@pytest.fixture(params=PARAMS)
def worker(request, loop):
asyncio.set_event_loop(loop)
ret = request.param()
ret.notify = mock.Mock()
return ret
def test_init_process(worker) -> None:
with mock.patch("aiohttp.worker.asyncio") as m_asyncio:
try:
worker.init_process()
except TypeError:
pass
assert m_asyncio.get_event_loop.return_value.close.called
assert m_asyncio.new_event_loop.called
assert m_asyncio.set_event_loop.called
def test_run(worker, loop) -> None:
worker.log = mock.Mock()
worker.cfg = mock.Mock()
worker.cfg.access_log_format = ACCEPTABLE_LOG_FORMAT
worker.cfg.is_ssl = False
worker.sockets = []
worker.loop = loop
with pytest.raises(SystemExit):
worker.run()
worker.log.exception.assert_not_called()
assert loop.is_closed()
def test_run_async_factory(worker, loop) -> None:
worker.log = mock.Mock()
worker.cfg = mock.Mock()
worker.cfg.access_log_format = ACCEPTABLE_LOG_FORMAT
worker.cfg.is_ssl = False
worker.sockets = []
app = worker.wsgi
async def make_app():
return app
worker.wsgi = make_app
worker.loop = loop
worker.alive = False
with pytest.raises(SystemExit):
worker.run()
worker.log.exception.assert_not_called()
assert loop.is_closed()
def test_run_not_app(worker, loop) -> None:
worker.log = mock.Mock()
worker.cfg = mock.Mock()
worker.cfg.access_log_format = ACCEPTABLE_LOG_FORMAT
worker.loop = loop
worker.wsgi = "not-app"
worker.alive = False
with pytest.raises(SystemExit):
worker.run()
worker.log.exception.assert_called_with("Exception in gunicorn worker")
assert loop.is_closed()
def test_handle_quit(worker, loop) -> None:
worker.loop = mock.Mock()
worker.handle_quit(object(), object())
assert not worker.alive
assert worker.exit_code == 0
worker.loop.call_later.asset_called_with(0.1, worker._notify_waiter_done)
def test_handle_abort(worker) -> None:
with mock.patch("aiohttp.worker.sys") as m_sys:
worker.handle_abort(object(), object())
assert not worker.alive
assert worker.exit_code == 1
m_sys.exit.assert_called_with(1)
def test__wait_next_notify(worker) -> None:
worker.loop = mock.Mock()
worker._notify_waiter_done = mock.Mock()
fut = worker._wait_next_notify()
assert worker._notify_waiter == fut
worker.loop.call_later.assert_called_with(1.0, worker._notify_waiter_done, fut)
def test__notify_waiter_done(worker) -> None:
worker._notify_waiter = None
worker._notify_waiter_done()
assert worker._notify_waiter is None
waiter = worker._notify_waiter = mock.Mock()
worker._notify_waiter.done.return_value = False
worker._notify_waiter_done()
assert worker._notify_waiter is None
waiter.set_result.assert_called_with(True)
def test__notify_waiter_done_explicit_waiter(worker) -> None:
worker._notify_waiter = None
assert worker._notify_waiter is None
waiter = worker._notify_waiter = mock.Mock()
waiter.done.return_value = False
waiter2 = worker._notify_waiter = mock.Mock()
worker._notify_waiter_done(waiter)
assert worker._notify_waiter is waiter2
waiter.set_result.assert_called_with(True)
assert not waiter2.set_result.called
def test_init_signals(worker) -> None:
worker.loop = mock.Mock()
worker.init_signals()
assert worker.loop.add_signal_handler.called
@pytest.mark.parametrize(
"source,result",
[
(ACCEPTABLE_LOG_FORMAT, ACCEPTABLE_LOG_FORMAT),
(
AsyncioWorker.DEFAULT_GUNICORN_LOG_FORMAT,
AsyncioWorker.DEFAULT_AIOHTTP_LOG_FORMAT,
),
],
)
def test__get_valid_log_format_ok(worker, source, result) -> None:
assert result == worker._get_valid_log_format(source)
def test__get_valid_log_format_exc(worker) -> None:
with pytest.raises(ValueError) as exc:
worker._get_valid_log_format(WRONG_LOG_FORMAT)
assert "%(name)s" in str(exc.value)
async def test__run_ok_parent_changed(worker, loop, aiohttp_unused_port) -> None:
skip_if_no_dict(loop)
worker.ppid = 0
worker.alive = True
sock = socket.socket()
addr = ("localhost", aiohttp_unused_port())
sock.bind(addr)
worker.sockets = [sock]
worker.log = mock.Mock()
worker.loop = loop
worker.cfg.access_log_format = ACCEPTABLE_LOG_FORMAT
worker.cfg.max_requests = 0
worker.cfg.is_ssl = False
await worker._run()
worker.notify.assert_called_with()
worker.log.info.assert_called_with("Parent changed, shutting down: %s", worker)
async def test__run_exc(worker, loop, aiohttp_unused_port) -> None:
skip_if_no_dict(loop)
worker.ppid = os.getppid()
worker.alive = True
sock = socket.socket()
addr = ("localhost", aiohttp_unused_port())
sock.bind(addr)
worker.sockets = [sock]
worker.log = mock.Mock()
worker.loop = loop
worker.cfg.access_log_format = ACCEPTABLE_LOG_FORMAT
worker.cfg.max_requests = 0
worker.cfg.is_ssl = False
def raiser():
waiter = worker._notify_waiter
worker.alive = False
waiter.set_exception(RuntimeError())
loop.call_later(0.1, raiser)
await worker._run()
worker.notify.assert_called_with()
def test__create_ssl_context_without_certs_and_ciphers(
worker,
tls_certificate_pem_path,
) -> None:
worker.cfg.ssl_version = ssl.PROTOCOL_TLS_CLIENT
worker.cfg.cert_reqs = ssl.CERT_OPTIONAL
worker.cfg.certfile = tls_certificate_pem_path
worker.cfg.keyfile = tls_certificate_pem_path
worker.cfg.ca_certs = None
worker.cfg.ciphers = None
ctx = worker._create_ssl_context(worker.cfg)
assert isinstance(ctx, ssl.SSLContext)
def test__create_ssl_context_with_ciphers(
worker,
tls_certificate_pem_path,
) -> None:
worker.cfg.ssl_version = ssl.PROTOCOL_TLS_CLIENT
worker.cfg.cert_reqs = ssl.CERT_OPTIONAL
worker.cfg.certfile = tls_certificate_pem_path
worker.cfg.keyfile = tls_certificate_pem_path
worker.cfg.ca_certs = None
worker.cfg.ciphers = "3DES PSK"
ctx = worker._create_ssl_context(worker.cfg)
assert isinstance(ctx, ssl.SSLContext)
def test__create_ssl_context_with_ca_certs(
worker,
tls_ca_certificate_pem_path,
tls_certificate_pem_path,
) -> None:
worker.cfg.ssl_version = ssl.PROTOCOL_TLS_CLIENT
worker.cfg.cert_reqs = ssl.CERT_OPTIONAL
worker.cfg.certfile = tls_certificate_pem_path
worker.cfg.keyfile = tls_certificate_pem_path
worker.cfg.ca_certs = tls_ca_certificate_pem_path
worker.cfg.ciphers = None
ctx = worker._create_ssl_context(worker.cfg)
assert isinstance(ctx, ssl.SSLContext)
|