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
22 from .test_container import CollectionMock
24 if not os.getenv('ARVADOS_DEBUG'):
25 logging.getLogger('arvados.cwl-runner').setLevel(logging.WARN)
26 logging.getLogger('arvados.arv-run').setLevel(logging.WARN)
28 class TestJob(unittest.TestCase):
30 def helper(self, runner, enable_reuse=True):
31 document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0")
33 make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess,
34 collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0))
35 loadingContext = arvados_cwl.context.ArvLoadingContext(
36 {"avsc_names": avsc_names,
38 "make_fs_access": make_fs_access,
40 "metadata": {"cwlVersion": "v1.0"},
41 "makeTool": runner.arv_make_tool})
42 runtimeContext = arvados_cwl.context.ArvRuntimeContext(
45 "name": "test_run_job_"+str(enable_reuse),
46 "make_fs_access": make_fs_access,
47 "enable_reuse": enable_reuse,
50 return loadingContext, runtimeContext
52 # The test passes no builder.resources
53 # Hence the default resources will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
54 @mock.patch('arvados.commands.keepdocker.list_images_in_arv')
55 def test_run(self, list_images_in_arv):
56 for enable_reuse in (True, False):
57 runner = mock.MagicMock()
58 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
59 runner.ignore_docker_for_reuse = False
60 runner.num_retries = 0
62 list_images_in_arv.return_value = [["zzzzz-4zz18-zzzzzzzzzzzzzzz"]]
63 runner.api.collections().get().execute.return_value = {"portable_data_hash": "99999999999999999999999999999993+99"}
64 # Simulate reused job from another project so that we can check is a can_read
66 runner.api.jobs().create().execute.return_value = {
67 'state': 'Complete' if enable_reuse else 'Queued',
68 'owner_uuid': 'zzzzz-tpzed-yyyyyyyyyyyyyyy' if enable_reuse else 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
69 'uuid': 'zzzzz-819sb-yyyyyyyyyyyyyyy',
77 "arguments": [{"valueFrom": "$(runtime.outdir)"}],
79 "class": "CommandLineTool"
82 loadingContext, runtimeContext = self.helper(runner, enable_reuse)
84 arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext)
85 arvtool.formatgraph = None
86 for j in arvtool.job({}, mock.MagicMock(), runtimeContext):
88 runner.api.jobs().create.assert_called_with(
89 body=JsonDiffMatcher({
90 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
91 'runtime_constraints': {},
92 'script_parameters': {
94 'task.env': {'HOME': '$(task.outdir)', 'TMPDIR': '$(task.tmpdir)'},
95 'command': ['ls', '$(task.outdir)']
98 'script_version': 'master',
99 'minimum_script_version': 'a3f2cb186e437bfce0031b024b2157b73ed2717d',
100 'repository': 'arvados',
101 'script': 'crunchrunner',
102 'runtime_constraints': {
103 'docker_image': 'arvados/jobs',
104 'min_cores_per_node': 1,
105 'min_ram_mb_per_node': 1024,
106 'min_scratch_mb_per_node': 2048 # tmpdirSize + outdirSize
109 find_or_create=enable_reuse,
110 filters=[['repository', '=', 'arvados'],
111 ['script', '=', 'crunchrunner'],
112 ['script_version', 'in git', 'a3f2cb186e437bfce0031b024b2157b73ed2717d'],
113 ['docker_image_locator', 'in docker', 'arvados/jobs']]
116 runner.api.links().create.assert_called_with(
117 body=JsonDiffMatcher({
118 'link_class': 'permission',
120 "tail_uuid": "zzzzz-8i9sb-zzzzzzzzzzzzzzz",
121 "head_uuid": "zzzzz-819sb-yyyyyyyyyyyyyyy",
124 # Simulate an API excepction when trying to create a
125 # sharing link on the job
126 runner.api.links().create.side_effect = ApiError(
127 mock.MagicMock(return_value={'status': 403}),
129 j.run(runtimeContext)
131 assert not runner.api.links().create.called
133 # The test passes some fields in builder.resources
134 # For the remaining fields, the defaults will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
135 @mock.patch('arvados.commands.keepdocker.list_images_in_arv')
136 def test_resource_requirements(self, list_images_in_arv):
137 runner = mock.MagicMock()
138 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
139 runner.ignore_docker_for_reuse = False
140 runner.num_retries = 0
141 arvados_cwl.add_arv_hints()
143 list_images_in_arv.return_value = [["zzzzz-4zz18-zzzzzzzzzzzzzzz"]]
144 runner.api.collections().get().execute.return_vaulue = {"portable_data_hash": "99999999999999999999999999999993+99"}
150 "class": "ResourceRequirement",
155 "class": "http://arvados.org/cwl#RuntimeConstraints",
157 "outputDirType": "keep_output_dir"
159 "class": "http://arvados.org/cwl#APIRequirement",
162 "class": "http://arvados.org/cwl#ReuseRequirement",
167 "class": "CommandLineTool"
170 loadingContext, runtimeContext = self.helper(runner)
172 arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext)
173 arvtool.formatgraph = None
174 for j in arvtool.job({}, mock.MagicMock(), runtimeContext):
175 j.run(runtimeContext)
176 runner.api.jobs().create.assert_called_with(
177 body=JsonDiffMatcher({
178 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
179 'runtime_constraints': {},
180 'script_parameters': {
182 'task.env': {'HOME': '$(task.outdir)', 'TMPDIR': '$(task.tmpdir)'},
183 'task.keepTmpOutput': True,
187 'script_version': 'master',
188 'minimum_script_version': 'a3f2cb186e437bfce0031b024b2157b73ed2717d',
189 'repository': 'arvados',
190 'script': 'crunchrunner',
191 'runtime_constraints': {
192 'docker_image': 'arvados/jobs',
193 'min_cores_per_node': 3,
194 'min_ram_mb_per_node': 3512, # ramMin + keep_cache
195 'min_scratch_mb_per_node': 5024, # tmpdirSize + outdirSize
196 'keep_cache_mb_per_task': 512
199 find_or_create=False,
200 filters=[['repository', '=', 'arvados'],
201 ['script', '=', 'crunchrunner'],
202 ['script_version', 'in git', 'a3f2cb186e437bfce0031b024b2157b73ed2717d'],
203 ['docker_image_locator', 'in docker', 'arvados/jobs']])
205 @mock.patch("arvados.collection.CollectionReader")
206 def test_done(self, reader):
207 api = mock.MagicMock()
209 runner = mock.MagicMock()
211 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
212 runner.num_retries = 0
213 runner.ignore_docker_for_reuse = False
215 reader().open.return_value = StringIO.StringIO(
216 """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
217 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
218 2016-11-02_23:12:18 c97qk-8i9sb-cryqw2blvzy4yaj 13358 0 stderr 2016/11/02 23:12:18 crunchrunner: $(task.keep)=/keep
220 api.collections().list().execute.side_effect = ({"items": []},
221 {"items": [{"manifest_text": "XYZ"}]},
223 {"items": [{"manifest_text": "ABC"}]})
225 arvjob = arvados_cwl.ArvadosJob(runner,
232 arvjob.output_callback = mock.MagicMock()
233 arvjob.collect_outputs = mock.MagicMock()
234 arvjob.collect_outputs.return_value = {"out": "stuff"}
238 "output": "99999999999999999999999999999993+99",
239 "log": "99999999999999999999999999999994+99",
240 "uuid": "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
243 api.collections().list.assert_has_calls([
245 # Output collection check
246 mock.call(filters=[['owner_uuid', '=', 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'],
247 ['portable_data_hash', '=', '99999999999999999999999999999993+99'],
248 ['name', '=', 'Output 9999999 of testjob']]),
249 mock.call().execute(num_retries=0),
250 mock.call(limit=1, filters=[['portable_data_hash', '=', '99999999999999999999999999999993+99']],
251 select=['manifest_text']),
252 mock.call().execute(num_retries=0),
253 # Log collection's turn
254 mock.call(filters=[['owner_uuid', '=', 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'],
255 ['portable_data_hash', '=', '99999999999999999999999999999994+99'],
256 ['name', '=', 'Log of zzzzz-8i9sb-zzzzzzzzzzzzzzz']]),
257 mock.call().execute(num_retries=0),
258 mock.call(limit=1, filters=[['portable_data_hash', '=', '99999999999999999999999999999994+99']],
259 select=['manifest_text']),
260 mock.call().execute(num_retries=0)])
262 api.collections().create.assert_has_calls([
263 mock.call(ensure_unique_name=True,
264 body={'portable_data_hash': '99999999999999999999999999999993+99',
265 'manifest_text': 'XYZ',
266 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
267 'name': 'Output 9999999 of testjob'}),
268 mock.call().execute(num_retries=0),
269 mock.call(ensure_unique_name=True,
270 body={'portable_data_hash': '99999999999999999999999999999994+99',
271 'manifest_text': 'ABC',
272 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
273 'name': 'Log of zzzzz-8i9sb-zzzzzzzzzzzzzzz'}),
274 mock.call().execute(num_retries=0),
277 arvjob.output_callback.assert_called_with({"out": "stuff"}, "success")
279 @mock.patch("arvados.collection.CollectionReader")
280 def test_done_use_existing_collection(self, reader):
281 api = mock.MagicMock()
283 runner = mock.MagicMock()
285 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
286 runner.num_retries = 0
288 reader().open.return_value = StringIO.StringIO(
289 """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
290 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
291 2016-11-02_23:12:18 c97qk-8i9sb-cryqw2blvzy4yaj 13358 0 stderr 2016/11/02 23:12:18 crunchrunner: $(task.keep)=/keep
294 api.collections().list().execute.side_effect = (
295 {"items": [{"uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz2"}]},
296 {"items": [{"uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz2"}]},
299 arvjob = arvados_cwl.ArvadosJob(runner,
306 arvjob.output_callback = mock.MagicMock()
307 arvjob.collect_outputs = mock.MagicMock()
308 arvjob.collect_outputs.return_value = {"out": "stuff"}
312 "output": "99999999999999999999999999999993+99",
313 "log": "99999999999999999999999999999994+99",
314 "uuid": "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
317 api.collections().list.assert_has_calls([
320 mock.call(filters=[['owner_uuid', '=', 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'],
321 ['portable_data_hash', '=', '99999999999999999999999999999993+99'],
322 ['name', '=', 'Output 9999999 of testjob']]),
323 mock.call().execute(num_retries=0),
325 mock.call(filters=[['owner_uuid', '=', 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'],
326 ['portable_data_hash', '=', '99999999999999999999999999999994+99'],
327 ['name', '=', 'Log of zzzzz-8i9sb-zzzzzzzzzzzzzzz']]),
328 mock.call().execute(num_retries=0)
331 self.assertFalse(api.collections().create.called)
333 arvjob.output_callback.assert_called_with({"out": "stuff"}, "success")
336 class TestWorkflow(unittest.TestCase):
337 def helper(self, runner, enable_reuse=True):
338 document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0")
340 make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess,
341 collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0))
343 document_loader.fetcher_constructor = functools.partial(arvados_cwl.CollectionFetcher, api_client=runner.api, fs_access=make_fs_access(""))
344 document_loader.fetcher = document_loader.fetcher_constructor(document_loader.cache, document_loader.session)
345 document_loader.fetch_text = document_loader.fetcher.fetch_text
346 document_loader.check_exists = document_loader.fetcher.check_exists
348 loadingContext = arvados_cwl.context.ArvLoadingContext(
349 {"avsc_names": avsc_names,
351 "make_fs_access": make_fs_access,
352 "loader": document_loader,
353 "metadata": {"cwlVersion": "v1.0"},
354 "construct_tool_object": runner.arv_make_tool})
355 runtimeContext = arvados_cwl.context.ArvRuntimeContext(
358 "name": "test_run_wf_"+str(enable_reuse),
359 "make_fs_access": make_fs_access,
360 "enable_reuse": enable_reuse,
363 return loadingContext, runtimeContext
365 # The test passes no builder.resources
366 # Hence the default resources will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
367 @mock.patch("arvados.collection.CollectionReader")
368 @mock.patch("arvados.collection.Collection")
369 @mock.patch('arvados.commands.keepdocker.list_images_in_arv')
370 def test_run(self, list_images_in_arv, mockcollection, mockcollectionreader):
371 arvados_cwl.add_arv_hints()
373 api = mock.MagicMock()
374 api._rootDesc = get_rootDesc()
376 runner = arvados_cwl.ArvCwlRunner(api)
377 self.assertEqual(runner.work_api, 'jobs')
379 list_images_in_arv.return_value = [["zzzzz-4zz18-zzzzzzzzzzzzzzz"]]
380 runner.api.collections().get().execute.return_vaulue = {"portable_data_hash": "99999999999999999999999999999993+99"}
381 runner.api.collections().list().execute.return_vaulue = {"items": [{"portable_data_hash": "99999999999999999999999999999993+99"}]}
383 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
384 runner.ignore_docker_for_reuse = False
385 runner.num_retries = 0
387 loadingContext, runtimeContext = self.helper(runner)
389 tool, metadata = loadingContext.loader.resolve_ref("tests/wf/scatter2.cwl")
390 metadata["cwlVersion"] = tool["cwlVersion"]
392 mockc = mock.MagicMock()
393 mockcollection.side_effect = lambda *args, **kwargs: CollectionMock(mockc, *args, **kwargs)
394 mockcollectionreader().find.return_value = arvados.arvfile.ArvadosFile(mock.MagicMock(), "token.txt")
396 arvtool = arvados_cwl.ArvadosWorkflow(runner, tool, loadingContext)
397 arvtool.formatgraph = None
398 it = arvtool.job({}, mock.MagicMock(), runtimeContext)
400 it.next().run(runtimeContext)
401 it.next().run(runtimeContext)
403 with open("tests/wf/scatter2_subwf.cwl") as f:
404 subwf = StripYAMLComments(f.read())
406 runner.api.jobs().create.assert_called_with(
407 body=JsonDiffMatcher({
408 'minimum_script_version': 'a3f2cb186e437bfce0031b024b2157b73ed2717d',
409 'repository': 'arvados',
410 'script_version': 'master',
411 'script': 'crunchrunner',
412 'script_parameters': {
413 'tasks': [{'task.env': {
414 'HOME': '$(task.outdir)',
415 'TMPDIR': '$(task.tmpdir)'},
417 'workflow.cwl': '$(task.keep)/99999999999999999999999999999996+99/workflow.cwl',
418 'cwl.input.yml': '$(task.keep)/99999999999999999999999999999996+99/cwl.input.yml'
420 'command': [u'cwltool', u'--no-container', u'--move-outputs', u'--preserve-entire-environment', u'workflow.cwl#main', u'cwl.input.yml'],
421 'task.stdout': 'cwl.output.json'}]},
422 'runtime_constraints': {
423 'min_scratch_mb_per_node': 2048,
424 'min_cores_per_node': 1,
425 'docker_image': 'arvados/jobs',
426 'min_ram_mb_per_node': 1024
428 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'}),
429 filters=[['repository', '=', 'arvados'],
430 ['script', '=', 'crunchrunner'],
431 ['script_version', 'in git', 'a3f2cb186e437bfce0031b024b2157b73ed2717d'],
432 ['docker_image_locator', 'in docker', 'arvados/jobs']],
435 mockc.open().__enter__().write.assert_has_calls([mock.call(subwf)])
436 mockc.open().__enter__().write.assert_has_calls([mock.call(
439 "basename": "token.txt",
441 "location": "/keep/99999999999999999999999999999999+118/token.txt",
447 # The test passes no builder.resources
448 # Hence the default resources will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
449 @mock.patch("arvados.collection.CollectionReader")
450 @mock.patch("arvados.collection.Collection")
451 @mock.patch('arvados.commands.keepdocker.list_images_in_arv')
452 def test_overall_resource_singlecontainer(self, list_images_in_arv, mockcollection, mockcollectionreader):
453 arvados_cwl.add_arv_hints()
455 api = mock.MagicMock()
456 api._rootDesc = get_rootDesc()
458 runner = arvados_cwl.ArvCwlRunner(api)
459 self.assertEqual(runner.work_api, 'jobs')
461 list_images_in_arv.return_value = [["zzzzz-4zz18-zzzzzzzzzzzzzzz"]]
462 runner.api.collections().get().execute.return_vaulue = {"portable_data_hash": "99999999999999999999999999999993+99"}
463 runner.api.collections().list().execute.return_vaulue = {"items": [{"portable_data_hash": "99999999999999999999999999999993+99"}]}
465 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
466 runner.ignore_docker_for_reuse = False
467 runner.num_retries = 0
469 loadingContext, runtimeContext = self.helper(runner)
471 tool, metadata = loadingContext.loader.resolve_ref("tests/wf/echo-wf.cwl")
472 metadata["cwlVersion"] = tool["cwlVersion"]
474 mockcollection.side_effect = lambda *args, **kwargs: CollectionMock(mock.MagicMock(), *args, **kwargs)
476 arvtool = arvados_cwl.ArvadosWorkflow(runner, tool, loadingContext)
477 arvtool.formatgraph = None
478 it = arvtool.job({}, mock.MagicMock(), runtimeContext)
479 it.next().run(runtimeContext)
480 it.next().run(runtimeContext)
482 with open("tests/wf/echo-subwf.cwl") as f:
483 subwf = StripYAMLComments(f.read())
485 runner.api.jobs().create.assert_called_with(
486 body=JsonDiffMatcher({
487 'minimum_script_version': 'a3f2cb186e437bfce0031b024b2157b73ed2717d',
488 'repository': 'arvados',
489 'script_version': 'master',
490 'script': 'crunchrunner',
491 'script_parameters': {
492 'tasks': [{'task.env': {
493 'HOME': '$(task.outdir)',
494 'TMPDIR': '$(task.tmpdir)'},
496 'workflow.cwl': '$(task.keep)/99999999999999999999999999999996+99/workflow.cwl',
497 'cwl.input.yml': '$(task.keep)/99999999999999999999999999999996+99/cwl.input.yml'
499 'command': [u'cwltool', u'--no-container', u'--move-outputs', u'--preserve-entire-environment', u'workflow.cwl#main', u'cwl.input.yml'],
500 'task.stdout': 'cwl.output.json'}]},
501 'runtime_constraints': {
502 'min_scratch_mb_per_node': 4096,
503 'min_cores_per_node': 3,
504 'docker_image': 'arvados/jobs',
505 'min_ram_mb_per_node': 1024
507 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'}),
508 filters=[['repository', '=', 'arvados'],
509 ['script', '=', 'crunchrunner'],
510 ['script_version', 'in git', 'a3f2cb186e437bfce0031b024b2157b73ed2717d'],
511 ['docker_image_locator', 'in docker', 'arvados/jobs']],
514 def test_default_work_api(self):
515 arvados_cwl.add_arv_hints()
517 api = mock.MagicMock()
518 api._rootDesc = copy.deepcopy(get_rootDesc())
519 del api._rootDesc.get('resources')['jobs']['methods']['create']
520 runner = arvados_cwl.ArvCwlRunner(api)
521 self.assertEqual(runner.work_api, 'containers')