update tests for pytest-asyncio

- remove gen_test marker
- use async def
- find/replace yield->await approximately one million times
This commit is contained in:
Min RK
2018-12-12 13:05:45 +01:00
parent d5f87fe09f
commit 37cdba370f
16 changed files with 666 additions and 828 deletions

View File

@@ -16,6 +16,7 @@ from .mocking import MockHub
from .test_api import api_request, add_user
from ..utils import wait_for_http_server, url_path_join as ujoin
@pytest.fixture
def disable_check_routes(app):
# disable periodic check_routes while we are testing
@@ -25,9 +26,8 @@ def disable_check_routes(app):
finally:
app.last_activity_callback.start()
@pytest.mark.gen_test
def test_external_proxy(request):
async def test_external_proxy(request):
auth_token = 'secret!'
proxy_ip = '127.0.0.1'
proxy_port = 54321
@@ -71,23 +71,23 @@ def test_external_proxy(request):
def wait_for_proxy():
return wait_for_http_server('http://%s:%i' % (proxy_ip, proxy_port))
yield wait_for_proxy()
await wait_for_proxy()
yield app.initialize([])
yield app.start()
await app.initialize([])
await app.start()
assert app.proxy.proxy_process is None
# test if api service has a root route '/'
routes = yield app.proxy.get_all_routes()
routes = await app.proxy.get_all_routes()
assert list(routes.keys()) == [app.hub.routespec]
# add user to the db and start a single user server
name = 'river'
add_user(app.db, app, name=name)
r = yield api_request(app, 'users', name, 'server', method='post')
r = await api_request(app, 'users', name, 'server', method='post')
r.raise_for_status()
routes = yield app.proxy.get_all_routes()
routes = await app.proxy.get_all_routes()
# sets the desired path result
user_path = ujoin(app.base_url, 'user/river') + '/'
print(app.base_url, user_path)
@@ -101,18 +101,18 @@ def test_external_proxy(request):
proxy.terminate()
proxy.wait(timeout=10)
proxy = Popen(cmd, env=env)
yield wait_for_proxy()
await wait_for_proxy()
routes = yield app.proxy.get_all_routes()
routes = await app.proxy.get_all_routes()
assert list(routes.keys()) == []
# poke the server to update the proxy
r = yield api_request(app, 'proxy', method='post')
r = await api_request(app, 'proxy', method='post')
r.raise_for_status()
# check that the routes are correct
routes = yield app.proxy.get_all_routes()
routes = await app.proxy.get_all_routes()
assert sorted(routes.keys()) == [app.hub.routespec, user_spec]
# teardown the proxy, and start a new one with different auth and port
@@ -131,11 +131,11 @@ def test_external_proxy(request):
if app.subdomain_host:
cmd.append('--host-routing')
proxy = Popen(cmd, env=env)
yield wait_for_proxy()
await wait_for_proxy()
# tell the hub where the new proxy is
new_api_url = 'http://{}:{}'.format(proxy_ip, proxy_port)
r = yield api_request(app, 'proxy', method='patch', data=json.dumps({
r = await api_request(app, 'proxy', method='patch', data=json.dumps({
'api_url': new_api_url,
'auth_token': new_auth_token,
}))
@@ -145,11 +145,10 @@ def test_external_proxy(request):
assert app.proxy.auth_token == new_auth_token
# check that the routes are correct
routes = yield app.proxy.get_all_routes()
routes = await app.proxy.get_all_routes()
assert sorted(routes.keys()) == [app.hub.routespec, user_spec]
@pytest.mark.gen_test
@pytest.mark.parametrize("username", [
'zoe',
'50fia',
@@ -157,27 +156,27 @@ def test_external_proxy(request):
'~TestJH',
'has@',
])
def test_check_routes(app, username, disable_check_routes):
async def test_check_routes(app, username, disable_check_routes):
proxy = app.proxy
test_user = add_user(app.db, app, name=username)
r = yield api_request(app, 'users/%s/server' % username, method='post')
r = await api_request(app, 'users/%s/server' % username, method='post')
r.raise_for_status()
# check a valid route exists for user
routes = yield app.proxy.get_all_routes()
routes = await app.proxy.get_all_routes()
before = sorted(routes)
assert test_user.proxy_spec in before
# check if a route is removed when user deleted
yield app.proxy.check_routes(app.users, app._service_map)
yield proxy.delete_user(test_user)
routes = yield app.proxy.get_all_routes()
await app.proxy.check_routes(app.users, app._service_map)
await proxy.delete_user(test_user)
routes = await app.proxy.get_all_routes()
during = sorted(routes)
assert test_user.proxy_spec not in during
# check if a route exists for user
yield app.proxy.check_routes(app.users, app._service_map)
routes = yield app.proxy.get_all_routes()
await app.proxy.check_routes(app.users, app._service_map)
routes = await app.proxy.get_all_routes()
after = sorted(routes)
assert test_user.proxy_spec in after
@@ -185,7 +184,6 @@ def test_check_routes(app, username, disable_check_routes):
assert before == after
@pytest.mark.gen_test
@pytest.mark.parametrize("routespec", [
'/has%20space/foo/',
'/missing-trailing/slash',
@@ -194,11 +192,11 @@ def test_check_routes(app, username, disable_check_routes):
'host.name/path/',
'other.host/path/no/slash',
])
def test_add_get_delete(app, routespec, disable_check_routes):
async def test_add_get_delete(app, routespec, disable_check_routes):
arg = routespec
if not routespec.endswith('/'):
routespec = routespec + '/'
# host-routes when not host-routing raises an error
# and vice versa
expect_value_error = bool(app.subdomain_host) ^ (not routespec.startswith('/'))
@@ -213,26 +211,25 @@ def test_add_get_delete(app, routespec, disable_check_routes):
proxy = app.proxy
target = 'https://localhost:1234'
with context():
yield proxy.add_route(arg, target, {})
routes = yield proxy.get_all_routes()
await proxy.add_route(arg, target, {})
routes = await proxy.get_all_routes()
if not expect_value_error:
assert routespec in routes.keys()
with context():
route = yield proxy.get_route(arg)
route = await proxy.get_route(arg)
assert route == {
'target': target,
'routespec': routespec,
'data': route.get('data'),
}
with context():
yield proxy.delete_route(arg)
await proxy.delete_route(arg)
with context():
route = yield proxy.get_route(arg)
route = await proxy.get_route(arg)
assert route is None
@pytest.mark.gen_test
@pytest.mark.parametrize("test_data", [None, 'notjson', json.dumps([])])
def test_proxy_patch_bad_request_data(app, test_data):
r = yield api_request(app, 'proxy', method='patch', data=test_data)
async def test_proxy_patch_bad_request_data(app, test_data):
r = await api_request(app, 'proxy', method='patch', data=test_data)
assert r.status_code == 400