mirror of
https://github.com/jupyterhub/jupyterhub.git
synced 2025-10-13 13:03:01 +00:00
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:
@@ -134,7 +134,7 @@ def test_auth_api(app):
|
||||
|
||||
|
||||
@mark.gen_test
|
||||
def test_referer_check(app, io_loop):
|
||||
def test_referer_check(app):
|
||||
url = ujoin(public_host(app), app.hub.base_url)
|
||||
host = urlparse(url).netloc
|
||||
user = find_user(app.db, 'admin')
|
||||
@@ -390,7 +390,7 @@ def test_make_admin(app):
|
||||
|
||||
|
||||
@mark.gen_test
|
||||
def test_spawn(app, io_loop):
|
||||
def test_spawn(app):
|
||||
db = app.db
|
||||
name = 'wash'
|
||||
user = add_user(db, app=app, name=name)
|
||||
@@ -445,7 +445,7 @@ def test_spawn(app, io_loop):
|
||||
|
||||
|
||||
@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.start()
|
||||
request.addfinalizer(patch.stop)
|
||||
@@ -495,7 +495,7 @@ def test_slow_spawn(app, io_loop, no_patience, request):
|
||||
|
||||
|
||||
@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.start()
|
||||
request.addfinalizer(patch.stop)
|
||||
@@ -520,7 +520,7 @@ def test_never_spawn(app, io_loop, no_patience, request):
|
||||
|
||||
|
||||
@mark.gen_test
|
||||
def test_get_proxy(app, io_loop):
|
||||
def test_get_proxy(app):
|
||||
r = yield api_request(app, 'proxy')
|
||||
r.raise_for_status()
|
||||
reply = r.json()
|
||||
|
@@ -56,7 +56,9 @@ def test_generate_config():
|
||||
assert 'Spawner.cmd' 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:
|
||||
db_file = os.path.join(td, 'jupyterhub.sqlite')
|
||||
tokens = {
|
||||
@@ -65,7 +67,7 @@ def test_init_tokens(io_loop):
|
||||
'boagasdfasdf': 'chell',
|
||||
}
|
||||
app = MockHub(db_url=db_file, api_tokens=tokens)
|
||||
io_loop.run_sync(lambda : app.initialize([]))
|
||||
yield app.initialize([])
|
||||
db = app.db
|
||||
for token, username in tokens.items():
|
||||
api_token = orm.APIToken.find(db, token)
|
||||
@@ -75,7 +77,7 @@ def test_init_tokens(io_loop):
|
||||
|
||||
# simulate second startup, reloading same tokens:
|
||||
app = MockHub(db_url=db_file, api_tokens=tokens)
|
||||
io_loop.run_sync(lambda : app.initialize([]))
|
||||
yield app.initialize([])
|
||||
db = app.db
|
||||
for token, username in tokens.items():
|
||||
api_token = orm.APIToken.find(db, token)
|
||||
@@ -87,7 +89,7 @@ def test_init_tokens(io_loop):
|
||||
tokens['short'] = 'gman'
|
||||
app = MockHub(db_url=db_file, api_tokens=tokens)
|
||||
with pytest.raises(ValueError):
|
||||
io_loop.run_sync(lambda : app.initialize([]))
|
||||
yield app.initialize([])
|
||||
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)
|
||||
|
||||
|
||||
def test_load_groups(io_loop):
|
||||
@pytest.mark.gen_test
|
||||
def test_load_groups():
|
||||
to_load = {
|
||||
'blue': ['cyclops', 'rogue', 'wolverine'],
|
||||
'gold': ['storm', 'jean-grey', 'colossus'],
|
||||
}
|
||||
hub = MockHub(load_groups=to_load)
|
||||
hub.init_db()
|
||||
io_loop.run_sync(hub.init_users)
|
||||
hub.init_groups()
|
||||
yield hub.init_users()
|
||||
yield hub.init_groups()
|
||||
db = hub.db
|
||||
blue = orm.Group.find(db, name='blue')
|
||||
assert blue is not None
|
||||
@@ -158,6 +161,3 @@ def test_load_groups(io_loop):
|
||||
assert gold is not None
|
||||
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)
|
||||
|
@@ -10,38 +10,42 @@ from .mocking import MockPAMAuthenticator
|
||||
|
||||
from jupyterhub import auth, orm
|
||||
|
||||
def test_pam_auth(io_loop):
|
||||
|
||||
@pytest.mark.gen_test
|
||||
def test_pam_auth():
|
||||
authenticator = MockPAMAuthenticator()
|
||||
authorized = io_loop.run_sync(lambda : authenticator.get_authenticated_user(None, {
|
||||
authorized = yield authenticator.get_authenticated_user(None, {
|
||||
'username': 'match',
|
||||
'password': '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',
|
||||
'password': 'nomatch',
|
||||
}))
|
||||
})
|
||||
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'})
|
||||
authorized = io_loop.run_sync(lambda : authenticator.get_authenticated_user(None, {
|
||||
authorized = yield authenticator.get_authenticated_user(None, {
|
||||
'username': 'kaylee',
|
||||
'password': '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',
|
||||
'password': 'nomatch',
|
||||
}))
|
||||
})
|
||||
assert authorized is None
|
||||
|
||||
authorized = io_loop.run_sync(lambda : authenticator.get_authenticated_user(None, {
|
||||
authorized = yield authenticator.get_authenticated_user(None, {
|
||||
'username': 'mal',
|
||||
'password': 'mal',
|
||||
}))
|
||||
})
|
||||
assert authorized is None
|
||||
|
||||
|
||||
@@ -50,7 +54,8 @@ class MockGroup:
|
||||
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')
|
||||
def getgrnam(name):
|
||||
return g
|
||||
@@ -58,38 +63,41 @@ def test_pam_auth_group_whitelist(io_loop):
|
||||
authenticator = MockPAMAuthenticator(group_whitelist={'group'})
|
||||
|
||||
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',
|
||||
'password': 'kaylee',
|
||||
}))
|
||||
})
|
||||
assert authorized['name'] == 'kaylee'
|
||||
|
||||
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',
|
||||
'password': 'mal',
|
||||
}))
|
||||
})
|
||||
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'})
|
||||
authorized = io_loop.run_sync(lambda : authenticator.get_authenticated_user(None, {
|
||||
authorized = yield authenticator.get_authenticated_user(None, {
|
||||
'username': 'kaylee',
|
||||
'password': 'kaylee',
|
||||
}))
|
||||
})
|
||||
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')
|
||||
authenticator = auth.PAMAuthenticator(whitelist={'mal'})
|
||||
authenticator.create_system_users = False
|
||||
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')
|
||||
authenticator = auth.PAMAuthenticator(whitelist={'mal'})
|
||||
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 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'
|
||||
|
||||
|
||||
def test_add_system_user(io_loop):
|
||||
@pytest.mark.gen_test
|
||||
def test_add_system_user():
|
||||
user = orm.User(name='lioness4321')
|
||||
authenticator = auth.PAMAuthenticator(whitelist={'mal'})
|
||||
authenticator.create_system_users = True
|
||||
@@ -131,11 +140,12 @@ def test_add_system_user(io_loop):
|
||||
return
|
||||
|
||||
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']
|
||||
|
||||
|
||||
def test_delete_user(io_loop):
|
||||
@pytest.mark.gen_test
|
||||
def test_delete_user():
|
||||
user = orm.User(name='zoe')
|
||||
a = MockPAMAuthenticator(whitelist={'mal'})
|
||||
|
||||
@@ -160,49 +170,52 @@ def test_handlers(app):
|
||||
assert handlers[0][0] == '/login'
|
||||
|
||||
|
||||
def test_normalize_names(io_loop):
|
||||
@pytest.mark.gen_test
|
||||
def test_normalize_names():
|
||||
a = MockPAMAuthenticator()
|
||||
authorized = io_loop.run_sync(lambda : a.get_authenticated_user(None, {
|
||||
authorized = yield a.get_authenticated_user(None, {
|
||||
'username': 'ZOE',
|
||||
'password': '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',
|
||||
'password': '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',
|
||||
'password': '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',
|
||||
'password': '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'})
|
||||
authorized = io_loop.run_sync(lambda : a.get_authenticated_user(None, {
|
||||
authorized = yield a.get_authenticated_user(None, {
|
||||
'username': 'WASH',
|
||||
'password': 'WASH',
|
||||
}))
|
||||
})
|
||||
|
||||
assert authorized['name'] == 'alpha'
|
||||
|
||||
authorized = io_loop.run_sync(lambda : a.get_authenticated_user(None, {
|
||||
authorized = yield a.get_authenticated_user(None, {
|
||||
'username': 'Inara',
|
||||
'password': 'Inara',
|
||||
}))
|
||||
})
|
||||
assert authorized['name'] == 'inara'
|
||||
|
||||
|
||||
def test_validate_names(io_loop):
|
||||
def test_validate_names():
|
||||
a = auth.PAMAuthenticator()
|
||||
assert a.validate_username('willow')
|
||||
assert a.validate_username('giles')
|
||||
|
@@ -3,6 +3,8 @@ import os
|
||||
import shutil
|
||||
|
||||
from sqlalchemy.exc import OperationalError
|
||||
|
||||
import pytest
|
||||
from pytest import raises
|
||||
|
||||
from ..dbutil import upgrade
|
||||
@@ -24,7 +26,8 @@ def test_upgrade(tmpdir):
|
||||
print(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))
|
||||
tmpdir.chdir()
|
||||
tokenapp = NewToken()
|
||||
@@ -36,7 +39,7 @@ def test_upgrade_entrypoint(tmpdir, io_loop):
|
||||
assert len(sqlite_files) == 1
|
||||
|
||||
upgradeapp = UpgradeDB()
|
||||
io_loop.run_sync(lambda : upgradeapp.initialize([]))
|
||||
yield upgradeapp.initialize([])
|
||||
upgradeapp.start()
|
||||
|
||||
# check that backup was created:
|
||||
|
@@ -140,7 +140,8 @@ def test_token_find(db):
|
||||
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')
|
||||
db.add(orm_user)
|
||||
db.commit()
|
||||
@@ -156,7 +157,7 @@ def test_spawn_fails(db, io_loop):
|
||||
})
|
||||
|
||||
with pytest.raises(RuntimeError) as exc:
|
||||
io_loop.run_sync(user.spawn)
|
||||
yield user.spawn()
|
||||
assert user.spawners[''].server is None
|
||||
assert not user.running('')
|
||||
|
||||
|
@@ -96,7 +96,7 @@ def wait_for_spawner(spawner, timeout=10):
|
||||
yield wait()
|
||||
|
||||
|
||||
@pytest.mark.gen_test(run_sync=False)
|
||||
@pytest.mark.gen_test
|
||||
def test_single_user_spawner(app, request):
|
||||
user = next(iter(app.users.values()), None)
|
||||
spawner = user.spawner
|
||||
@@ -112,42 +112,45 @@ def test_single_user_spawner(app, request):
|
||||
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])
|
||||
io_loop.run_sync(spawner.start)
|
||||
yield spawner.start()
|
||||
|
||||
# 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
|
||||
|
||||
io_loop.run_sync(spawner.stop)
|
||||
status = io_loop.run_sync(spawner.poll)
|
||||
yield spawner.stop()
|
||||
status = yield spawner.poll()
|
||||
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)
|
||||
io_loop.run_sync(spawner.start)
|
||||
yield spawner.start()
|
||||
|
||||
# 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
|
||||
|
||||
io_loop.run_sync(lambda : spawner.stop(now=True))
|
||||
status = io_loop.run_sync(spawner.poll)
|
||||
yield spawner.stop(now=True)
|
||||
status = yield spawner.poll()
|
||||
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)
|
||||
user = first_spawner.user
|
||||
io_loop.run_sync(first_spawner.start)
|
||||
yield first_spawner.start()
|
||||
proc = first_spawner.proc
|
||||
status = io_loop.run_sync(first_spawner.poll)
|
||||
status = yield first_spawner.poll()
|
||||
assert status is None
|
||||
if user.state is None:
|
||||
user.state = {}
|
||||
@@ -159,21 +162,21 @@ def test_spawner_poll(db, io_loop):
|
||||
spawner.start_polling()
|
||||
|
||||
# wait for the process to get to the while True: loop
|
||||
io_loop.run_sync(lambda : gen.sleep(1))
|
||||
status = io_loop.run_sync(spawner.poll)
|
||||
yield gen.sleep(1)
|
||||
status = yield spawner.poll()
|
||||
assert status is None
|
||||
|
||||
# kill the process
|
||||
proc.terminate()
|
||||
for i in range(10):
|
||||
if proc.poll() is None:
|
||||
time.sleep(1)
|
||||
yield gen.sleep(1)
|
||||
else:
|
||||
break
|
||||
assert proc.poll() is not None
|
||||
|
||||
io_loop.run_sync(lambda : gen.sleep(2))
|
||||
status = io_loop.run_sync(spawner.poll)
|
||||
yield gen.sleep(2)
|
||||
status = yield spawner.poll()
|
||||
assert status is not None
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user