mirror of
https://github.com/jupyterhub/the-littlest-jupyterhub.git
synced 2025-12-18 21:54:05 +08:00
pre-commit: run black with string normalization
This commit is contained in:
@@ -15,11 +15,11 @@ from tljh.normalize import generate_system_username
|
||||
|
||||
# Use sudo to invoke it, since this is how users invoke it.
|
||||
# This catches issues with PATH
|
||||
TLJH_CONFIG_PATH = ['sudo', 'tljh-config']
|
||||
TLJH_CONFIG_PATH = ["sudo", "tljh-config"]
|
||||
|
||||
|
||||
def test_hub_up():
|
||||
r = requests.get('http://127.0.0.1')
|
||||
r = requests.get("http://127.0.0.1")
|
||||
r.raise_for_status()
|
||||
|
||||
|
||||
@@ -30,32 +30,32 @@ async def test_user_code_execute():
|
||||
"""
|
||||
# This *must* be localhost, not an IP
|
||||
# aiohttp throws away cookies if we are connecting to an IP!
|
||||
hub_url = 'http://localhost'
|
||||
hub_url = "http://localhost"
|
||||
username = secrets.token_hex(8)
|
||||
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
|
||||
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
||||
).wait()
|
||||
)
|
||||
|
||||
async with User(username, hub_url, partial(login_dummy, password='')) as u:
|
||||
async with User(username, hub_url, partial(login_dummy, password="")) as u:
|
||||
await u.login()
|
||||
await u.ensure_server_simulate()
|
||||
await u.start_kernel()
|
||||
await u.assert_code_output("5 * 4", "20", 5, 5)
|
||||
|
||||
# Assert that the user exists
|
||||
assert pwd.getpwnam(f'jupyter-{username}') is not None
|
||||
assert pwd.getpwnam(f"jupyter-{username}") is not None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -73,7 +73,7 @@ async def test_user_server_started_with_custom_base_url():
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
|
||||
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
@@ -81,18 +81,18 @@ async def test_user_server_started_with_custom_base_url():
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'base_url', base_url
|
||||
*TLJH_CONFIG_PATH, "set", "base_url", base_url
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
||||
).wait()
|
||||
)
|
||||
|
||||
async with User(username, hub_url, partial(login_dummy, password='')) as u:
|
||||
async with User(username, hub_url, partial(login_dummy, password="")) as u:
|
||||
await u.login()
|
||||
await u.ensure_server_simulate()
|
||||
|
||||
@@ -101,14 +101,14 @@ async def test_user_server_started_with_custom_base_url():
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'unset', 'base_url'
|
||||
*TLJH_CONFIG_PATH, "unset", "base_url"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
||||
).wait()
|
||||
)
|
||||
|
||||
@@ -120,14 +120,14 @@ async def test_user_admin_add():
|
||||
"""
|
||||
# This *must* be localhost, not an IP
|
||||
# aiohttp throws away cookies if we are connecting to an IP!
|
||||
hub_url = 'http://localhost'
|
||||
hub_url = "http://localhost"
|
||||
username = secrets.token_hex(8)
|
||||
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
|
||||
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
@@ -135,26 +135,26 @@ async def test_user_admin_add():
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'add-item', 'users.admin', username
|
||||
*TLJH_CONFIG_PATH, "add-item", "users.admin", username
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
||||
).wait()
|
||||
)
|
||||
|
||||
async with User(username, hub_url, partial(login_dummy, password='')) as u:
|
||||
async with User(username, hub_url, partial(login_dummy, password="")) as u:
|
||||
await u.login()
|
||||
await u.ensure_server_simulate()
|
||||
|
||||
# Assert that the user exists
|
||||
assert pwd.getpwnam(f'jupyter-{username}') is not None
|
||||
assert pwd.getpwnam(f"jupyter-{username}") is not None
|
||||
|
||||
# Assert that the user has admin rights
|
||||
assert f'jupyter-{username}' in grp.getgrnam('jupyterhub-admins').gr_mem
|
||||
assert f"jupyter-{username}" in grp.getgrnam("jupyterhub-admins").gr_mem
|
||||
|
||||
|
||||
# FIXME: Make this test pass
|
||||
@@ -168,14 +168,14 @@ async def test_user_admin_remove():
|
||||
"""
|
||||
# This *must* be localhost, not an IP
|
||||
# aiohttp throws away cookies if we are connecting to an IP!
|
||||
hub_url = 'http://localhost'
|
||||
hub_url = "http://localhost"
|
||||
username = secrets.token_hex(8)
|
||||
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
|
||||
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
@@ -183,39 +183,39 @@ async def test_user_admin_remove():
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'add-item', 'users.admin', username
|
||||
*TLJH_CONFIG_PATH, "add-item", "users.admin", username
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
||||
).wait()
|
||||
)
|
||||
|
||||
async with User(username, hub_url, partial(login_dummy, password='')) as u:
|
||||
async with User(username, hub_url, partial(login_dummy, password="")) as u:
|
||||
await u.login()
|
||||
await u.ensure_server_simulate()
|
||||
|
||||
# Assert that the user exists
|
||||
assert pwd.getpwnam(f'jupyter-{username}') is not None
|
||||
assert pwd.getpwnam(f"jupyter-{username}") is not None
|
||||
|
||||
# Assert that the user has admin rights
|
||||
assert f'jupyter-{username}' in grp.getgrnam('jupyterhub-admins').gr_mem
|
||||
assert f"jupyter-{username}" in grp.getgrnam("jupyterhub-admins").gr_mem
|
||||
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'remove-item', 'users.admin', username
|
||||
*TLJH_CONFIG_PATH, "remove-item", "users.admin", username
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
||||
).wait()
|
||||
)
|
||||
|
||||
@@ -223,7 +223,7 @@ async def test_user_admin_remove():
|
||||
await u.ensure_server_simulate()
|
||||
|
||||
# Assert that the user does *not* have admin rights
|
||||
assert f'jupyter-{username}' not in grp.getgrnam('jupyterhub-admins').gr_mem
|
||||
assert f"jupyter-{username}" not in grp.getgrnam("jupyterhub-admins").gr_mem
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -233,37 +233,37 @@ async def test_long_username():
|
||||
"""
|
||||
# This *must* be localhost, not an IP
|
||||
# aiohttp throws away cookies if we are connecting to an IP!
|
||||
hub_url = 'http://localhost'
|
||||
hub_url = "http://localhost"
|
||||
username = secrets.token_hex(32)
|
||||
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
|
||||
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
||||
).wait()
|
||||
)
|
||||
|
||||
try:
|
||||
async with User(username, hub_url, partial(login_dummy, password='')) as u:
|
||||
async with User(username, hub_url, partial(login_dummy, password="")) as u:
|
||||
await u.login()
|
||||
await u.ensure_server_simulate()
|
||||
|
||||
# Assert that the user exists
|
||||
system_username = generate_system_username(f'jupyter-{username}')
|
||||
system_username = generate_system_username(f"jupyter-{username}")
|
||||
assert pwd.getpwnam(system_username) is not None
|
||||
|
||||
await u.stop_server()
|
||||
except:
|
||||
# If we have any errors, print jupyterhub logs before exiting
|
||||
subprocess.check_call(['journalctl', '-u', 'jupyterhub', '--no-pager'])
|
||||
subprocess.check_call(["journalctl", "-u", "jupyterhub", "--no-pager"])
|
||||
raise
|
||||
|
||||
|
||||
@@ -274,17 +274,17 @@ async def test_user_group_adding():
|
||||
"""
|
||||
# This *must* be localhost, not an IP
|
||||
# aiohttp throws away cookies if we are connecting to an IP!
|
||||
hub_url = 'http://localhost'
|
||||
hub_url = "http://localhost"
|
||||
username = secrets.token_hex(8)
|
||||
groups = {"somegroup": [username]}
|
||||
# Create the group we want to add the user to
|
||||
system('groupadd somegroup')
|
||||
system("groupadd somegroup")
|
||||
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
|
||||
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
@@ -293,8 +293,8 @@ async def test_user_group_adding():
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH,
|
||||
'add-item',
|
||||
'users.extra_user_groups.somegroup',
|
||||
"add-item",
|
||||
"users.extra_user_groups.somegroup",
|
||||
username,
|
||||
)
|
||||
).wait()
|
||||
@@ -302,28 +302,28 @@ async def test_user_group_adding():
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
||||
).wait()
|
||||
)
|
||||
|
||||
try:
|
||||
async with User(username, hub_url, partial(login_dummy, password='')) as u:
|
||||
async with User(username, hub_url, partial(login_dummy, password="")) as u:
|
||||
await u.login()
|
||||
await u.ensure_server_simulate()
|
||||
|
||||
# Assert that the user exists
|
||||
system_username = generate_system_username(f'jupyter-{username}')
|
||||
system_username = generate_system_username(f"jupyter-{username}")
|
||||
assert pwd.getpwnam(system_username) is not None
|
||||
|
||||
# Assert that the user was added to the specified group
|
||||
assert f'jupyter-{username}' in grp.getgrnam('somegroup').gr_mem
|
||||
assert f"jupyter-{username}" in grp.getgrnam("somegroup").gr_mem
|
||||
|
||||
await u.stop_server()
|
||||
# Delete the group
|
||||
system('groupdel somegroup')
|
||||
system("groupdel somegroup")
|
||||
except:
|
||||
# If we have any errors, print jupyterhub logs before exiting
|
||||
subprocess.check_call(['journalctl', '-u', 'jupyterhub', '--no-pager'])
|
||||
subprocess.check_call(["journalctl", "-u", "jupyterhub", "--no-pager"])
|
||||
raise
|
||||
|
||||
|
||||
@@ -335,14 +335,14 @@ async def test_idle_server_culled():
|
||||
"""
|
||||
# This *must* be localhost, not an IP
|
||||
# aiohttp throws away cookies if we are connecting to an IP!
|
||||
hub_url = 'http://localhost'
|
||||
hub_url = "http://localhost"
|
||||
username = secrets.token_hex(8)
|
||||
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
|
||||
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
@@ -351,7 +351,7 @@ async def test_idle_server_culled():
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'services.cull.every', "10"
|
||||
*TLJH_CONFIG_PATH, "set", "services.cull.every", "10"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
@@ -360,7 +360,7 @@ async def test_idle_server_culled():
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'services.cull.users', "True"
|
||||
*TLJH_CONFIG_PATH, "set", "services.cull.users", "True"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
@@ -369,36 +369,36 @@ async def test_idle_server_culled():
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'services.cull.max_age', "60"
|
||||
*TLJH_CONFIG_PATH, "set", "services.cull.max_age", "60"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
||||
).wait()
|
||||
)
|
||||
|
||||
async with User(username, hub_url, partial(login_dummy, password='')) as u:
|
||||
async with User(username, hub_url, partial(login_dummy, password="")) as u:
|
||||
await u.login()
|
||||
# Start user's server
|
||||
await u.ensure_server_simulate()
|
||||
# Assert that the user exists
|
||||
assert pwd.getpwnam(f'jupyter-{username}') is not None
|
||||
assert pwd.getpwnam(f"jupyter-{username}") is not None
|
||||
|
||||
# Check that we can get to the user's server
|
||||
r = await u.session.get(
|
||||
u.hub_url / 'hub/api/users' / username,
|
||||
headers={'Referer': str(u.hub_url / 'hub/')},
|
||||
u.hub_url / "hub/api/users" / username,
|
||||
headers={"Referer": str(u.hub_url / "hub/")},
|
||||
)
|
||||
assert r.status == 200
|
||||
|
||||
async def _check_culling_done():
|
||||
# Check that after 60s, the user and server have been culled and are not reacheable anymore
|
||||
r = await u.session.get(
|
||||
u.hub_url / 'hub/api/users' / username,
|
||||
headers={'Referer': str(u.hub_url / 'hub/')},
|
||||
u.hub_url / "hub/api/users" / username,
|
||||
headers={"Referer": str(u.hub_url / "hub/")},
|
||||
)
|
||||
print(r.status)
|
||||
return r.status == 403
|
||||
@@ -418,14 +418,14 @@ async def test_active_server_not_culled():
|
||||
"""
|
||||
# This *must* be localhost, not an IP
|
||||
# aiohttp throws away cookies if we are connecting to an IP!
|
||||
hub_url = 'http://localhost'
|
||||
hub_url = "http://localhost"
|
||||
username = secrets.token_hex(8)
|
||||
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
|
||||
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
@@ -434,7 +434,7 @@ async def test_active_server_not_culled():
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'services.cull.every', "10"
|
||||
*TLJH_CONFIG_PATH, "set", "services.cull.every", "10"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
@@ -443,7 +443,7 @@ async def test_active_server_not_culled():
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'services.cull.users', "True"
|
||||
*TLJH_CONFIG_PATH, "set", "services.cull.users", "True"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
@@ -452,36 +452,36 @@ async def test_active_server_not_culled():
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(
|
||||
*TLJH_CONFIG_PATH, 'set', 'services.cull.max_age', "60"
|
||||
*TLJH_CONFIG_PATH, "set", "services.cull.max_age", "60"
|
||||
)
|
||||
).wait()
|
||||
)
|
||||
assert (
|
||||
0
|
||||
== await (
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
|
||||
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
||||
).wait()
|
||||
)
|
||||
|
||||
async with User(username, hub_url, partial(login_dummy, password='')) as u:
|
||||
async with User(username, hub_url, partial(login_dummy, password="")) as u:
|
||||
await u.login()
|
||||
# Start user's server
|
||||
await u.ensure_server_simulate()
|
||||
# Assert that the user exists
|
||||
assert pwd.getpwnam(f'jupyter-{username}') is not None
|
||||
assert pwd.getpwnam(f"jupyter-{username}") is not None
|
||||
|
||||
# Check that we can get to the user's server
|
||||
r = await u.session.get(
|
||||
u.hub_url / 'hub/api/users' / username,
|
||||
headers={'Referer': str(u.hub_url / 'hub/')},
|
||||
u.hub_url / "hub/api/users" / username,
|
||||
headers={"Referer": str(u.hub_url / "hub/")},
|
||||
)
|
||||
assert r.status == 200
|
||||
|
||||
async def _check_culling_done():
|
||||
# Check that after 30s, we can still reach the user's server
|
||||
r = await u.session.get(
|
||||
u.hub_url / 'hub/api/users' / username,
|
||||
headers={'Referer': str(u.hub_url / 'hub/')},
|
||||
u.hub_url / "hub/api/users" / username,
|
||||
headers={"Referer": str(u.hub_url / "hub/")},
|
||||
)
|
||||
print(r.status)
|
||||
return r.status != 200
|
||||
|
||||
Reference in New Issue
Block a user