Files
the-littlest-jupyterhub/tests/test_configurer.py

257 lines
6.2 KiB
Python
Raw Normal View History

2018-07-15 21:52:25 -07:00
"""
Test configurer
2018-07-15 21:52:25 -07:00
"""
2020-10-20 00:14:15 +02:00
from traitlets import Dict
from traitlets.config import Config
import os
2019-06-12 17:03:39 +03:00
import sys
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
"""
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({})
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'}})
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.
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({})
assert c.JupyterHub.authenticator_class == 'firstuseauthenticator.FirstUseAuthenticator'
# 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
"""
c = apply_mock_config({
'auth': {
'type': 'dummy',
'DummyAuthenticator': {
2018-07-15 21:52:25 -07:00
'password': 'test'
}
}
})
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
"""
c = apply_mock_config({
'users': {
2019-06-21 11:38:26 +03:00
'extra_user_groups': {
2019-06-20 21:54:51 +03:00
"g1": ["u1", "u2"],
2019-06-21 11:38:26 +03:00
"g2": ["u3", "u4"]
2019-06-20 21:54:51 +03:00
},
}
})
assert c.UserCreatingSpawner.user_groups == {
2020-10-20 00:14:15 +02:00
"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
"""
c = apply_mock_config({
'auth': {
'type': 'firstuseauthenticator.FirstUseAuthenticator',
'FirstUseAuthenticator': {
2018-07-15 21:52:25 -07:00
'create_users': True
}
}
})
assert c.JupyterHub.authenticator_class == 'firstuseauthenticator.FirstUseAuthenticator'
assert c.FirstUseAuthenticator.create_users
def test_auth_github():
"""
Test using GitHub authenticator
"""
c = apply_mock_config({
'auth': {
'type': 'oauthenticator.github.GitHubOAuthenticator',
'GitHubOAuthenticator': {
'client_id': 'something',
'client_secret': 'something-else'
}
}
})
assert c.JupyterHub.authenticator_class == 'oauthenticator.github.GitHubOAuthenticator'
assert c.GitHubOAuthenticator.client_id == 'something'
assert c.GitHubOAuthenticator.client_secret == 'something-else'
2019-02-11 09:24:16 +02: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
def test_set_traefik_api():
2019-02-11 09:24:16 +02:00
"""
Test setting per traefik api credentials
"""
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-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 = [
2020-10-20 00:14:15 +02:00
sys.executable, '-m', 'jupyterhub_idle_culler',
'--timeout=600', '--cull-every=60', '--concurrency=5',
'--max-age=0'
2019-06-12 17:03:39 +03:00
]
assert c.JupyterHub.services == [{
'name': 'cull-idle',
'admin': True,
'command': cull_cmd,
}]
def test_set_cull_service():
"""
Test setting cull service options
"""
c = apply_mock_config({
'services': {
'cull': {
'every': 10,
'users': True,
'max_age': 60
}
}
})
cull_cmd = [
2020-10-20 00:14:15 +02:00
sys.executable, '-m', 'jupyterhub_idle_culler',
'--timeout=600', '--cull-every=10', '--concurrency=5',
'--max-age=60', '--cull-users'
2019-06-12 17:03:39 +03:00
]
assert c.JupyterHub.services == [{
'name': 'cull-idle',
'admin': True,
'command': cull_cmd,
}]
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"
def test_auth_native():
"""
Test setting Native Authenticator
"""
c = apply_mock_config({
'auth': {
'type': 'nativeauthenticator.NativeAuthenticator',
'NativeAuthenticator': {
'open_signup': True,
}
}
})
assert c.JupyterHub.authenticator_class == 'nativeauthenticator.NativeAuthenticator'
assert c.NativeAuthenticator.open_signup == True