From 49dddeb6c21d18c2216316194dd4d81b2bf46838 Mon Sep 17 00:00:00 2001 From: Espen Hagen <2492641+espenhgn@users.noreply.github.com> Date: Wed, 2 Oct 2024 14:21:57 +0200 Subject: [PATCH] check that build action can use TMPDIR Fixes #52 --- tests/test_container_template.py | 34 +++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/tests/test_container_template.py b/tests/test_container_template.py index aab0bb7..22b2a34 100644 --- a/tests/test_container_template.py +++ b/tests/test_container_template.py @@ -11,6 +11,7 @@ import os import socket import subprocess +import tempfile # port used by tests @@ -18,17 +19,27 @@ sock.bind(('', 0)) port = sock.getsockname()[1] -# Check that (1) singularity exist, and (2) if not, check for docker. +# Check that (1) singularity or apptainer executables exist, +# and (2) if not, check for docker. # If neither are found, tests will fall back to plain python. +# This may be useful for testing on a local machine, but should +# be revised for the particular usecase. try: pth = os.path.join('containers', 'container_template.sif') - out = subprocess.run('singularity') + try: + out = subprocess.run('singularity', check=False) + except FileNotFoundError: + try: + out = subprocess.run('apptainer', check=False) + except FileNotFoundError as exc: + raise FileNotFoundError from exc cwd = os.getcwd() PREFIX = f'singularity run {pth} python' PREFIX_MOUNT = f'singularity run --home={cwd}:/home/ {pth} python' + PREFIX_CUSTOM_MOUNT = f'singularity run --home={cwd}:/home/ ' + '{custom_mount}' + f'{pth} python' except FileNotFoundError: try: - out = subprocess.run('docker') + out = subprocess.run('docker', check=False) pwd = os.getcwd() PREFIX = (f'docker run -p {port}:{port} ' + 'ghcr.io/precimed/container_template python') @@ -36,6 +47,11 @@ f'docker run -p {port}:{port} ' + f'--mount type=bind,source={pwd},target={pwd} ' + 'ghcr.io/precimed/container_template python') + PREFIX_CUSTOM_MOUNT = ( + f'docker run -p {port}:{port} ' + + f'--mount type=bind,source={pwd},target={pwd} ' + + '{custom_mount} ' + + 'ghcr.io/precimed/container_template python') except FileNotFoundError: # neither singularity nor docker found, fall back to plain python PREFIX = 'python' @@ -62,6 +78,17 @@ def test_container_template_python_script(): assert out.returncode == 0 +def test_container_template_python_script_from_tempdir(): + '''test that the tempdir is working''' + with tempfile.TemporaryDirectory() as d: + os.system(f'cp {pwd}/tests/extras/hello.py {d}/') + custom_mount = f'--mount type=bind,source={d},target=/temp/' + call = f'{PREFIX_CUSTOM_MOUNT.format(custom_mount=custom_mount)} ' + \ + '/temp/hello.py' + out = subprocess.run(call.split(' '), check=False) + assert out.returncode == 0 + + def test_container_template_python_packages(): '''test that the Python packages are installed''' packages = [ @@ -78,3 +105,4 @@ def test_container_template_python_packages(): call = f"{PREFIX} -c '{importstr}'" out = subprocess.run(call, shell=True) assert out.returncode == 0 +