2018-07-15 21:52:25 -07:00
|
|
|
"""
|
2019-02-22 11:41:50 +01:00
|
|
|
Test configurer
|
2018-07-15 21:52:25 -07:00
|
|
|
"""
|
|
|
|
|
|
2019-02-22 11:41:50 +01:00
|
|
|
import os
|
2019-06-12 17:03:39 +03:00
|
|
|
import sys
|
2019-02-22 11:41:50 +01:00
|
|
|
|
2023-05-15 08:51:35 +00:00
|
|
|
from traitlets.config import Config
|
|
|
|
|
|
2018-07-15 21:52:25 -07:00
|
|
|
from tljh import configurer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_mock_config(overrides):
|
|
|
|
|
"""
|
|
|
|
|
Configure a mock configurer with given overrides.
|
|
|
|
|
|
|
|
|
|
overrides should be a dict that matches what you parse from a config.yaml
|
|
|
|
|
"""
|
2021-10-27 09:14:33 +02:00
|
|
|
c = Config()
|
2018-07-15 21:52:25 -07:00
|
|
|
configurer.apply_config(overrides, c)
|
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
|
|
2020-10-20 00:14:15 +02:00
|
|
|
def test_default_base_url():
|
|
|
|
|
"""
|
|
|
|
|
Test default JupyterHub base_url
|
|
|
|
|
"""
|
|
|
|
|
c = apply_mock_config({})
|
2021-11-03 23:55:34 +01:00
|
|
|
assert c.JupyterHub.base_url == "/"
|
2020-10-20 00:14:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_set_base_url():
|
|
|
|
|
"""
|
|
|
|
|
Test set JupyterHub base_url
|
|
|
|
|
"""
|
2021-11-03 23:55:34 +01:00
|
|
|
c = apply_mock_config({"base_url": "/custom-base"})
|
|
|
|
|
assert c.JupyterHub.base_url == "/custom-base"
|
2020-10-20 00:14:15 +02:00
|
|
|
|
|
|
|
|
|
2018-10-15 16:03:31 +02:00
|
|
|
def test_default_memory_limit():
|
|
|
|
|
"""
|
|
|
|
|
Test default per user memory limit
|
|
|
|
|
"""
|
|
|
|
|
c = apply_mock_config({})
|
2019-05-18 13:43:34 -07:00
|
|
|
assert c.Spawner.mem_limit is None
|
2018-10-15 16:03:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_set_memory_limit():
|
|
|
|
|
"""
|
|
|
|
|
Test setting per user memory limit
|
|
|
|
|
"""
|
2021-11-03 23:55:34 +01:00
|
|
|
c = apply_mock_config({"limits": {"memory": "42G"}})
|
|
|
|
|
assert c.Spawner.mem_limit == "42G"
|
2018-10-15 16:03:31 +02:00
|
|
|
|
|
|
|
|
|
2018-07-15 21:52:25 -07:00
|
|
|
def test_app_default():
|
|
|
|
|
"""
|
|
|
|
|
Test default application with no config overrides.
|
|
|
|
|
"""
|
|
|
|
|
c = apply_mock_config({})
|
2023-08-09 10:31:32 +02:00
|
|
|
assert c.Spawner.default_url == "/lab"
|
2018-07-15 21:52:25 -07:00
|
|
|
|
|
|
|
|
|
2021-12-16 22:26:24 -05:00
|
|
|
def test_app_classic():
|
2018-07-15 21:52:25 -07:00
|
|
|
"""
|
2021-12-16 22:26:24 -05:00
|
|
|
Test setting classic as default application
|
2018-07-15 21:52:25 -07:00
|
|
|
"""
|
2021-12-16 22:26:24 -05:00
|
|
|
c = apply_mock_config({"user_environment": {"default_app": "classic"}})
|
|
|
|
|
assert c.Spawner.default_url == "/tree"
|
2018-07-15 21:52:25 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_auth_default():
|
|
|
|
|
"""
|
|
|
|
|
Test default authentication settings with no overrides
|
|
|
|
|
"""
|
|
|
|
|
c = apply_mock_config({})
|
|
|
|
|
|
2021-11-01 09:42:45 +01:00
|
|
|
assert (
|
|
|
|
|
c.JupyterHub.authenticator_class
|
2021-11-03 23:55:34 +01:00
|
|
|
== "firstuseauthenticator.FirstUseAuthenticator"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
2018-07-15 21:52:25 -07:00
|
|
|
# Do not auto create users who haven't been manually added by default
|
|
|
|
|
assert not c.FirstUseAuthenticator.create_users
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_auth_dummy():
|
|
|
|
|
"""
|
|
|
|
|
Test setting Dummy Authenticator & password
|
|
|
|
|
"""
|
2021-11-01 09:42:45 +01:00
|
|
|
c = apply_mock_config(
|
2021-11-03 23:55:34 +01:00
|
|
|
{"auth": {"type": "dummy", "DummyAuthenticator": {"password": "test"}}}
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
2021-11-03 23:55:34 +01:00
|
|
|
assert c.JupyterHub.authenticator_class == "dummy"
|
|
|
|
|
assert c.DummyAuthenticator.password == "test"
|
2018-07-15 21:52:25 -07:00
|
|
|
|
2020-10-20 00:14:15 +02:00
|
|
|
|
2019-06-20 21:54:51 +03:00
|
|
|
def test_user_groups():
|
|
|
|
|
"""
|
|
|
|
|
Test setting user groups
|
|
|
|
|
"""
|
2021-11-01 09:42:45 +01:00
|
|
|
c = apply_mock_config(
|
|
|
|
|
{
|
2021-11-03 23:55:34 +01:00
|
|
|
"users": {
|
|
|
|
|
"extra_user_groups": {"g1": ["u1", "u2"], "g2": ["u3", "u4"]},
|
2021-11-01 09:42:45 +01:00
|
|
|
}
|
2019-06-20 21:54:51 +03:00
|
|
|
}
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
assert c.UserCreatingSpawner.user_groups == {"g1": ["u1", "u2"], "g2": ["u3", "u4"]}
|
2019-06-20 21:54:51 +03:00
|
|
|
|
2018-07-15 21:52:25 -07:00
|
|
|
|
|
|
|
|
def test_auth_firstuse():
|
|
|
|
|
"""
|
|
|
|
|
Test setting FirstUse Authenticator options
|
|
|
|
|
"""
|
2021-11-01 09:42:45 +01:00
|
|
|
c = apply_mock_config(
|
|
|
|
|
{
|
2021-11-03 23:55:34 +01:00
|
|
|
"auth": {
|
|
|
|
|
"type": "firstuseauthenticator.FirstUseAuthenticator",
|
|
|
|
|
"FirstUseAuthenticator": {"create_users": True},
|
2018-07-15 21:52:25 -07:00
|
|
|
}
|
|
|
|
|
}
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
c.JupyterHub.authenticator_class
|
2021-11-03 23:55:34 +01:00
|
|
|
== "firstuseauthenticator.FirstUseAuthenticator"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
2018-07-15 21:52:25 -07:00
|
|
|
assert c.FirstUseAuthenticator.create_users
|
2018-07-16 12:03:45 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_auth_github():
|
|
|
|
|
"""
|
2018-07-16 16:05:44 -07:00
|
|
|
Test using GitHub authenticator
|
2018-07-16 12:03:45 -07:00
|
|
|
"""
|
2021-11-01 09:42:45 +01:00
|
|
|
c = apply_mock_config(
|
|
|
|
|
{
|
2021-11-03 23:55:34 +01:00
|
|
|
"auth": {
|
|
|
|
|
"type": "oauthenticator.github.GitHubOAuthenticator",
|
|
|
|
|
"GitHubOAuthenticator": {
|
|
|
|
|
"client_id": "something",
|
|
|
|
|
"client_secret": "something-else",
|
2021-11-01 09:42:45 +01:00
|
|
|
},
|
2018-07-16 12:03:45 -07:00
|
|
|
}
|
|
|
|
|
}
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
assert (
|
2021-11-03 23:55:34 +01:00
|
|
|
c.JupyterHub.authenticator_class == "oauthenticator.github.GitHubOAuthenticator"
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
2021-11-03 23:55:34 +01:00
|
|
|
assert c.GitHubOAuthenticator.client_id == "something"
|
|
|
|
|
assert c.GitHubOAuthenticator.client_secret == "something-else"
|
2019-02-11 09:24:16 +02:00
|
|
|
|
|
|
|
|
|
2019-02-22 11:41:50 +01:00
|
|
|
def test_traefik_api_default():
|
2019-02-11 09:24:16 +02:00
|
|
|
"""
|
|
|
|
|
Test default traefik api authentication settings with no overrides
|
|
|
|
|
"""
|
|
|
|
|
c = apply_mock_config({})
|
|
|
|
|
|
2023-05-15 10:53:53 +02:00
|
|
|
assert c.TraefikProxy.traefik_api_username == "api_admin"
|
|
|
|
|
assert len(c.TraefikProxy.traefik_api_password) == 0
|
2019-02-11 09:24:16 +02:00
|
|
|
|
|
|
|
|
|
2019-02-22 11:41:50 +01:00
|
|
|
def test_set_traefik_api():
|
2019-02-11 09:24:16 +02:00
|
|
|
"""
|
|
|
|
|
Test setting per traefik api credentials
|
|
|
|
|
"""
|
2021-11-01 09:42:45 +01:00
|
|
|
c = apply_mock_config(
|
2021-11-03 23:55:34 +01:00
|
|
|
{"traefik_api": {"username": "some_user", "password": "1234"}}
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
2023-05-15 10:53:53 +02:00
|
|
|
assert c.TraefikProxy.traefik_api_username == "some_user"
|
|
|
|
|
assert c.TraefikProxy.traefik_api_password == "1234"
|
2019-02-22 11:41:50 +01:00
|
|
|
|
|
|
|
|
|
2019-06-12 17:03:39 +03:00
|
|
|
def test_cull_service_default():
|
|
|
|
|
"""
|
|
|
|
|
Test default cull service settings with no overrides
|
|
|
|
|
"""
|
|
|
|
|
c = apply_mock_config({})
|
|
|
|
|
|
|
|
|
|
cull_cmd = [
|
2021-11-01 09:42:45 +01:00
|
|
|
sys.executable,
|
2021-11-03 23:55:34 +01:00
|
|
|
"-m",
|
|
|
|
|
"jupyterhub_idle_culler",
|
|
|
|
|
"--timeout=600",
|
|
|
|
|
"--cull-every=60",
|
|
|
|
|
"--concurrency=5",
|
|
|
|
|
"--max-age=0",
|
2021-11-01 09:42:45 +01:00
|
|
|
]
|
|
|
|
|
assert c.JupyterHub.services == [
|
|
|
|
|
{
|
2021-11-03 23:55:34 +01:00
|
|
|
"name": "cull-idle",
|
|
|
|
|
"admin": True,
|
|
|
|
|
"command": cull_cmd,
|
2021-11-01 09:42:45 +01:00
|
|
|
}
|
2019-06-12 17:03:39 +03:00
|
|
|
]
|
|
|
|
|
|
2023-04-24 00:10:25 +00:00
|
|
|
|
2023-03-20 17:53:31 +00:00
|
|
|
def test_set_cull_service():
|
2023-03-20 17:32:24 +00:00
|
|
|
"""
|
2023-03-20 17:53:31 +00:00
|
|
|
Test setting cull service options
|
2023-03-20 17:32:24 +00:00
|
|
|
"""
|
|
|
|
|
c = apply_mock_config(
|
2023-03-20 17:53:31 +00:00
|
|
|
{"services": {"cull": {"every": 10, "users": True, "max_age": 60}}}
|
2023-03-20 17:32:24 +00:00
|
|
|
)
|
|
|
|
|
cull_cmd = [
|
|
|
|
|
sys.executable,
|
|
|
|
|
"-m",
|
|
|
|
|
"jupyterhub_idle_culler",
|
|
|
|
|
"--timeout=600",
|
|
|
|
|
"--cull-every=10",
|
|
|
|
|
"--concurrency=5",
|
|
|
|
|
"--max-age=60",
|
2023-03-20 17:53:31 +00:00
|
|
|
"--cull-users",
|
2023-03-20 17:32:24 +00:00
|
|
|
]
|
|
|
|
|
assert c.JupyterHub.services == [
|
|
|
|
|
{
|
|
|
|
|
"name": "cull-idle",
|
|
|
|
|
"admin": True,
|
|
|
|
|
"command": cull_cmd,
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
2023-04-24 00:10:25 +00:00
|
|
|
|
2023-03-20 17:53:31 +00:00
|
|
|
def test_cull_service_named():
|
2019-06-12 17:03:39 +03:00
|
|
|
"""
|
2023-03-20 17:53:31 +00:00
|
|
|
Test default cull service settings with named server removal
|
2019-06-12 17:03:39 +03:00
|
|
|
"""
|
2021-11-01 09:42:45 +01:00
|
|
|
c = apply_mock_config(
|
2023-04-24 00:10:25 +00:00
|
|
|
{
|
|
|
|
|
"services": {
|
|
|
|
|
"cull": {
|
|
|
|
|
"every": 10,
|
|
|
|
|
"cull_users": True,
|
|
|
|
|
"remove_named_servers": True,
|
|
|
|
|
"max_age": 60,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
2023-03-20 17:53:31 +00:00
|
|
|
|
2019-06-12 17:03:39 +03:00
|
|
|
cull_cmd = [
|
2021-11-01 09:42:45 +01:00
|
|
|
sys.executable,
|
2021-11-03 23:55:34 +01:00
|
|
|
"-m",
|
|
|
|
|
"jupyterhub_idle_culler",
|
|
|
|
|
"--timeout=600",
|
|
|
|
|
"--cull-every=10",
|
|
|
|
|
"--concurrency=5",
|
|
|
|
|
"--max-age=60",
|
|
|
|
|
"--cull-users",
|
2023-03-20 17:53:31 +00:00
|
|
|
"--remove-named-servers",
|
2021-11-01 09:42:45 +01:00
|
|
|
]
|
|
|
|
|
assert c.JupyterHub.services == [
|
|
|
|
|
{
|
2021-11-03 23:55:34 +01:00
|
|
|
"name": "cull-idle",
|
|
|
|
|
"admin": True,
|
|
|
|
|
"command": cull_cmd,
|
2021-11-01 09:42:45 +01:00
|
|
|
}
|
2019-06-12 17:03:39 +03:00
|
|
|
]
|
|
|
|
|
|
2023-04-24 00:10:25 +00:00
|
|
|
|
2019-02-22 11:41:50 +01:00
|
|
|
def test_load_secrets(tljh_dir):
|
|
|
|
|
"""
|
|
|
|
|
Test loading secret files
|
|
|
|
|
"""
|
2021-11-03 23:55:34 +01:00
|
|
|
with open(os.path.join(tljh_dir, "state", "traefik-api.secret"), "w") as f:
|
2019-02-22 11:41:50 +01:00
|
|
|
f.write("traefik-password")
|
|
|
|
|
|
|
|
|
|
tljh_config = configurer.load_config()
|
2021-11-03 23:55:34 +01:00
|
|
|
assert tljh_config["traefik_api"]["password"] == "traefik-password"
|
2019-02-22 11:41:50 +01:00
|
|
|
c = apply_mock_config(tljh_config)
|
2023-05-15 10:53:53 +02:00
|
|
|
assert c.TraefikProxy.traefik_api_password == "traefik-password"
|
2019-02-22 12:33:55 +01:00
|
|
|
|
2020-04-27 17:07:54 +05:30
|
|
|
|
2019-02-20 17:18:21 +00:00
|
|
|
def test_auth_native():
|
|
|
|
|
"""
|
|
|
|
|
Test setting Native Authenticator
|
|
|
|
|
"""
|
2021-11-01 09:42:45 +01:00
|
|
|
c = apply_mock_config(
|
|
|
|
|
{
|
2021-11-03 23:55:34 +01:00
|
|
|
"auth": {
|
|
|
|
|
"type": "nativeauthenticator.NativeAuthenticator",
|
|
|
|
|
"NativeAuthenticator": {
|
|
|
|
|
"open_signup": True,
|
2021-11-01 09:42:45 +01:00
|
|
|
},
|
2019-02-20 17:18:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
2021-11-03 23:55:34 +01:00
|
|
|
assert c.JupyterHub.authenticator_class == "nativeauthenticator.NativeAuthenticator"
|
2019-02-20 17:18:21 +00:00
|
|
|
assert c.NativeAuthenticator.open_signup == True
|