From 18cbf55b90c27a977ca299a82048310e9c7816ee Mon Sep 17 00:00:00 2001 From: Min RK Date: Fri, 3 Feb 2023 15:28:37 +0100 Subject: [PATCH] test postBuild behavior with legacy Python 3.6 --- tests/conda/py36-postBuild/environment.yml | 3 + tests/conda/py36-postBuild/postBuild | 9 +++ tests/conda/py36-postBuild/verify | 3 + tests/conda/py36-postBuild/verify.py | 85 ++++++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 tests/conda/py36-postBuild/environment.yml create mode 100755 tests/conda/py36-postBuild/postBuild create mode 100755 tests/conda/py36-postBuild/verify create mode 100644 tests/conda/py36-postBuild/verify.py diff --git a/tests/conda/py36-postBuild/environment.yml b/tests/conda/py36-postBuild/environment.yml new file mode 100644 index 000000000..c6ce14436 --- /dev/null +++ b/tests/conda/py36-postBuild/environment.yml @@ -0,0 +1,3 @@ +dependencies: + - python=3.6 + - numpy diff --git a/tests/conda/py36-postBuild/postBuild b/tests/conda/py36-postBuild/postBuild new file mode 100755 index 000000000..cf0db0125 --- /dev/null +++ b/tests/conda/py36-postBuild/postBuild @@ -0,0 +1,9 @@ +#!/bin/bash +set -ex + +# mamba/conda installs in kernel env +mamba install -y make + +# note `pip` on path is _not_ the kernel env! +# is this what we (or users) want? +pip install pytest diff --git a/tests/conda/py36-postBuild/verify b/tests/conda/py36-postBuild/verify new file mode 100755 index 000000000..e576d7ab5 --- /dev/null +++ b/tests/conda/py36-postBuild/verify @@ -0,0 +1,3 @@ +#!/bin/bash +set -ex +pytest -vs ./verify.py diff --git a/tests/conda/py36-postBuild/verify.py b/tests/conda/py36-postBuild/verify.py new file mode 100644 index 000000000..806a5c9a5 --- /dev/null +++ b/tests/conda/py36-postBuild/verify.py @@ -0,0 +1,85 @@ +""" +tests to be run with pytest inside the container + +can't be called test_whatever.py because then _host_ pytest will try to run it! +""" + +import json +import os +import shutil +from subprocess import check_output + +from pytest import fixture + +kernel_prefix = os.environ.get("KERNEL_PYTHON_PREFIX") +server_prefix = os.environ.get("NB_PYTHON_PREFIX") + + +def json_cmd(cmd): + """Run a command and decode its JSON output""" + out = check_output(cmd) + return json.loads(out.decode("utf8", "replace")) + + +def conda_pkgs(prefix): + """Conda package list as a dict""" + conda_json = json_cmd(["conda", "list", "--json", "-p", prefix]) + return {pkg["name"]: pkg for pkg in conda_json} + + +def pip_pkgs(prefix): + """Pip package list as a dict""" + pip_json = json_cmd([f"{prefix}/bin/pip", "list", "--format=json"]) + return {pkg["name"]: pkg for pkg in pip_json} + + +@fixture(scope="session") +def kernel_conda(): + return conda_pkgs(kernel_prefix) + + +@fixture(scope="session") +def server_conda(): + return conda_pkgs(server_prefix) + + +@fixture(scope="session") +def kernel_pip(): + return pip_pkgs(kernel_prefix) + + +@fixture(scope="session") +def server_pip(): + return pip_pkgs(server_prefix) + + +def test_which_python(): + # server python comes first. Is this expected? + assert shutil.which("python3") == f"{server_prefix}/bin/python3" + + +def test_kernel_env(kernel_conda): + assert kernel_prefix != server_prefix + kernel_python = kernel_conda["python"]["version"] + assert kernel_python[:3] == "3.6" + # test environment.yml packages + assert "numpy" in kernel_conda + + +def test_server_env(server_conda): + # this should be the default version + # it will need updating when the default changes + assert server_conda["python"]["version"][:3] == "3.7" + + +def test_conda_install(kernel_conda, server_conda): + # test that postBuild conda install went in the kernel env + assert "make" in kernel_conda + assert "make" not in server_conda + + +def test_pip_install(kernel_pip, server_pip): + # server env comes first for pip + # is this expected? + assert "pytest" not in kernel_pip + assert "pytest" in server_pip