-
Notifications
You must be signed in to change notification settings - Fork 8
/
integration_test.py
362 lines (285 loc) · 12 KB
/
integration_test.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import asyncio
import io
import multiprocessing
import os.path
import time
import urllib
from asyncio import run_coroutine_threadsafe
from concurrent.futures import CancelledError
import ephemeral_port_reserve
import monotonic
import pytest
from bravado import requests_client
from bravado.client import SwaggerClient
from bravado.exception import BravadoTimeoutError
from bravado.exception import HTTPBadRequest
from bravado.exception import HTTPInternalServerError
from bravado.exception import HTTPNotFound
from bravado.requests_client import RequestsClient
from bravado_core.model import Model
from bravado_asyncio import http_client
from bravado_asyncio import thread_loop
from testing.integration_server import INTEGRATION_SERVER_HOST
from testing.integration_server import start_integration_server
shm_request_received = None
def wait_unit_service_starts(url, timeout=10):
start = time.time()
while time.time() < start + timeout:
try:
urllib.request.urlopen(url, timeout=timeout)
except urllib.error.HTTPError: # pragma: no cover
return
except urllib.error.URLError: # pragma: no cover
time.sleep(0.1)
@pytest.fixture(scope="module")
def integration_server():
global shm_request_received
shm_request_received = multiprocessing.Value("i", 0, lock=False)
server_port = ephemeral_port_reserve.reserve()
server_process = multiprocessing.Process(
target=start_integration_server, args=(server_port, shm_request_received)
)
server_process.daemon = True
server_process.start()
server_url = "http://{host}:{port}".format(
host=INTEGRATION_SERVER_HOST, port=server_port
)
wait_unit_service_starts(server_url)
yield server_url
server_process.terminate()
server_process.join(timeout=1)
@pytest.fixture(
scope="module", params=[http_client.AsyncioClient, requests_client.RequestsClient]
)
def swagger_client(integration_server, request):
# Run all integration tests twice, once with our AsyncioClient and once again with the RequestsClient
# to make sure they both behave the same.
# Once this integration suite has become stable (i.e. we're happy with the approach and the test coverage)
# it could move to bravado and test all major HTTP clients (requests, fido, asyncio).
return get_swagger_client(integration_server, request.param())
def get_swagger_client(server_url, http_client_instance):
spec_url = "{}/swagger.yaml".format(server_url)
return SwaggerClient.from_url(
spec_url,
http_client=http_client_instance,
config={"also_return_response": True},
)
def test_get_query_args(swagger_client):
response = swagger_client.user.loginUser(
username="asyncio", password="p%s&wörd?", invalidate_sessions=True
).response(timeout=1)
assert response.result == "success"
# let's make sure we can access the headers through the response object
assert response.metadata.headers["X-Rate-Limit"] == "4711"
assert response.metadata.headers["X-Expires-After"] == "Expiration date"
def test_param_multi(swagger_client):
result = swagger_client.pet.getPetsByIds(petIds=[23, 42]).response(timeout=1).result
assert len(result) == 2
assert result[0]._as_dict() == {
"id": 23,
"name": "Takamoto",
"photoUrls": [],
"category": None,
"status": None,
"tags": None,
}
assert result[1]._as_dict() == {
"id": 42,
"name": "Lili",
"photoUrls": [],
"category": None,
"status": None,
"tags": None,
}
def test_response_headers(swagger_client):
"""Make sure response headers are returned in the same format across HTTP clients. Namely,
make sure names and values are str, and that it's possible to access headers in a
case-insensitive manner."""
metadata = swagger_client.pet.getPetById(petId=42).response(timeout=1).metadata
assert (
metadata.headers["content-type"]
== metadata.headers["Content-Type"]
== "application/json; charset=utf-8"
)
def test_post_form_data(swagger_client):
result = (
swagger_client.pet.updatePetWithForm(
petId=12,
name="Vivi",
status="sold",
userId=42,
photoUrls=(
"http://first.url?param1=value1¶m2=ß%$",
"http://second.url",
),
)
.response(timeout=1)
.result
)
assert result is None
def test_put_json_body(swagger_client):
# the test server would raise a 404 if the data didn't match
result = (
swagger_client.pet.updatePet(
body={
"id": 42,
"category": {"name": "extracute"},
"name": "Lili",
"photoUrls": [],
"status": "sold",
}
)
.response(timeout=1)
.result
)
assert result is None
def test_delete_query_args(swagger_client):
result = swagger_client.pet.deletePet(petId=5).response(timeout=1).result
assert result is None
def test_post_file_upload(swagger_client):
if isinstance(swagger_client.swagger_spec.http_client, RequestsClient):
pytest.xfail(
"Bug in aiohttp (used in the integration server) - see https://github.com/aio-libs/aiohttp/pull/4090"
)
with open(
os.path.join(os.path.dirname(__file__), "../../testing/sample.jpg"), "rb"
) as image:
result = (
swagger_client.pet.uploadFile(petId=42, file=image, userId=12)
.response(timeout=1)
.result
)
assert result is None
def test_post_file_upload_stream_no_name(swagger_client):
if isinstance(swagger_client.swagger_spec.http_client, RequestsClient):
pytest.xfail(
"Bug in aiohttp (used in the integration server) - see https://github.com/aio-libs/aiohttp/pull/4090"
)
with open(
os.path.join(os.path.dirname(__file__), "../../testing/sample.jpg"), "rb"
) as image:
bytes_io = io.BytesIO(image.read()) # BytesIO has no attribute 'name'
result = (
swagger_client.pet.uploadFile(petId=42, file=bytes_io, userId=12)
.response(timeout=1)
.result
)
assert result is None
def test_multiple_requests(swagger_client):
fut1 = swagger_client.store.getInventory()
fut2 = swagger_client.pet.deletePet(petId=5)
assert fut1.response().result == {}
assert fut2.response().result is None
def test_get_msgpack(swagger_client):
response = swagger_client.pet.getPetsByName(petName="lili").response(timeout=1)
assert len(response.result) == 1
assert response.result[0]._as_dict() == {
"id": 42,
"name": "Lili",
"photoUrls": [],
"category": None,
"status": None,
"tags": None,
}
assert response.metadata.headers["Content-Type"] == "application/msgpack"
def test_server_400(swagger_client):
with pytest.raises(HTTPBadRequest):
swagger_client.user.loginUser(username="not", password="correct").response(
timeout=1
)
def test_server_404(swagger_client):
with pytest.raises(HTTPNotFound):
swagger_client.pet.getPetById(petId=5).response(timeout=1)
def test_server_500(swagger_client):
with pytest.raises(HTTPInternalServerError):
swagger_client.pet.deletePet(petId=42).response(timeout=1)
def test_cancellation(integration_server):
swagger_client = get_swagger_client(integration_server, http_client.AsyncioClient())
bravado_future = (
swagger_client.store.getInventory()
) # request takes roughly 1 second to complete
bravado_future.cancel()
with pytest.raises(CancelledError):
bravado_future.result()
bravado_future.cancel() # make sure we can call it again without issues
@pytest.mark.xfail(
reason="Setting the timeout through aiohttp does not seem to have an effect sometimes"
)
def test_timeout_request_options(swagger_client):
other_future = swagger_client.pet.getPetById(petId=42)
with pytest.raises(BravadoTimeoutError):
bravado_future = swagger_client.store.getInventory(
_request_options={"timeout": 0.1}
)
bravado_future.response(timeout=None)
# make sure the exception doesn't disrupt the other request
assert isinstance(other_future.response(timeout=1).result, Model)
def test_timeout_on_future(swagger_client):
other_future = swagger_client.pet.getPetById(petId=42)
with pytest.raises(BravadoTimeoutError):
bravado_future = swagger_client.store.getInventory()
bravado_future.response(timeout=0.1)
# make sure the exception doesn't disrupt the other request
assert isinstance(other_future.response(timeout=1).result, Model)
def test_time_until_request_done(integration_server):
swagger_client = get_swagger_client(integration_server, http_client.AsyncioClient())
start_time = monotonic.monotonic()
bravado_future = swagger_client.ping.ping()
time_waited = 0
while not shm_request_received.value and time_waited < 1:
time.sleep(0.01)
time_waited += 0.01
wait_request_received_time = monotonic.monotonic() - start_time
bravado_future.response(timeout=1)
assert time_waited < 0.2, "Waited {}s for request to be received by server,".format(
wait_request_received_time
) + "shm value is now {}".format(shm_request_received.value)
@pytest.mark.xfail(
reason="Execution time is not always below 2 seconds especially for Python 3.5"
)
def test_client_from_asyncio(integration_server):
"""Let's make sure that the event loop for our HTTP client that runs in a different thread
behaves properly with the 'standard' asyncio loop that people would normally use when doing
asynchronous programming. While we're at it, let's also make sure two instances of
AsyncioClient work well together."""
# recreate the separate event loop and client session for the HTTP client so we start with a clean slate
# this is important since we measure the time this test takes, and the test_timeout() tasks might
# interfere with it
loop = thread_loop.get_thread_loop()
if hasattr(loop, "_bravado_asyncio_client_session"):
run_coroutine_threadsafe(loop._bravado_asyncio_client_session.close(), loop)
del loop._bravado_asyncio_client_session
# not going to properly shut down the running loop, this will be cleaned up on exit
thread_loop.event_loop = None
# get a second event loop running in the current thread; we'll use this one to run the test
loop = asyncio.get_event_loop()
start_time = time.time()
loop.run_until_complete(_test_asyncio_client(integration_server))
end_time = time.time()
# There are three things being executed asynchronously:
# 1. sleep 1 second in the main event loop
# 2. fetch the response for client1 (the server sleeps 1 second)
# 3. fetch the response for client2 (the server sleeps 1 second)
# All of this combined should take only a bit more than one second.
# While this assertion could become flaky depending on how busy the system that runs the test
# is for now it's a nice confirmation that things work as expected. We can remove it later if
# it becomes a problem.
assert end_time - start_time < 2
async def sleep_coroutine():
await asyncio.sleep(1)
return 42
async def _test_asyncio_client(integration_server):
# schedule our first coroutine (after _test_asyncio_client) in the default event loop
future = asyncio.ensure_future(sleep_coroutine())
client1 = get_swagger_client(integration_server, http_client.AsyncioClient())
client2 = get_swagger_client(integration_server, http_client.AsyncioClient(),)
# two tasks for the event loop running in a separate thread
future1 = client1.store.getInventory()
future2 = client2.store.getInventory()
result = await future
assert result == 42
result1 = future1.response(timeout=5).result
assert result1 == {}
result2 = future2.response(timeout=5).result
assert result2 == {}
return True