use gen_test in place of IOLoop.run_sync

even where not strictly required

for consistency, now that we are using gen_test on the main app tests
This commit is contained in:
Min RK
2017-07-27 11:28:03 +02:00
parent 91d042f6f3
commit 9e8b6503a0
6 changed files with 106 additions and 86 deletions

View File

@@ -134,7 +134,7 @@ def test_auth_api(app):
@mark.gen_test @mark.gen_test
def test_referer_check(app, io_loop): def test_referer_check(app):
url = ujoin(public_host(app), app.hub.base_url) url = ujoin(public_host(app), app.hub.base_url)
host = urlparse(url).netloc host = urlparse(url).netloc
user = find_user(app.db, 'admin') user = find_user(app.db, 'admin')
@@ -390,7 +390,7 @@ def test_make_admin(app):
@mark.gen_test @mark.gen_test
def test_spawn(app, io_loop): def test_spawn(app):
db = app.db db = app.db
name = 'wash' name = 'wash'
user = add_user(db, app=app, name=name) user = add_user(db, app=app, name=name)
@@ -445,7 +445,7 @@ def test_spawn(app, io_loop):
@mark.gen_test @mark.gen_test
def test_slow_spawn(app, io_loop, no_patience, request): def test_slow_spawn(app, no_patience, request):
patch = mock.patch.dict(app.tornado_settings, {'spawner_class': mocking.SlowSpawner}) patch = mock.patch.dict(app.tornado_settings, {'spawner_class': mocking.SlowSpawner})
patch.start() patch.start()
request.addfinalizer(patch.stop) request.addfinalizer(patch.stop)
@@ -495,7 +495,7 @@ def test_slow_spawn(app, io_loop, no_patience, request):
@mark.gen_test @mark.gen_test
def test_never_spawn(app, io_loop, no_patience, request): def test_never_spawn(app, no_patience, request):
patch = mock.patch.dict(app.tornado_settings, {'spawner_class': mocking.NeverSpawner}) patch = mock.patch.dict(app.tornado_settings, {'spawner_class': mocking.NeverSpawner})
patch.start() patch.start()
request.addfinalizer(patch.stop) request.addfinalizer(patch.stop)
@@ -520,7 +520,7 @@ def test_never_spawn(app, io_loop, no_patience, request):
@mark.gen_test @mark.gen_test
def test_get_proxy(app, io_loop): def test_get_proxy(app):
r = yield api_request(app, 'proxy') r = yield api_request(app, 'proxy')
r.raise_for_status() r.raise_for_status()
reply = r.json() reply = r.json()

View File

