Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit fc20d71

Browse files
authored
MRG: Merge pull request #8 from octue/enhancement/add-output-manifest
Add output manifest and output datasets upload
2 parents 44154cf + 50b3033 commit fc20d71

File tree

7 files changed

+74
-4
lines changed

7 files changed

+74
-4
lines changed

app.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import logging
2+
import os
3+
import tempfile
24
import time
35

6+
from octue.resources import Datafile, Dataset
7+
48
from example_package.submodule import do_something
59

610

@@ -10,6 +14,15 @@
1014
def run(analysis):
1115
logger.info("Started example analysis.")
1216
do_something()
13-
time.sleep(5)
17+
time.sleep(2)
1418
analysis.output_values = [1, 2, 3, 4, 5]
19+
20+
with tempfile.TemporaryDirectory() as temporary_directory:
21+
22+
with Datafile(os.path.join(temporary_directory, "output.dat"), mode="w") as (datafile, f):
23+
f.write("This is some example service output.")
24+
25+
analysis.output_manifest.datasets["example_dataset"] = Dataset(path=temporary_directory, files={datafile})
26+
analysis.finalise(upload_output_datasets_to="gs://octue-test-bucket/example_output_datasets")
27+
1528
logger.info("Finished example analysis.")

requirements-dev.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
gcp-storage-emulator==2022.4.13
2+
3+
-e .

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
setup(
44
name="example-service",
5-
version="0.1.6",
5+
version="0.1.7",
66
install_requires=[
7-
"octue==0.15.7",
7+
"octue==0.21.0",
88
],
99
url="https://www.github.com/octue/example-service-cloud-run",
1010
author="cortadocodes",

tests/__init__.py

Whitespace-only changes.

tests/test_app.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import os
2+
import unittest
3+
from unittest.mock import patch
4+
5+
from octue import Runner
6+
from octue.cloud.emulators import GoogleCloudStorageEmulatorTestResultModifier, mock_generate_signed_url
7+
from octue.resources import Manifest
8+
9+
10+
REPOSITORY_ROOT = os.path.dirname(os.path.dirname(__file__))
11+
TWINE_PATH = os.path.join(REPOSITORY_ROOT, "twine.json")
12+
TEST_BUCKET_NAME = "octue-test-bucket"
13+
14+
15+
class TestApp(unittest.TestCase):
16+
test_result_modifier = GoogleCloudStorageEmulatorTestResultModifier(default_bucket_name=TEST_BUCKET_NAME)
17+
setattr(unittest.TestResult, "startTestRun", test_result_modifier.startTestRun)
18+
setattr(unittest.TestResult, "stopTestRun", test_result_modifier.stopTestRun)
19+
20+
def test_app(self):
21+
"""Test that the app takes in input in the correct format and returns an analysis with the correct output
22+
values.
23+
"""
24+
runner = Runner(app_src=REPOSITORY_ROOT, twine=TWINE_PATH)
25+
26+
with patch("octue.resources.dataset.Dataset._save_local_metadata"):
27+
with patch("google.cloud.storage.blob.Blob.generate_signed_url", mock_generate_signed_url):
28+
analysis = runner.run(input_values={"n_iterations": 3})
29+
30+
# Check the output values.
31+
self.assertEqual(analysis.output_values, [1, 2, 3, 4, 5])
32+
33+
# Test that the signed URLs for the dataset and its files work and can be used to reinstantiate the output
34+
# manifest after serialisation.
35+
downloaded_output_manifest = Manifest.deserialise(analysis.output_manifest.to_primitive())
36+
37+
# Check that the output dataset and its files can be accessed.
38+
with downloaded_output_manifest.datasets["example_dataset"].files.one() as (datafile, f):
39+
self.assertEqual(f.read(), "This is some example service output.")

tox.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[tox]
2+
envlist = {py38}
3+
4+
[testenv]
5+
passenv = GOOGLE_APPLICATION_CREDENTIALS GOOGLE_CLOUD_PROJECT TEST_PROJECT_NAME
6+
setenv =
7+
PYTHONPATH = {toxinidir}:{toxinidir}/octue
8+
commands =
9+
python -m unittest discover
10+
deps = -r requirements-dev.txt

twine.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@
1515
"items": {
1616
"type": "number"
1717
}
18-
}
18+
},
19+
"output_manifest": {
20+
"datasets": {
21+
"example_dataset": {}
22+
}
23+
}
1924
}

0 commit comments

Comments
 (0)