Skip to content

Commit

Permalink
check that build action can use TMPDIR
Browse files Browse the repository at this point in the history
Fixes #52
  • Loading branch information
espenhgn committed Oct 2, 2024
1 parent eed8a23 commit 49dddeb
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions tests/test_container_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,47 @@
import os
import socket
import subprocess
import tempfile


# port used by tests
sock = socket.socket()
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,

Check warning on line 22 in tests/test_container_template.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_container_template.py#L22 <291>

trailing whitespace
Raw output
./tests/test_container_template.py:22:61: W291 trailing whitespace
# 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

Check warning on line 25 in tests/test_container_template.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_container_template.py#L25 <291>

trailing whitespace
Raw output
./tests/test_container_template.py:25:64: W291 trailing whitespace
# 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'

Check failure on line 39 in tests/test_container_template.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_container_template.py#L39 <501>

line too long (102 > 79 characters)
Raw output
./tests/test_container_template.py:39:80: E501 line too long (102 > 79 characters)
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')
PREFIX_MOUNT = (
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'
Expand All @@ -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 = [
Expand All @@ -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

Check warning on line 108 in tests/test_container_template.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_container_template.py#L108 <391>

blank line at end of file
Raw output
./tests/test_container_template.py:108:1: W391 blank line at end of file

0 comments on commit 49dddeb

Please sign in to comment.