@@ -56,7 +56,9 @@ def test_generate_config():
assert 'Spawner.cmd' in cfg_text assert 'Spawner.cmd' in cfg_text
assert 'Authenticator.whitelist' in cfg_text assert 'Authenticator.whitelist' in cfg_text
def test_init_tokens(io_loop):
@pytest.mark.gen_test
def test_init_tokens():
with TemporaryDirectory() as td: with TemporaryDirectory() as td:
db_file = os.path.join(td, 'jupyterhub.sqlite') db_file = os.path.join(td, 'jupyterhub.sqlite')
tokens = { tokens = {
@@ -65,7 +67,7 @@ def test_init_tokens(io_loop):
'boagasdfasdf': 'chell', 'boagasdfasdf': 'chell',
} }
app = MockHub(db_url=db_file, api_tokens=tokens) app = MockHub(db_url=db_file, api_tokens=tokens)
io_loop.run_sync(lambda : app.initialize([])) yield app.initialize([])
db = app.db db = app.db
for token, username in tokens.items(): for token, username in tokens.items():
api_token = orm.APIToken.find(db, token) api_token = orm.APIToken.find(db, token)
@@ -75,7 +77,7 @@ def test_init_tokens(io_loop):
# simulate second startup, reloading same tokens: # simulate second startup, reloading same tokens:
app = MockHub(db_url=db_file, api_tokens=tokens) app = MockHub(db_url=db_file, api_tokens=tokens)
io_loop.run_sync(lambda : app.initialize([])) yield app.initialize([])
db = app.db db = app.db
for token, username in tokens.items(): for token, username in tokens.items():
api_token = orm.APIToken.find(db, token) api_token = orm.APIToken.find(db, token)
@@ -87,7 +89,7 @@ def test_init_tokens(io_loop):
tokens['short'] = 'gman' tokens['short'] = 'gman'
app = MockHub(db_url=db_file, api_tokens=tokens) app = MockHub(db_url=db_file, api_tokens=tokens)
with pytest.raises(ValueError): with pytest.raises(ValueError):
io_loop.run_sync(lambda : app.initialize([])) yield app.initialize([])
assert orm.User.find(app.db, 'gman') is None assert orm.User.find(app.db, 'gman') is None
@@ -141,15 +143,16 @@ def test_cookie_secret_env(tmpdir):
assert not os.path.exists(hub.cookie_secret_file) assert not os.path.exists(hub.cookie_secret_file)
def test_load_groups(io_loop): @pytest.mark.gen_test
def test_load_groups():
to_load = { to_load = {
'blue': ['cyclops', 'rogue', 'wolverine'], 'blue': ['cyclops', 'rogue', 'wolverine'],
'gold': ['storm', 'jean-grey', 'colossus'], 'gold': ['storm', 'jean-grey', 'colossus'],
} }
hub = MockHub(load_groups=to_load) hub = MockHub(load_groups=to_load)
hub.init_db() hub.init_db()
io_loop.run_sync(hub.init_users) yield hub.init_users()
hub.init_groups() yield hub.init_groups()
db = hub.db db = hub.db
blue = orm.Group.find(db, name='blue') blue = orm.Group.find(db, name='blue')
assert blue is not None assert blue is not None
@@ -158,6 +161,3 @@ def test_load_groups(io_loop):
assert gold is not None assert gold is not None
assert sorted([ u.name for u in gold.users ]) == sorted(to_load['gold']) assert sorted([ u.name for u in gold.users ]) == sorted(to_load['gold'])
def test_version():
if sys.version_info[:2] < (3, 3):
assertRaises(ValueError)

View File

