Skip to content

Commit

Permalink
refactor: use temporary directory (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
athith-g authored Aug 7, 2024
1 parent fefefa3 commit 467c3e8
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 75 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,4 @@ poetry.toml
# LSP config files
pyrightconfig.json

# End of https://www.toptal.com/developers/gitignore/api/python

outputs/
# End of https://www.toptal.com/developers/gitignore/api/python
124 changes: 66 additions & 58 deletions tests/tasks/task_bodies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

DIR = Path(__file__).parents[2]
input_dir = DIR / "inputs"
output_dir = DIR / "outputs"

uppercase_task_body = {

def get_uppercase_task_body(tmp_dir):
"""Returns TES task body that makes the contents of a file uppercase."""
return {
"name": "Hello world",
"inputs": [
{
Expand All @@ -22,7 +24,7 @@
],
"outputs": [
{
"url": f"file://{output_dir}/hello-upper.txt",
"url": f"file://{tmp_dir}/hello-upper.txt",
"path": "/outputs/hello-upper.txt",
"type": "FILE"
}
Expand All @@ -40,7 +42,10 @@
]
}

decryption_task_body = {

def get_decryption_task_body(tmp_dir):
"""Returns TES task body that decrypts a crypt4GH file."""
return {
"name": "Decrypt with secret key as environment variable",
"inputs": [
{
Expand All @@ -61,7 +66,7 @@
],
"outputs": [
{
"url": f"file://{output_dir}/hello-decrypted.txt",
"url": f"file://{tmp_dir}/hello-decrypted.txt",
"path": "/outputs/hello-decrypted.txt",
"type": "FILE"
}
Expand All @@ -79,58 +84,61 @@
]
}

uppercase_task_with_decryption_body = {
"name": "Decrypt with secret key as environment variable",
"inputs": [
{
"url": f"file://{input_dir}/hello.c4gh",
"path": "/inputs/hello.c4gh",
"type": "FILE"
},
{
"url": f"file://{input_dir}/alice.sec",
"path": "/inputs/alice.sec",
"type": "FILE"
},
{
"url": f"file://{input_dir}/decrypt.sh",
"path": "/inputs/decrypt.sh",
"type": "FILE"
},
{
"url": f"file://{input_dir}/make_uppercase.py",
"path": "/inputs/make_uppercase.py",
"type": "FILE"
}
],
"outputs": [
{
"url": f"file://{output_dir}/hello-upper-decrypt.txt",
"path": "/outputs/hello-upper-decrypt.txt",
"type": "FILE"
}
],
"executors": [
{
"image": "athitheyag/c4gh:1.0",
"command": ["bash", "/inputs/decrypt.sh"],
"env": {
"INPUT_LOC": "/inputs/hello.c4gh",
"OUTPUT_LOC": "/vol/A/inputs/hello.txt",
"SECRET": "/inputs/alice.sec"

def get_uppercase_task_with_decryption_body(tmp_dir):
"""Returns TES task body that makes the contents of a crypt4GH file uppercase."""
return {
"name": "Decrypt with secret key as environment variable",
"inputs": [
{
"url": f"file://{input_dir}/hello.c4gh",
"path": "/inputs/hello.c4gh",
"type": "FILE"
},
{
"url": f"file://{input_dir}/alice.sec",
"path": "/inputs/alice.sec",
"type": "FILE"
},
{
"url": f"file://{input_dir}/decrypt.sh",
"path": "/inputs/decrypt.sh",
"type": "FILE"
},
{
"url": f"file://{input_dir}/make_uppercase.py",
"path": "/inputs/make_uppercase.py",
"type": "FILE"
}
},
{
"image": "python:3",
"command": [
"python3",
"/inputs/make_uppercase.py",
"/vol/A/inputs/hello.txt"
],
"stdout": "/outputs/hello-upper-decrypt.txt"
}
],
"volumes": [
"/vol/A/inputs"
]
],
"outputs": [
{
"url": f"file://{tmp_dir}/hello-upper-decrypt.txt",
"path": "/outputs/hello-upper-decrypt.txt",
"type": "FILE"
}
],
"executors": [
{
"image": "athitheyag/c4gh:1.0",
"command": ["bash", "/inputs/decrypt.sh"],
"env": {
"INPUT_LOC": "/inputs/hello.c4gh",
"OUTPUT_LOC": "/vol/A/inputs/hello.txt",
"SECRET": "/inputs/alice.sec"
}
},
{
"image": "python:3",
"command": [
"python3",
"/inputs/make_uppercase.py",
"/vol/A/inputs/hello.txt"
],
"stdout": "/outputs/hello-upper-decrypt.txt"
}
],
"volumes": [
"/vol/A/inputs"
]
}
34 changes: 20 additions & 14 deletions tests/tasks/test_tasks.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
"""Tests for I/O and decryption tasks"""

import json
from pathlib import Path
from tempfile import TemporaryDirectory
from time import sleep

import pytest
import requests

from task_bodies import (
output_dir,
uppercase_task_body,
decryption_task_body,
uppercase_task_with_decryption_body
get_decryption_task_body,
get_uppercase_task_body,
get_uppercase_task_with_decryption_body,
)
from tests.utils import timeout

Expand All @@ -20,17 +21,23 @@
INPUT_TEXT = "hello world from the input!"


def wait_for_file_download(filename):
def wait_for_file_download(filename, output_path):
"""Waits for file with given filename to download."""
while not (output_dir/filename).exists():
while not (output_path/filename).exists():
sleep(1)


@pytest.fixture(name="output_dir")
def fixture_output_dir():
"""Returns temporary directory to store task outputs."""
return Path(TemporaryDirectory().name)


@pytest.fixture(name="task")
def fixture_task(request):
def fixture_task(request, output_dir):
"""Returns response received after creating task."""
return requests.post(
url=f"{TES_URL}/tasks", headers=HEADERS, json=request.param
url=f"{TES_URL}/tasks", headers=HEADERS, json=request.param(output_dir)
)


Expand All @@ -54,17 +61,16 @@ def fixture_final_task_info(task):


@pytest.mark.parametrize("task,filename,expected_output", [
(uppercase_task_body, "hello-upper.txt", INPUT_TEXT.upper()),
(decryption_task_body, "hello-decrypted.txt", INPUT_TEXT),
(uppercase_task_with_decryption_body, "hello-upper-decrypt.txt", INPUT_TEXT.upper())
(get_uppercase_task_body, "hello-upper.txt", INPUT_TEXT.upper()),
(get_decryption_task_body, "hello-decrypted.txt", INPUT_TEXT),
(get_uppercase_task_with_decryption_body, "hello-upper-decrypt.txt", INPUT_TEXT.upper())
], indirect=["task"])
@timeout(time_limit=10)
def test_task(task, final_task_info, filename, expected_output): # pylint: disable=unused-argument
def test_task(task, final_task_info, filename, expected_output, output_dir): # pylint: disable=unused-argument
"""Test tasks for successful completion and intended behavior."""
assert final_task_info["state"] == "COMPLETE"

wait_for_file_download(filename)

wait_for_file_download(filename=filename, output_path=output_dir)
with open(output_dir/filename, encoding="utf-8") as f:
output = f.read()
assert output == expected_output
Expand Down

0 comments on commit 467c3e8

Please sign in to comment.