pre-commit: run black without string normalization

This commit is contained in:
Erik Sundell
2021-11-01 09:42:45 +01:00
parent e75de14839
commit 771ae59636
32 changed files with 691 additions and 587 deletions

View File

@@ -5,13 +5,13 @@ import os
from pytest import fixture
@fixture
def preserve_config(request):
"""Fixture to save and restore config around tests"""
# Import TLJH only when needed. This lets us run tests in places
# where TLJH is not installed - particularly, the 'distro check' test.
from tljh.config import CONFIG_FILE, reload_component
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE) as f:
save_config = f.read()

View File

@@ -17,12 +17,14 @@ def tljh_extra_user_pip_packages():
'django',
]
@hookimpl
def tljh_extra_hub_pip_packages():
return [
'there',
]
@hookimpl
def tljh_extra_apt_packages():
return [
@@ -33,9 +35,8 @@ def tljh_extra_apt_packages():
@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):

View File

@@ -19,6 +19,7 @@ async def test_admin_login():
# If user is not logged in, this will raise an exception
await u.ensure_server_simulate()
@pytest.mark.asyncio
@pytest.mark.parametrize(
"username, password",

View File

@@ -38,6 +38,7 @@ def get_bootstrap_script_location(container_name, show_progress_page):
subprocess.check_call(["docker", "cp", source_path, f"{container_name}:/srv/src"])
return bootstrap_script
# FIXME: Refactor this function to easier to understand using the following
# parameters
#
@@ -55,7 +56,9 @@ def get_bootstrap_script_location(container_name, show_progress_page):
# running against the systemd container that cab be built by
# integration-test.py.
#
def run_bootstrap_after_preparing_container(container_name, image, show_progress_page=False):
def run_bootstrap_after_preparing_container(
container_name, image, show_progress_page=False
):
"""
1. Stops old container
2. Starts --detached container
@@ -163,7 +166,7 @@ def test_progress_page():
run_bootstrap_after_preparing_container,
"progress-page",
f"ubuntu:{os.getenv('UBUNTU_VERSION', '20.04')}",
True
True,
)
# Check if progress page started

View File

@@ -6,39 +6,43 @@ def test_serverextensions():
Validate serverextensions we want are installed
"""
# jupyter-serverextension writes to stdout and stderr weirdly
proc = subprocess.run([
'/opt/tljh/user/bin/jupyter-serverextension',
'list', '--sys-prefix'
], stderr=subprocess.PIPE)
proc = subprocess.run(
['/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'
'jupyter_resource_usage',
]
for e in extensions:
assert e in proc.stderr.decode()
def test_nbextensions():
"""
Validate nbextensions we want are installed & enabled
"""
# jupyter-nbextension writes to stdout and stderr weirdly
proc = subprocess.run([
'/opt/tljh/user/bin/jupyter-nbextension',
'list', '--sys-prefix'
], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
proc = subprocess.run(
['/opt/tljh/user/bin/jupyter-nbextension', 'list', '--sys-prefix'],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
)
extensions = [
'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()
# 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(extensions)
assert proc.stderr.decode() == ' - Validating: \x1b[32mOK\x1b[0m\n' * len(
extensions
)

View File

@@ -33,8 +33,20 @@ async def test_user_code_execute():
hub_url = 'http://localhost'
username = secrets.token_hex(8)
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy')).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
)
).wait()
)
assert (
0
== await (
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
).wait()
)
async with User(username, hub_url, partial(login_dummy, password='')) as u:
await u.login()
@@ -57,17 +69,48 @@ async def test_user_server_started_with_custom_base_url():
hub_url = f"http://localhost{base_url}"
username = secrets.token_hex(8)
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy')).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'base_url', base_url)).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
)
).wait()
)
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'base_url', base_url
)
).wait()
)
assert (
0
== await (
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
).wait()
)
async with User(username, hub_url, partial(login_dummy, password='')) as u:
await u.login()
await u.ensure_server_simulate()
# unset base_url to avoid problems with other tests
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'unset', 'base_url')).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'unset', 'base_url'
)
).wait()
)
assert (
0
== await (
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
).wait()
)
@pytest.mark.asyncio
@@ -80,9 +123,28 @@ async def test_user_admin_add():
hub_url = 'http://localhost'
username = secrets.token_hex(8)
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy')).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'add-item', 'users.admin', username)).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
)
).wait()
)
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'add-item', 'users.admin', username
)
).wait()
)
assert (
0
== await (
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
).wait()
)
async with User(username, hub_url, partial(login_dummy, password='')) as u:
await u.login()
@@ -92,8 +154,7 @@ async def test_user_admin_add():
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
@@ -110,9 +171,28 @@ async def test_user_admin_remove():
hub_url = 'http://localhost'
username = secrets.token_hex(8)
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy')).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'add-item', 'users.admin', username)).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
)
).wait()
)
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'add-item', 'users.admin', username
)
).wait()
)
assert (
0
== await (
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
).wait()
)
async with User(username, hub_url, partial(login_dummy, password='')) as u:
await u.login()
@@ -122,18 +202,28 @@ async def test_user_admin_remove():
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)).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'remove-item', 'users.admin', username
)
).wait()
)
assert (
0
== await (
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
).wait()
)
await u.stop_server()
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
@@ -146,8 +236,20 @@ async def test_long_username():
hub_url = 'http://localhost'
username = secrets.token_hex(32)
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy')).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
)
).wait()
)
assert (
0
== await (
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
).wait()
)
try:
async with User(username, hub_url, partial(login_dummy, password='')) as u:
@@ -161,11 +263,7 @@ async def test_long_username():
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
@@ -182,9 +280,31 @@ async def test_user_group_adding():
# Create the group we want to add the user to
system('groupadd somegroup')
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy')).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'add-item', 'users.extra_user_groups.somegroup', username)).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
)
).wait()
)
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH,
'add-item',
'users.extra_user_groups.somegroup',
username,
)
).wait()
)
assert (
0
== await (
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
).wait()
)
try:
async with User(username, hub_url, partial(login_dummy, password='')) as u:
@@ -203,11 +323,7 @@ async def test_user_group_adding():
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
@@ -222,14 +338,47 @@ async def test_idle_server_culled():
hub_url = 'http://localhost'
username = secrets.token_hex(8)
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy')).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
)
).wait()
)
# Check every 10s for idle servers to cull
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'services.cull.every', "10")).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'services.cull.every', "10"
)
).wait()
)
# Apart from servers, also cull users
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'services.cull.users', "True")).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'services.cull.users', "True"
)
).wait()
)
# Cull servers and users after 60s of activity
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'services.cull.max_age', "60")).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'services.cull.max_age', "60"
)
).wait()
)
assert (
0
== await (
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
).wait()
)
async with User(username, hub_url, partial(login_dummy, password='')) as u:
await u.login()
@@ -239,14 +388,18 @@ async def test_idle_server_culled():
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/')})
r = await u.session.get(
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/')})
r = await u.session.get(
u.hub_url / 'hub/api/users' / username,
headers={'Referer': str(u.hub_url / 'hub/')},
)
print(r.status)
return r.status == 403
@@ -268,14 +421,47 @@ async def test_active_server_not_culled():
hub_url = 'http://localhost'
username = secrets.token_hex(8)
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy')).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummy'
)
).wait()
)
# Check every 10s for idle servers to cull
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'services.cull.every', "10")).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'services.cull.every', "10"
)
).wait()
)
# Apart from servers, also cull users
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'services.cull.users', "True")).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'services.cull.users', "True"
)
).wait()
)
# Cull servers and users after 60s of activity
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'services.cull.max_age', "60")).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')).wait()
assert (
0
== await (
await asyncio.create_subprocess_exec(
*TLJH_CONFIG_PATH, 'set', 'services.cull.max_age', "60"
)
).wait()
)
assert (
0
== await (
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')
).wait()
)
async with User(username, hub_url, partial(login_dummy, password='')) as u:
await u.login()
@@ -285,14 +471,18 @@ async def test_active_server_not_culled():
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/')})
r = await u.session.get(
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/')})
r = await u.session.get(
u.hub_url / 'hub/api/users' / username,
headers={'Referer': str(u.hub_url / 'hub/')},
)
print(r.status)
return r.status != 200

View File

@@ -172,7 +172,16 @@ def test_pip_install(group, allowed):
# get a failure if the user can't install to the global site. Which is
# what we wanted to test for here.
subprocess.check_call(
[python, "-m", "pip", "install", "--no-user", "--ignore-installed", "--no-deps", "flit"],
[
python,
"-m",
"pip",
"install",
"--no-user",
"--ignore-installed",
"--no-deps",
"flit",
],
preexec_fn=partial(setgroup, group),
)
if allowed:

View File

@@ -90,7 +90,6 @@ def test_manual_https(preserve_config):
)
assert resp.code == 200
# cleanup
shutil.rmtree(ssl_dir)
set_config_value(CONFIG_FILE, "https.enabled", False)
@@ -105,7 +104,6 @@ def test_extra_traefik_config():
dynamic_config_dir = os.path.join(STATE_DIR, "rules")
os.makedirs(dynamic_config_dir, exist_ok=True)
extra_static_config = {
"entryPoints": {"no_auth_api": {"address": "127.0.0.1:9999"}},
"api": {"dashboard": True, "entrypoint": "no_auth_api"},
@@ -126,7 +124,6 @@ def test_extra_traefik_config():
},
}
success = False
for i in range(5):
time.sleep(i)

View File

@@ -23,28 +23,16 @@ 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():
@@ -80,7 +68,7 @@ def test_new_user_create():
"""
Test that plugin receives username as arg
"""
username="user1"
username = "user1"
# Call ensure_user to make sure the user plugin gets called
user.ensure_user(username)