test refactor: update simplest plugin tests

With this refactor, the simplest plugin stops interfering with other
tests.
This commit is contained in:
Erik Sundell
2023-06-08 22:44:21 +02:00
parent 971b7d5c7e
commit 40b88cb780
2 changed files with 60 additions and 62 deletions

View File

@@ -1,55 +1,47 @@
""" """
Simplest plugin that exercises all the hooks Simplest plugin that exercises all the hooks defined in tljh/hooks.py.
""" """
from tljh.hooks import hookimpl from tljh.hooks import hookimpl
@hookimpl @hookimpl
def tljh_extra_user_conda_packages(): def tljh_extra_user_conda_packages():
return [ return ["tqdm"]
"hypothesis",
]
@hookimpl @hookimpl
def tljh_extra_user_pip_packages(): def tljh_extra_user_pip_packages():
return [ return ["django"]
"django",
]
@hookimpl @hookimpl
def tljh_extra_hub_pip_packages(): def tljh_extra_hub_pip_packages():
return [ return ["there"]
"there",
]
@hookimpl @hookimpl
def tljh_extra_apt_packages(): def tljh_extra_apt_packages():
return [ return ["sl"]
"sl",
]
@hookimpl
def tljh_config_post_install(config):
# Put an arbitrary marker we can test for
config["simplest_plugin"] = {"present": True}
@hookimpl @hookimpl
def tljh_custom_jupyterhub_config(c): def tljh_custom_jupyterhub_config(c):
c.JupyterHub.authenticator_class = "tmpauthenticator.TmpAuthenticator" c.Test.jupyterhub_config_set_by_simplest_plugin = True
@hookimpl
def tljh_config_post_install(config):
config["Test"] = {"tljh_config_set_by_simplest_plugin": True}
@hookimpl @hookimpl
def tljh_post_install(): def tljh_post_install():
with open("test_post_install", "w") as f: with open("test_tljh_post_install", "w") as f:
f.write("123456789") f.write("file_written_by_simplest_plugin")
@hookimpl @hookimpl
def tljh_new_user_create(username): def tljh_new_user_create(username):
with open("test_new_user_create", "w") as f: with open("test_new_user_create", "w") as f:
f.write("file_written_by_simplest_plugin")
f.write(username) f.write(username)

View File

@@ -1,79 +1,85 @@
""" """
Test simplest plugin Test the plugin in integration-tests/plugins/simplest that makes use of all tljh
recognized plugin hooks that are defined in tljh/hooks.py.
""" """
import os import os
import subprocess import subprocess
import requests
from ruamel.yaml import YAML from ruamel.yaml import YAML
from tljh import user from tljh import user
from tljh.config import CONFIG_FILE, HUB_ENV_PREFIX, USER_ENV_PREFIX from tljh.config import CONFIG_FILE, HUB_ENV_PREFIX, USER_ENV_PREFIX
GIT_REPO_PATH = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
yaml = YAML(typ="rt") yaml = YAML(typ="rt")
def test_apt_packages(): def test_tljh_extra_user_conda_packages():
""" subprocess.check_call([f"{USER_ENV_PREFIX}/bin/python3", "-c", "import tqdm"])
Test extra apt packages are installed
"""
assert os.path.exists("/usr/games/sl")
def test_pip_packages(): def test_tljh_extra_user_pip_packages():
"""
Test extra user & hub pip packages are installed
"""
subprocess.check_call([f"{USER_ENV_PREFIX}/bin/python3", "-c", "import django"]) subprocess.check_call([f"{USER_ENV_PREFIX}/bin/python3", "-c", "import django"])
def test_tljh_extra_hub_pip_packages():
subprocess.check_call([f"{HUB_ENV_PREFIX}/bin/python3", "-c", "import there"]) subprocess.check_call([f"{HUB_ENV_PREFIX}/bin/python3", "-c", "import there"])
def test_conda_packages(): def test_tljh_extra_apt_packages():
""" assert os.path.exists("/usr/games/sl")
Test extra user conda packages are installed
"""
subprocess.check_call([f"{USER_ENV_PREFIX}/bin/python3", "-c", "import hypothesis"])
def test_config_hook(): def test_tljh_custom_jupyterhub_config():
""" """
Check config changes are present Test that the provided tljh_custom_jupyterhub_config hook has made the tljh
jupyterhub load additional jupyterhub config.
"""
tljh_jupyterhub_config = os.path.join(GIT_REPO_PATH, "tljh", "jupyterhub_config.py")
output = subprocess.check_output(
[
f"{HUB_ENV_PREFIX}/bin/python3",
"-m",
"jupyterhub",
"--show-config",
"--config",
tljh_jupyterhub_config,
],
text=True,
)
assert "jupyterhub_config_set_by_simplest_plugin" in output
def test_tljh_config_post_install():
"""
Test that the provided tljh_config_post_install hook has made tljh recognize
additional tljh config.
""" """
with open(CONFIG_FILE) as f: with open(CONFIG_FILE) as f:
data = yaml.load(f) tljh_config = yaml.load(f)
assert tljh_config["Test"]["tljh_config_set_by_simplest_plugin"]
assert data["simplest_plugin"]["present"]
def test_jupyterhub_config_hook(): def test_tljh_post_install():
""" """
Test that tmpauthenticator is enabled by our custom config plugin Test that the provided tljh_post_install hook has been executed by looking
for a specific file written.
""" """
resp = requests.get("http://localhost/hub/tmplogin", allow_redirects=False) with open("test_tljh_post_install") as f:
assert resp.status_code == 302
assert resp.headers["Location"] == "/hub/spawn"
def test_post_install_hook():
"""
Test that the test_post_install file has the correct content
"""
with open("test_post_install") as f:
content = f.read() content = f.read()
assert "file_written_by_simplest_plugin" in content
assert content == "123456789"
def test_new_user_create(): def test_tljh_new_user_create():
""" """
Test that plugin receives username as arg Test that the provided tljh_new_user_create hook has been executed by
looking for a specific file written.
""" """
# Trigger the hook by letting tljh's code create a user
username = "user1" username = "user1"
# Call ensure_user to make sure the user plugin gets called
user.ensure_user(username) user.ensure_user(username)
with open("test_new_user_create") as f: with open("test_new_user_create") as f:
content = f.read() content = f.read()
assert "file_written_by_simplest_plugin" in content
assert content == username assert username in content