Skip to content

Commit ecd583c

Browse files
committed
Bump the package requirements and simplify a bit on the tests
1 parent 3dcb165 commit ecd583c

File tree

5 files changed

+125
-67
lines changed

5 files changed

+125
-67
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ maintainer-clean:
4545
@echo 'deletes files that may need special tools to rebuild.'
4646
$(MAKE) distclean
4747

48+
install-dev:
49+
$(VENV)/pip install -r requirements-dev.txt
50+
4851
install-dep:
4952
$(VENV)/pip install -r requirements.txt
5053

@@ -54,6 +57,7 @@ install:
5457

5558
uninstall:
5659
$(VENV)/pip uninstall -y -r requirements.txt
60+
$(VENV)/pip uninstall -y -r requirements-dev.txt
5761
$(VENV)/pip uninstall -y -r $(PACKAGE_NAME)
5862

5963
installcheck:

requirements-dev.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
black==20.8b1
2-
docutils==0.16
3-
flake8==3.8.4
1+
black==22.3.0
2+
docutils==0.18.1
3+
flake8==4.0.1

tests/requirements.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
requests==2.21.0
2-
docker==3.4.0
3-
docutils==0.14
4-
flake8==3.7.*
5-
autopep8==1.4.*
1+
requests==2.27.1
2+
docker==5.0.3
3+
docutils==0.18.1
4+
flake8==4.0.*
5+
autopep8==1.6.*
66
gen==0.1
7-
pytest==6.1.*
7+
pytest==7.1.1

tests/test_ldap_hooks.py

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
import requests
21
import docker
2+
import os
3+
import requests
4+
import logging
35
import pytest
46
from ldap3 import ALL_ATTRIBUTES
57
from ldap_hooks import search_for, ConnectionManager
68
from os.path import join, dirname, realpath
79
from docker.types import Mount
810
from docker.errors import NotFound
11+
from .util import wait_for_site
912

1013
# root dir
1114
docker_path = dirname(dirname(realpath(__file__)))
1215

16+
# Logger
17+
logging.basicConfig(level=logging.INFO)
18+
test_logger = logging.getLogger()
19+
1320
JHUB_IMAGE_NAME = "jupyterhub-ldap-hooks"
1421
JHUB_IMAGE_TAG = "test"
1522
JHUB_IMAGE = "".join([JHUB_IMAGE_NAME, ":", JHUB_IMAGE_TAG])
23+
PORT = 8000
1624

1725
jhub_image_spec = {"path": docker_path, "tag": JHUB_IMAGE, "rm": "True", "pull": "True"}
1826

@@ -21,14 +29,14 @@
2129
LDAP_IMAGE_TAG = "1.2.3"
2230
LDAP_IMAGE = "".join([LDAP_IMAGE_PATH, ":", LDAP_IMAGE_TAG])
2331

24-
JHUB_URL = "http://127.0.0.1:8000"
32+
JHUB_URL = "http://127.0.0.1:{}".format(PORT)
2533
LDAP_URL = "http://openldap"
2634

2735
# Config setup
2836
config_path = join(dirname(realpath(__file__)), "configs")
2937

3038
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")
3240

3341
ldap_schema = join(config_path, "openldap", "mount_schema")
3442
ldap_target_schema = "/container/service/slapd/assets/config/bootstrap/schema"
@@ -44,8 +52,8 @@
4452
}
4553

