Skip to content

Commit 19086d3

Browse files
authored
refactor: update test suite to run locally (#195)
This change aims to simplify testing of PySlurm. The test suite, using pytest, leverages a Slurm container for running tests locally. Instructions are added to the README to use a test container, or an existing Slurm cluster that you have access to.
1 parent 05c8259 commit 19086d3

File tree

8 files changed

+233
-52
lines changed

8 files changed

+233
-52
lines changed

Pipfile

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ cython = "*"
88

99
[dev-packages]
1010
black = "*"
11+
pytest = "*"
12+
pytest-sugar = "*"
13+
pytest-testinfra = "*"
14+
isort = "*"
1115

1216
[pipenv]
1317
allow_prereleases = true

Pipfile.lock

+147-42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.rst

+56
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,62 @@ the pyslurm module once it is built:
7575
make html
7676
7777
78+
Testing
79+
*******
80+
81+
PySlurm requires an installation of Slurm.
82+
83+
Using a Test Container
84+
----------------------
85+
86+
To run tests locally without an existing Slurm cluster, `docker` and
87+
`docker-compose` is required.
88+
89+
Clone the project::
90+
91+
git clone https://github.com/PySlurm/pyslurm.git
92+
cd pyslurm
93+
94+
Start the Slurm container in the background::
95+
96+
docker-compose up -d
97+
98+
The cluster takes a few seconds to start all the required Slurm services. Tail the logs::
99+
100+
docker logs -f slurmctl
101+
102+
When the cluster is ready, you will see the following log message::
103+
104+
Cluster is now available
105+
106+
Press CTRL+C to stop tailing the logs. Slurm is now running in a container in detached mode. `docker-compose` also bind mounds the git directory
107+
inside the container at `/pyslurm` so that the container has access to the test cases.
108+
109+
Install test dependencies::
110+
111+
pipenv sync --dev
112+
113+
Execute the tests inside the container::
114+
115+
pipenv run pytest -sv scripts/run_tests_in_container.py
116+
117+
When testing is complete, stop the running Slurm container::
118+
119+
docker-compose down
120+
121+
Testing on an Existing Slurm Cluster
122+
------------------------------------
123+
124+
You may also choose to clone the project and run tests on a node where Slurm is already compiled and installed::
125+
126+
git clone https://github.com/PySlurm/pyslurm.git
127+
cd pyslurm
128+
python3.9 setup.py build
129+
python3.9 setup.py install
130+
./scripts/configure.sh
131+
pipenv sync --dev
132+
pipenv run pytest -sv
133+
78134
Authors
79135
*******
80136

docker-compose.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: "3.8"
2+
3+
services:
4+
slurm:
5+
image: giovtorres/docker-centos7-slurm:20.11.8
6+
hostname: slurmctl
7+
container_name: slurmctl
8+
stdin_open: true
9+
tty: true
10+
working_dir: /pyslurm
11+
environment:
12+
PYTHON: "3.9"
13+
volumes:
14+
- ${PWD}:/pyslurm

pyslurm/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
from .pyslurm import *
1717
from .__version__ import __version__
1818

19+
1920
def version():
2021
return __version__

scripts/configure.sh

+2-9
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ then
5252
fi
5353

5454
# Add the cluster to the slurm database
55-
sacctmgr --immediate add cluster name=linux
55+
#sacctmgr --immediate add cluster name=linux
5656

5757
# Restart Slurm components to apply configuration changes
5858
supervisorctl restart slurmctld
@@ -66,14 +66,7 @@ supervisorctl restart slurmd
6666
# Wait for nodes to become IDLE
6767
wait_for_cmd "sinfo | grep -q normal.*idle" "Waiting for nodes to transition to IDLE"
6868

69-
# Print the PySlurm version
70-
echo "---> PySlurm version"
71-
python$PYTHON -c "import pyslurm; print(pyslurm.version())"
72-
73-
# Print the Slurm API version
74-
echo "---> Slurm API version"
75-
python$PYTHON -c "import pyslurm; print(pyslurm.slurm_api_version())"
76-
69+
# TODO: convert to setup method/fixture
7770
# Submit test job with jobstep via srun for testing
7871
echo "---> Submitting sleep job"
7972
sbatch --wrap="srun sleep 1000"

scripts/run_tests_in_container.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import testinfra
2+
3+
4+
def test_run():
5+
host = testinfra.get_host(f"docker://slurmctl")
6+
print(host.check_output("python3.9 setup.py build"))
7+
print(host.check_output("python3.9 setup.py install"))
8+
print(host.check_output("./scripts/configure.sh"))
9+
print(host.check_output("pytest -v"))

tests/test_slurmdb.py

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def get_user():
4848
["squeue", "-O", "username", "-h"], stdout=subprocess.PIPE, stderr=None
4949
).communicate()
5050
for username in users[0].splitlines():
51-
print(username.decode())
5251
uid = pwd.getpwnam("{}".format(username.strip().decode()))
5352
yield username.strip().decode(), uid.pw_uid
5453

0 commit comments

Comments
 (0)