1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
16 import cwltool.process
17 from arvados.errors import ApiError
18 from schema_salad.ref_resolver import Loader
19 from schema_salad.sourceline import cmap
20 from .mock_discovery import get_rootDesc
21 from .matcher import JsonDiffMatcher, StripYAMLComments
23 if not os.getenv('ARVADOS_DEBUG'):
24 logging.getLogger('arvados.cwl-runner').setLevel(logging.WARN)
25 logging.getLogger('arvados.arv-run').setLevel(logging.WARN)
27 class TestJob(unittest.TestCase):
29 # The test passes no builder.resources
30 # Hence the default resources will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
31 @mock.patch('arvados.commands.keepdocker.list_images_in_arv')
32 def test_run(self, list_images_in_arv):
33 for enable_reuse in (True, False):
34 runner = mock.MagicMock()
35 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
36 runner.ignore_docker_for_reuse = False
37 runner.num_retries = 0
38 document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0")
40 list_images_in_arv.return_value = [["zzzzz-4zz18-zzzzzzzzzzzzzzz"]]
41 runner.api.collections().get().execute.return_value = {"portable_data_hash": "99999999999999999999999999999993+99"}
42 # Simulate reused job from another project so that we can check is a can_read
44 runner.api.jobs().create().execute.return_value = {
45 'state': 'Complete' if enable_reuse else 'Queued',
46 'owner_uuid': 'zzzzz-tpzed-yyyyyyyyyyyyyyy' if enable_reuse else 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
47 'uuid': 'zzzzz-819sb-yyyyyyyyyyyyyyy',
55 "arguments": [{"valueFrom": "$(runtime.outdir)"}],
58 make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess,
59 collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0))
60 arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, work_api="jobs", avsc_names=avsc_names,
61 basedir="", make_fs_access=make_fs_access, loader=Loader({}))
62 arvtool.formatgraph = None
63 for j in arvtool.job({}, mock.MagicMock(), basedir="", make_fs_access=make_fs_access):
64 j.run(enable_reuse=enable_reuse)
65 runner.api.jobs().create.assert_called_with(
66 body=JsonDiffMatcher({
67 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
68 'runtime_constraints': {},
69 'script_parameters': {
71 'task.env': {'HOME': '$(task.outdir)', 'TMPDIR': '$(task.tmpdir)'},
72 'command': ['ls', '$(task.outdir)']
75 'script_version': 'master',
76 'minimum_script_version': 'a3f2cb186e437bfce0031b024b2157b73ed2717d',
77 'repository': 'arvados',
78 'script': 'crunchrunner',
79 'runtime_constraints': {
80 'docker_image': 'arvados/jobs',
81 'min_cores_per_node': 1,
82 'min_ram_mb_per_node': 1024,
83 'min_scratch_mb_per_node': 2048 # tmpdirSize + outdirSize
86 find_or_create=enable_reuse,
87 filters=[['repository', '=', 'arvados'],
88 ['script', '=', 'crunchrunner'],
89 ['script_version', 'in git', 'a3f2cb186e437bfce0031b024b2157b73ed2717d'],
90 ['docker_image_locator', 'in docker', 'arvados/jobs']]
93 runner.api.links().create.assert_called_with(
94 body=JsonDiffMatcher({
95 'link_class': 'permission',
97 "tail_uuid": "zzzzz-8i9sb-zzzzzzzzzzzzzzz",
98 "head_uuid": "zzzzz-819sb-yyyyyyyyyyyyyyy",
101 # Simulate an API excepction when trying to create a
102 # sharing link on the job
103 runner.api.links().create.side_effect = ApiError(
104 mock.MagicMock(return_value={'status': 403}),
106 j.run(enable_reuse=enable_reuse)
107 j.output_callback.assert_called_with({}, 'success')
109 assert not runner.api.links().create.called
111 # The test passes some fields in builder.resources
112 # For the remaining fields, the defaults will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
113 @mock.patch('arvados.commands.keepdocker.list_images_in_arv')
114 def test_resource_requirements(self, list_images_in_arv):
115 runner = mock.MagicMock()
116 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
117 runner.ignore_docker_for_reuse = False
118 runner.num_retries = 0
119 arvados_cwl.add_arv_hints()
121 list_images_in_arv.return_value = [["zzzzz-4zz18-zzzzzzzzzzzzzzz"]]
122 runner.api.collections().get().execute.return_vaulue = {"portable_data_hash": "99999999999999999999999999999993+99"}
124 document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0")
131 "class": "ResourceRequirement",
136 "class": "http://arvados.org/cwl#RuntimeConstraints",
138 "outputDirType": "keep_output_dir"
140 "class": "http://arvados.org/cwl#APIRequirement",
143 "class": "http://arvados.org/cwl#ReuseRequirement",
149 make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess,
150 collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0))
151 arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, work_api="jobs", avsc_names=avsc_names,
152 make_fs_access=make_fs_access, loader=Loader({}))
153 arvtool.formatgraph = None
154 for j in arvtool.job({}, mock.MagicMock(), basedir="", make_fs_access=make_fs_access):
155 j.run(enable_reuse=True)
156 runner.api.jobs().create.assert_called_with(
157 body=JsonDiffMatcher({
158 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
159 'runtime_constraints': {},
160 'script_parameters': {
162 'task.env': {'HOME': '$(task.outdir)', 'TMPDIR': '$(task.tmpdir)'},
163 'task.keepTmpOutput': True,
167 'script_version': 'master',
168 'minimum_script_version': 'a3f2cb186e437bfce0031b024b2157b73ed2717d',
169 'repository': 'arvados',
170 'script': 'crunchrunner',
171 'runtime_constraints': {
172 'docker_image': 'arvados/jobs',
173 'min_cores_per_node': 3,
174 'min_ram_mb_per_node': 3512, # ramMin + keep_cache
175 'min_scratch_mb_per_node': 5024, # tmpdirSize + outdirSize
176 'keep_cache_mb_per_task': 512
179 find_or_create=False,
180 filters=[['repository', '=', 'arvados'],
181 ['script', '=', 'crunchrunner'],
182 ['script_version', 'in git', 'a3f2cb186e437bfce0031b024b2157b73ed2717d'],
183 ['docker_image_locator', 'in docker', 'arvados/jobs']])
185 @mock.patch("arvados.collection.CollectionReader")
186 def test_done(self, reader):
187 api = mock.MagicMock()
189 runner = mock.MagicMock()
191 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
192 runner.num_retries = 0
193 runner.ignore_docker_for_reuse = False
195 reader().open.return_value = StringIO.StringIO(
196 """2016-11-02_23:12:18 c97qk-8i9sb-cryqw2blvzy4yaj 13358 0 stderr 2016/11/02 23:12:18 crunchrunner: $(task.tmpdir)=/tmp/crunch-job-task-work/compute3.1/tmpdir
197 2016-11-02_23:12:18 c97qk-8i9sb-cryqw2blvzy4yaj 13358 0 stderr 2016/11/02 23:12:18 crunchrunner: $(task.outdir)=/tmp/crunch-job-task-work/compute3.1/outdir
198 2016-11-02_23:12:18 c97qk-8i9sb-cryqw2blvzy4yaj 13358 0 stderr 2016/11/02 23:12:18 crunchrunner: $(task.keep)=/keep
200 api.collections().list().execute.side_effect = ({"items": []},
201 {"items": [{"manifest_text": "XYZ"}]},
203 {"items": [{"manifest_text": "ABC"}]})
205 arvjob = arvados_cwl.ArvadosJob(runner)
206 arvjob.name = "testjob"
207 arvjob.builder = mock.MagicMock()
208 arvjob.output_callback = mock.MagicMock()
209 arvjob.collect_outputs = mock.MagicMock()
210 arvjob.collect_outputs.return_value = {"out": "stuff"}
214 "output": "99999999999999999999999999999993+99",
215 "log": "99999999999999999999999999999994+99",
216 "uuid": "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
219 api.collections().list.assert_has_calls([
221 # Output collection check
222 mock.call(filters=[['owner_uuid', '=', 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'],
223 ['portable_data_hash', '=', '99999999999999999999999999999993+99'],
224 ['name', '=', 'Output 9999999 of testjob']]),
225 mock.call().execute(num_retries=0),
226 mock.call(limit=1, filters=[['portable_data_hash', '=', '99999999999999999999999999999993+99']],
227 select=['manifest_text']),
228 mock.call().execute(num_retries=0),
229 # Log collection's turn
230 mock.call(filters=[['owner_uuid', '=', 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'],
231 ['portable_data_hash', '=', '99999999999999999999999999999994+99'],
232 ['name', '=', 'Log of zzzzz-8i9sb-zzzzzzzzzzzzzzz']]),
233 mock.call().execute(num_retries=0),
234 mock.call(limit=1, filters=[['portable_data_hash', '=', '99999999999999999999999999999994+99']],
235 select=['manifest_text']),
236 mock.call().execute(num_retries=0)])
238 api.collections().create.assert_has_calls([
239 mock.call(ensure_unique_name=True,
240 body={'portable_data_hash': '99999999999999999999999999999993+99',
241 'manifest_text': 'XYZ',
242 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
243 'name': 'Output 9999999 of testjob'}),
244 mock.call().execute(num_retries=0),
245 mock.call(ensure_unique_name=True,
246 body={'portable_data_hash': '99999999999999999999999999999994+99',
247 'manifest_text': 'ABC',
248 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
249 'name': 'Log of zzzzz-8i9sb-zzzzzzzzzzzzzzz'}),
250 mock.call().execute(num_retries=0),
253 arvjob.output_callback.assert_called_with({"out": "stuff"}, "success")
255 @mock.patch("arvados.collection.CollectionReader")
256 def test_done_use_existing_collection(self, reader):
257 api = mock.MagicMock()
259 runner = mock.MagicMock()
261 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
262 runner.num_retries = 0
264 reader().open.return_value = StringIO.StringIO(
265 """2016-11-02_23:12:18 c97qk-8i9sb-cryqw2blvzy4yaj 13358 0 stderr 2016/11/02 23:12:18 crunchrunner: $(task.tmpdir)=/tmp/crunch-job-task-work/compute3.1/tmpdir
266 2016-11-02_23:12:18 c97qk-8i9sb-cryqw2blvzy4yaj 13358 0 stderr 2016/11/02 23:12:18 crunchrunner: $(task.outdir)=/tmp/crunch-job-task-work/compute3.1/outdir
267 2016-11-02_23:12:18 c97qk-8i9sb-cryqw2blvzy4yaj 13358 0 stderr 2016/11/02 23:12:18 crunchrunner: $(task.keep)=/keep
270 api.collections().list().execute.side_effect = (
271 {"items": [{"uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz2"}]},
272 {"items": [{"uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz2"}]},
275 arvjob = arvados_cwl.ArvadosJob(runner)
276 arvjob.name = "testjob"
277 arvjob.builder = mock.MagicMock()
278 arvjob.output_callback = mock.MagicMock()
279 arvjob.collect_outputs = mock.MagicMock()
280 arvjob.collect_outputs.return_value = {"out": "stuff"}
284 "output": "99999999999999999999999999999993+99",
285 "log": "99999999999999999999999999999994+99",
286 "uuid": "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
289 api.collections().list.assert_has_calls([
292 mock.call(filters=[['owner_uuid', '=', 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'],
293 ['portable_data_hash', '=', '99999999999999999999999999999993+99'],
294 ['name', '=', 'Output 9999999 of testjob']]),
295 mock.call().execute(num_retries=0),
297 mock.call(filters=[['owner_uuid', '=', 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'],
298 ['portable_data_hash', '=', '99999999999999999999999999999994+99'],
299 ['name', '=', 'Log of zzzzz-8i9sb-zzzzzzzzzzzzzzz']]),
300 mock.call().execute(num_retries=0)
303 self.assertFalse(api.collections().create.called)
305 arvjob.output_callback.assert_called_with({"out": "stuff"}, "success")
308 class TestWorkflow(unittest.TestCase):
309 # The test passes no builder.resources
310 # Hence the default resources will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
311 @mock.patch("arvados.collection.CollectionReader")
312 @mock.patch("arvados.collection.Collection")
313 @mock.patch('arvados.commands.keepdocker.list_images_in_arv')
314 def test_run(self, list_images_in_arv, mockcollection, mockcollectionreader):
315 arvados_cwl.add_arv_hints()
317 api = mock.MagicMock()
318 api._rootDesc = get_rootDesc()
320 runner = arvados_cwl.ArvCwlRunner(api)
321 self.assertEqual(runner.work_api, 'jobs')
323 list_images_in_arv.return_value = [["zzzzz-4zz18-zzzzzzzzzzzzzzz"]]
324 runner.api.collections().get().execute.return_vaulue = {"portable_data_hash": "99999999999999999999999999999993+99"}
325 runner.api.collections().list().execute.return_vaulue = {"items": [{"portable_data_hash": "99999999999999999999999999999993+99"}]}
327 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
328 runner.ignore_docker_for_reuse = False
329 runner.num_retries = 0
330 document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0")
332 make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess,
333 collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0))
334 document_loader.fetcher_constructor = functools.partial(arvados_cwl.CollectionFetcher, api_client=api, fs_access=make_fs_access(""))
335 document_loader.fetcher = document_loader.fetcher_constructor(document_loader.cache, document_loader.session)
336 document_loader.fetch_text = document_loader.fetcher.fetch_text
337 document_loader.check_exists = document_loader.fetcher.check_exists
339 tool, metadata = document_loader.resolve_ref("tests/wf/scatter2.cwl")
340 metadata["cwlVersion"] = tool["cwlVersion"]
342 mockcollection().portable_data_hash.return_value = "99999999999999999999999999999999+118"
344 arvtool = arvados_cwl.ArvadosWorkflow(runner, tool, work_api="jobs", avsc_names=avsc_names,
345 basedir="", make_fs_access=make_fs_access, loader=document_loader,
346 makeTool=runner.arv_make_tool, metadata=metadata)
347 arvtool.formatgraph = None
348 it = arvtool.job({}, mock.MagicMock(), basedir="", make_fs_access=make_fs_access)
352 with open("tests/wf/scatter2_subwf.cwl") as f:
353 subwf = StripYAMLComments(f.read())
355 runner.api.jobs().create.assert_called_with(
356 body=JsonDiffMatcher({
357 'minimum_script_version': 'a3f2cb186e437bfce0031b024b2157b73ed2717d',
358 'repository': 'arvados',
359 'script_version': 'master',
360 'script': 'crunchrunner',
361 'script_parameters': {
362 'tasks': [{'task.env': {
363 'HOME': '$(task.outdir)',
364 'TMPDIR': '$(task.tmpdir)'},
366 'workflow.cwl': '$(task.keep)/99999999999999999999999999999999+118/workflow.cwl',
367 'cwl.input.yml': '$(task.keep)/99999999999999999999999999999999+118/cwl.input.yml'
369 'command': [u'cwltool', u'--no-container', u'--move-outputs', u'--preserve-entire-environment', u'workflow.cwl#main', u'cwl.input.yml'],
370 'task.stdout': 'cwl.output.json'}]},
371 'runtime_constraints': {
372 'min_scratch_mb_per_node': 2048,
373 'min_cores_per_node': 1,
374 'docker_image': 'arvados/jobs',
375 'min_ram_mb_per_node': 1024
377 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'}),
378 filters=[['repository', '=', 'arvados'],
379 ['script', '=', 'crunchrunner'],
380 ['script_version', 'in git', 'a3f2cb186e437bfce0031b024b2157b73ed2717d'],
381 ['docker_image_locator', 'in docker', 'arvados/jobs']],
384 mockcollection().open().__enter__().write.assert_has_calls([mock.call(subwf)])
385 mockcollection().open().__enter__().write.assert_has_calls([mock.call(
388 "basename": "token.txt",
390 "location": "/keep/99999999999999999999999999999999+118/token.txt"
395 def test_default_work_api(self):
396 arvados_cwl.add_arv_hints()
398 api = mock.MagicMock()
399 api._rootDesc = copy.deepcopy(get_rootDesc())
400 del api._rootDesc.get('resources')['jobs']['methods']['create']
401 runner = arvados_cwl.ArvCwlRunner(api)
402 self.assertEqual(runner.work_api, 'containers')