4654
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"),
4957
read_only=True,
5058
type="bind",
5159
)
@@ -63,7 +71,7 @@
6371
),
6472
docker_mount,
6573
],
66-
"ports": {8000: 8000},
74+
"ports": {PORT: PORT},
6775
"network": LDAP_NETWORK_NAME,
6876
"detach": "True",
6977
"command": "jupyterhub --debug -f " + jhub_target_config,
@@ -110,24 +118,20 @@ def test_ldap_person_hook(build_image, network, containers):
110118
Test that the ldap_person_hook is able to create an LDAP DIT entry,
111119
with the provided JupyterHub Spawner attribute.
112120
"""
121+
test_logger.info("Start of ldap person hook testing")
113122
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
116126
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
122127
# 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)
125129
assert login_response.status_code == 200
126130

127131
resp = s.get(JHUB_URL + "/hub/home")
128132
assert resp.status_code == 200
129133

130-
dn_str = "/telephoneNumber=23012303403/SN=My Surname/CN=" + user
134+
dn_str = "/telephoneNumber=23012303403/SN=My Surname/CN=" + username
131135
# Pass LDAP DN for creation on spawn
132136
post_dn = s.post(
133137
JHUB_URL + "/hub/user-data", json={"data": {"PersonDN": dn_str}}
@@ -138,7 +142,7 @@ def test_ldap_person_hook(build_image, network, containers):
138142
spawn_response = s.post(JHUB_URL + "/hub/spawn")
139143
assert spawn_response.status_code == 200
140144

141-
container_name = "jupyter-" + user
145+
container_name = "jupyter-" + username
142146
spawned = False
143147
wait_attempts = 20
144148
while not spawned and wait_attempts > 0:
@@ -160,7 +164,7 @@ def test_ldap_person_hook(build_image, network, containers):
160164
search_base = "dc=example,dc=org"
161165
search_filter = (
162166
"(&(objectclass=Person)(telephoneNumber=23012303403)"
163-
"(SN=My Surname)(CN=" + user + "))"
167+
"(SN=My Surname)(CN=" + username + "))"
164168
)
165169

166170
conn_manager = ConnectionManager(
@@ -180,14 +184,14 @@ def test_ldap_person_hook(build_image, network, containers):
180184
assert attributes["objectClass"] == ["person"]
181185
assert attributes["telephoneNumber"] == ["23012303403"]
182186
assert attributes["sn"] == ["My Surname"]
183-
assert attributes["cn"] == [user]
187+
assert attributes["cn"] == [username]
184188
assert attributes["description"] == ["A default person account"]
185189

186190
# Teardown notebook
187191
user_container = client.containers.get(container_name)
188192
resp = s.delete(
189193
JHUB_URL + "/hub/api/users/{}/server".format(user),
190-
headers={"Referer": "127.0.0.1:8000/hub/"},
194+
headers={"Referer": "{}/hub/".format(JHUB_URL)},
191195
)
192196
assert resp.status_code == 204
193197
user_container.stop()
@@ -226,7 +230,7 @@ def test_ldap_person_hook(build_image, network, containers):
226230
),
227231
docker_mount,
228232
],
229-
"ports": {8000: 8000},
233+
"ports": {PORT: PORT},
230234
"network": LDAP_NETWORK_NAME,
231235
"detach": "True",
232236
"command": "jupyterhub --debug -f " + jhub_target_config,
@@ -243,18 +247,14 @@ def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
243247
Test that the ldap_person_hook is able to create an LDAP DIT entry,
244248
with a dynamic provided spawner attribute
245249
"""
250+
test_logger.info("Start of ldap person dynamic attribute hook")
246251
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
249255
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
255256
# 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)
258258
assert login_response.status_code == 200
259259

260260
resp = s.get(JHUB_URL + "/hub/home")
@@ -265,7 +265,7 @@ def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
265265
"/description="
266266
+ desc
267267
+ "/telephoneNumber=23012303403/SN=My Surname/CN="
268-
+ user
268+
+ username
269269
)
270270
# Pass LDAP DN for creation on spawn
271271
post_dn = s.post(
@@ -277,7 +277,7 @@ def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
277277
spawn_response = s.post(JHUB_URL + "/hub/spawn")
278278
assert spawn_response.status_code == 200
279279

280-
container_name = "jupyter-" + user
280+
container_name = "jupyter-" + username
281281
spawned = False
282282
wait_attempts = 20
283283
while not spawned and wait_attempts > 0:
@@ -300,7 +300,7 @@ def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
300300
"(&(objectclass=Person)(description="
301301
+ desc
302302
+ ")(telephoneNumber=23012303403)(SN=My Surname)(CN="
303-
+ user
303+
+ username
304304
+ "))"
305305
)
306306

