1
- import requests
2
1
import docker
2
+ import os
3
+ import requests
4
+ import logging
3
5
import pytest
4
6
from ldap3 import ALL_ATTRIBUTES
5
7
from ldap_hooks import search_for , ConnectionManager
6
8
from os .path import join , dirname , realpath
7
9
from docker .types import Mount
8
10
from docker .errors import NotFound
11
+ from .util import wait_for_site
9
12
10
13
# root dir
11
14
docker_path = dirname (dirname (realpath (__file__ )))
12
15
16
+ # Logger
17
+ logging .basicConfig (level = logging .INFO )
18
+ test_logger = logging .getLogger ()
19
+
13
20
JHUB_IMAGE_NAME = "jupyterhub-ldap-hooks"
14
21
JHUB_IMAGE_TAG = "test"
15
22
JHUB_IMAGE = "" .join ([JHUB_IMAGE_NAME , ":" , JHUB_IMAGE_TAG ])
23
+ PORT = 8000
16
24
17
25
jhub_image_spec = {"path" : docker_path , "tag" : JHUB_IMAGE , "rm" : "True" , "pull" : "True" }
18
26
21
29
LDAP_IMAGE_TAG = "1.2.3"
22
30
LDAP_IMAGE = "" .join ([LDAP_IMAGE_PATH , ":" , LDAP_IMAGE_TAG ])
23
31
24
- JHUB_URL = "http://127.0.0.1:8000"
32
+ JHUB_URL = "http://127.0.0.1:{}" . format ( PORT )
25
33
LDAP_URL = "http://openldap"
26
34
27
35
# Config setup
28
36
config_path = join (dirname (realpath (__file__ )), "configs" )
29
37
30
38
jhub_config_path = join (config_path , "jhub" , "ldap_person_hook.py" )
31
- jhub_target_config = "/ etc/ jupyterhub/ jupyterhub_config.py"
39
+ jhub_target_config = os . path . join ( os . sep , " etc" , " jupyterhub" , " jupyterhub_config.py")
32
40
33
41
ldap_schema = join (config_path , "openldap" , "mount_schema" )
34
42
ldap_target_schema = "/container/service/slapd/assets/config/bootstrap/schema"
44
52
}
45
53
46
54
docker_mount = Mount (
47
- source = "/ var/ run/ docker.sock" ,
48
- target = "/ var/ run/ docker.sock" ,
55
+ source = os . path . join ( os . sep , " var" , " run" , " docker.sock") ,
56
+ target = os . path . join ( os . sep , " var" , " run" , " docker.sock") ,
49
57
read_only = True ,
50
58
type = "bind" ,
51
59
)
63
71
),
64
72
docker_mount ,
65
73
],
66
- "ports" : {8000 : 8000 },
74
+ "ports" : {PORT : PORT },
67
75
"network" : LDAP_NETWORK_NAME ,
68
76
"detach" : "True" ,
69
77
"command" : "jupyterhub --debug -f " + jhub_target_config ,
@@ -110,24 +118,20 @@ def test_ldap_person_hook(build_image, network, containers):
110
118
Test that the ldap_person_hook is able to create an LDAP DIT entry,
111
119
with the provided JupyterHub Spawner attribute.
112
120
"""
121
+ test_logger .info ("Start of ldap person hook testing" )
113
122
client = docker .from_env ()
114
- containers = client .containers .list ()
115
- assert len (containers ) > 0
123
+ username = "ldap-user"
124
+ auth_headers = {"Remote-User" : username }
125
+ assert wait_for_site (JHUB_URL , valid_status_code = 401 ) is True
116
126
with requests .Session () as s :
117
- ready = False
118
- while not ready :
119
- resp = s .get ("" .join ([JHUB_URL , "/hub/home" ]))
120
- if resp .status_code != 404 :
121
- ready = True
122
127
# Login
123
- user = "a-new-user"
124
- login_response = s .post (JHUB_URL + "/hub/login" , headers = {"Remote-User" : user })
128
+ login_response = s .post (JHUB_URL + "/hub/login" , headers = auth_headers )
125
129
assert login_response .status_code == 200
126
130
127
131
resp = s .get (JHUB_URL + "/hub/home" )
128
132
assert resp .status_code == 200
129
133
130
- dn_str = "/telephoneNumber=23012303403/SN=My Surname/CN=" + user
134
+ dn_str = "/telephoneNumber=23012303403/SN=My Surname/CN=" + username
131
135
# Pass LDAP DN for creation on spawn
132
136
post_dn = s .post (
133
137
JHUB_URL + "/hub/user-data" , json = {"data" : {"PersonDN" : dn_str }}
@@ -138,7 +142,7 @@ def test_ldap_person_hook(build_image, network, containers):
138
142
spawn_response = s .post (JHUB_URL + "/hub/spawn" )
139
143
assert spawn_response .status_code == 200
140
144
141
- container_name = "jupyter-" + user
145
+ container_name = "jupyter-" + username
142
146
spawned = False
143
147
wait_attempts = 20
144
148
while not spawned and wait_attempts > 0 :
@@ -160,7 +164,7 @@ def test_ldap_person_hook(build_image, network, containers):
160
164
search_base = "dc=example,dc=org"
161
165
search_filter = (
162
166
"(&(objectclass=Person)(telephoneNumber=23012303403)"
163
- "(SN=My Surname)(CN=" + user + "))"
167
+ "(SN=My Surname)(CN=" + username + "))"
164
168
)
165
169
166
170
conn_manager = ConnectionManager (
@@ -180,14 +184,14 @@ def test_ldap_person_hook(build_image, network, containers):
180
184
assert attributes ["objectClass" ] == ["person" ]
181
185
assert attributes ["telephoneNumber" ] == ["23012303403" ]
182
186
assert attributes ["sn" ] == ["My Surname" ]
183
- assert attributes ["cn" ] == [user ]
187
+ assert attributes ["cn" ] == [username ]
184
188
assert attributes ["description" ] == ["A default person account" ]
185
189
186
190
# Teardown notebook
187
191
user_container = client .containers .get (container_name )
188
192
resp = s .delete (
189
193
JHUB_URL + "/hub/api/users/{}/server" .format (user ),
190
- headers = {"Referer" : "127.0.0.1:8000 /hub/" },
194
+ headers = {"Referer" : "{} /hub/" . format ( JHUB_URL ) },
191
195
)
192
196
assert resp .status_code == 204
193
197
user_container .stop ()
@@ -226,7 +230,7 @@ def test_ldap_person_hook(build_image, network, containers):
226
230
),
227
231
docker_mount ,
228
232
],
229
- "ports" : {8000 : 8000 },
233
+ "ports" : {PORT : PORT },
230
234
"network" : LDAP_NETWORK_NAME ,
231
235
"detach" : "True" ,
232
236
"command" : "jupyterhub --debug -f " + jhub_target_config ,
@@ -243,18 +247,14 @@ def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
243
247
Test that the ldap_person_hook is able to create an LDAP DIT entry,
244
248
with a dynamic provided spawner attribute
245
249
"""
250
+ test_logger .info ("Start of ldap person dynamic attribute hook" )
246
251
client = docker .from_env ()
247
- containers = client .containers .list ()
248
- assert len (containers ) > 0
252
+ username = "a-new-dynamic-user"
253
+ auth_headers = {"Remote-User" : username }
254
+ assert wait_for_site (JHUB_URL , valid_status_code = 401 ) is True
249
255
with requests .Session () as s :
250
- ready = False
251
- while not ready :
252
- resp = s .get ("" .join ([JHUB_URL , "/hub/home" ]))
253
- if resp .status_code != 404 :
254
- ready = True
255
256
# Login
256
- user = "a-new-dynamic-user"
257
- login_response = s .post (JHUB_URL + "/hub/login" , headers = {"Remote-User" : user })
257
+ login_response = s .post (JHUB_URL + "/hub/login" , headers = auth_headers )
258
258
assert login_response .status_code == 200
259
259
260
260
resp = s .get (JHUB_URL + "/hub/home" )
@@ -265,7 +265,7 @@ def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
265
265
"/description="
266
266
+ desc
267
267
+ "/telephoneNumber=23012303403/SN=My Surname/CN="
268
- + user
268
+ + username
269
269
)
270
270
# Pass LDAP DN for creation on spawn
271
271
post_dn = s .post (
@@ -277,7 +277,7 @@ def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
277
277
spawn_response = s .post (JHUB_URL + "/hub/spawn" )
278
278
assert spawn_response .status_code == 200
279
279
280
- container_name = "jupyter-" + user
280
+ container_name = "jupyter-" + username
281
281
spawned = False
282
282
wait_attempts = 20
283
283
while not spawned and wait_attempts > 0 :
@@ -300,7 +300,7 @@ def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
300
300
"(&(objectclass=Person)(description="
301
301
+ desc
302
302
+ ")(telephoneNumber=23012303403)(SN=My Surname)(CN="
303
- + user
303
+ + username
304
304
+ "))"
305
305
)
306
306
@@ -321,7 +321,7 @@ def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
321
321
assert attributes ["objectClass" ] == ["person" ]
322
322
assert attributes ["telephoneNumber" ] == ["23012303403" ]
323
323
assert attributes ["sn" ] == ["My Surname" ]
324
- assert attributes ["cn" ] == [user ]
324
+ assert attributes ["cn" ] == [username ]
325
325
assert attributes ["description" ] == [desc ]
326
326
327
327
# Check that the notebook has the description env
@@ -334,8 +334,8 @@ def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
334
334
335
335
# Teardown notebook
336
336
resp = s .delete (
337
- JHUB_URL + "/hub/api/users/{}/server" .format (user ),
338
- headers = {"Referer" : "127.0.0.1:8000 /hub/" },
337
+ JHUB_URL + "/hub/api/users/{}/server" .format (username ),
338
+ headers = {"Referer" : "{} /hub/" . format ( JHUB_URL ) },
339
339
)
340
340
assert resp .status_code == 204
341
341
user_container .stop ()
@@ -374,7 +374,7 @@ def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
374
374
),
375
375
docker_mount ,
376
376
],
377
- "ports" : {8000 : 8000 },
377
+ "ports" : {PORT : PORT },
378
378
"network" : LDAP_NETWORK_NAME ,
379
379
"detach" : "True" ,
380
380
"command" : "jupyterhub --debug -f " + jhub_obj_spw_target_config ,
@@ -391,18 +391,14 @@ def test_dynamic_object_spawner_attributes(build_image, network, containers):
391
391
Test that the ldap_person_hook is able to create an LDAP DIT entry,
392
392
with a dynamic provided spawner attribute
393
393
"""
394
+ test_logger .info ("Start of ldap dynamic object spawner attributes testing" )
394
395
client = docker .from_env ()
395
- containers = client .containers .list ()
396
- assert len (containers ) > 0
396
+ username = "mynewuser"
397
+ auth_headers = {"Remote-User" : username }
398
+ assert wait_for_site (JHUB_URL , valid_status_code = 401 ) is True
397
399
with requests .Session () as s :
398
- ready = False
399
- while not ready :
400
- resp = s .get ("" .join ([JHUB_URL , "/hub/home" ]))
401
- if resp .status_code != 404 :
402
- ready = True
403
400
# Login
404
- user = "mynewuser"
405
- login_response = s .post (JHUB_URL + "/hub/login" , headers = {"Remote-User" : user })
401
+ login_response = s .post (JHUB_URL + "/hub/login" , headers = auth_headers )
406
402
assert login_response .status_code == 200
407
403
408
404
resp = s .get (JHUB_URL + "/hub/home" )
@@ -413,7 +409,7 @@ def test_dynamic_object_spawner_attributes(build_image, network, containers):
413
409
"/description="
414
410
+ desc
415
411
+ "/telephoneNumber=23012303403/SN=My Surname/CN="
416
- + user
412
+ + username
417
413
)
418
414
# Pass LDAP DN for creation on spawn
419
415
post_dn = s .post (
@@ -425,7 +421,7 @@ def test_dynamic_object_spawner_attributes(build_image, network, containers):
425
421
spawn_response = s .post (JHUB_URL + "/hub/spawn" )
426
422
assert spawn_response .status_code == 200
427
423
428
- container_name = "jupyter-" + user
424
+ container_name = "jupyter-" + username
429
425
spawned = False
430
426
wait_attempts = 20
431
427
while not spawned and wait_attempts > 0 :
@@ -438,12 +434,12 @@ def test_dynamic_object_spawner_attributes(build_image, network, containers):
438
434
# Check that the notebook has the NB_USER env
439
435
user_container = client .containers .get (container_name )
440
436
container_nbuser = user_container .attrs ["Config" ]["Env" ][0 ]
441
- assert "NB_USER=" + user == container_nbuser
437
+ assert "NB_USER=" + username == container_nbuser
442
438
443
439
# Teardown notebook
444
440
resp = s .delete (
445
- JHUB_URL + "/hub/api/users/{}/server" .format (user ),
446
- headers = {"Referer" : "127.0.0.1:8000 /hub/" },
441
+ JHUB_URL + "/hub/api/users/{}/server" .format (username ),
442
+ headers = {"Referer" : "{} /hub/" . format ( JHUB_URL ) },
447
443
)
448
444
assert resp .status_code == 204
449
445
user_container .stop ()
@@ -474,16 +470,16 @@ def test_dynamic_object_spawner_attributes(build_image, network, containers):
474
470
# Validate that the env is still correct
475
471
user_container = client .containers .get (container_name )
476
472
container_nbuser = user_container .attrs ["Config" ]["Env" ][0 ]
477
- assert "NB_USER=" + user == container_nbuser
473
+ assert "NB_USER=" + username == container_nbuser
478
474
479
475
# Validate that the ldap DIT still only has 1 entry
480
476
search_base = "dc=example,dc=org"
481
477
search_filter = (
482
478
"(&(objectclass=inetOrgPerson)"
483
479
"(objectclass=posixAccount)(uid="
484
- + user
480
+ + username
485
481
+ ")(telephoneNumber=23012303403)(SN=My Surname)(CN="
486
- + user
482
+ + username
487
483
+ "))"
488
484
)
489
485
@@ -507,13 +503,13 @@ def test_dynamic_object_spawner_attributes(build_image, network, containers):
507
503
assert attributes ["objectClass" ] == ["inetOrgPerson" , "posixAccount" ]
508
504
assert attributes ["telephoneNumber" ] == ["23012303403" ]
509
505
assert attributes ["sn" ] == ["My Surname" ]
510
- assert attributes ["cn" ] == [user ]
511
- assert attributes ["uid" ] == [user ]
506
+ assert attributes ["cn" ] == [username ]
507
+ assert attributes ["uid" ] == [username ]
512
508
513
509
# Teardown notebook
514
510
resp = s .delete (
515
- JHUB_URL + "/hub/api/users/{}/server" .format (user ),
516
- headers = {"Referer" : "127.0.0.1:8000 /hub/" },
511
+ JHUB_URL + "/hub/api/users/{}/server" .format (username ),
512
+ headers = {"Referer" : "{} /hub/" . format ( JHUB_URL ) },
517
513
)
518
514
assert resp .status_code == 204
519
515
user_container .stop ()
0 commit comments