@@ -10,38 +10,42 @@ from .mocking import MockPAMAuthenticator
from jupyterhub import auth, orm from jupyterhub import auth, orm
def test_pam_auth(io_loop):
@pytest.mark.gen_test
def test_pam_auth():
authenticator = MockPAMAuthenticator() authenticator = MockPAMAuthenticator()
authorized = io_loop.run_sync(lambda : authenticator.get_authenticated_user(None, { authorized = yield authenticator.get_authenticated_user(None, {
'username': 'match', 'username': 'match',
'password': 'match', 'password': 'match',
})) })
assert authorized['name'] == 'match' assert authorized['name'] == 'match'
authorized = io_loop.run_sync(lambda : authenticator.get_authenticated_user(None, { authorized = yield authenticator.get_authenticated_user(None, {
'username': 'match', 'username': 'match',
'password': 'nomatch', 'password': 'nomatch',
})) })
assert authorized is None assert authorized is None
def test_pam_auth_whitelist(io_loop):
@pytest.mark.gen_test
def test_pam_auth_whitelist():
authenticator = MockPAMAuthenticator(whitelist={'wash', 'kaylee'}) authenticator = MockPAMAuthenticator(whitelist={'wash', 'kaylee'})
authorized = io_loop.run_sync(lambda : authenticator.get_authenticated_user(None, { authorized = yield authenticator.get_authenticated_user(None, {
'username': 'kaylee', 'username': 'kaylee',
'password': 'kaylee', 'password': 'kaylee',
})) })
assert authorized['name'] == 'kaylee' assert authorized['name'] == 'kaylee'
authorized = io_loop.run_sync(lambda : authenticator.get_authenticated_user(None, { authorized = yield authenticator.get_authenticated_user(None, {
'username': 'wash', 'username': 'wash',
'password': 'nomatch', 'password': 'nomatch',
})) })
assert authorized is None assert authorized is None
authorized = io_loop.run_sync(lambda : authenticator.get_authenticated_user(None, { authorized = yield authenticator.get_authenticated_user(None, {
'username': 'mal', 'username': 'mal',
'password': 'mal', 'password': 'mal',
})) })
assert authorized is None assert authorized is None
@@ -50,7 +54,8 @@ class MockGroup:
self.gr_mem = names self.gr_mem = names
def test_pam_auth_group_whitelist(io_loop): @pytest.mark.gen_test
def test_pam_auth_group_whitelist():
g = MockGroup('kaylee') g = MockGroup('kaylee')
def getgrnam(name): def getgrnam(name):
return g return g
@@ -58,38 +63,41 @@ def test_pam_auth_group_whitelist(io_loop):
authenticator = MockPAMAuthenticator(group_whitelist={'group'}) authenticator = MockPAMAuthenticator(group_whitelist={'group'})
with mock.patch.object(auth, 'getgrnam', getgrnam): with mock.patch.object(auth, 'getgrnam', getgrnam):
authorized = io_loop.run_sync(lambda : authenticator.get_authenticated_user(None, { authorized = yield authenticator.get_authenticated_user(None, {
'username': 'kaylee', 'username': 'kaylee',
'password': 'kaylee', 'password': 'kaylee',
})) })
assert authorized['name'] == 'kaylee' assert authorized['name'] == 'kaylee'
with mock.patch.object(auth, 'getgrnam', getgrnam): with mock.patch.object(auth, 'getgrnam', getgrnam):
authorized = io_loop.run_sync(lambda : authenticator.get_authenticated_user(None, { authorized = yield authenticator.get_authenticated_user(None, {
'username': 'mal', 'username': 'mal',
'password': 'mal', 'password': 'mal',
})) })
assert authorized is None assert authorized is None
def test_pam_auth_no_such_group(io_loop): @pytest.mark.gen_test
def test_pam_auth_no_such_group():
authenticator = MockPAMAuthenticator(group_whitelist={'nosuchcrazygroup'}) authenticator = MockPAMAuthenticator(group_whitelist={'nosuchcrazygroup'})
authorized = io_loop.run_sync(lambda : authenticator.get_authenticated_user(None, { authorized = yield authenticator.get_authenticated_user(None, {
'username': 'kaylee', 'username': 'kaylee',
'password': 'kaylee', 'password': 'kaylee',
})) })
assert authorized is None assert authorized is None
def test_wont_add_system_user(io_loop): @pytest.mark.gen_test
def test_wont_add_system_user():
user = orm.User(name='lioness4321') user = orm.User(name='lioness4321')
authenticator = auth.PAMAuthenticator(whitelist={'mal'}) authenticator = auth.PAMAuthenticator(whitelist={'mal'})
authenticator.create_system_users = False authenticator.create_system_users = False
with pytest.raises(KeyError): with pytest.raises(KeyError):
io_loop.run_sync(lambda : authenticator.add_user(user)) yield authenticator.add_user(user)
def test_cant_add_system_user(io_loop): @pytest.mark.gen_test
def test_cant_add_system_user():
user = orm.User(name='lioness4321') user = orm.User(name='lioness4321')
authenticator = auth.PAMAuthenticator(whitelist={'mal'}) authenticator = auth.PAMAuthenticator(whitelist={'mal'})
authenticator.add_user_cmd = ['jupyterhub-fake-command'] authenticator.add_user_cmd = ['jupyterhub-fake-command']
@@ -111,11 +119,12 @@ def test_cant_add_system_user(io_loop):
with mock.patch.object(auth, 'Popen', DummyPopen): with mock.patch.object(auth, 'Popen', DummyPopen):
with pytest.raises(RuntimeError) as exc: with pytest.raises(RuntimeError) as exc:
io_loop.run_sync(lambda : authenticator.add_user(user)) yield authenticator.add_user(user)
assert str(exc.value) == 'Failed to create system user lioness4321: dummy error' assert str(exc.value) == 'Failed to create system user lioness4321: dummy error'
def test_add_system_user(io_loop): @pytest.mark.gen_test
def test_add_system_user():
user = orm.User(name='lioness4321') user = orm.User(name='lioness4321')
authenticator = auth.PAMAuthenticator(whitelist={'mal'}) authenticator = auth.PAMAuthenticator(whitelist={'mal'})
authenticator.create_system_users = True authenticator.create_system_users = True
@@ -131,11 +140,12 @@ def test_add_system_user(io_loop):
return return
with mock.patch.object(auth, 'Popen', DummyPopen): with mock.patch.object(auth, 'Popen', DummyPopen):
io_loop.run_sync(lambda : authenticator.add_user(user)) yield authenticator.add_user(user)
assert record['cmd'] == ['echo', '/home/lioness4321', 'lioness4321'] assert record['cmd'] == ['echo', '/home/lioness4321', 'lioness4321']
def test_delete_user(io_loop): @pytest.mark.gen_test
def test_delete_user():
user = orm.User(name='zoe') user = orm.User(name='zoe')
a = MockPAMAuthenticator(whitelist={'mal'}) a = MockPAMAuthenticator(whitelist={'mal'})
@@ -160,49 +170,52 @@ def test_handlers(app):
assert handlers[0][0] == '/login' assert handlers[0][0] == '/login'
def test_normalize_names(io_loop): @pytest.mark.gen_test
def test_normalize_names():
a = MockPAMAuthenticator() a = MockPAMAuthenticator()
authorized = io_loop.run_sync(lambda : a.get_authenticated_user(None, { authorized = yield a.get_authenticated_user(None, {
'username': 'ZOE', 'username': 'ZOE',
'password': 'ZOE', 'password': 'ZOE',
})) })
assert authorized['name'] == 'zoe' assert authorized['name'] == 'zoe'
authorized = io_loop.run_sync(lambda: a.get_authenticated_user(None, { authorized = yield a.get_authenticated_user(None, {
'username': 'Glenn', 'username': 'Glenn',
'password': 'Glenn', 'password': 'Glenn',
})) })
assert authorized['name'] == 'glenn' assert authorized['name'] == 'glenn'
authorized = io_loop.run_sync(lambda: a.get_authenticated_user(None, { authorized = yield a.get_authenticated_user(None, {
'username': 'hExi', 'username': 'hExi',
'password': 'hExi', 'password': 'hExi',
})) })
assert authorized['name'] == 'hexi' assert authorized['name'] == 'hexi'
authorized = io_loop.run_sync(lambda: a.get_authenticated_user(None, { authorized = yield a.get_authenticated_user(None, {
'username': 'Test', 'username': 'Test',
'password': 'Test', 'password': 'Test',
})) })
assert authorized['name'] == 'test' assert authorized['name'] == 'test'
def test_username_map(io_loop):
@pytest.mark.gen_test
def test_username_map():
a = MockPAMAuthenticator(username_map={'wash': 'alpha'}) a = MockPAMAuthenticator(username_map={'wash': 'alpha'})
authorized = io_loop.run_sync(lambda : a.get_authenticated_user(None, { authorized = yield a.get_authenticated_user(None, {
'username': 'WASH', 'username': 'WASH',
'password': 'WASH', 'password': 'WASH',
})) })
assert authorized['name'] == 'alpha' assert authorized['name'] == 'alpha'
authorized = io_loop.run_sync(lambda : a.get_authenticated_user(None, { authorized = yield a.get_authenticated_user(None, {
'username': 'Inara', 'username': 'Inara',
'password': 'Inara', 'password': 'Inara',
})) })
assert authorized['name'] == 'inara' assert authorized['name'] == 'inara'
def test_validate_names(io_loop): def test_validate_names():
a = auth.PAMAuthenticator() a = auth.PAMAuthenticator()
assert a.validate_username('willow') assert a.validate_username('willow')
assert a.validate_username('giles') assert a.validate_username('giles')