@@ -321,7 +321,7 @@ def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
321321
assert attributes["objectClass"] == ["person"]
322322
assert attributes["telephoneNumber"] == ["23012303403"]
323323
assert attributes["sn"] == ["My Surname"]
324-
assert attributes["cn"] == [user]
324+
assert attributes["cn"] == [username]
325325
assert attributes["description"] == [desc]
326326

327327
# Check that the notebook has the description env
@@ -334,8 +334,8 @@ def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
334334

335335
# Teardown notebook
336336
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)},
339339
)
340340
assert resp.status_code == 204
341341
user_container.stop()
@@ -374,7 +374,7 @@ def test_ldap_person_dynamic_attr_hook(build_image, network, containers):
374374
),
375375
docker_mount,
376376
],
377-
"ports": {8000: 8000},
377+
"ports": {PORT: PORT},
378378
"network": LDAP_NETWORK_NAME,
379379
"detach": "True",
380380
"command": "jupyterhub --debug -f " + jhub_obj_spw_target_config,
@@ -391,18 +391,14 @@ def test_dynamic_object_spawner_attributes(build_image, network, containers):
391391
Test that the ldap_person_hook is able to create an LDAP DIT entry,
392392
with a dynamic provided spawner attribute
393393
"""
394+
test_logger.info("Start of ldap dynamic object spawner attributes testing")
394395
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
397399
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
403400
# 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)
406402
assert login_response.status_code == 200
407403

408404
resp = s.get(JHUB_URL + "/hub/home")
@@ -413,7 +409,7 @@ def test_dynamic_object_spawner_attributes(build_image, network, containers):
413409
"/description="
414410
+ desc
415411
+ "/telephoneNumber=23012303403/SN=My Surname/CN="
416-
+ user
412+
+ username
417413
)
418414
# Pass LDAP DN for creation on spawn
419415
post_dn = s.post(
@@ -425,7 +421,7 @@ def test_dynamic_object_spawner_attributes(build_image, network, containers):
425421
spawn_response = s.post(JHUB_URL + "/hub/spawn")
426422
assert spawn_response.status_code == 200
427423

428-
container_name = "jupyter-" + user
424+
container_name = "jupyter-" + username
429425
spawned = False
430426
wait_attempts = 20
431427
while not spawned and wait_attempts > 0:
@@ -438,12 +434,12 @@ def test_dynamic_object_spawner_attributes(build_image, network, containers):
438434
# Check that the notebook has the NB_USER env
439435
user_container = client.containers.get(container_name)
440436
container_nbuser = user_container.attrs["Config"]["Env"][0]
441-
assert "NB_USER=" + user == container_nbuser
437+
assert "NB_USER=" + username == container_nbuser
442438

443439
# Teardown notebook
444440
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)},
447443
)
448444
assert resp.status_code == 204
449445
user_container.stop()
@@ -474,16 +470,16 @@ def test_dynamic_object_spawner_attributes(build_image, network, containers):
474470
# Validate that the env is still correct
475471
user_container = client.containers.get(container_name)
476472
container_nbuser = user_container.attrs["Config"]["Env"][0]
477-
assert "NB_USER=" + user == container_nbuser
473+
assert "NB_USER=" + username == container_nbuser
478474

479475
# Validate that the ldap DIT still only has 1 entry
480476
search_base = "dc=example,dc=org"
481477
search_filter = (
482478
"(&(objectclass=inetOrgPerson)"
483479
"(objectclass=posixAccount)(uid="
484-
+ user
480+
+ username
485481
+ ")(telephoneNumber=23012303403)(SN=My Surname)(CN="
486-
+ user
482+
+ username
487483
+ "))"
488484
)
489485

@@ -507,13 +503,13 @@ def test_dynamic_object_spawner_attributes(build_image, network, containers):
507503
assert attributes["objectClass"] == ["inetOrgPerson", "posixAccount"]
508504
assert attributes["telephoneNumber"] == ["23012303403"]
509505
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]
512508

513509
# Teardown notebook
514510
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)},
517513
)
518514
assert resp.status_code == 204
519515
user_container.stop()

0 commit comments

Comments
 (0)