pre-commit: run black with string normalization

This commit is contained in:
Erik Sundell
2021-11-03 23:55:34 +01:00
parent 2ba942ba76
commit e0aaa4f995
31 changed files with 700 additions and 692 deletions

View File

@@ -7,49 +7,49 @@ from tljh.hooks import hookimpl
@hookimpl
def tljh_extra_user_conda_packages():
return [
'hypothesis',
"hypothesis",
]
@hookimpl
def tljh_extra_user_pip_packages():
return [
'django',
"django",
]
@hookimpl
def tljh_extra_hub_pip_packages():
return [
'there',
"there",
]
@hookimpl
def tljh_extra_apt_packages():
return [
'sl',
"sl",
]
@hookimpl
def tljh_config_post_install(config):
# Put an arbitrary marker we can test for
config['simplest_plugin'] = {'present': True}
config["simplest_plugin"] = {"present": True}
@hookimpl
def tljh_custom_jupyterhub_config(c):
c.JupyterHub.authenticator_class = 'tmpauthenticator.TmpAuthenticator'
c.JupyterHub.authenticator_class = "tmpauthenticator.TmpAuthenticator"
@hookimpl
def tljh_post_install():
with open('test_post_install', 'w') as f:
f.write('123456789')
with open("test_post_install", "w") as f:
f.write("123456789")
@hookimpl
def tljh_new_user_create(username):
with open('test_new_user_create', 'w') as f:
with open("test_new_user_create", "w") as f:
f.write(username)

View File

@@ -10,7 +10,7 @@ async def test_admin_login():
Test if the admin that was added during install can login with
the password provided.
"""
hub_url = 'http://localhost'
hub_url = "http://localhost"
username = "admin"
password = "admin"
@@ -33,7 +33,7 @@ async def test_unsuccessful_login(username, password):
"""
Ensure nobody but the admin that was added during install can login
"""
hub_url = 'http://localhost'
hub_url = "http://localhost"
async with User(username, hub_url, partial(login_dummy, password="")) as u:
user_logged_in = await u.login()

View File

@@ -7,15 +7,15 @@ def test_serverextensions():
"""
# jupyter-serverextension writes to stdout and stderr weirdly
proc = subprocess.run(
['/opt/tljh/user/bin/jupyter-serverextension', 'list', '--sys-prefix'],
["/opt/tljh/user/bin/jupyter-serverextension", "list", "--sys-prefix"],
stderr=subprocess.PIPE,
)
extensions = [
'jupyterlab 3.',
'nbgitpuller 1.',
'nteract_on_jupyter 2.1.',
'jupyter_resource_usage',
"jupyterlab 3.",
"nbgitpuller 1.",
"nteract_on_jupyter 2.1.",
"jupyter_resource_usage",
]
for e in extensions:
@@ -28,21 +28,21 @@ def test_nbextensions():
"""
# jupyter-nbextension writes to stdout and stderr weirdly
proc = subprocess.run(
['/opt/tljh/user/bin/jupyter-nbextension', 'list', '--sys-prefix'],
["/opt/tljh/user/bin/jupyter-nbextension", "list", "--sys-prefix"],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
)
extensions = [
'jupyter_resource_usage/main',
"jupyter_resource_usage/main",
# This is what ipywidgets nbextension is called
'jupyter-js-widgets/extension',
"jupyter-js-widgets/extension",
]
for e in extensions:
assert f'{e} \x1b[32m enabled \x1b[0m' in proc.stdout.decode()
assert f"{e} \x1b[32m enabled \x1b[0m" in proc.stdout.decode()
# Ensure we have 'OK' messages in our stdout, to make sure everything is importable
assert proc.stderr.decode() == ' - Validating: \x1b[32mOK\x1b[0m\n' * len(
assert proc.stderr.decode() == " - Validating: \x1b[32mOK\x1b[0m\n" * len(
extensions
)

View File

@@ -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

View File

@@ -119,7 +119,7 @@ def test_admin_writable():
def test_installer_log_readable():
# Test that installer.log is owned by root, and not readable by anyone else
file_stat = os.stat('/opt/tljh/installer.log')
file_stat = os.stat("/opt/tljh/installer.log")
assert file_stat.st_uid == 0
assert file_stat.st_mode == 0o100500
@@ -234,4 +234,4 @@ def test_symlinks():
"""
Test we symlink tljh-config to /usr/local/bin
"""
assert os.path.exists('/usr/bin/tljh-config')
assert os.path.exists("/usr/bin/tljh-config")

View File

@@ -9,30 +9,30 @@ import subprocess
from tljh.config import CONFIG_FILE, USER_ENV_PREFIX, HUB_ENV_PREFIX
from tljh import user
yaml = YAML(typ='rt')
yaml = YAML(typ="rt")
def test_apt_packages():
"""
Test extra apt packages are installed
"""
assert os.path.exists('/usr/games/sl')
assert os.path.exists("/usr/games/sl")
def test_pip_packages():
"""
Test extra user & hub pip packages are installed
"""
subprocess.check_call([f'{USER_ENV_PREFIX}/bin/python3', '-c', 'import django'])
subprocess.check_call([f"{USER_ENV_PREFIX}/bin/python3", "-c", "import django"])
subprocess.check_call([f'{HUB_ENV_PREFIX}/bin/python3', '-c', 'import there'])
subprocess.check_call([f"{HUB_ENV_PREFIX}/bin/python3", "-c", "import there"])
def test_conda_packages():
"""
Test extra user conda packages are installed
"""
subprocess.check_call([f'{USER_ENV_PREFIX}/bin/python3', '-c', 'import hypothesis'])
subprocess.check_call([f"{USER_ENV_PREFIX}/bin/python3", "-c", "import hypothesis"])
def test_config_hook():
@@ -42,16 +42,16 @@ def test_config_hook():
with open(CONFIG_FILE) as f:
data = yaml.load(f)
assert data['simplest_plugin']['present']
assert data["simplest_plugin"]["present"]
def test_jupyterhub_config_hook():
"""
Test that tmpauthenticator is enabled by our custom config plugin
"""
resp = requests.get('http://localhost/hub/tmplogin', allow_redirects=False)
resp = requests.get("http://localhost/hub/tmplogin", allow_redirects=False)
assert resp.status_code == 302
assert resp.headers['Location'] == '/hub/spawn'
assert resp.headers["Location"] == "/hub/spawn"
def test_post_install_hook():