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
|
|
|
|
2018-07-15 21:52:25 -07:00
|
|
|
from tljh import configurer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MockConfigurer:
|
|
|
|
|
"""
|
|
|
|
|
Mock a Traitlet Configurable object.
|
|
|
|
|
|
|
|
|
|
Equivalent to the `c` in `c.JupyterHub.some_property` method of setting
|
|
|
|
|
traitlet properties. If an accessed attribute doesn't exist, a new instance
|
|
|
|
|
of EmtpyObject is returned. This lets us set arbitrary attributes two
|
|
|
|
|
levels deep.
|
|
|
|
|
|
|
|
|
|
>>> c = MockConfigurer()
|
|
|
|
|
>>> c.FirstLevel.second_level = 'hi'
|
|
|
|
|
>>> c.FirstLevel.second_level == 'hi'
|
|
|
|
|
True
|
|
|
|
|
>>> hasattr(c.FirstLevel, 'does_not_exist')
|
|
|
|
|
False
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
class _EmptyObject:
|
|
|
|
|
"""
|
|
|
|
|
Empty class for putting attributes in.
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def __getattr__(self, k):
|
|
|
|
|
if k not in self.__dict__:
|
|
|
|
|
self.__dict__[k] = MockConfigurer._EmptyObject()
|
|
|
|
|
return self.__dict__[k]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_mock_configurer():
|
|
|
|
|
"""
|
|
|
|
|
Test the MockConfigurer's mocking ability
|
|
|
|
|
"""
|
|
|
|
|
m = MockConfigurer()
|
|
|
|
|
m.SomethingSomething = 'hi'
|
|
|
|
|
m.FirstLevel.second_level = 'boo'
|
|
|
|
|
|
|
|
|
|
assert m.SomethingSomething == 'hi'
|
|
|
|
|
assert m.FirstLevel.second_level == 'boo'
|
|
|
|
|
|
|
|
|
|
assert not hasattr(m.FirstLevel, 'non_existent')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = MockConfigurer()
|
|
|
|
|
configurer.apply_config(overrides, c)
|
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
assert not hasattr(c.Spawner, 'default_url')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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': {
|
2018-07-16 16:05:44 -07:00
|
|
|
'type': 'dummyauthenticator.DummyAuthenticator',
|
|
|
|
|
'DummyAuthenticator': {
|
2018-07-15 21:52:25 -07:00
|
|
|
'password': 'test'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
assert c.JupyterHub.authenticator_class == 'dummyauthenticator.DummyAuthenticator'
|
|
|
|
|
assert c.DummyAuthenticator.password == 'test'
|
|
|
|
|
|
|
|
|
|
|
2019-06-20 21:54:51 +03:00
|
|
|
def test_user_groups():
|
|
|
|
|
"""
|
|
|
|
|
Test setting user groups
|
|
|
|
|
"""
|
|
|
|
|
c = apply_mock_config({
|
|
|
|
|
'users': {
|
|
|
|
|
'groups': {
|
|
|
|
|
"g1": ["u1", "u2"],
|
|
|
|
|
"g1": ["u3", "u4"]
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
assert c.UserCreatingSpawner.user_groups == {
|
|
|
|
|
"g1": ["u1", "u2"],
|
|
|
|
|
"g1": ["u3", "u4"]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-07-15 21:52:25 -07:00
|
|
|
def test_auth_firstuse():
|
|
|
|
|
"""
|
|
|
|
|
Test setting FirstUse Authenticator options
|
|
|
|
|
"""
|
|
|
|
|
c = apply_mock_config({
|
|
|
|
|
'auth': {
|
2018-07-16 16:05:44 -07:00
|
|
|
'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
|
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
|
|
|
"""
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
"""
|
|
|
|
|
c = apply_mock_config({
|
2019-02-22 11:41:50 +01:00
|
|
|
'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 = [
|
|
|
|
|
sys.executable, '/srv/src/tljh/cull_idle_servers.py',
|
2019-06-12 17:32:33 +03:00
|
|
|
'--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 = [
|
|
|
|
|
sys.executable, '/srv/src/tljh/cull_idle_servers.py',
|
2019-06-12 17:32:33 +03:00
|
|
|
'--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,
|
|
|
|
|
}]
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-02-20 17:18:21 +00:00
|
|
|
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
|
2019-02-22 12:33:55 +01:00
|
|
|
|