X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/5282d5462d86721c73849c2e5f3d057657c74b51..5b8dfb5d5f4b34fa730e69c45c517f0cfb3fdc9f:/sdk/cwl/tests/test_container.py diff --git a/sdk/cwl/tests/test_container.py b/sdk/cwl/tests/test_container.py index 522946a4f4..69f3ae046e 100644 --- a/sdk/cwl/tests/test_container.py +++ b/sdk/cwl/tests/test_container.py @@ -3,7 +3,9 @@ # SPDX-License-Identifier: Apache-2.0 import arvados_cwl +import arvados_cwl.context from arvados_cwl.arvdocker import arv_docker_clear_cache +import arvados.config import logging import mock import unittest @@ -20,9 +22,56 @@ if not os.getenv('ARVADOS_DEBUG'): logging.getLogger('arvados.cwl-runner').setLevel(logging.WARN) logging.getLogger('arvados.arv-run').setLevel(logging.WARN) +class CollectionMock(object): + def __init__(self, vwdmock, *args, **kwargs): + self.vwdmock = vwdmock + self.count = 0 + + def open(self, *args, **kwargs): + self.count += 1 + return self.vwdmock.open(*args, **kwargs) + + def copy(self, *args, **kwargs): + self.count += 1 + self.vwdmock.copy(*args, **kwargs) + + def save_new(self, *args, **kwargs): + pass + + def __len__(self): + return self.count + + def portable_data_hash(self): + if self.count == 0: + return arvados.config.EMPTY_BLOCK_LOCATOR + else: + return "99999999999999999999999999999996+99" + class TestContainer(unittest.TestCase): + def helper(self, runner, enable_reuse=True): + document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0") + + make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess, + collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0)) + loadingContext = arvados_cwl.context.ArvLoadingContext( + {"avsc_names": avsc_names, + "basedir": "", + "make_fs_access": make_fs_access, + "loader": Loader({}), + "metadata": {"cwlVersion": "v1.0"}}) + runtimeContext = arvados_cwl.context.ArvRuntimeContext( + {"work_api": "containers", + "basedir": "", + "name": "test_run_"+str(enable_reuse), + "make_fs_access": make_fs_access, + "tmpdir": "/tmp", + "enable_reuse": enable_reuse, + "priority": 500}) + + return loadingContext, runtimeContext + # The test passes no builder.resources # Hence the default resources will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024} @mock.patch("arvados.commands.keepdocker.list_images_in_arv") @@ -40,8 +89,6 @@ class TestContainer(unittest.TestCase): runner.api.collections().get().execute.return_value = { "portable_data_hash": "99999999999999999999999999999993+99"} - document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0") - tool = cmap({ "inputs": [], "outputs": [], @@ -50,14 +97,14 @@ class TestContainer(unittest.TestCase): "id": "#", "class": "CommandLineTool" }) - make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess, - collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0)) - arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, work_api="containers", avsc_names=avsc_names, - basedir="", make_fs_access=make_fs_access, loader=Loader({})) + + loadingContext, runtimeContext = self.helper(runner, enable_reuse) + + arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext) arvtool.formatgraph = None - for j in arvtool.job({}, mock.MagicMock(), basedir="", name="test_run_"+str(enable_reuse), - make_fs_access=make_fs_access, tmpdir="/tmp"): - j.run(enable_reuse=enable_reuse, priority=500) + + for j in arvtool.job({}, mock.MagicMock(), runtimeContext): + j.run(runtimeContext) runner.api.container_requests().create.assert_called_with( body=JsonDiffMatcher({ 'environment': { @@ -79,6 +126,7 @@ class TestContainer(unittest.TestCase): "capacity": 1073741824 } }, 'state': 'Committed', + 'output_name': 'Output for step test_run_'+str(enable_reuse), 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz', 'output_path': '/var/spool/cwl', 'output_ttl': 0, @@ -101,8 +149,6 @@ class TestContainer(unittest.TestCase): runner.intermediate_output_ttl = 3600 runner.secret_store = cwltool.secrets.SecretStore() - document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0") - keepdocker.return_value = [("zzzzz-4zz18-zzzzzzzzzzzzzz3", "")] runner.api.collections().get().execute.return_value = { "portable_data_hash": "99999999999999999999999999999993+99"} @@ -135,15 +181,14 @@ class TestContainer(unittest.TestCase): "id": "#", "class": "CommandLineTool" }) - make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess, - collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0)) - arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, work_api="containers", - avsc_names=avsc_names, make_fs_access=make_fs_access, - loader=Loader({})) + + loadingContext, runtimeContext = self.helper(runner) + runtimeContext.name = "test_resource_requirements" + + arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext) arvtool.formatgraph = None - for j in arvtool.job({}, mock.MagicMock(), basedir="", name="test_resource_requirements", - make_fs_access=make_fs_access, tmpdir="/tmp"): - j.run(enable_reuse=True, priority=500) + for j in arvtool.job({}, mock.MagicMock(), runtimeContext): + j.run(runtimeContext) call_args, call_kwargs = runner.api.container_requests().create.call_args @@ -168,6 +213,7 @@ class TestContainer(unittest.TestCase): "capacity": 5242880000 } }, 'state': 'Committed', + 'output_name': 'Output for step test_resource_requirements', 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz', 'output_path': '/var/spool/cwl', 'output_ttl': 7200, @@ -199,8 +245,6 @@ class TestContainer(unittest.TestCase): runner.intermediate_output_ttl = 0 runner.secret_store = cwltool.secrets.SecretStore() - document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0") - keepdocker.return_value = [("zzzzz-4zz18-zzzzzzzzzzzzzz3", "")] runner.api.collections().get().execute.return_value = { "portable_data_hash": "99999999999999999999999999999993+99"} @@ -214,8 +258,7 @@ class TestContainer(unittest.TestCase): runner.fs_access.get_collection.side_effect = get_collection_mock vwdmock = mock.MagicMock() - collection_mock.return_value = vwdmock - vwdmock.portable_data_hash.return_value = "99999999999999999999999999999996+99" + collection_mock.side_effect = lambda *args, **kwargs: CollectionMock(vwdmock, *args, **kwargs) tool = cmap({ "inputs": [], @@ -247,15 +290,14 @@ class TestContainer(unittest.TestCase): "id": "#", "class": "CommandLineTool" }) - make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess, - collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0)) - arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, work_api="containers", - avsc_names=avsc_names, make_fs_access=make_fs_access, - loader=Loader({})) + + loadingContext, runtimeContext = self.helper(runner) + runtimeContext.name = "test_initial_work_dir" + + arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext) arvtool.formatgraph = None - for j in arvtool.job({}, mock.MagicMock(), basedir="", name="test_initial_work_dir", - make_fs_access=make_fs_access, tmpdir="/tmp"): - j.run(priority=500) + for j in arvtool.job({}, mock.MagicMock(), runtimeContext): + j.run(runtimeContext) call_args, call_kwargs = runner.api.container_requests().create.call_args @@ -303,6 +345,7 @@ class TestContainer(unittest.TestCase): } }, 'state': 'Committed', + 'output_name': 'Output for step test_initial_work_dir', 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz', 'output_path': '/var/spool/cwl', 'output_ttl': 0, @@ -349,14 +392,14 @@ class TestContainer(unittest.TestCase): "id": "#", "class": "CommandLineTool" }) - make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess, - collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0)) - arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, work_api="containers", avsc_names=avsc_names, - basedir="", make_fs_access=make_fs_access, loader=Loader({})) + + loadingContext, runtimeContext = self.helper(runner) + runtimeContext.name = "test_run_redirect" + + arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext) arvtool.formatgraph = None - for j in arvtool.job({}, mock.MagicMock(), basedir="", name="test_run_redirect", - make_fs_access=make_fs_access, tmpdir="/tmp"): - j.run(priority=500) + for j in arvtool.job({}, mock.MagicMock(), runtimeContext): + j.run(runtimeContext) runner.api.container_requests().create.assert_called_with( body=JsonDiffMatcher({ 'environment': { @@ -390,6 +433,7 @@ class TestContainer(unittest.TestCase): }, }, 'state': 'Committed', + "output_name": "Output for step test_run_redirect", 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz', 'output_path': '/var/spool/cwl', 'output_ttl': 0, @@ -419,9 +463,13 @@ class TestContainer(unittest.TestCase): col().open.return_value = [] - arvjob = arvados_cwl.ArvadosContainer(runner) - arvjob.name = "testjob" - arvjob.builder = mock.MagicMock() + arvjob = arvados_cwl.ArvadosContainer(runner, + mock.MagicMock(), + {}, + None, + [], + [], + "testjob") arvjob.output_callback = mock.MagicMock() arvjob.collect_outputs = mock.MagicMock() arvjob.successCodes = [0] @@ -459,7 +507,8 @@ class TestContainer(unittest.TestCase): keepdocker.return_value = [("zzzzz-4zz18-zzzzzzzzzzzzzz3", "")] runner.api.collections().get().execute.return_value = { - "portable_data_hash": "99999999999999999999999999999993+99"} + "portable_data_hash": "99999999999999999999999999999994+99", + "manifest_text": ". 99999999999999999999999999999994+99 0:0:file1 0:0:file2"} document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0") @@ -474,10 +523,11 @@ class TestContainer(unittest.TestCase): "id": "#", "class": "CommandLineTool" }) - make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess, - collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0)) - arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, work_api="containers", avsc_names=avsc_names, - basedir="", make_fs_access=make_fs_access, loader=Loader({})) + + loadingContext, runtimeContext = self.helper(runner) + runtimeContext.name = "test_run_mounts" + + arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext) arvtool.formatgraph = None job_order = { "p1": { @@ -495,9 +545,8 @@ class TestContainer(unittest.TestCase): ] } } - for j in arvtool.job(job_order, mock.MagicMock(), basedir="", name="test_run_mounts", - make_fs_access=make_fs_access, tmpdir="/tmp"): - j.run(priority=500) + for j in arvtool.job(job_order, mock.MagicMock(), runtimeContext): + j.run(runtimeContext) runner.api.container_requests().create.assert_called_with( body=JsonDiffMatcher({ 'environment': { @@ -522,6 +571,7 @@ class TestContainer(unittest.TestCase): "capacity": 1073741824 } }, 'state': 'Committed', + 'output_name': 'Output for step test_run_mounts', 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz', 'output_path': '/var/spool/cwl', 'output_ttl': 0, @@ -581,18 +631,18 @@ class TestContainer(unittest.TestCase): ] } ]}) - make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess, - collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0)) - arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, work_api="containers", avsc_names=avsc_names, - basedir="", make_fs_access=make_fs_access, loader=Loader({})) + + loadingContext, runtimeContext = self.helper(runner) + runtimeContext.name = "test_secrets" + + arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext) arvtool.formatgraph = None job_order = {"pw": "blorp"} runner.secret_store.store(["pw"], job_order) - for j in arvtool.job(job_order, mock.MagicMock(), basedir="", name="test_secrets", - make_fs_access=make_fs_access, tmpdir="/tmp"): - j.run(enable_reuse=True, priority=500) + for j in arvtool.job(job_order, mock.MagicMock(), runtimeContext): + j.run(runtimeContext) runner.api.container_requests().create.assert_called_with( body=JsonDiffMatcher({ 'environment': { @@ -614,6 +664,7 @@ class TestContainer(unittest.TestCase): "capacity": 1073741824 } }, 'state': 'Committed', + 'output_name': 'Output for step test_secrets', 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz', 'output_path': '/var/spool/cwl', 'output_ttl': 0, @@ -629,3 +680,46 @@ class TestContainer(unittest.TestCase): } } })) + + # The test passes no builder.resources + # Hence the default resources will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024} + @mock.patch("arvados.commands.keepdocker.list_images_in_arv") + def test_timelimit(self, keepdocker): + arv_docker_clear_cache() + + runner = mock.MagicMock() + runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz" + runner.ignore_docker_for_reuse = False + runner.intermediate_output_ttl = 0 + runner.secret_store = cwltool.secrets.SecretStore() + + keepdocker.return_value = [("zzzzz-4zz18-zzzzzzzzzzzzzz3", "")] + runner.api.collections().get().execute.return_value = { + "portable_data_hash": "99999999999999999999999999999993+99"} + + tool = cmap({ + "inputs": [], + "outputs": [], + "baseCommand": "ls", + "arguments": [{"valueFrom": "$(runtime.outdir)"}], + "id": "#", + "class": "CommandLineTool", + "hints": [ + { + "class": "http://commonwl.org/cwltool#TimeLimit", + "timelimit": 42 + } + ] + }) + + loadingContext, runtimeContext = self.helper(runner) + runtimeContext.name = "test_timelimit" + + arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext) + arvtool.formatgraph = None + + for j in arvtool.job({}, mock.MagicMock(), runtimeContext): + j.run(runtimeContext) + + _, kwargs = runner.api.container_requests().create.call_args + self.assertEqual(42, kwargs['body']['scheduling_parameters'].get('max_run_time'))