Skip to content

Commit 33caee1

Browse files
authored
Merge pull request #1386 from mathbunnyru/asalikhov/units_test
Add a way to easily test units
2 parents d6432e7 + c73a1d1 commit 33caee1

File tree

8 files changed

+70
-74
lines changed

8 files changed

+70
-74
lines changed

pyspark-notebook/test/test_spark.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,3 @@ def test_spark_shell(container):
1616
logs = c.logs(stdout=True).decode('utf-8')
1717
LOGGER.debug(logs)
1818
assert 'res0: Int = 2' in logs, "spark-shell does not work"
19-
20-
21-
def test_pyspark(container):
22-
"""PySpark should be in the Python path"""
23-
c = container.run(
24-
tty=True,
25-
command=['start.sh', 'python', '-c', 'import pyspark']
26-
)
27-
rv = c.wait(timeout=30)
28-
logs = c.logs(stdout=True).decode('utf-8')
29-
LOGGER.debug(logs)
30-
assert rv == 0 or rv["StatusCode"] == 0, "pyspark not in PYTHONPATH"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) Jupyter Development Team.
2+
# Distributed under the terms of the Modified BSD License.
3+
4+
import pyspark # noqa: F401

scipy-notebook/test/test_pandas.py

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) Jupyter Development Team.
2+
# Distributed under the terms of the Modified BSD License.
3+
4+
import numpy as np
5+
import pandas as pd
6+
7+
8+
np.random.seed(0)
9+
print(pd.Series(np.random.randint(0, 7, size=10)).sum())

tensorflow-notebook/test/test_tensorflow.py

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) Jupyter Development Team.
2+
# Distributed under the terms of the Modified BSD License.
3+
import tensorflow as tf
4+
5+
6+
print(tf.constant('Hello, TensorFlow'))
7+
print(tf.reduce_sum(tf.random.normal([1000, 1000])))

test/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Docker stacks testing
2+
3+
We test our images using `pytest` module.
4+
5+
`conftest.py` and `pytest.ini` in the root of our repository define the environment in which tests are run.
6+
More info on pytest can be found [here](https://docs.pytest.org/en/latest/contents.html).
7+
8+
There are two kinds of tests we use:
9+
10+
- General tests - these are located in [this](https://github.com/jupyter/docker-stacks/blob/master/test) folder
11+
- Image specific tests - for example, [base-notebook/test](https://github.com/jupyter/docker-stacks/blob/master/base-notebook/test) folder
12+
13+
We also have a way to easily run arbitrary python files in a container.
14+
This is useful for running unit tests of packages we use, so we put these files in `{image}/test/units` folder.
15+
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).

test/test_units.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright (c) Jupyter Development Team.
2+
# Distributed under the terms of the Modified BSD License.
3+
4+
import logging
5+
import os
6+
7+
LOGGER = logging.getLogger(__name__)
8+
THIS_DIR = os.path.dirname(os.path.realpath(__file__))
9+
10+
11+
def test_units(container):
12+
"""Various units tests
13+
Add a py file in the {image}/test/units dir and it will be automatically tested
14+
"""
15+
short_image_name = container.image_name[container.image_name.rfind('/') + 1:]
16+
host_data_dir = os.path.join(THIS_DIR, f"../{short_image_name}/test/units")
17+
LOGGER.info(f"Searching for units tests in {host_data_dir}")
18+
cont_data_dir = "/home/jovyan/data"
19+
20+
if not os.path.exists(host_data_dir):
21+
LOGGER.info(f"Not found unit tests for image: {container.image_name}")
22+
return
23+
24+
for test_file in os.listdir(host_data_dir):
25+
LOGGER.info(f"Running unit test: {test_file}")
26+
27+
c = container.run(
28+
volumes={host_data_dir: {"bind": cont_data_dir, "mode": "ro"}},
29+
tty=True,
30+
command=['start.sh', 'python', f'{cont_data_dir}/{test_file}']
31+
)
32+
rv = c.wait(timeout=30)
33+
logs = c.logs(stdout=True).decode('utf-8')
34+
LOGGER.debug(logs)
35+
assert rv == 0 or rv["StatusCode"] == 0

0 commit comments

Comments
 (0)