12 import arvados.collection
14 import arvados_cwl.runner
17 from .matcher import JsonDiffMatcher
18 from .mock_discovery import get_rootDesc
20 import ruamel.yaml as yaml
25 @functools.wraps(func)
26 @mock.patch("arvados.commands.keepdocker.list_images_in_arv")
27 @mock.patch("arvados.collection.KeepClient")
28 @mock.patch("arvados.keep.KeepClient")
29 @mock.patch("arvados.events.subscribe")
30 def wrapped(self, events, keep_client1, keep_client2, keepdocker, *args, **kwargs):
35 stubs.keepdocker = keepdocker
38 def putstub(p, **kwargs):
39 return "%s+%i" % (hashlib.md5(p).hexdigest(), len(p))
40 keep_client1().put.side_effect = putstub
41 keep_client1.put.side_effect = putstub
42 keep_client2().put.side_effect = putstub
43 keep_client2.put.side_effect = putstub
45 stubs.keep_client = keep_client2
46 stubs.keepdocker.return_value = [("zzzzz-4zz18-zzzzzzzzzzzzzz3", "")]
47 stubs.fake_user_uuid = "zzzzz-tpzed-zzzzzzzzzzzzzzz"
49 stubs.api = mock.MagicMock()
50 stubs.api._rootDesc = get_rootDesc()
52 stubs.api.users().current().execute.return_value = {
53 "uuid": stubs.fake_user_uuid,
55 stubs.api.collections().list().execute.return_value = {"items": []}
56 stubs.api.collections().create().execute.side_effect = ({
57 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz1",
58 "portable_data_hash": "99999999999999999999999999999991+99",
61 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz2",
62 "portable_data_hash": "99999999999999999999999999999992+99",
63 "manifest_text": "./tool 00000000000000000000000000000000+0 0:0:submit_tool.cwl 0:0:blub.txt"
66 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz4",
67 "portable_data_hash": "99999999999999999999999999999994+99",
71 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz5",
72 "portable_data_hash": "99999999999999999999999999999995+99",
76 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz6",
77 "portable_data_hash": "99999999999999999999999999999996+99",
81 stubs.api.collections().get().execute.return_value = {
82 "portable_data_hash": "99999999999999999999999999999993+99", "manifest_text": "./tool 00000000000000000000000000000000+0 0:0:submit_tool.cwl 0:0:blub.txt"}
84 stubs.expect_job_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
85 stubs.api.jobs().create().execute.return_value = {
86 "uuid": stubs.expect_job_uuid,
90 stubs.expect_container_request_uuid = "zzzzz-xvhdp-zzzzzzzzzzzzzzz"
91 stubs.api.container_requests().create().execute.return_value = {
92 "uuid": stubs.expect_container_request_uuid,
93 "container_uuid": "zzzzz-dz642-zzzzzzzzzzzzzzz",
97 stubs.expect_pipeline_template_uuid = "zzzzz-d1hrv-zzzzzzzzzzzzzzz"
98 stubs.api.pipeline_templates().create().execute.return_value = {
99 "uuid": stubs.expect_pipeline_template_uuid,
101 stubs.expect_job_spec = {
102 'runtime_constraints': {
103 'docker_image': 'arvados/jobs:'+arvados_cwl.__version__,
104 'min_ram_mb_per_node': 1024
106 'script_parameters': {
108 'basename': 'blorp.txt',
109 'location': 'keep:99999999999999999999999999999992+99/blorp.txt',
113 'basename': '99999999999999999999999999999998+99',
114 'location': 'keep:99999999999999999999999999999998+99',
118 'basename': 'anonymous',
120 "basename": "renamed.txt",
122 "location": "keep:99999999999999999999999999999998+99/file1.txt"
127 '99999999999999999999999999999994+99/workflow.cwl#main'
129 'repository': 'arvados',
130 'script_version': 'master',
131 'minimum_script_version': '570509ab4d2ef93d870fd2b1f2eab178afb1bad9',
132 'script': 'cwl-runner'
134 stubs.pipeline_component = stubs.expect_job_spec.copy()
135 stubs.expect_pipeline_instance = {
136 'name': 'submit_wf.cwl',
137 'state': 'RunningOnServer',
141 'runtime_constraints': {'docker_image': 'arvados/jobs:'+arvados_cwl.__version__, 'min_ram_mb_per_node': 1024},
142 'script_parameters': {
143 'y': {"value": {'basename': '99999999999999999999999999999998+99', 'location': 'keep:99999999999999999999999999999998+99', 'class': 'Directory'}},
144 'x': {"value": {'basename': 'blorp.txt', 'class': 'File', 'location': 'keep:99999999999999999999999999999992+99/blorp.txt'}},
145 'z': {"value": {'basename': 'anonymous', 'class': 'Directory',
147 {'basename': 'renamed.txt', 'class': 'File', 'location': 'keep:99999999999999999999999999999998+99/file1.txt'}
149 'cwl:tool': '99999999999999999999999999999994+99/workflow.cwl#main',
150 'arv:enable_reuse': True,
151 'arv:on_error': 'continue'
153 'repository': 'arvados',
154 'script_version': 'master',
155 'minimum_script_version': '570509ab4d2ef93d870fd2b1f2eab178afb1bad9',
156 'script': 'cwl-runner',
157 'job': {'state': 'Queued', 'uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'}
161 stubs.pipeline_create = copy.deepcopy(stubs.expect_pipeline_instance)
162 stubs.expect_pipeline_uuid = "zzzzz-d1hrv-zzzzzzzzzzzzzzz"
163 stubs.pipeline_create["uuid"] = stubs.expect_pipeline_uuid
164 stubs.pipeline_with_job = copy.deepcopy(stubs.pipeline_create)
165 stubs.pipeline_with_job["components"]["cwl-runner"]["job"] = {
166 "uuid": "zzzzz-8i9sb-zzzzzzzzzzzzzzz",
169 stubs.api.pipeline_instances().create().execute.return_value = stubs.pipeline_create
170 stubs.api.pipeline_instances().get().execute.return_value = stubs.pipeline_with_job
172 with open("tests/wf/submit_wf_packed.cwl") as f:
173 expect_packed_workflow = yaml.round_trip_load(f)
175 stubs.expect_container_spec = {
182 '/var/lib/cwl/workflow.json': {
183 'content': expect_packed_workflow,
187 'path': '/var/spool/cwl/cwl.output.json',
190 '/var/lib/cwl/cwl.input.json': {
193 'y': {'basename': '99999999999999999999999999999998+99', 'location': 'keep:99999999999999999999999999999998+99', 'class': 'Directory'},
194 'x': {'basename': u'blorp.txt', 'class': 'File', 'location': u'keep:99999999999999999999999999999992+99/blorp.txt'},
195 'z': {'basename': 'anonymous', 'class': 'Directory', 'listing': [
196 {'basename': 'renamed.txt', 'class': 'File', 'location': 'keep:99999999999999999999999999999998+99/file1.txt'}
202 'state': 'Committed',
204 'command': ['arvados-cwl-runner', '--local', '--api=containers', '--no-log-timestamps',
205 '--enable-reuse', '--on-error=continue',
206 '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json'],
207 'name': 'submit_wf.cwl',
208 'container_image': 'arvados/jobs:'+arvados_cwl.__version__,
209 'output_path': '/var/spool/cwl',
210 'cwd': '/var/spool/cwl',
211 'runtime_constraints': {
214 'ram': 1024*1024*1024
219 stubs.expect_workflow_uuid = "zzzzz-7fd4e-zzzzzzzzzzzzzzz"
220 stubs.api.workflows().create().execute.return_value = {
221 "uuid": stubs.expect_workflow_uuid,
223 def update_mock(**kwargs):
224 stubs.updated_uuid = kwargs.get('uuid')
226 stubs.api.workflows().update.side_effect = update_mock
227 stubs.api.workflows().update().execute.side_effect = lambda **kwargs: {
228 "uuid": stubs.updated_uuid,
231 return func(self, stubs, *args, **kwargs)
235 class TestSubmit(unittest.TestCase):
236 @mock.patch("arvados_cwl.runner.arv_docker_get_image")
237 @mock.patch("time.sleep")
239 def test_submit(self, stubs, tm, arvdock):
240 capture_stdout = cStringIO.StringIO()
241 exited = arvados_cwl.main(
242 ["--submit", "--no-wait", "--api=jobs", "--debug",
243 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
244 capture_stdout, sys.stderr, api_client=stubs.api)
245 self.assertEqual(exited, 0)
247 stubs.api.collections().create.assert_has_calls([
249 mock.call(body=JsonDiffMatcher({
251 '. 5bcc9fe8f8d5992e6cf418dc7ce4dbb3+16 0:16:blub.txt\n',
253 'name': 'submit_tool.cwl dependencies',
254 }), ensure_unique_name=True),
255 mock.call().execute(),
256 mock.call(body=JsonDiffMatcher({
258 '. 979af1245a12a1fed634d4222473bfdc+16 0:16:blorp.txt\n',
260 'name': 'submit_wf.cwl input',
261 }), ensure_unique_name=True),
262 mock.call().execute()])
264 arvdock.assert_has_calls([
265 mock.call(stubs.api, {"class": "DockerRequirement", "dockerPull": "debian:8"}, True, None),
266 mock.call(stubs.api, {'dockerPull': 'arvados/jobs:'+arvados_cwl.__version__}, True, None)
269 expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
270 stubs.api.pipeline_instances().create.assert_called_with(
271 body=JsonDiffMatcher(expect_pipeline))
272 self.assertEqual(capture_stdout.getvalue(),
273 stubs.expect_pipeline_uuid + '\n')
276 @mock.patch("time.sleep")
278 def test_submit_no_reuse(self, stubs, tm):
279 capture_stdout = cStringIO.StringIO()
280 exited = arvados_cwl.main(
281 ["--submit", "--no-wait", "--api=jobs", "--debug", "--disable-reuse",
282 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
283 capture_stdout, sys.stderr, api_client=stubs.api)
284 self.assertEqual(exited, 0)
286 expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
287 expect_pipeline["components"]["cwl-runner"]["script_parameters"]["arv:enable_reuse"] = {"value": False}
289 stubs.api.pipeline_instances().create.assert_called_with(
290 body=JsonDiffMatcher(expect_pipeline))
291 self.assertEqual(capture_stdout.getvalue(),
292 stubs.expect_pipeline_uuid + '\n')
294 @mock.patch("time.sleep")
296 def test_submit_on_error(self, stubs, tm):
297 capture_stdout = cStringIO.StringIO()
298 exited = arvados_cwl.main(
299 ["--submit", "--no-wait", "--api=jobs", "--debug", "--on-error=stop",
300 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
301 capture_stdout, sys.stderr, api_client=stubs.api)
302 self.assertEqual(exited, 0)
304 expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
305 expect_pipeline["components"]["cwl-runner"]["script_parameters"]["arv:on_error"] = "stop"
307 stubs.api.pipeline_instances().create.assert_called_with(
308 body=JsonDiffMatcher(expect_pipeline))
309 self.assertEqual(capture_stdout.getvalue(),
310 stubs.expect_pipeline_uuid + '\n')
313 @mock.patch("time.sleep")
315 def test_submit_runner_ram(self, stubs, tm):
316 capture_stdout = cStringIO.StringIO()
317 exited = arvados_cwl.main(
318 ["--submit", "--no-wait", "--debug", "--submit-runner-ram=2048",
319 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
320 capture_stdout, sys.stderr, api_client=stubs.api)
321 self.assertEqual(exited, 0)
323 expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
324 expect_pipeline["components"]["cwl-runner"]["runtime_constraints"]["min_ram_mb_per_node"] = 2048
326 stubs.api.pipeline_instances().create.assert_called_with(
327 body=JsonDiffMatcher(expect_pipeline))
328 self.assertEqual(capture_stdout.getvalue(),
329 stubs.expect_pipeline_uuid + '\n')
332 @mock.patch("time.sleep")
334 def test_submit_invalid_runner_ram(self, stubs, tm):
335 capture_stdout = cStringIO.StringIO()
336 exited = arvados_cwl.main(
337 ["--submit", "--no-wait", "--debug", "--submit-runner-ram=-2048",
338 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
339 capture_stdout, sys.stderr, api_client=stubs.api)
340 self.assertEqual(exited, 1)
342 @mock.patch("time.sleep")
344 def test_submit_output_name(self, stubs, tm):
345 output_name = "test_output_name"
347 capture_stdout = cStringIO.StringIO()
348 exited = arvados_cwl.main(
349 ["--submit", "--no-wait", "--debug", "--output-name", output_name,
350 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
351 capture_stdout, sys.stderr, api_client=stubs.api)
352 self.assertEqual(exited, 0)
354 expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
355 expect_pipeline["components"]["cwl-runner"]["script_parameters"]["arv:output_name"] = output_name
357 stubs.api.pipeline_instances().create.assert_called_with(
358 body=JsonDiffMatcher(expect_pipeline))
359 self.assertEqual(capture_stdout.getvalue(),
360 stubs.expect_pipeline_uuid + '\n')
363 @mock.patch("time.sleep")
365 def test_submit_pipeline_name(self, stubs, tm):
366 capture_stdout = cStringIO.StringIO()
367 exited = arvados_cwl.main(
368 ["--submit", "--no-wait", "--debug", "--name=hello job 123",
369 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
370 capture_stdout, sys.stderr, api_client=stubs.api)
371 self.assertEqual(exited, 0)
373 expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
374 expect_pipeline["name"] = "hello job 123"
376 stubs.api.pipeline_instances().create.assert_called_with(
377 body=expect_pipeline)
378 self.assertEqual(capture_stdout.getvalue(),
379 stubs.expect_pipeline_uuid + '\n')
381 @mock.patch("time.sleep")
383 def test_submit_output_tags(self, stubs, tm):
384 output_tags = "tag0,tag1,tag2"
386 capture_stdout = cStringIO.StringIO()
387 exited = arvados_cwl.main(
388 ["--submit", "--no-wait", "--debug", "--output-tags", output_tags,
389 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
390 capture_stdout, sys.stderr, api_client=stubs.api)
391 self.assertEqual(exited, 0)
393 expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
394 expect_pipeline["components"]["cwl-runner"]["script_parameters"]["arv:output_tags"] = output_tags
396 stubs.api.pipeline_instances().create.assert_called_with(
397 body=JsonDiffMatcher(expect_pipeline))
398 self.assertEqual(capture_stdout.getvalue(),
399 stubs.expect_pipeline_uuid + '\n')
401 @mock.patch("time.sleep")
403 def test_submit_with_project_uuid(self, stubs, tm):
404 project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
406 exited = arvados_cwl.main(
407 ["--submit", "--no-wait",
408 "--project-uuid", project_uuid,
409 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
410 sys.stdout, sys.stderr, api_client=stubs.api)
411 self.assertEqual(exited, 0)
413 expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
414 expect_pipeline["owner_uuid"] = project_uuid
415 stubs.api.pipeline_instances().create.assert_called_with(
416 body=JsonDiffMatcher(expect_pipeline))
419 def test_submit_container(self, stubs):
420 capture_stdout = cStringIO.StringIO()
422 exited = arvados_cwl.main(
423 ["--submit", "--no-wait", "--api=containers", "--debug",
424 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
425 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
426 self.assertEqual(exited, 0)
428 logging.exception("")
430 stubs.api.collections().create.assert_has_calls([
432 mock.call(body=JsonDiffMatcher({
434 '. 5bcc9fe8f8d5992e6cf418dc7ce4dbb3+16 0:16:blub.txt\n',
436 'name': 'submit_tool.cwl dependencies',
437 }), ensure_unique_name=True),
438 mock.call().execute(),
439 mock.call(body=JsonDiffMatcher({
441 '. 979af1245a12a1fed634d4222473bfdc+16 0:16:blorp.txt\n',
443 'name': 'submit_wf.cwl input',
444 }), ensure_unique_name=True),
445 mock.call().execute()])
447 expect_container = copy.deepcopy(stubs.expect_container_spec)
448 stubs.api.container_requests().create.assert_called_with(
449 body=JsonDiffMatcher(expect_container))
450 self.assertEqual(capture_stdout.getvalue(),
451 stubs.expect_container_request_uuid + '\n')
454 def test_submit_container_no_reuse(self, stubs):
455 capture_stdout = cStringIO.StringIO()
457 exited = arvados_cwl.main(
458 ["--submit", "--no-wait", "--api=containers", "--debug", "--disable-reuse",
459 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
460 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
461 self.assertEqual(exited, 0)
463 logging.exception("")
465 expect_container = copy.deepcopy(stubs.expect_container_spec)
466 expect_container["command"] = ['arvados-cwl-runner', '--local', '--api=containers', '--no-log-timestamps',
467 '--disable-reuse', '--on-error=continue',
468 '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
470 stubs.api.container_requests().create.assert_called_with(
471 body=JsonDiffMatcher(expect_container))
472 self.assertEqual(capture_stdout.getvalue(),
473 stubs.expect_container_request_uuid + '\n')
477 def test_submit_container_on_error(self, stubs):
478 capture_stdout = cStringIO.StringIO()
480 exited = arvados_cwl.main(
481 ["--submit", "--no-wait", "--api=containers", "--debug", "--on-error=stop",
482 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
483 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
484 self.assertEqual(exited, 0)
486 logging.exception("")
488 expect_container = copy.deepcopy(stubs.expect_container_spec)
489 expect_container["command"] = ['arvados-cwl-runner', '--local', '--api=containers', '--no-log-timestamps',
490 '--enable-reuse', '--on-error=stop',
491 '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
493 stubs.api.container_requests().create.assert_called_with(
494 body=JsonDiffMatcher(expect_container))
495 self.assertEqual(capture_stdout.getvalue(),
496 stubs.expect_container_request_uuid + '\n')
499 def test_submit_container_output_name(self, stubs):
500 output_name = "test_output_name"
502 capture_stdout = cStringIO.StringIO()
504 exited = arvados_cwl.main(
505 ["--submit", "--no-wait", "--api=containers", "--debug", "--output-name", output_name,
506 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
507 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
508 self.assertEqual(exited, 0)
510 logging.exception("")
512 expect_container = copy.deepcopy(stubs.expect_container_spec)
513 expect_container["command"] = ['arvados-cwl-runner', '--local', '--api=containers', '--no-log-timestamps',
514 "--output-name="+output_name, '--enable-reuse', '--on-error=continue',
515 '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
516 expect_container["output_name"] = output_name
518 stubs.api.container_requests().create.assert_called_with(
519 body=JsonDiffMatcher(expect_container))
520 self.assertEqual(capture_stdout.getvalue(),
521 stubs.expect_container_request_uuid + '\n')
524 def test_submit_container_output_tags(self, stubs):
525 output_tags = "tag0,tag1,tag2"
527 capture_stdout = cStringIO.StringIO()
529 exited = arvados_cwl.main(
530 ["--submit", "--no-wait", "--api=containers", "--debug", "--output-tags", output_tags,
531 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
532 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
533 self.assertEqual(exited, 0)
535 logging.exception("")
537 expect_container = copy.deepcopy(stubs.expect_container_spec)
538 expect_container["command"] = ['arvados-cwl-runner', '--local', '--api=containers', '--no-log-timestamps',
539 "--output-tags="+output_tags, '--enable-reuse', '--on-error=continue',
540 '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
542 stubs.api.container_requests().create.assert_called_with(
543 body=JsonDiffMatcher(expect_container))
544 self.assertEqual(capture_stdout.getvalue(),
545 stubs.expect_container_request_uuid + '\n')
548 def test_submit_container_runner_ram(self, stubs):
549 capture_stdout = cStringIO.StringIO()
551 exited = arvados_cwl.main(
552 ["--submit", "--no-wait", "--api=containers", "--debug", "--submit-runner-ram=2048",
553 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
554 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
555 self.assertEqual(exited, 0)
557 logging.exception("")
559 expect_container = copy.deepcopy(stubs.expect_container_spec)
560 expect_container["runtime_constraints"]["ram"] = 2048*1024*1024
562 stubs.api.container_requests().create.assert_called_with(
563 body=JsonDiffMatcher(expect_container))
564 self.assertEqual(capture_stdout.getvalue(),
565 stubs.expect_container_request_uuid + '\n')
567 @mock.patch("arvados.collection.CollectionReader")
568 @mock.patch("time.sleep")
570 def test_submit_file_keepref(self, stubs, tm, collectionReader):
571 capture_stdout = cStringIO.StringIO()
572 exited = arvados_cwl.main(
573 ["--submit", "--no-wait", "--api=containers", "--debug",
574 "tests/wf/submit_keepref_wf.cwl"],
575 capture_stdout, sys.stderr, api_client=stubs.api)
576 self.assertEqual(exited, 0)
579 @mock.patch("arvados.collection.CollectionReader")
580 @mock.patch("time.sleep")
582 def test_submit_keepref(self, stubs, tm, reader):
583 capture_stdout = cStringIO.StringIO()
585 with open("tests/wf/expect_arvworkflow.cwl") as f:
586 reader().open().__enter__().read.return_value = f.read()
588 exited = arvados_cwl.main(
589 ["--submit", "--no-wait", "--api=containers", "--debug",
590 "keep:99999999999999999999999999999994+99/expect_arvworkflow.cwl#main", "-x", "XxX"],
591 capture_stdout, sys.stderr, api_client=stubs.api)
592 self.assertEqual(exited, 0)
602 'path': '/var/spool/cwl/cwl.output.json',
605 '/var/lib/cwl/workflow': {
606 'portable_data_hash': '99999999999999999999999999999994+99',
609 '/var/lib/cwl/cwl.input.json': {
615 }, 'state': 'Committed',
617 'output_path': '/var/spool/cwl',
618 'name': 'expect_arvworkflow.cwl#main',
619 'container_image': 'arvados/jobs:'+arvados_cwl.__version__,
620 'command': ['arvados-cwl-runner', '--local', '--api=containers', '--no-log-timestamps',
621 '--enable-reuse', '--on-error=continue',
622 '/var/lib/cwl/workflow/expect_arvworkflow.cwl#main', '/var/lib/cwl/cwl.input.json'],
623 'cwd': '/var/spool/cwl',
624 'runtime_constraints': {
632 stubs.api.container_requests().create.assert_called_with(
633 body=JsonDiffMatcher(expect_container))
634 self.assertEqual(capture_stdout.getvalue(),
635 stubs.expect_container_request_uuid + '\n')
638 @mock.patch("arvados.collection.CollectionReader")
639 @mock.patch("time.sleep")
641 def test_submit_jobs_keepref(self, stubs, tm, reader):
642 capture_stdout = cStringIO.StringIO()
644 with open("tests/wf/expect_arvworkflow.cwl") as f:
645 reader().open().__enter__().read.return_value = f.read()
647 exited = arvados_cwl.main(
648 ["--submit", "--no-wait", "--api=jobs", "--debug",
649 "keep:99999999999999999999999999999994+99/expect_arvworkflow.cwl#main", "-x", "XxX"],
650 capture_stdout, sys.stderr, api_client=stubs.api)
651 self.assertEqual(exited, 0)
653 expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
654 expect_pipeline["components"]["cwl-runner"]["script_parameters"]["x"] = "XxX"
655 del expect_pipeline["components"]["cwl-runner"]["script_parameters"]["y"]
656 del expect_pipeline["components"]["cwl-runner"]["script_parameters"]["z"]
657 expect_pipeline["components"]["cwl-runner"]["script_parameters"]["cwl:tool"] = "99999999999999999999999999999994+99/expect_arvworkflow.cwl#main"
658 expect_pipeline["name"] = "expect_arvworkflow.cwl#main"
659 stubs.api.pipeline_instances().create.assert_called_with(
660 body=JsonDiffMatcher(expect_pipeline))
662 @mock.patch("time.sleep")
664 def test_submit_arvworkflow(self, stubs, tm):
665 capture_stdout = cStringIO.StringIO()
667 with open("tests/wf/expect_arvworkflow.cwl") as f:
668 stubs.api.workflows().get().execute.return_value = {"definition": f.read(), "name": "a test workflow"}
670 exited = arvados_cwl.main(
671 ["--submit", "--no-wait", "--api=containers", "--debug",
672 "962eh-7fd4e-gkbzl62qqtfig37", "-x", "XxX"],
673 capture_stdout, sys.stderr, api_client=stubs.api)
674 self.assertEqual(exited, 0)
684 'path': '/var/spool/cwl/cwl.output.json',
687 '/var/lib/cwl/workflow.json': {
690 'cwlVersion': 'v1.0',
695 {'type': 'string', 'id': '#main/x'}
698 {'in': [{'source': '#main/x', 'id': '#main/step1/x'}],
699 'run': '#submit_tool.cwl',
709 'inputBinding': {'position': 1},
711 'id': '#submit_tool.cwl/x'}
714 {'dockerPull': 'debian:8', 'class': 'DockerRequirement'}
716 'id': '#submit_tool.cwl',
718 'baseCommand': 'cat',
719 'class': 'CommandLineTool'
724 '/var/lib/cwl/cwl.input.json': {
730 }, 'state': 'Committed',
732 'output_path': '/var/spool/cwl',
733 'name': 'a test workflow',
734 'container_image': 'arvados/jobs:'+arvados_cwl.__version__,
735 'command': ['arvados-cwl-runner', '--local', '--api=containers', '--no-log-timestamps',
736 '--enable-reuse', '--on-error=continue',
737 '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json'],
738 'cwd': '/var/spool/cwl',
739 'runtime_constraints': {
745 "template_uuid": "962eh-7fd4e-gkbzl62qqtfig37"
749 stubs.api.container_requests().create.assert_called_with(
750 body=JsonDiffMatcher(expect_container))
751 self.assertEqual(capture_stdout.getvalue(),
752 stubs.expect_container_request_uuid + '\n')
756 def test_submit_container_name(self, stubs):
757 capture_stdout = cStringIO.StringIO()
759 exited = arvados_cwl.main(
760 ["--submit", "--no-wait", "--api=containers", "--debug", "--name=hello container 123",
761 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
762 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
763 self.assertEqual(exited, 0)
765 logging.exception("")
767 expect_container = copy.deepcopy(stubs.expect_container_spec)
768 expect_container["name"] = "hello container 123"
770 stubs.api.container_requests().create.assert_called_with(
771 body=JsonDiffMatcher(expect_container))
772 self.assertEqual(capture_stdout.getvalue(),
773 stubs.expect_container_request_uuid + '\n')
777 def test_submit_job_runner_image(self, stubs):
778 capture_stdout = cStringIO.StringIO()
780 exited = arvados_cwl.main(
781 ["--submit", "--no-wait", "--api=jobs", "--debug", "--submit-runner-image=arvados/jobs:123",
782 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
783 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
784 self.assertEqual(exited, 0)
786 logging.exception("")
788 stubs.expect_pipeline_instance["components"]["cwl-runner"]["runtime_constraints"]["docker_image"] = "arvados/jobs:123"
790 expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
791 stubs.api.pipeline_instances().create.assert_called_with(
792 body=JsonDiffMatcher(expect_pipeline))
793 self.assertEqual(capture_stdout.getvalue(),
794 stubs.expect_pipeline_uuid + '\n')
797 def test_submit_container_runner_image(self, stubs):
798 capture_stdout = cStringIO.StringIO()
800 exited = arvados_cwl.main(
801 ["--submit", "--no-wait", "--api=containers", "--debug", "--submit-runner-image=arvados/jobs:123",
802 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
803 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
804 self.assertEqual(exited, 0)
806 logging.exception("")
808 stubs.expect_container_spec["container_image"] = "arvados/jobs:123"
810 expect_container = copy.deepcopy(stubs.expect_container_spec)
811 stubs.api.container_requests().create.assert_called_with(
812 body=JsonDiffMatcher(expect_container))
813 self.assertEqual(capture_stdout.getvalue(),
814 stubs.expect_container_request_uuid + '\n')
817 @mock.patch("arvados.commands.keepdocker.find_one_image_hash")
818 @mock.patch("cwltool.docker.get_image")
819 @mock.patch("arvados.api")
820 def test_arvados_jobs_image(self, api, get_image, find_one_image_hash):
821 arvrunner = mock.MagicMock()
822 arvrunner.project_uuid = ""
823 api.return_value = mock.MagicMock()
824 arvrunner.api = api.return_value
825 arvrunner.api.links().list().execute.side_effect = ({"items": [], "items_available": 0, "offset": 0},
826 {"items": [], "items_available": 0, "offset": 0},
827 {"items": [], "items_available": 0, "offset": 0},
828 {"items": [{"created_at": "",
830 "link_class": "docker_image_hash",
833 "properties": {"image_timestamp": ""}}], "items_available": 1, "offset": 0},
834 {"items": [], "items_available": 0, "offset": 0},
835 {"items": [{"created_at": "",
837 "link_class": "docker_image_repo+tag",
838 "name": "arvados/jobs:"+arvados_cwl.__version__,
840 "properties": {"image_timestamp": ""}}], "items_available": 1, "offset": 0},
841 {"items": [{"created_at": "",
843 "link_class": "docker_image_hash",
846 "properties": {"image_timestamp": ""}}], "items_available": 1, "offset": 0} ,
848 find_one_image_hash.return_value = "123456"
850 arvrunner.api.collections().list().execute.side_effect = ({"items": [], "items_available": 0, "offset": 0},
851 {"items": [{"uuid": "",
855 }], "items_available": 1, "offset": 0},
856 {"items": [{"uuid": ""}], "items_available": 1, "offset": 0})
857 arvrunner.api.collections().create().execute.return_value = {"uuid": ""}
858 self.assertEqual("arvados/jobs:"+arvados_cwl.__version__, arvados_cwl.runner.arvados_jobs_image(arvrunner, "arvados/jobs:"+arvados_cwl.__version__))
860 class TestCreateTemplate(unittest.TestCase):
861 existing_template_uuid = "zzzzz-d1hrv-validworkfloyml"
863 def _adjust_script_params(self, expect_component):
864 expect_component['script_parameters']['x'] = {
868 'value': '99999999999999999999999999999992+99/blorp.txt',
870 expect_component['script_parameters']['y'] = {
871 'dataclass': 'Collection',
874 'value': '99999999999999999999999999999998+99',
876 expect_component['script_parameters']['z'] = {
877 'dataclass': 'Collection',
883 def test_create(self, stubs):
884 project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
886 capture_stdout = cStringIO.StringIO()
888 exited = arvados_cwl.main(
889 ["--create-workflow", "--debug",
891 "--project-uuid", project_uuid,
892 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
893 capture_stdout, sys.stderr, api_client=stubs.api)
894 self.assertEqual(exited, 0)
896 stubs.api.pipeline_instances().create.refute_called()
897 stubs.api.jobs().create.refute_called()
899 expect_component = copy.deepcopy(stubs.expect_job_spec)
900 self._adjust_script_params(expect_component)
903 "submit_wf.cwl": expect_component,
905 "name": "submit_wf.cwl",
906 "owner_uuid": project_uuid,
908 stubs.api.pipeline_templates().create.assert_called_with(
909 body=JsonDiffMatcher(expect_template), ensure_unique_name=True)
911 self.assertEqual(capture_stdout.getvalue(),
912 stubs.expect_pipeline_template_uuid + '\n')
916 def test_create_name(self, stubs):
917 project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
919 capture_stdout = cStringIO.StringIO()
921 exited = arvados_cwl.main(
922 ["--create-workflow", "--debug",
923 "--project-uuid", project_uuid,
925 "--name", "testing 123",
926 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
927 capture_stdout, sys.stderr, api_client=stubs.api)
928 self.assertEqual(exited, 0)
930 stubs.api.pipeline_instances().create.refute_called()
931 stubs.api.jobs().create.refute_called()
933 expect_component = copy.deepcopy(stubs.expect_job_spec)
934 self._adjust_script_params(expect_component)
937 "testing 123": expect_component,
939 "name": "testing 123",
940 "owner_uuid": project_uuid,
942 stubs.api.pipeline_templates().create.assert_called_with(
943 body=JsonDiffMatcher(expect_template), ensure_unique_name=True)
945 self.assertEqual(capture_stdout.getvalue(),
946 stubs.expect_pipeline_template_uuid + '\n')
950 def test_update_name(self, stubs):
951 project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
953 capture_stdout = cStringIO.StringIO()
955 exited = arvados_cwl.main(
956 ["--update-workflow", self.existing_template_uuid,
958 "--project-uuid", project_uuid,
960 "--name", "testing 123",
961 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
962 capture_stdout, sys.stderr, api_client=stubs.api)
963 self.assertEqual(exited, 0)
965 stubs.api.pipeline_instances().create.refute_called()
966 stubs.api.jobs().create.refute_called()
968 expect_component = copy.deepcopy(stubs.expect_job_spec)
969 self._adjust_script_params(expect_component)
972 "testing 123": expect_component,
974 "name": "testing 123",
975 "owner_uuid": project_uuid,
977 stubs.api.pipeline_templates().create.refute_called()
978 stubs.api.pipeline_templates().update.assert_called_with(
979 body=JsonDiffMatcher(expect_template), uuid=self.existing_template_uuid)
981 self.assertEqual(capture_stdout.getvalue(),
982 self.existing_template_uuid + '\n')
985 class TestCreateWorkflow(unittest.TestCase):
986 existing_workflow_uuid = "zzzzz-7fd4e-validworkfloyml"
987 expect_workflow = open("tests/wf/expect_packed.cwl").read()
990 def test_create(self, stubs):
991 project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
993 capture_stdout = cStringIO.StringIO()
995 exited = arvados_cwl.main(
996 ["--create-workflow", "--debug",
998 "--project-uuid", project_uuid,
999 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1000 capture_stdout, sys.stderr, api_client=stubs.api)
1001 self.assertEqual(exited, 0)
1003 stubs.api.pipeline_templates().create.refute_called()
1004 stubs.api.container_requests().create.refute_called()
1008 "owner_uuid": project_uuid,
1009 "name": "submit_wf.cwl",
1011 "definition": self.expect_workflow,
1014 stubs.api.workflows().create.assert_called_with(
1015 body=JsonDiffMatcher(body))
1017 self.assertEqual(capture_stdout.getvalue(),
1018 stubs.expect_workflow_uuid + '\n')
1022 def test_create_name(self, stubs):
1023 project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
1025 capture_stdout = cStringIO.StringIO()
1027 exited = arvados_cwl.main(
1028 ["--create-workflow", "--debug",
1030 "--project-uuid", project_uuid,
1031 "--name", "testing 123",
1032 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1033 capture_stdout, sys.stderr, api_client=stubs.api)
1034 self.assertEqual(exited, 0)
1036 stubs.api.pipeline_templates().create.refute_called()
1037 stubs.api.container_requests().create.refute_called()
1041 "owner_uuid": project_uuid,
1042 "name": "testing 123",
1044 "definition": self.expect_workflow,
1047 stubs.api.workflows().create.assert_called_with(
1048 body=JsonDiffMatcher(body))
1050 self.assertEqual(capture_stdout.getvalue(),
1051 stubs.expect_workflow_uuid + '\n')
1054 def test_incompatible_api(self, stubs):
1055 capture_stderr = cStringIO.StringIO()
1056 logging.getLogger('arvados.cwl-runner').addHandler(
1057 logging.StreamHandler(capture_stderr))
1059 exited = arvados_cwl.main(
1060 ["--update-workflow", self.existing_workflow_uuid,
1063 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1064 sys.stderr, sys.stderr, api_client=stubs.api)
1065 self.assertEqual(exited, 1)
1066 self.assertRegexpMatches(
1067 capture_stderr.getvalue(),
1068 "--update-workflow arg '{}' uses 'containers' API, but --api='jobs' specified".format(self.existing_workflow_uuid))
1071 def test_update(self, stubs):
1072 capture_stdout = cStringIO.StringIO()
1074 exited = arvados_cwl.main(
1075 ["--update-workflow", self.existing_workflow_uuid,
1077 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1078 capture_stdout, sys.stderr, api_client=stubs.api)
1079 self.assertEqual(exited, 0)
1083 "name": "submit_wf.cwl",
1085 "definition": self.expect_workflow,
1088 stubs.api.workflows().update.assert_called_with(
1089 uuid=self.existing_workflow_uuid,
1090 body=JsonDiffMatcher(body))
1091 self.assertEqual(capture_stdout.getvalue(),
1092 self.existing_workflow_uuid + '\n')
1096 def test_update_name(self, stubs):
1097 capture_stdout = cStringIO.StringIO()
1099 exited = arvados_cwl.main(
1100 ["--update-workflow", self.existing_workflow_uuid,
1101 "--debug", "--name", "testing 123",
1102 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1103 capture_stdout, sys.stderr, api_client=stubs.api)
1104 self.assertEqual(exited, 0)
1108 "name": "testing 123",
1110 "definition": self.expect_workflow,
1113 stubs.api.workflows().update.assert_called_with(
1114 uuid=self.existing_workflow_uuid,
1115 body=JsonDiffMatcher(body))
1116 self.assertEqual(capture_stdout.getvalue(),
1117 self.existing_workflow_uuid + '\n')
1120 class TestTemplateInputs(unittest.TestCase):
1123 "inputs_test.cwl": {
1124 'runtime_constraints': {
1125 'docker_image': 'arvados/jobs:'+arvados_cwl.__version__,
1126 'min_ram_mb_per_node': 1024
1128 'script_parameters': {
1130 '99999999999999999999999999999992+99/workflow.cwl#main',
1131 'optionalFloatInput': None,
1134 'dataclass': 'File',
1136 'title': "It's a file; we expect to find some characters in it.",
1137 'description': 'If there were anything further to say, it would be said here,\nor here.'
1141 'dataclass': 'number',
1143 'title': 'Floats like a duck',
1147 'optionalFloatInput': {
1148 'type': ['null', 'float'],
1149 'dataclass': 'number',
1154 'dataclass': 'boolean',
1156 'title': 'True or false?',
1159 'repository': 'arvados',
1160 'script_version': 'master',
1161 'minimum_script_version': '570509ab4d2ef93d870fd2b1f2eab178afb1bad9',
1162 'script': 'cwl-runner',
1165 "name": "inputs_test.cwl",
1169 def test_inputs_empty(self, stubs):
1170 exited = arvados_cwl.main(
1171 ["--create-template",
1172 "tests/wf/inputs_test.cwl", "tests/order/empty_order.json"],
1173 cStringIO.StringIO(), sys.stderr, api_client=stubs.api)
1174 self.assertEqual(exited, 0)
1176 stubs.api.pipeline_templates().create.assert_called_with(
1177 body=JsonDiffMatcher(self.expect_template), ensure_unique_name=True)
1180 def test_inputs(self, stubs):
1181 exited = arvados_cwl.main(
1182 ["--create-template",
1183 "tests/wf/inputs_test.cwl", "tests/order/inputs_test_order.json"],
1184 cStringIO.StringIO(), sys.stderr, api_client=stubs.api)
1185 self.assertEqual(exited, 0)
1187 expect_template = copy.deepcopy(self.expect_template)
1188 params = expect_template[
1189 "components"]["inputs_test.cwl"]["script_parameters"]
1190 params["fileInput"]["value"] = '99999999999999999999999999999992+99/blorp.txt'
1191 params["cwl:tool"] = '99999999999999999999999999999994+99/workflow.cwl#main'
1192 params["floatInput"]["value"] = 1.234
1193 params["boolInput"]["value"] = True
1195 stubs.api.pipeline_templates().create.assert_called_with(
1196 body=JsonDiffMatcher(expect_template), ensure_unique_name=True)