View File

@@ -3,6 +3,8 @@ import os
import shutil import shutil
from sqlalchemy.exc import OperationalError from sqlalchemy.exc import OperationalError
import pytest
from pytest import raises from pytest import raises
from ..dbutil import upgrade from ..dbutil import upgrade
@@ -24,7 +26,8 @@ def test_upgrade(tmpdir):
print(db_url) print(db_url)
upgrade(db_url) upgrade(db_url)
def test_upgrade_entrypoint(tmpdir, io_loop): @pytest.mark.gen_test
def test_upgrade_entrypoint(tmpdir):
generate_old_db(str(tmpdir)) generate_old_db(str(tmpdir))
tmpdir.chdir() tmpdir.chdir()
tokenapp = NewToken() tokenapp = NewToken()
@@ -36,7 +39,7 @@ def test_upgrade_entrypoint(tmpdir, io_loop):
assert len(sqlite_files) == 1 assert len(sqlite_files) == 1
upgradeapp = UpgradeDB() upgradeapp = UpgradeDB()
io_loop.run_sync(lambda : upgradeapp.initialize([])) yield upgradeapp.initialize([])
upgradeapp.start() upgradeapp.start()
# check that backup was created: # check that backup was created:

View File

@@ -140,7 +140,8 @@ def test_token_find(db):
assert found is None assert found is None
def test_spawn_fails(db, io_loop): @pytest.mark.gen_test
def test_spawn_fails(db):
orm_user = orm.User(name='aeofel') orm_user = orm.User(name='aeofel')
db.add(orm_user) db.add(orm_user)
db.commit() db.commit()
@@ -156,7 +157,7 @@ def test_spawn_fails(db, io_loop):
}) })
with pytest.raises(RuntimeError) as exc: with pytest.raises(RuntimeError) as exc:
io_loop.run_sync(user.spawn) yield user.spawn()
assert user.spawners[''].server is None assert user.spawners[''].server is None
assert not user.running('') assert not user.running('')

