From 30a1d17085cce7a49b67d70b543e83485bfca502 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sat, 26 Jun 2021 14:40:45 +0300 Subject: [PATCH 01/17] Add a way to easily test units --- .../test/test_spark_notebooks.py | 3 +- base-notebook/test/test_package_managers.py | 11 ++++--- base-notebook/test/test_pandoc.py | 3 +- base-notebook/test/test_start_container.py | 6 +++- datascience-notebook/test/test_julia.py | 3 +- minimal-notebook/test/test_nbconvert.py | 16 ++++++--- pyspark-notebook/test/test_spark.py | 12 ------- pyspark-notebook/test/units/unit_spark.py | 4 +++ requirements-dev.txt | 1 + scipy-notebook/test/test_matplotlib.py | 18 +++++----- scipy-notebook/test/test_pandas.py | 32 ------------------ scipy-notebook/test/units/unit_pandas.py | 9 +++++ tensorflow-notebook/test/test_tensorflow.py | 30 ----------------- .../test/units/unit_tensorflow.py | 7 ++++ test/helpers.py | 3 +- test/test_units.py | 33 +++++++++++++++++++ 16 files changed, 93 insertions(+), 98 deletions(-) create mode 100644 pyspark-notebook/test/units/unit_spark.py delete mode 100644 scipy-notebook/test/test_pandas.py create mode 100644 scipy-notebook/test/units/unit_pandas.py delete mode 100644 tensorflow-notebook/test/test_tensorflow.py create mode 100644 tensorflow-notebook/test/units/unit_tensorflow.py create mode 100644 test/test_units.py diff --git a/all-spark-notebook/test/test_spark_notebooks.py b/all-spark-notebook/test/test_spark_notebooks.py index 2e232daf4..1a1d85281 100644 --- a/all-spark-notebook/test/test_spark_notebooks.py +++ b/all-spark-notebook/test/test_spark_notebooks.py @@ -7,6 +7,7 @@ import os LOGGER = logging.getLogger(__name__) +THIS_DIR = os.path.dirname(os.path.realpath(__file__)) @pytest.mark.parametrize( @@ -16,7 +17,7 @@ ) def test_nbconvert(container, test_file): """Check if Spark notebooks can be executed""" - host_data_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data") + host_data_dir = os.path.join(THIS_DIR, "data") cont_data_dir = "/home/jovyan/data" output_dir = "/tmp" timeout_ms = 600 diff --git a/base-notebook/test/test_package_managers.py b/base-notebook/test/test_package_managers.py index ebd8f294d..060706e3b 100644 --- a/base-notebook/test/test_package_managers.py +++ b/base-notebook/test/test_package_managers.py @@ -8,22 +8,23 @@ @pytest.mark.parametrize( - "package_manager, cmd", + "package_manager, version_arg", [ ("apt", "--version"), ("conda", "--version"), ("mamba", "--version"), ("npm", "--version"), - ("pip", "--version"), - ], + ("pip", "--version") + ] ) -def test_package_manager(container, package_manager, cmd): +def test_package_manager(container, package_manager, version_arg): """Test the notebook start-notebook script""" LOGGER.info( f"Test that the package manager {package_manager} is working properly ..." ) c = container.run( - tty=True, command=["start.sh", "bash", "-c", f"{package_manager} {cmd}"] + tty=True, + command=["start.sh", "bash", "-c", f"{package_manager} {version_arg}"] ) rv = c.wait(timeout=5) logs = c.logs(stdout=True).decode("utf-8") diff --git a/base-notebook/test/test_pandoc.py b/base-notebook/test/test_pandoc.py index c84787c11..bc128921a 100644 --- a/base-notebook/test/test_pandoc.py +++ b/base-notebook/test/test_pandoc.py @@ -9,7 +9,8 @@ def test_pandoc(container): """Pandoc shall be able to convert MD to HTML.""" c = container.run( - tty=True, command=["start.sh", "bash", "-c", 'echo "**BOLD**" | pandoc'] + tty=True, + command=["start.sh", "bash", "-c", 'echo "**BOLD**" | pandoc'] ) c.wait(timeout=10) logs = c.logs(stdout=True).decode("utf-8") diff --git a/base-notebook/test/test_start_container.py b/base-notebook/test/test_start_container.py index f55b55f68..3ffd4356f 100644 --- a/base-notebook/test/test_start_container.py +++ b/base-notebook/test/test_start_container.py @@ -8,7 +8,11 @@ @pytest.mark.parametrize( - "env,expected_server", [(["JUPYTER_ENABLE_LAB=yes"], "lab"), (None, "notebook"), ], + "env,expected_server", + [ + (["JUPYTER_ENABLE_LAB=yes"], "lab"), + (None, "notebook") + ] ) def test_start_notebook(container, http_client, env, expected_server): """Test the notebook start-notebook script""" diff --git a/datascience-notebook/test/test_julia.py b/datascience-notebook/test/test_julia.py index eaabf559d..5fa631a8f 100644 --- a/datascience-notebook/test/test_julia.py +++ b/datascience-notebook/test/test_julia.py @@ -9,7 +9,8 @@ def test_julia(container): """Basic julia test""" LOGGER.info("Test that julia is correctly installed ...") running_container = container.run( - tty=True, command=["start.sh", "bash", "-c", "sleep infinity"] + tty=True, + command=["start.sh", "bash", "-c", "sleep infinity"] ) command = "julia --version" cmd = running_container.exec_run(command) diff --git a/minimal-notebook/test/test_nbconvert.py b/minimal-notebook/test/test_nbconvert.py index a9f3093b7..e0f45d547 100644 --- a/minimal-notebook/test/test_nbconvert.py +++ b/minimal-notebook/test/test_nbconvert.py @@ -7,15 +7,21 @@ import os LOGGER = logging.getLogger(__name__) +THIS_DIR = os.path.dirname(os.path.realpath(__file__)) -@pytest.mark.parametrize("test_file, output_format,", [ - ("notebook_math", "pdf"), ("notebook_math", "html"), - ("notebook_svg", "pdf"), ("notebook_svg", "html"), -]) +@pytest.mark.parametrize( + "test_file, output_format", + [ + ("notebook_math", "pdf"), + ("notebook_math", "html"), + ("notebook_svg", "pdf"), + ("notebook_svg", "html") + ] +) def test_nbconvert(container, test_file, output_format): """Check if nbconvert is able to convert a notebook file""" - host_data_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data") + host_data_dir = os.path.join(THIS_DIR, "data") cont_data_dir = "/home/jovyan/data" output_dir = "/tmp" LOGGER.info(f"Test that the example notebook {test_file} can be converted to {output_format.upper()} ...") diff --git a/pyspark-notebook/test/test_spark.py b/pyspark-notebook/test/test_spark.py index 09d744810..a98a26665 100644 --- a/pyspark-notebook/test/test_spark.py +++ b/pyspark-notebook/test/test_spark.py @@ -16,15 +16,3 @@ def test_spark_shell(container): logs = c.logs(stdout=True).decode('utf-8') LOGGER.debug(logs) assert 'res0: Int = 2' in logs, "spark-shell does not work" - - -def test_pyspark(container): - """PySpark should be in the Python path""" - c = container.run( - tty=True, - command=['start.sh', 'python', '-c', 'import pyspark'] - ) - rv = c.wait(timeout=30) - assert rv == 0 or rv["StatusCode"] == 0, "pyspark not in PYTHONPATH" - logs = c.logs(stdout=True).decode('utf-8') - LOGGER.debug(logs) diff --git a/pyspark-notebook/test/units/unit_spark.py b/pyspark-notebook/test/units/unit_spark.py new file mode 100644 index 000000000..80b47d439 --- /dev/null +++ b/pyspark-notebook/test/units/unit_spark.py @@ -0,0 +1,4 @@ +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. + +import pyspark # noqa: F401 diff --git a/requirements-dev.txt b/requirements-dev.txt index a82294f2d..7eb7a7cdb 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,5 +1,6 @@ docker myst-parser +packaging plumbum pre-commit pytest diff --git a/scipy-notebook/test/test_matplotlib.py b/scipy-notebook/test/test_matplotlib.py index 34a63f79b..ac6d7ceb9 100644 --- a/scipy-notebook/test/test_matplotlib.py +++ b/scipy-notebook/test/test_matplotlib.py @@ -7,23 +7,23 @@ import os LOGGER = logging.getLogger(__name__) +THIS_DIR = os.path.dirname(os.path.realpath(__file__)) -@pytest.mark.parametrize("test_file,expected_file,description", - [ - ("matplotlib_1.py", "test.png", - "Test that matplotlib is able to plot a graph and write it as an image ..."), - ("matplotlib_fonts_1.py", "test_fonts.png", - "Test cm-super latex labels in matplotlib ...") - ]) +@pytest.mark.parametrize( + "test_file,expected_file,description", + [ + ("matplotlib_1.py", "test.png", "Test that matplotlib is able to plot a graph and write it as an image ..."), + ("matplotlib_fonts_1.py", "test_fonts.png", "Test cm-super latex labels in matplotlib ...") + ] +) def test_matplotlib(container, test_file, expected_file, description): """Various tests performed on matplotlib - Test that matplotlib is able to plot a graph and write it as an image - Test matplotlib latex fonts, which depend on the cm-super package """ - host_data_dir = os.path.join(os.path.dirname( - os.path.realpath(__file__)), "data") + host_data_dir = os.path.join(THIS_DIR, "data") cont_data_dir = "/home/jovyan/data" output_dir = "/tmp" LOGGER.info(description) diff --git a/scipy-notebook/test/test_pandas.py b/scipy-notebook/test/test_pandas.py deleted file mode 100644 index 410f50232..000000000 --- a/scipy-notebook/test/test_pandas.py +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright (c) Jupyter Development Team. -# Distributed under the terms of the Modified BSD License. -import logging - -import pytest - -LOGGER = logging.getLogger(__name__) - - -@pytest.mark.parametrize( - "name,command_list", - [ - ( - "Sum series", - [ - "import pandas as pd", - "import numpy as np", - "np.random.seed(0)", - "print(pd.Series(np.random.randint(0, 7, size=10)).sum())" - ] - ), - ], -) -def test_pandas(container, name, command_list): - """Basic pandas tests""" - LOGGER.info(f"Testing pandas: {name} ...") - command = ';'.join(command_list) - c = container.run(tty=True, command=["start.sh", "python", "-c", command]) - rv = c.wait(timeout=30) - assert rv == 0 or rv["StatusCode"] == 0, f"Command {command} failed" - logs = c.logs(stdout=True).decode("utf-8") - LOGGER.debug(logs) diff --git a/scipy-notebook/test/units/unit_pandas.py b/scipy-notebook/test/units/unit_pandas.py new file mode 100644 index 000000000..5036f823d --- /dev/null +++ b/scipy-notebook/test/units/unit_pandas.py @@ -0,0 +1,9 @@ +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. + +import numpy as np +import pandas as pd + + +np.random.seed(0) +print(pd.Series(np.random.randint(0, 7, size=10)).sum()) diff --git a/tensorflow-notebook/test/test_tensorflow.py b/tensorflow-notebook/test/test_tensorflow.py deleted file mode 100644 index f5a6910fd..000000000 --- a/tensorflow-notebook/test/test_tensorflow.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright (c) Jupyter Development Team. -# Distributed under the terms of the Modified BSD License. -import logging - -import pytest - -LOGGER = logging.getLogger(__name__) - - -@pytest.mark.parametrize( - "name,command", - [ - ( - "Hello world", - "import tensorflow as tf;print(tf.constant('Hello, TensorFlow'))", - ), - ( - "Sum", - "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))", - ), - ], -) -def test_tensorflow(container, name, command): - """Basic tensorflow tests""" - LOGGER.info(f"Testing tensorflow: {name} ...") - c = container.run(tty=True, command=["start.sh", "python", "-c", command]) - rv = c.wait(timeout=30) - assert rv == 0 or rv["StatusCode"] == 0, f"Command {command} failed" - logs = c.logs(stdout=True).decode("utf-8") - LOGGER.debug(logs) diff --git a/tensorflow-notebook/test/units/unit_tensorflow.py b/tensorflow-notebook/test/units/unit_tensorflow.py new file mode 100644 index 000000000..3d5a60c07 --- /dev/null +++ b/tensorflow-notebook/test/units/unit_tensorflow.py @@ -0,0 +1,7 @@ +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. +import tensorflow as tf + + +print(tf.constant('Hello, TensorFlow')) +print(tf.reduce_sum(tf.random.normal([1000, 1000]))) diff --git a/test/helpers.py b/test/helpers.py index 70919c400..7fe41062d 100644 --- a/test/helpers.py +++ b/test/helpers.py @@ -50,7 +50,8 @@ def start_container(container): """Start the TrackedContainer and return an instance of a running container""" LOGGER.info(f"Starting container {container.image_name} ...") return container.run( - tty=True, command=["start.sh", "bash", "-c", "sleep infinity"] + tty=True, + command=["start.sh", "bash", "-c", "sleep infinity"] ) @staticmethod diff --git a/test/test_units.py b/test/test_units.py new file mode 100644 index 000000000..ecd74cbd4 --- /dev/null +++ b/test/test_units.py @@ -0,0 +1,33 @@ +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. + +import logging +import os + +LOGGER = logging.getLogger(__name__) +THIS_DIR = os.path.dirname(os.path.realpath(__file__)) + + +def test_units(container): + """Various units tests + Add a py file in the {image}/test/units dir and it will be automatically tested + """ + host_data_dir = os.path.join(THIS_DIR, "../", container.image_name(), "test/units") + cont_data_dir = "/home/jovyan/data" + + if not os.path.exists(host_data_dir): + LOGGER.info(f"Not found unit tests for image: {container.image_name()}") + return + + command = "sleep infinity" + running_container = container.run( + volumes={host_data_dir: {"bind": cont_data_dir, "mode": "ro"}}, + tty=True, + command=["start.sh", "bash", "-c", command], + ) + for test_file in os.listdir(host_data_dir): + LOGGER.info("Running unit test: {test_file}") + command = f"python {cont_data_dir}/{test_file}" + cmd = running_container.exec_run(command) + assert cmd.exit_code == 0, f"Command {command} failed" + LOGGER.debug(cmd.output.decode("utf-8")) From bbb91c41a6c8994d13ea2c2466f2c513fdf320c2 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sat, 26 Jun 2021 14:44:01 +0300 Subject: [PATCH 02/17] Fix style --- all-spark-notebook/test/test_spark_notebooks.py | 2 +- scipy-notebook/test/test_extensions.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/all-spark-notebook/test/test_spark_notebooks.py b/all-spark-notebook/test/test_spark_notebooks.py index 1a1d85281..5c6abc3c4 100644 --- a/all-spark-notebook/test/test_spark_notebooks.py +++ b/all-spark-notebook/test/test_spark_notebooks.py @@ -13,7 +13,7 @@ @pytest.mark.parametrize( "test_file", # TODO: add local_sparklyr - ["local_pyspark", "local_spylon", "local_sparkR", "issue_1168"], + ["local_pyspark", "local_spylon", "local_sparkR", "issue_1168"] ) def test_nbconvert(container, test_file): """Check if Spark notebooks can be executed""" diff --git a/scipy-notebook/test/test_extensions.py b/scipy-notebook/test/test_extensions.py index 3571a73c2..cf6b53eb7 100644 --- a/scipy-notebook/test/test_extensions.py +++ b/scipy-notebook/test/test_extensions.py @@ -13,8 +13,8 @@ [ "@bokeh/jupyter_bokeh", "@jupyter-widgets/jupyterlab-manager", - "jupyter-matplotlib", - ], + "jupyter-matplotlib" + ] ) def test_check_extension(container, extension): """Basic check of each extension From 21927f8451e2694713fab2332bbd18a13ae051de Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sat, 26 Jun 2021 14:54:14 +0300 Subject: [PATCH 03/17] Remove redundant upper --- minimal-notebook/test/test_nbconvert.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minimal-notebook/test/test_nbconvert.py b/minimal-notebook/test/test_nbconvert.py index e0f45d547..125c57570 100644 --- a/minimal-notebook/test/test_nbconvert.py +++ b/minimal-notebook/test/test_nbconvert.py @@ -24,7 +24,7 @@ def test_nbconvert(container, test_file, output_format): host_data_dir = os.path.join(THIS_DIR, "data") cont_data_dir = "/home/jovyan/data" output_dir = "/tmp" - LOGGER.info(f"Test that the example notebook {test_file} can be converted to {output_format.upper()} ...") + LOGGER.info(f"Test that the example notebook {test_file} can be converted to {output_format} ...") command = f"jupyter nbconvert {cont_data_dir}/{test_file}.ipynb --output-dir {output_dir} --to {output_format}" c = container.run( volumes={host_data_dir: {"bind": cont_data_dir, "mode": "ro"}}, From 9bf20478b6fafc5d562c8dca5f5aa97c4116f2b9 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sat, 26 Jun 2021 14:57:18 +0300 Subject: [PATCH 04/17] Fix style --- minimal-notebook/test/test_inkscape.py | 3 ++- scipy-notebook/test/test_extensions.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/minimal-notebook/test/test_inkscape.py b/minimal-notebook/test/test_inkscape.py index e8f50e2c7..fdb0fbcff 100644 --- a/minimal-notebook/test/test_inkscape.py +++ b/minimal-notebook/test/test_inkscape.py @@ -10,7 +10,8 @@ def test_inkscape(container): """Inkscape shall be installed to be able to convert SVG files.""" LOGGER.info("Test that inkscape is working by printing its version ...") c = container.run( - tty=True, command=["start.sh", "bash", "-c", "inkscape --version"] + tty=True, + command=["start.sh", "bash", "-c", "inkscape --version"] ) c.wait(timeout=10) logs = c.logs(stdout=True).decode("utf-8") diff --git a/scipy-notebook/test/test_extensions.py b/scipy-notebook/test/test_extensions.py index cf6b53eb7..460760a7e 100644 --- a/scipy-notebook/test/test_extensions.py +++ b/scipy-notebook/test/test_extensions.py @@ -26,7 +26,8 @@ def test_check_extension(container, extension): """ LOGGER.info(f"Checking the extension: {extension} ...") c = container.run( - tty=True, command=["start.sh", "jupyter", "labextension", "check", extension] + tty=True, + command=["start.sh", "jupyter", "labextension", "check", extension] ) rv = c.wait(timeout=10) logs = c.logs(stdout=True).decode("utf-8") From 856577b35dcdd7eef93339b9fac5ba7dfddce648 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sat, 26 Jun 2021 15:07:26 +0300 Subject: [PATCH 05/17] Image name is a property --- test/test_units.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_units.py b/test/test_units.py index ecd74cbd4..82da86e7c 100644 --- a/test/test_units.py +++ b/test/test_units.py @@ -12,11 +12,11 @@ def test_units(container): """Various units tests Add a py file in the {image}/test/units dir and it will be automatically tested """ - host_data_dir = os.path.join(THIS_DIR, "../", container.image_name(), "test/units") + host_data_dir = os.path.join(THIS_DIR, "../", container.image_name, "test/units") cont_data_dir = "/home/jovyan/data" if not os.path.exists(host_data_dir): - LOGGER.info(f"Not found unit tests for image: {container.image_name()}") + LOGGER.info(f"Not found unit tests for image: {container.image_name}") return command = "sleep infinity" From c66273974fadc829c0252975c20367ac124863f8 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sat, 26 Jun 2021 15:07:40 +0300 Subject: [PATCH 06/17] Simplify conftest --- conftest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conftest.py b/conftest.py index b3c20df70..90723701a 100644 --- a/conftest.py +++ b/conftest.py @@ -33,10 +33,10 @@ def docker_client(): @pytest.fixture(scope='session') def image_name(): """Image name to test""" - return os.getenv('TEST_IMAGE', 'jupyter/base-notebook') + return os.getenv('TEST_IMAGE') -class TrackedContainer(object): +class TrackedContainer: """Wrapper that collects docker container configuration and delays container creation/execution. From 847724465eaef0a8628dc2eee15a346c5d0006dc Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sat, 26 Jun 2021 15:16:46 +0300 Subject: [PATCH 07/17] Add tests README --- test/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 test/README.md diff --git a/test/README.md b/test/README.md new file mode 100644 index 000000000..f0c887ead --- /dev/null +++ b/test/README.md @@ -0,0 +1,12 @@ +# Docker stacks testing + +We test our images using `pytest` module. + +There are two kinds of tests we use: + +- General tests - these are located in [this](https://github.com/jupyter/docker-stacks/blob/master/test) folder +- Image specific tests - for example, [base-notebook/test](https://github.com/jupyter/docker-stacks/blob/master/base-notebook/test) folder + +We also have a way to easily run arbitrary python files in a container. +This is useful for running unit tests of packages we use, so we put these files in `{image}/test/units` folder. +An example of such a test is [unit_pandas.py](https://github.com/jupyter/docker-stacks/blob/master/scipy-notebook/test/units) From 466a5fb661efe3fb662b9bc5a361a710f978c8c2 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sat, 26 Jun 2021 15:17:54 +0300 Subject: [PATCH 08/17] Fix syntax --- test/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/README.md b/test/README.md index f0c887ead..887a244a1 100644 --- a/test/README.md +++ b/test/README.md @@ -9,4 +9,4 @@ There are two kinds of tests we use: We also have a way to easily run arbitrary python files in a container. This is useful for running unit tests of packages we use, so we put these files in `{image}/test/units` folder. -An example of such a test is [unit_pandas.py](https://github.com/jupyter/docker-stacks/blob/master/scipy-notebook/test/units) +An example of such a test is [unit_pandas.py](https://github.com/jupyter/docker-stacks/blob/master/scipy-notebook/test/units). From a93c13a8aac159786620c380f40aa7f343ca82ca Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sat, 26 Jun 2021 15:21:20 +0300 Subject: [PATCH 09/17] Add pytest info --- test/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/README.md b/test/README.md index 887a244a1..26ebdf353 100644 --- a/test/README.md +++ b/test/README.md @@ -2,6 +2,9 @@ We test our images using `pytest` module. +`conftest.py` and `pytest.ini` in the root of our repository define the environment in which tests are run. +More info on pytest can be found [here](https://docs.pytest.org/en/latest/reference/index.html). + There are two kinds of tests we use: - General tests - these are located in [this](https://github.com/jupyter/docker-stacks/blob/master/test) folder From bf05f4243a2f773771e0c31e22251058845b5e9c Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sat, 26 Jun 2021 18:53:54 +0300 Subject: [PATCH 10/17] Some fixes --- test/test_units.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test_units.py b/test/test_units.py index 82da86e7c..a5a5f5033 100644 --- a/test/test_units.py +++ b/test/test_units.py @@ -12,7 +12,9 @@ def test_units(container): """Various units tests Add a py file in the {image}/test/units dir and it will be automatically tested """ - host_data_dir = os.path.join(THIS_DIR, "../", container.image_name, "test/units") + short_image_name = container.image_name[container.image_name.find('/') + 1:] + host_data_dir = os.path.join(THIS_DIR, f"../{short_image_name}/test/units") + LOGGER.info(f"Searching for units tests in {host_data_dir}") cont_data_dir = "/home/jovyan/data" if not os.path.exists(host_data_dir): @@ -26,7 +28,7 @@ def test_units(container): command=["start.sh", "bash", "-c", command], ) for test_file in os.listdir(host_data_dir): - LOGGER.info("Running unit test: {test_file}") + LOGGER.info(f"Running unit test: {test_file}") command = f"python {cont_data_dir}/{test_file}" cmd = running_container.exec_run(command) assert cmd.exit_code == 0, f"Command {command} failed" From 1cef89a029de01b5155e58f7e88bd60309abd458 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sat, 26 Jun 2021 18:55:29 +0300 Subject: [PATCH 11/17] Fix link --- test/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/README.md b/test/README.md index 26ebdf353..7cf2b8056 100644 --- a/test/README.md +++ b/test/README.md @@ -3,7 +3,7 @@ We test our images using `pytest` module. `conftest.py` and `pytest.ini` in the root of our repository define the environment in which tests are run. -More info on pytest can be found [here](https://docs.pytest.org/en/latest/reference/index.html). +More info on pytest can be found [here](https://docs.pytest.org/en/latest/contents.html). There are two kinds of tests we use: From e264a5e702699fcb63c5b4017f0c6f9bc9e49acb Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sat, 26 Jun 2021 19:08:04 +0300 Subject: [PATCH 12/17] Fix link --- test/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/README.md b/test/README.md index 7cf2b8056..3b32fc28a 100644 --- a/test/README.md +++ b/test/README.md @@ -12,4 +12,4 @@ There are two kinds of tests we use: We also have a way to easily run arbitrary python files in a container. This is useful for running unit tests of packages we use, so we put these files in `{image}/test/units` folder. -An example of such a test is [unit_pandas.py](https://github.com/jupyter/docker-stacks/blob/master/scipy-notebook/test/units). +An example of such a test is [unit_pandas.py](https://github.com/jupyter/docker-stacks/blob/master/scipy-notebook/test/units/unit_pandas.py). From a305eaa5564deb0bb65224bd1836348f4f2bbb8d Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sun, 27 Jun 2021 00:46:42 +0300 Subject: [PATCH 13/17] Fix container running --- test/test_units.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/test/test_units.py b/test/test_units.py index a5a5f5033..0d511dcbd 100644 --- a/test/test_units.py +++ b/test/test_units.py @@ -21,15 +21,14 @@ def test_units(container): LOGGER.info(f"Not found unit tests for image: {container.image_name}") return - command = "sleep infinity" - running_container = container.run( - volumes={host_data_dir: {"bind": cont_data_dir, "mode": "ro"}}, - tty=True, - command=["start.sh", "bash", "-c", command], - ) for test_file in os.listdir(host_data_dir): LOGGER.info(f"Running unit test: {test_file}") - command = f"python {cont_data_dir}/{test_file}" - cmd = running_container.exec_run(command) - assert cmd.exit_code == 0, f"Command {command} failed" - LOGGER.debug(cmd.output.decode("utf-8")) + + c = container.run( + tty=True, + command=['start.sh', 'python', f'{cont_data_dir}/{test_file}'] + ) + rv = c.wait(timeout=30) + assert rv == 0 or rv["StatusCode"] == 0 + logs = c.logs(stdout=True).decode('utf-8') + LOGGER.debug(logs) From a3a0a5b54bdfc03810b956a280ae54b38200fe00 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sun, 27 Jun 2021 01:14:06 +0300 Subject: [PATCH 14/17] Fix --- test/test_units.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_units.py b/test/test_units.py index 0d511dcbd..1a1cacba8 100644 --- a/test/test_units.py +++ b/test/test_units.py @@ -25,6 +25,7 @@ def test_units(container): LOGGER.info(f"Running unit test: {test_file}") c = container.run( + volumes={host_data_dir: {"bind": cont_data_dir, "mode": "ro"}}, tty=True, command=['start.sh', 'python', f'{cont_data_dir}/{test_file}'] ) From 532d3734dc1879b2920614a6acd9ea49f3f163a9 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sun, 27 Jun 2021 12:30:07 +0300 Subject: [PATCH 15/17] Apply suggestions from code review --- all-spark-notebook/test/test_spark_notebooks.py | 2 +- base-notebook/test/test_package_managers.py | 4 ++-- base-notebook/test/test_start_container.py | 4 ++-- minimal-notebook/test/test_nbconvert.py | 4 ++-- scipy-notebook/test/test_extensions.py | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/all-spark-notebook/test/test_spark_notebooks.py b/all-spark-notebook/test/test_spark_notebooks.py index 5c6abc3c4..1a1d85281 100644 --- a/all-spark-notebook/test/test_spark_notebooks.py +++ b/all-spark-notebook/test/test_spark_notebooks.py @@ -13,7 +13,7 @@ @pytest.mark.parametrize( "test_file", # TODO: add local_sparklyr - ["local_pyspark", "local_spylon", "local_sparkR", "issue_1168"] + ["local_pyspark", "local_spylon", "local_sparkR", "issue_1168"], ) def test_nbconvert(container, test_file): """Check if Spark notebooks can be executed""" diff --git a/base-notebook/test/test_package_managers.py b/base-notebook/test/test_package_managers.py index 060706e3b..49b9bc8af 100644 --- a/base-notebook/test/test_package_managers.py +++ b/base-notebook/test/test_package_managers.py @@ -14,8 +14,8 @@ ("conda", "--version"), ("mamba", "--version"), ("npm", "--version"), - ("pip", "--version") - ] + ("pip", "--version"), + ], ) def test_package_manager(container, package_manager, version_arg): """Test the notebook start-notebook script""" diff --git a/base-notebook/test/test_start_container.py b/base-notebook/test/test_start_container.py index 3ffd4356f..e1231e194 100644 --- a/base-notebook/test/test_start_container.py +++ b/base-notebook/test/test_start_container.py @@ -11,8 +11,8 @@ "env,expected_server", [ (["JUPYTER_ENABLE_LAB=yes"], "lab"), - (None, "notebook") - ] + (None, "notebook"), + ], ) def test_start_notebook(container, http_client, env, expected_server): """Test the notebook start-notebook script""" diff --git a/minimal-notebook/test/test_nbconvert.py b/minimal-notebook/test/test_nbconvert.py index 125c57570..6e2675eb0 100644 --- a/minimal-notebook/test/test_nbconvert.py +++ b/minimal-notebook/test/test_nbconvert.py @@ -16,8 +16,8 @@ ("notebook_math", "pdf"), ("notebook_math", "html"), ("notebook_svg", "pdf"), - ("notebook_svg", "html") - ] + ("notebook_svg", "html"), + ], ) def test_nbconvert(container, test_file, output_format): """Check if nbconvert is able to convert a notebook file""" diff --git a/scipy-notebook/test/test_extensions.py b/scipy-notebook/test/test_extensions.py index 460760a7e..168d92ade 100644 --- a/scipy-notebook/test/test_extensions.py +++ b/scipy-notebook/test/test_extensions.py @@ -13,8 +13,8 @@ [ "@bokeh/jupyter_bokeh", "@jupyter-widgets/jupyterlab-manager", - "jupyter-matplotlib" - ] + "jupyter-matplotlib", + ], ) def test_check_extension(container, extension): """Basic check of each extension From 52a91a3e760e55e7028f98f803ada2a7ba76d6c1 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sun, 27 Jun 2021 14:16:48 +0300 Subject: [PATCH 16/17] assert after logs output --- test/test_units.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_units.py b/test/test_units.py index 1a1cacba8..ce5a92501 100644 --- a/test/test_units.py +++ b/test/test_units.py @@ -30,6 +30,6 @@ def test_units(container): command=['start.sh', 'python', f'{cont_data_dir}/{test_file}'] ) rv = c.wait(timeout=30) - assert rv == 0 or rv["StatusCode"] == 0 logs = c.logs(stdout=True).decode('utf-8') LOGGER.debug(logs) + assert rv == 0 or rv["StatusCode"] == 0 From c73a1d1b5d96e01cb0a5f5d9cda4b16f2c13727e Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Mon, 28 Jun 2021 22:29:59 +0300 Subject: [PATCH 17/17] Update test/test_units.py Co-authored-by: Erik Sundell --- test/test_units.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_units.py b/test/test_units.py index ce5a92501..8edef5c98 100644 --- a/test/test_units.py +++ b/test/test_units.py @@ -12,7 +12,7 @@ def test_units(container): """Various units tests Add a py file in the {image}/test/units dir and it will be automatically tested """ - short_image_name = container.image_name[container.image_name.find('/') + 1:] + short_image_name = container.image_name[container.image_name.rfind('/') + 1:] host_data_dir = os.path.join(THIS_DIR, f"../{short_image_name}/test/units") LOGGER.info(f"Searching for units tests in {host_data_dir}") cont_data_dir = "/home/jovyan/data"