Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check that build action can use TMPDIR #53

Merged
merged 11 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions .github/workflows/container_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
tags:
- "v*.*.*"
env:
TEST_TAG: ghcr.io/precimed/container_template:test
LATEST_TAG: ghcr.io/precimed/container_template:latest

jobs:
docker:
Expand Down Expand Up @@ -51,13 +51,18 @@ jobs:
with:
context: "{{defaultContext}}:docker"
load: true
tags: ${{ env.TEST_TAG }}
tags: ${{ env.LATEST_TAG }}
file:
./dockerfiles/container_template/Dockerfile

- name: Run unit tests
- name: Run unit tests I
run: |
docker run --rm -v ${{ github.workspace }}:/home ${{ env.TEST_TAG }} py.test -v tests
docker run --rm -v ${{ github.workspace }}:/home ${{ env.LATEST_TAG }} py.test -v tests

- name: Run unit tests II
run: |
pip install -r test-requirements.txt
py.test -v tests

- name: Build
uses: docker/build-push-action@v6
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest
2 changes: 2 additions & 0 deletions tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
cache_dir = /tmp/pytest-cache/
67 changes: 49 additions & 18 deletions tests/test_container_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,52 @@
"""

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,
# 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.
cwd = os.getcwd()
try:
pth = os.path.join('containers', 'container_template.sif')
out = subprocess.run('singularity')
cwd = os.getcwd()
PREFIX = f'singularity run {pth} python'
PREFIX_MOUNT = f'singularity run --home={cwd}:/home/ {pth} python'
try:
runtime = 'apptainer'
out = subprocess.run(runtime, check=False)
except FileNotFoundError:
try:
runtime = 'singularity'
out = subprocess.run(runtime, check=False)
except FileNotFoundError as exc:
raise FileNotFoundError from exc
PREFIX = f'{runtime} run {pth} python'
PREFIX_MOUNT = f'{runtime} run --home={cwd}:/home/ {pth} python'
PREFIX_CUSTOM_MOUNT = f'{runtime} run --home={cwd}:/home/ ' + \
'{custom_mount}' + f'{pth} python'
except FileNotFoundError:
try:
out = subprocess.run('docker')
pwd = os.getcwd()
PREFIX = (f'docker run -p {port}:{port} ' +
runtime = 'docker'
out = subprocess.run(runtime, check=False)
PREFIX = (f'{runtime} run ' +
'ghcr.io/precimed/container_template python')
PREFIX_MOUNT = (
f'docker run -p {port}:{port} ' +
f'--mount type=bind,source={pwd},target={pwd} ' +
f'{runtime} run ' +
f'--mount type=bind,source={cwd},target={cwd} ' +
'ghcr.io/precimed/container_template python')
PREFIX_CUSTOM_MOUNT = (
f'{runtime} run ' +
f'--mount type=bind,source={cwd},target={cwd} ' +
'{custom_mount} ' +
'ghcr.io/precimed/container_template python')
except FileNotFoundError:
# neither singularity nor docker found, fall back to plain python
runtime = None
PREFIX = 'python'
PREFIX_MOUNT = 'python'
PREFIX_CUSTOM_MOUNT = 'python'


def test_assert():
Expand All @@ -56,12 +71,28 @@ def test_container_template_python():

def test_container_template_python_script():
'''test that Python can run a script'''
pwd = os.getcwd() if PREFIX.rfind('docker') >= 0 else '.'
call = f'''{PREFIX_MOUNT} {pwd}/tests/extras/hello.py'''
cwd = os.getcwd() if runtime == 'docker' else '.'
call = f'''{PREFIX_MOUNT} {cwd}/tests/extras/hello.py'''
out = subprocess.run(call.split(' '), capture_output=True)
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 {cwd}/tests/extras/hello.py {d}/')
if runtime == 'docker':
custom_mount = f'--mount type=bind,source={d},target={d}'
elif runtime in ['apptainer', 'singularity']:
custom_mount = f'--bind {d}:{d} '
else:
custom_mount = ''
call = f'{PREFIX_CUSTOM_MOUNT.format(custom_mount=custom_mount)} ' + \
f'{d}/hello.py'
out = subprocess.run(call, shell=True, check=False)
assert out.returncode == 0


def test_container_template_python_packages():
'''test that the Python packages are installed'''
packages = [
Expand Down
Loading