View File

@@ -96,7 +96,7 @@ def wait_for_spawner(spawner, timeout=10):
yield wait() yield wait()
@pytest.mark.gen_test(run_sync=False) @pytest.mark.gen_test
def test_single_user_spawner(app, request): def test_single_user_spawner(app, request):
user = next(iter(app.users.values()), None) user = next(iter(app.users.values()), None)
spawner = user.spawner spawner = user.spawner
@@ -112,42 +112,45 @@ def test_single_user_spawner(app, request):
assert status == 0 assert status == 0
def test_stop_spawner_sigint_fails(db, io_loop): @pytest.mark.gen_test
def test_stop_spawner_sigint_fails(db):
spawner = new_spawner(db, cmd=[sys.executable, '-c', _uninterruptible]) spawner = new_spawner(db, cmd=[sys.executable, '-c', _uninterruptible])
io_loop.run_sync(spawner.start) yield spawner.start()
# wait for the process to get to the while True: loop # wait for the process to get to the while True: loop
time.sleep(1) yield gen.sleep(1)
status = io_loop.run_sync(spawner.poll) status = yield spawner.poll()
assert status is None assert status is None
io_loop.run_sync(spawner.stop) yield spawner.stop()
status = io_loop.run_sync(spawner.poll) status = yield spawner.poll()
assert status == -signal.SIGTERM assert status == -signal.SIGTERM
def test_stop_spawner_stop_now(db, io_loop): @pytest.mark.gen_test
def test_stop_spawner_stop_now(db):
spawner = new_spawner(db) spawner = new_spawner(db)
io_loop.run_sync(spawner.start) yield spawner.start()
# wait for the process to get to the while True: loop # wait for the process to get to the while True: loop
time.sleep(1) yield gen.sleep(1)
status = io_loop.run_sync(spawner.poll) status = yield spawner.poll()
assert status is None assert status is None
io_loop.run_sync(lambda : spawner.stop(now=True)) yield spawner.stop(now=True)
status = io_loop.run_sync(spawner.poll) status = yield spawner.poll()
assert status == -signal.SIGTERM assert status == -signal.SIGTERM
def test_spawner_poll(db, io_loop): @pytest.mark.gen_test
def test_spawner_poll(db):
first_spawner = new_spawner(db) first_spawner = new_spawner(db)
user = first_spawner.user user = first_spawner.user
io_loop.run_sync(first_spawner.start) yield first_spawner.start()
proc = first_spawner.proc proc = first_spawner.proc
status = io_loop.run_sync(first_spawner.poll) status = yield first_spawner.poll()
assert status is None assert status is None
if user.state is None: if user.state is None:
user.state = {} user.state = {}
@@ -159,21 +162,21 @@ def test_spawner_poll(db, io_loop):
spawner.start_polling() spawner.start_polling()
# wait for the process to get to the while True: loop # wait for the process to get to the while True: loop
io_loop.run_sync(lambda : gen.sleep(1)) yield gen.sleep(1)
status = io_loop.run_sync(spawner.poll) status = yield spawner.poll()
assert status is None assert status is None
# kill the process # kill the process
proc.terminate() proc.terminate()
for i in range(10): for i in range(10):
if proc.poll() is None: if proc.poll() is None:
time.sleep(1) yield gen.sleep(1)
else: else:
break break
assert proc.poll() is not None assert proc.poll() is not None
io_loop.run_sync(lambda : gen.sleep(2)) yield gen.sleep(2)
status = io_loop.run_sync(spawner.poll) status = yield spawner.poll()
assert status is not None assert status is not None