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
|
|
|
"""
|
|
|
|
|
|
2021-10-27 09:14:33 +02:00
|
|
|
from traitlets.config import Config
|
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
|
|
|
|
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({})
|
|
|
|
|
assert c.JupyterHub.base_url == '/'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_set_base_url():
|
|
|
|
|
"""
|
|
|
|
|
Test set JupyterHub base_url
|
|
|
|
|
"""
|
|
|
|
|
c = apply_mock_config({'base_url': '/custom-base'})
|
|
|
|
|
assert c.JupyterHub.base_url == '/custom-base'
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
"""
|
|
|
|
|
c = apply_mock_config({'limits': {'memory': '42G'}})
|
2019-05-18 13:43:34 -07:00
|
|
|
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({})
|
|
|
|
|
# default_url is not set, so JupyterHub will pick default.
|
2021-10-27 09:14:33 +02:00
|
|
|
assert 'default_url' not in c.Spawner
|
2018-07-15 21:52:25 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_app_jupyterlab():
|
|
|
|
|
"""
|
|
|
|
|
Test setting JupyterLab as default application
|
|
|
|
|
"""
|
|
|
|
|
c = apply_mock_config({'user_environment': {'default_app': 'jupyterlab'}})
|
|
|
|
|
assert c.Spawner.default_url == '/lab'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_app_nteract():
|
|
|
|
|
"""
|
|
|
|
|
Test setting nteract as default application
|
|
|
|
|
"""
|
|
|
|
|
c = apply_mock_config({'user_environment': {'default_app': 'nteract'}})
|
|
|
|
|
assert c.Spawner.default_url == '/nteract'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
== 'firstuseauthenticator.FirstUseAuthenticator'
|
|
|
|
|
)
|
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(
|
|
|
|
|
{'auth': {'type': 'dummy', 'DummyAuthenticator': {'password': 'test'}}}
|
|
|
|
|
)
|
2021-10-19 11:56:16 +02:00
|
|
|
assert c.JupyterHub.authenticator_class == 'dummy'
|
2018-07-15 21:52:25 -07:00
|
|
|
assert c.DummyAuthenticator.password == 'test'
|
|
|
|
|
|
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(
|
|
|
|
|
{
|
|
|
|
|
'users': {
|
|
|
|
|
'extra_user_groups': {"g1": ["u1", "u2"], "g2": ["u3", "u4"]},
|
|
|
|
|
}
|
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(
|
|
|
|
|
{
|
|
|
|
|
'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
|
|
|
|
|
== 'firstuseauthenticator.FirstUseAuthenticator'
|
|
|
|
|
)
|
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(
|
|
|
|
|
{
|
|
|
|
|
'auth': {
|
|
|
|
|
'type': 'oauthenticator.github.GitHubOAuthenticator',
|
|
|
|
|
'GitHubOAuthenticator': {
|
|
|
|
|
'client_id': 'something',
|
|
|
|
|
'client_secret': 'something-else',
|
|
|
|
|
},
|
2018-07-16 12:03:45 -07:00
|
|
|
}
|
|
|
|
|
}
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
c.JupyterHub.authenticator_class == 'oauthenticator.github.GitHubOAuthenticator'
|
|
|
|
|
)
|
2018-07-16 12:03:45 -07: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({})
|
|
|
|
|
|
|
|
|
|
assert c.TraefikTomlProxy.traefik_api_username == 'api_admin'
|
2019-02-13 14:10:28 +02:00
|
|
|
assert len(c.TraefikTomlProxy.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(
|
|
|
|
|
{'traefik_api': {'username': 'some_user', 'password': '1234'}}
|
|
|
|
|
)
|
2019-02-11 09:24:16 +02:00
|
|
|
assert c.TraefikTomlProxy.traefik_api_username == 'some_user'
|
2019-02-12 14:35:59 +02:00
|
|
|
assert c.TraefikTomlProxy.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,
|
|
|
|
|
'-m',
|
|
|
|
|
'jupyterhub_idle_culler',
|
|
|
|
|
'--timeout=600',
|
|
|
|
|
'--cull-every=60',
|
|
|
|
|
'--concurrency=5',
|
|
|
|
|
'--max-age=0',
|
|
|
|
|
]
|
|
|
|
|
assert c.JupyterHub.services == [
|
|
|
|
|
{
|
|
|
|
|
'name': 'cull-idle',
|
|
|
|
|
'admin': True,
|
|
|
|
|
'command': cull_cmd,
|
|
|
|
|
}
|
2019-06-12 17:03:39 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_set_cull_service():
|
|
|
|
|
"""
|
|
|
|
|
Test setting cull service options
|
|
|
|
|
"""
|
2021-11-01 09:42:45 +01:00
|
|
|
c = apply_mock_config(
|
|
|
|
|
{'services': {'cull': {'every': 10, 'users': True, 'max_age': 60}}}
|
|
|
|
|
)
|
2019-06-12 17:03:39 +03:00
|
|
|
cull_cmd = [
|
2021-11-01 09:42:45 +01:00
|
|
|
sys.executable,
|
|
|
|
|
'-m',
|
|
|
|
|
'jupyterhub_idle_culler',
|
|
|
|
|
'--timeout=600',
|
|
|
|
|
'--cull-every=10',
|
|
|
|
|
'--concurrency=5',
|
|
|
|
|
'--max-age=60',
|
|
|
|
|
'--cull-users',
|
|
|
|
|
]
|
|
|
|
|
assert c.JupyterHub.services == [
|
|
|
|
|
{
|
|
|
|
|
'name': 'cull-idle',
|
|
|
|
|
'admin': True,
|
|
|
|
|
'command': cull_cmd,
|
|
|
|
|
}
|
2019-06-12 17:03:39 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2019-02-22 11:41:50 +01:00
|
|
|
def test_load_secrets(tljh_dir):
|
|
|
|
|
"""
|
|
|
|
|
Test loading secret files
|
|
|
|
|
"""
|
|
|
|
|
with open(os.path.join(tljh_dir, 'state', 'traefik-api.secret'), 'w') as f:
|
|
|
|
|
f.write("traefik-password")
|
|
|
|
|
|
|
|
|
|
tljh_config = configurer.load_config()
|
|
|
|
|
assert tljh_config['traefik_api']['password'] == "traefik-password"
|
|
|
|
|
c = apply_mock_config(tljh_config)
|
|
|
|
|
assert c.TraefikTomlProxy.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(
|
|
|
|
|
{
|
|
|
|
|
'auth': {
|
|
|
|
|
'type': 'nativeauthenticator.NativeAuthenticator',
|
|
|
|
|
'NativeAuthenticator': {
|
|
|
|
|
'open_signup': True,
|
|
|
|
|
},
|
2019-02-20 17:18:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-11-01 09:42:45 +01:00
|
|
|
)
|
2019-02-20 17:18:21 +00:00
|
|
|
assert c.JupyterHub.authenticator_class == 'nativeauthenticator.NativeAuthenticator'
|
|
|
|
|
assert c.NativeAuthenticator.open_signup == True
|