2018-07-28 11:05:29 -07:00
|
|
|
import asyncio
|
|
|
|
|
import grp
|
2023-05-15 08:51:35 +00:00
|
|
|
import pwd
|
|
|
|
|
import secrets
|
2018-09-14 11:55:01 -07:00
|
|
|
import subprocess
|
2023-05-15 08:51:35 +00:00
|
|
|
from functools import partial
|
2019-06-20 21:54:51 +03:00
|
|
|
from os import system
|
2023-05-15 08:51:35 +00:00
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
import requests
|
|
|
|
|
from hubtraf.auth.dummy import login_dummy
|
|
|
|
|
from hubtraf.user import User
|
|
|
|
|
from jupyterhub.utils import exponential_backoff
|
2023-05-11 10:47:49 +02:00
|
|
|
from packaging.version import Version as V
|
2018-07-16 17:52:44 -07:00
|
|
|
|
2023-05-15 08:51:35 +00:00
|
|
|
from tljh.normalize import generate_system_username
|
|
|
|
|
|
2018-07-31 13:31:12 -07:00
|
|
|
# Use sudo to invoke it, since this is how users invoke it.
|
|
|
|
|
# This catches issues with PATH
|
2021-11-03 23:55:34 +01:00
|
|
|
TLJH_CONFIG_PATH = ["sudo", "tljh-config"]
|
2018-07-31 13:31:12 -07:00
|
|
|
|
2023-05-11 10:47:49 +02:00
|
|
|
# This *must* be localhost, not an IP
|
|
|
|
|
# aiohttp throws away cookies if we are connecting to an IP!
|
2023-06-09 02:00:30 +02:00
|
|
|
HUB_URL = "http://localhost"
|
2023-05-11 10:47:49 +02:00
|
|
|
|
2020-10-26 21:22:11 +01:00
|
|
|
|
2018-07-16 17:52:44 -07:00
|
|
|
def test_hub_up():
|
2023-06-09 02:00:30 +02:00
|
|
|
r = requests.get(HUB_URL)
|
2023-05-11 10:47:49 +02:00
|
|
|
r.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_hub_version():
|
2023-06-09 02:00:30 +02:00
|
|
|
r = requests.get(HUB_URL + "/hub/api")
|
2018-07-16 17:52:44 -07:00
|
|
|
r.raise_for_status()
|
2023-05-11 10:47:49 +02:00
|
|
|
info = r.json()
|
2024-08-23 09:52:11 +02:00
|
|
|
assert V("5.1") <= V(info["version"]) <= V("6")
|
2018-07-28 00:52:02 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_user_code_execute():
|
|
|
|
|
"""
|
|
|
|
|
User logs in, starts a server & executes code
|
|
|
|
|
"""
|
|
|
|
|
username = secrets.token_hex(8)
|
|
|
|
|
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2021-11-03 23:55:34 +01:00
|
|
|
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
2021-11-03 23:55:34 +01:00
|
|
|
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
2021-11-01 09:42:45 +01:00
|
|
|
).wait()
|
|
|
|
|
)
|
2018-08-11 09:52:34 -07:00
|
|
|
|
2023-06-09 02:00:30 +02:00
|
|
|
async with User(username, HUB_URL, partial(login_dummy, password="")) as u:
|
|
|
|
|
assert await u.login()
|
2024-04-03 22:17:23 +02:00
|
|
|
assert await u.ensure_server_simulate(timeout=60, spawn_refresh_time=5)
|
|
|
|
|
assert await u.start_kernel()
|
|
|
|
|
assert await u.assert_code_output("5 * 4", "20", 5, 5)
|
2018-07-28 00:52:02 -07:00
|
|
|
|
2020-10-26 21:22:11 +01:00
|
|
|
|
2020-10-26 22:17:37 +01:00
|
|
|
async def test_user_server_started_with_custom_base_url():
|
2020-10-26 21:22:11 +01:00
|
|
|
"""
|
|
|
|
|
User logs in, starts a server with a custom base_url & executes code
|
|
|
|
|
"""
|
|
|
|
|
# This *must* be localhost, not an IP
|
|
|
|
|
# aiohttp throws away cookies if we are connecting to an IP!
|
2020-10-26 22:16:15 +01:00
|
|
|
base_url = "/custom-base"
|
2023-06-09 02:00:30 +02:00
|
|
|
custom_hub_url = f"{HUB_URL}{base_url}"
|
2020-10-26 21:22:11 +01:00
|
|
|
username = secrets.token_hex(8)
|
|
|
|
|
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2021-11-03 23:55:34 +01:00
|
|
|
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2021-11-03 23:55:34 +01:00
|
|
|
*TLJH_CONFIG_PATH, "set", "base_url", base_url
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
2021-11-03 23:55:34 +01:00
|
|
|
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
2021-11-01 09:42:45 +01:00
|
|
|
).wait()
|
|
|
|
|
)
|
2020-10-26 21:22:11 +01:00
|
|
|
|
2023-06-09 02:00:30 +02:00
|
|
|
async with User(username, custom_hub_url, partial(login_dummy, password="")) as u:
|
|
|
|
|
assert await u.login()
|
2023-06-07 01:35:34 +02:00
|
|
|
await u.ensure_server_simulate(timeout=60, spawn_refresh_time=5)
|
2020-10-26 21:22:11 +01:00
|
|
|
|
|
|
|
|
# unset base_url to avoid problems with other tests
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2021-11-03 23:55:34 +01:00
|
|
|
*TLJH_CONFIG_PATH, "unset", "base_url"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
2021-11-03 23:55:34 +01:00
|
|
|
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
2021-11-01 09:42:45 +01:00
|
|
|
).wait()
|
|
|
|
|
)
|
2018-07-28 11:05:29 -07:00
|
|
|
|
|
|
|
|
|
2018-07-28 11:57:11 -07:00
|
|
|
async def test_user_admin_add():
|
2018-07-28 11:05:29 -07:00
|
|
|
"""
|
2018-07-28 11:57:11 -07:00
|
|
|
User is made an admin, logs in and we check if they are in admin group
|
2018-07-28 11:05:29 -07:00
|
|
|
"""
|
|
|
|
|
# This *must* be localhost, not an IP
|
|
|
|
|
# aiohttp throws away cookies if we are connecting to an IP!
|
|
|
|
|
username = secrets.token_hex(8)
|
|
|
|
|
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2021-11-03 23:55:34 +01:00
|
|
|
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2021-11-03 23:55:34 +01:00
|
|
|
*TLJH_CONFIG_PATH, "add-item", "users.admin", username
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
2021-11-03 23:55:34 +01:00
|
|
|
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
2021-11-01 09:42:45 +01:00
|
|
|
).wait()
|
|
|
|
|
)
|
2018-07-28 11:05:29 -07:00
|
|
|
|
2023-06-09 02:00:30 +02:00
|
|
|
async with User(username, HUB_URL, partial(login_dummy, password="")) as u:
|
|
|
|
|
assert await u.login()
|
2023-06-07 01:35:34 +02:00
|
|
|
await u.ensure_server_simulate(timeout=60, spawn_refresh_time=5)
|
2018-07-28 11:05:29 -07:00
|
|
|
|
2020-10-26 21:22:11 +01:00
|
|
|
# Assert that the user exists
|
2021-11-03 23:55:34 +01:00
|
|
|
assert pwd.getpwnam(f"jupyter-{username}") is not None
|
2018-07-28 11:05:29 -07:00
|
|
|
|
2020-10-26 21:22:11 +01:00
|
|
|
# Assert that the user has admin rights
|
2021-11-03 23:55:34 +01:00
|
|
|
assert f"jupyter-{username}" in grp.getgrnam("jupyterhub-admins").gr_mem
|
2018-09-12 16:52:48 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_long_username():
|
|
|
|
|
"""
|
|
|
|
|
User with a long name logs in, and we check if their name is properly truncated.
|
|
|
|
|
"""
|
|
|
|
|
username = secrets.token_hex(32)
|
|
|
|
|
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2021-11-03 23:55:34 +01:00
|
|
|
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
2021-11-03 23:55:34 +01:00
|
|
|
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
2021-11-01 09:42:45 +01:00
|
|
|
).wait()
|
|
|
|
|
)
|
2018-09-12 16:52:48 -07:00
|
|
|
|
2018-09-14 10:48:35 -07:00
|
|
|
try:
|
2023-06-09 02:00:30 +02:00
|
|
|
async with User(username, HUB_URL, partial(login_dummy, password="")) as u:
|
|
|
|
|
assert await u.login()
|
2023-06-07 01:35:34 +02:00
|
|
|
await u.ensure_server_simulate(timeout=60, spawn_refresh_time=5)
|
2018-09-14 10:48:35 -07:00
|
|
|
|
2020-10-26 21:22:11 +01:00
|
|
|
# Assert that the user exists
|
2021-11-03 23:55:34 +01:00
|
|
|
system_username = generate_system_username(f"jupyter-{username}")
|
2020-10-26 21:22:11 +01:00
|
|
|
assert pwd.getpwnam(system_username) is not None
|
2018-09-14 10:48:35 -07:00
|
|
|
|
2020-10-26 21:22:11 +01:00
|
|
|
await u.stop_server()
|
2018-09-14 10:48:35 -07:00
|
|
|
except:
|
|
|
|
|
# If we have any errors, print jupyterhub logs before exiting
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.check_call(["journalctl", "-u", "jupyterhub", "--no-pager"])
|
2019-06-07 12:03:17 +03:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
2019-06-20 21:54:51 +03:00
|
|
|
async def test_user_group_adding():
|
|
|
|
|
"""
|
|
|
|
|
User logs in, and we check if they are added to the specified group.
|
|
|
|
|
"""
|
|
|
|
|
# This *must* be localhost, not an IP
|
|
|
|
|
# aiohttp throws away cookies if we are connecting to an IP!
|
|
|
|
|
username = secrets.token_hex(8)
|
|
|
|
|
groups = {"somegroup": [username]}
|
|
|
|
|
# Create the group we want to add the user to
|
2021-11-03 23:55:34 +01:00
|
|
|
system("groupadd somegroup")
|
2019-06-20 21:54:51 +03:00
|
|
|
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2021-11-03 23:55:34 +01:00
|
|
|
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
|
|
|
|
*TLJH_CONFIG_PATH,
|
2021-11-03 23:55:34 +01:00
|
|
|
"add-item",
|
|
|
|
|
"users.extra_user_groups.somegroup",
|
2021-11-01 09:42:45 +01:00
|
|
|
username,
|
|
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
2021-11-03 23:55:34 +01:00
|
|
|
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
2021-11-01 09:42:45 +01:00
|
|
|
).wait()
|
|
|
|
|
)
|
2019-06-20 21:54:51 +03:00
|
|
|
|
|
|
|
|
try:
|
2023-06-09 02:00:30 +02:00
|
|
|
async with User(username, HUB_URL, partial(login_dummy, password="")) as u:
|
|
|
|
|
assert await u.login()
|
2023-06-07 01:35:34 +02:00
|
|
|
await u.ensure_server_simulate(timeout=60, spawn_refresh_time=5)
|
2019-06-20 21:54:51 +03:00
|
|
|
|
2020-10-26 21:22:11 +01:00
|
|
|
# Assert that the user exists
|
2021-11-03 23:55:34 +01:00
|
|
|
system_username = generate_system_username(f"jupyter-{username}")
|
2020-10-26 21:22:11 +01:00
|
|
|
assert pwd.getpwnam(system_username) is not None
|
2019-06-20 21:54:51 +03:00
|
|
|
|
2020-10-26 21:22:11 +01:00
|
|
|
# Assert that the user was added to the specified group
|
2021-11-03 23:55:34 +01:00
|
|
|
assert f"jupyter-{username}" in grp.getgrnam("somegroup").gr_mem
|
2019-06-20 21:54:51 +03:00
|
|
|
|
2020-10-26 21:22:11 +01:00
|
|
|
await u.stop_server()
|
|
|
|
|
# Delete the group
|
2021-11-03 23:55:34 +01:00
|
|
|
system("groupdel somegroup")
|
2019-06-20 21:54:51 +03:00
|
|
|
except:
|
|
|
|
|
# If we have any errors, print jupyterhub logs before exiting
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.check_call(["journalctl", "-u", "jupyterhub", "--no-pager"])
|
2019-06-20 21:54:51 +03:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
2019-06-12 17:03:39 +03:00
|
|
|
async def test_idle_server_culled():
|
2019-06-07 12:03:17 +03:00
|
|
|
"""
|
2023-06-09 01:48:00 +02:00
|
|
|
User logs in, starts a server & stays idle for a while.
|
2019-06-12 17:03:39 +03:00
|
|
|
(the user's server should be culled during this period)
|
2019-06-07 12:03:17 +03:00
|
|
|
"""
|
|
|
|
|
username = secrets.token_hex(8)
|
|
|
|
|
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2021-11-03 23:55:34 +01:00
|
|
|
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
2023-03-28 15:06:04 +02:00
|
|
|
# Check every 5s for idle servers to cull
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2023-03-28 15:06:04 +02:00
|
|
|
*TLJH_CONFIG_PATH, "set", "services.cull.every", "5"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
2019-06-07 12:03:17 +03:00
|
|
|
# Apart from servers, also cull users
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2021-11-03 23:55:34 +01:00
|
|
|
*TLJH_CONFIG_PATH, "set", "services.cull.users", "True"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
2023-06-09 01:48:00 +02:00
|
|
|
# Cull servers and users after a while, regardless of activity
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2023-06-09 01:48:00 +02:00
|
|
|
*TLJH_CONFIG_PATH, "set", "services.cull.max_age", "15"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
2021-11-03 23:55:34 +01:00
|
|
|
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
2021-11-01 09:42:45 +01:00
|
|
|
).wait()
|
|
|
|
|
)
|
2019-06-07 12:03:17 +03:00
|
|
|
|
2023-06-09 02:00:30 +02:00
|
|
|
async with User(username, HUB_URL, partial(login_dummy, password="")) as u:
|
2023-05-11 23:13:59 +02:00
|
|
|
# Login the user
|
2023-06-09 02:00:30 +02:00
|
|
|
assert await u.login()
|
2023-05-11 23:13:59 +02:00
|
|
|
|
2020-10-26 21:22:11 +01:00
|
|
|
# Start user's server
|
2023-06-07 01:35:34 +02:00
|
|
|
await u.ensure_server_simulate(timeout=60, spawn_refresh_time=5)
|
2020-10-26 21:22:11 +01:00
|
|
|
# Assert that the user exists
|
2021-11-03 23:55:34 +01:00
|
|
|
assert pwd.getpwnam(f"jupyter-{username}") is not None
|
2020-10-26 21:22:11 +01:00
|
|
|
|
|
|
|
|
# Check that we can get to the user's server
|
2023-03-28 15:06:04 +02:00
|
|
|
user_url = u.notebook_url / "api/status"
|
|
|
|
|
r = await u.session.get(user_url, allow_redirects=False)
|
2020-10-26 21:22:11 +01:00
|
|
|
assert r.status == 200
|
|
|
|
|
|
2023-05-11 23:13:59 +02:00
|
|
|
# Extract the xsrf token from the _xsrf cookie set after visiting
|
|
|
|
|
# /hub/login with the u.session
|
2023-05-11 21:47:14 +00:00
|
|
|
hub_cookie = u.session.cookie_jar.filter_cookies(
|
|
|
|
|
str(u.hub_url / "hub/api/user")
|
|
|
|
|
)
|
2023-05-11 23:13:59 +02:00
|
|
|
assert "_xsrf" in hub_cookie
|
|
|
|
|
hub_xsrf_token = hub_cookie["_xsrf"].value
|
|
|
|
|
|
2023-03-28 15:06:04 +02:00
|
|
|
# Check that we can talk to JupyterHub itself
|
|
|
|
|
# use this as a proxy for whether the user still exists
|
|
|
|
|
async def hub_api_request():
|
2021-11-01 09:42:45 +01:00
|
|
|
r = await u.session.get(
|
2023-03-28 15:06:04 +02:00
|
|
|
u.hub_url / "hub/api/user",
|
2023-05-11 23:13:59 +02:00
|
|
|
headers={
|
|
|
|
|
# Referer is needed for JupyterHub <=3
|
|
|
|
|
"Referer": str(u.hub_url / "hub/"),
|
|
|
|
|
# X-XSRFToken is needed for JupyterHub >=4
|
|
|
|
|
"X-XSRFToken": hub_xsrf_token,
|
|
|
|
|
},
|
2023-03-28 15:06:04 +02:00
|
|
|
allow_redirects=False,
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
2023-03-28 15:06:04 +02:00
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
r = await hub_api_request()
|
|
|
|
|
assert r.status == 200
|
|
|
|
|
|
|
|
|
|
# Wait for culling
|
|
|
|
|
# step 1: check if the server is still running
|
2023-06-09 01:48:00 +02:00
|
|
|
timeout = 30
|
2023-03-28 15:06:04 +02:00
|
|
|
|
|
|
|
|
async def server_stopped():
|
|
|
|
|
"""Has the server been stopped?"""
|
|
|
|
|
r = await u.session.get(user_url, allow_redirects=False)
|
|
|
|
|
print(f"{r.status} {r.url}")
|
|
|
|
|
return r.status != 200
|
|
|
|
|
|
|
|
|
|
await exponential_backoff(
|
|
|
|
|
server_stopped,
|
|
|
|
|
"Server still running!",
|
|
|
|
|
timeout=timeout,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# step 2. wait for user to be deleted
|
|
|
|
|
async def user_removed():
|
2023-06-09 01:48:00 +02:00
|
|
|
# Check that after a while, the user has been culled
|
2023-03-28 15:06:04 +02:00
|
|
|
r = await hub_api_request()
|
|
|
|
|
print(f"{r.status} {r.url}")
|
2020-10-26 21:22:11 +01:00
|
|
|
return r.status == 403
|
2019-06-12 17:03:39 +03:00
|
|
|
|
2020-10-26 21:22:11 +01:00
|
|
|
await exponential_backoff(
|
2023-03-28 15:06:04 +02:00
|
|
|
user_removed,
|
|
|
|
|
"User still exists!",
|
|
|
|
|
timeout=timeout,
|
2020-10-26 21:22:11 +01:00
|
|
|
)
|
2019-06-12 17:03:39 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_active_server_not_culled():
|
|
|
|
|
"""
|
2023-06-09 01:48:00 +02:00
|
|
|
User logs in, starts a server & stays idle for a while
|
2019-06-12 17:03:39 +03:00
|
|
|
(the user's server should not be culled during this period).
|
|
|
|
|
"""
|
|
|
|
|
# This *must* be localhost, not an IP
|
|
|
|
|
# aiohttp throws away cookies if we are connecting to an IP!
|
|
|
|
|
username = secrets.token_hex(8)
|
|
|
|
|
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2021-11-03 23:55:34 +01:00
|
|
|
*TLJH_CONFIG_PATH, "set", "auth.type", "dummy"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
2023-03-28 15:06:04 +02:00
|
|
|
# Check every 5s for idle servers to cull
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2023-03-28 15:06:04 +02:00
|
|
|
*TLJH_CONFIG_PATH, "set", "services.cull.every", "5"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
2019-06-12 17:03:39 +03:00
|
|
|
# Apart from servers, also cull users
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2021-11-03 23:55:34 +01:00
|
|
|
*TLJH_CONFIG_PATH, "set", "services.cull.users", "True"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
2023-06-09 01:48:00 +02:00
|
|
|
# Cull servers and users after a while, regardless of activity
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
|
|
|
|
await asyncio.create_subprocess_exec(
|
2023-06-09 01:48:00 +02:00
|
|
|
*TLJH_CONFIG_PATH, "set", "services.cull.max_age", "30"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
).wait()
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
0
|
|
|
|
|
== await (
|
2021-11-03 23:55:34 +01:00
|
|
|
await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, "reload")
|
2021-11-01 09:42:45 +01:00
|
|
|
).wait()
|
|
|
|
|
)
|
2019-06-12 17:03:39 +03:00
|
|
|
|
2023-06-09 02:00:30 +02:00
|
|
|
async with User(username, HUB_URL, partial(login_dummy, password="")) as u:
|
|
|
|
|
assert await u.login()
|
2020-10-26 21:22:11 +01:00
|
|
|
# Start user's server
|
2023-06-07 01:35:34 +02:00
|
|
|
await u.ensure_server_simulate(timeout=60, spawn_refresh_time=5)
|
2020-10-26 21:22:11 +01:00
|
|
|
# Assert that the user exists
|
2021-11-03 23:55:34 +01:00
|
|
|
assert pwd.getpwnam(f"jupyter-{username}") is not None
|
2020-10-26 21:22:11 +01:00
|
|
|
|
|
|
|
|
# Check that we can get to the user's server
|
2023-03-28 15:06:04 +02:00
|
|
|
user_url = u.notebook_url / "api/status"
|
|
|
|
|
r = await u.session.get(user_url, allow_redirects=False)
|
2020-10-26 21:22:11 +01:00
|
|
|
assert r.status == 200
|
|
|
|
|
|
2023-03-28 15:06:04 +02:00
|
|
|
async def server_has_stopped():
|
2023-06-09 01:48:00 +02:00
|
|
|
# Check that after a while, we can still reach the user's server
|
2023-03-28 15:06:04 +02:00
|
|
|
r = await u.session.get(user_url, allow_redirects=False)
|
|
|
|
|
print(f"{r.status} {r.url}")
|
2020-10-26 21:22:11 +01:00
|
|
|
return r.status != 200
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
await exponential_backoff(
|
2023-03-28 15:06:04 +02:00
|
|
|
server_has_stopped,
|
|
|
|
|
"User's server is still reachable (good!)",
|
2023-06-09 01:48:00 +02:00
|
|
|
timeout=15,
|
2020-10-26 21:22:11 +01:00
|
|
|
)
|
2023-03-28 15:06:04 +02:00
|
|
|
except asyncio.TimeoutError:
|
|
|
|
|
# timeout error means the test passed - the server didn't go away while we were waiting
|
2020-10-26 21:22:11 +01:00
|
|
|
pass
|
2023-03-28 15:06:04 +02:00
|
|
|
else:
|
|
|
|
|
pytest.fail(f"Server at {user_url} got culled prematurely!")
|