|
| 1 | +""" |
| 2 | +tests to be run with pytest inside the container |
| 3 | +
|
| 4 | +can't be called test_whatever.py because then _host_ pytest will try to run it! |
| 5 | +""" |
| 6 | + |
| 7 | +import json |
| 8 | +import os |
| 9 | +import shutil |
| 10 | +from subprocess import check_output |
| 11 | + |
| 12 | +from pytest import fixture |
| 13 | + |
| 14 | +kernel_prefix = os.environ.get("KERNEL_PYTHON_PREFIX") |
| 15 | +server_prefix = os.environ.get("NB_PYTHON_PREFIX") |
| 16 | + |
| 17 | + |
| 18 | +def json_cmd(cmd): |
| 19 | + """Run a command and decode its JSON output""" |
| 20 | + out = check_output(cmd) |
| 21 | + return json.loads(out.decode("utf8", "replace")) |
| 22 | + |
| 23 | + |
| 24 | +def conda_pkgs(prefix): |
| 25 | + """Conda package list as a dict""" |
| 26 | + conda_json = json_cmd(["conda", "list", "--json", "-p", prefix]) |
| 27 | + return {pkg["name"]: pkg for pkg in conda_json} |
| 28 | + |
| 29 | + |
| 30 | +def pip_pkgs(prefix): |
| 31 | + """Pip package list as a dict""" |
| 32 | + pip_json = json_cmd([f"{prefix}/bin/pip", "list", "--format=json"]) |
| 33 | + return {pkg["name"]: pkg for pkg in pip_json} |
| 34 | + |
| 35 | + |
| 36 | +@fixture(scope="session") |
| 37 | +def kernel_conda(): |
| 38 | + return conda_pkgs(kernel_prefix) |
| 39 | + |
| 40 | + |
| 41 | +@fixture(scope="session") |
| 42 | +def server_conda(): |
| 43 | + return conda_pkgs(server_prefix) |
| 44 | + |
| 45 | + |
| 46 | +@fixture(scope="session") |
| 47 | +def kernel_pip(): |
| 48 | + return pip_pkgs(kernel_prefix) |
| 49 | + |
| 50 | + |
| 51 | +@fixture(scope="session") |
| 52 | +def server_pip(): |
| 53 | + return pip_pkgs(server_prefix) |
| 54 | + |
| 55 | + |
| 56 | +def test_which_python(): |
| 57 | + # server python comes first. Is this expected? |
| 58 | + assert shutil.which("python3") == f"{server_prefix}/bin/python3" |
| 59 | + |
| 60 | + |
| 61 | +def test_kernel_env(kernel_conda): |
| 62 | + assert kernel_prefix != server_prefix |
| 63 | + kernel_python = kernel_conda["python"]["version"] |
| 64 | + assert kernel_python[:3] == "3.6" |
| 65 | + # test environment.yml packages |
| 66 | + assert "numpy" in kernel_conda |
| 67 | + |
| 68 | + |
| 69 | +def test_server_env(server_conda): |
| 70 | + # this should be the default version |
| 71 | + # it will need updating when the default changes |
| 72 | + assert server_conda["python"]["version"][:3] == "3.7" |
| 73 | + |
| 74 | + |
| 75 | +def test_conda_install(kernel_conda, server_conda): |
| 76 | + # test that postBuild conda install went in the kernel env |
| 77 | + assert "make" in kernel_conda |
| 78 | + assert "make" not in server_conda |
| 79 | + |
| 80 | + |
| 81 | +def test_pip_install(kernel_pip, server_pip): |
| 82 | + # server env comes first for pip |
| 83 | + # is this expected? |
| 84 | + assert "pytest" not in kernel_pip |
| 85 | + assert "pytest" in server_pip |
0 commit comments