From caf66be763fd8f99ccfa57a8e465aaf1b85b5fe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Beraud?= Date: Tue, 16 Jun 2020 14:42:03 +0200 Subject: [PATCH] Fix wsgi SSL tests for wsgi module under python 3 Previously some tests were ignored under python 3 environment, this was due to some design changes introduced by python 3.7 [1] in the SSL module of the stdlib. These changes reactivate some of them (some other are still skipped and needs further works). Indeed, when we try to use requests with SSL in a monkey patched environment we faced the following issue: ``` TypeError: wrap_socket() got an unexpected keyword argument '_context' ``` This is due to the fact that we are in a monkey patched environment where `requests` is monkey patched too. We don't need `request` for our needs. Indeed we can easily send http requests through low level socket. Our main goal is to test our wsgi server and not to test the `requests` library, and `requests` was just used to make the code more simpler. In our case we can implement a code dedicated to send request to our green server, unlock our tests and move away from this bug/side effect. Also this reactivated test will check WSGI server with and without SSL, so these changes add changes that allow us to submit a request without wrapping the socket with SSL. These changes move away from `requests` which is badly monkey patched by eventlet [1]. Now we use monkey patched socket and ssl to request the green server which is executed in background. Low level (monkey patched) modules could help us to skirt layers that are possibly badly monkey patched on higher level modules (urllib, requests, etc...). [1] https://github.com/eventlet/eventlet/issues/526#issuecomment-482694279 [2] https://github.com/eventlet/eventlet/issues/526 Change-Id: I3a018d507d102266c1e2fc9b6732a9c09fa2bb49 Closes-Bug: #1482633 --- oslo_service/tests/test_wsgi.py | 37 +++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/oslo_service/tests/test_wsgi.py b/oslo_service/tests/test_wsgi.py index c0373809..80d3043a 100644 --- a/oslo_service/tests/test_wsgi.py +++ b/oslo_service/tests/test_wsgi.py @@ -265,15 +265,21 @@ class TestWSGIServer(WsgiTestCase): server.stop() -def requesting(host, port, ca_certs, method="POST", +def requesting(host, port, ca_certs=None, method="POST", content_type="application/x-www-form-urlencoded", address_familly=socket.AF_INET): frame = bytes("{verb} / HTTP/1.1\r\n\r\n".format(verb=method), "utf-8") with socket.socket(address_familly, socket.SOCK_STREAM) as sock: - with eventlet.wrap_ssl(sock, ca_certs=ca_certs) as wrappedSocket: - wrappedSocket.connect((host, port)) - wrappedSocket.send(frame) - data = wrappedSocket.recv(1024).decode() + if ca_certs: + with eventlet.wrap_ssl(sock, ca_certs=ca_certs) as wrappedSocket: + wrappedSocket.connect((host, port)) + wrappedSocket.send(frame) + data = wrappedSocket.recv(1024).decode() + return data + else: + sock.connect((host, port)) + sock.send(frame) + data = sock.recv(1024).decode() return data @@ -312,7 +318,6 @@ class TestWSGIServerWithSSL(WsgiTestCase): fake_ssl_server.stop() fake_ssl_server.wait() - @testtools.skip("bug/1482633: test hangs on Python 3") def test_two_servers(self): def test_app(env, start_response): start_response('200 OK', {}) @@ -328,14 +333,20 @@ class TestWSGIServerWithSSL(WsgiTestCase): fake_server.start() self.assertNotEqual(0, fake_server.port) - response = requests.post( - 'https://127.0.0.1:%s/' % fake_ssl_server.port, - verify=os.path.join(SSL_CERT_DIR, 'ca.crt'), data='PING') - self.assertEqual('PONG', response.text) + response = requesting( + method='GET', + host='127.0.0.1', + port=fake_ssl_server.port, + ca_certs=os.path.join(SSL_CERT_DIR, 'ca.crt'), + ) + self.assertEqual('PONG', response[-4:]) - response = requests.post( - 'http://127.0.0.1:%s/' % fake_server.port, data='PING') - self.assertEqual('PONG', response.text) + response = requesting( + method='GET', + host='127.0.0.1', + port=fake_server.port, + ) + self.assertEqual('PONG', response[-4:]) fake_ssl_server.stop() fake_ssl_server.wait()