forked from jupyterhub/the-littlest-jupyterhub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_configurer.py
259 lines (214 loc) · 6.26 KB
/
test_configurer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""
Test configurer
"""
from traitlets.config import Config
import os
import sys
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()
configurer.apply_config(overrides, c)
return c
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"
def test_default_memory_limit():
"""
Test default per user memory limit
"""
c = apply_mock_config({})
assert c.Spawner.mem_limit is None
def test_set_memory_limit():
"""
Test setting per user memory limit
"""
c = apply_mock_config({"limits": {"memory": "42G"}})
assert c.Spawner.mem_limit == "42G"
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
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": {"password": "test"}}}
)
assert c.JupyterHub.authenticator_class == "dummy"
assert c.DummyAuthenticator.password == "test"
def test_user_groups():
"""
Test setting user groups
"""
c = apply_mock_config(
{
"users": {
"extra_user_groups": {"g1": ["u1", "u2"], "g2": ["u3", "u4"]},
}
}
)
assert c.UserCreatingSpawner.user_groups == {"g1": ["u1", "u2"], "g2": ["u3", "u4"]}
def test_auth_firstuse():
"""
Test setting FirstUse Authenticator options
"""
c = apply_mock_config(
{
"auth": {
"type": "firstuseauthenticator.FirstUseAuthenticator",
"FirstUseAuthenticator": {"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"
def test_traefik_api_default():
"""
Test default traefik api authentication settings with no overrides
"""
c = apply_mock_config({})
assert c.TraefikTomlProxy.traefik_api_username == "api_admin"
assert len(c.TraefikTomlProxy.traefik_api_password) == 0
def test_set_traefik_api():
"""
Test setting per traefik api credentials
"""
c = apply_mock_config(
{"traefik_api": {"username": "some_user", "password": "1234"}}
)
assert c.TraefikTomlProxy.traefik_api_username == "some_user"
assert c.TraefikTomlProxy.traefik_api_password == "1234"
def test_cull_service_default():
"""
Test default cull service settings with no overrides
"""
c = apply_mock_config({})
cull_cmd = [
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,
}
]
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,
"-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,
}
]
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