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',
252 'replication_desired': None,
253 'name': 'submit_tool.cwl dependencies',
254 }), ensure_unique_name=True),
255 mock.call().execute(num_retries=4),
256 mock.call(body=JsonDiffMatcher({
258 '. 979af1245a12a1fed634d4222473bfdc+16 0:16:blorp.txt\n',
259 'replication_desired': None,
260 'name': 'submit_wf.cwl input',
261 }), ensure_unique_name=True),
262 mock.call().execute(num_retries=4)])
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=JsonDiffMatcher(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',
435 'replication_desired': None,
436 'name': 'submit_tool.cwl dependencies',
437 }), ensure_unique_name=True),
438 mock.call().execute(num_retries=4),
439 mock.call(body=JsonDiffMatcher({
441 '. 979af1245a12a1fed634d4222473bfdc+16 0:16:blorp.txt\n',
442 'replication_desired': None,
443 'name': 'submit_wf.cwl input',
444 }), ensure_unique_name=True),
445 mock.call().execute(num_retries=4)])
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": [{"created_at": "",
826 "head_uuid": "zzzzz-4zz18-zzzzzzzzzzzzzzb",
827 "link_class": "docker_image_repo+tag",
828 "name": "arvados/jobs:"+arvados_cwl.__version__,
830 "properties": {"image_timestamp": ""}}], "items_available": 1, "offset": 0},
831 {"items": [{"created_at": "",
833 "link_class": "docker_image_hash",
836 "properties": {"image_timestamp": ""}}], "items_available": 1, "offset": 0}
838 find_one_image_hash.return_value = "123456"
840 arvrunner.api.collections().list().execute.side_effect = ({"items": [{"uuid": "zzzzz-4zz18-zzzzzzzzzzzzzzb",
844 }], "items_available": 1, "offset": 0},)
845 arvrunner.api.collections().create().execute.return_value = {"uuid": ""}
846 self.assertEqual("arvados/jobs:"+arvados_cwl.__version__,
847 arvados_cwl.runner.arvados_jobs_image(arvrunner, "arvados/jobs:"+arvados_cwl.__version__))
849 class TestCreateTemplate(unittest.TestCase):
850 existing_template_uuid = "zzzzz-d1hrv-validworkfloyml"
852 def _adjust_script_params(self, expect_component):
853 expect_component['script_parameters']['x'] = {
857 'value': '99999999999999999999999999999992+99/blorp.txt',
859 expect_component['script_parameters']['y'] = {
860 'dataclass': 'Collection',
863 'value': '99999999999999999999999999999998+99',
865 expect_component['script_parameters']['z'] = {
866 'dataclass': 'Collection',
872 def test_create(self, stubs):
873 project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
875 capture_stdout = cStringIO.StringIO()
877 exited = arvados_cwl.main(
878 ["--create-workflow", "--debug",
880 "--project-uuid", project_uuid,
881 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
882 capture_stdout, sys.stderr, api_client=stubs.api)
883 self.assertEqual(exited, 0)
885 stubs.api.pipeline_instances().create.refute_called()
886 stubs.api.jobs().create.refute_called()
888 expect_component = copy.deepcopy(stubs.expect_job_spec)
889 self._adjust_script_params(expect_component)
892 "submit_wf.cwl": expect_component,
894 "name": "submit_wf.cwl",
895 "owner_uuid": project_uuid,
897 stubs.api.pipeline_templates().create.assert_called_with(
898 body=JsonDiffMatcher(expect_template), ensure_unique_name=True)
900 self.assertEqual(capture_stdout.getvalue(),
901 stubs.expect_pipeline_template_uuid + '\n')
905 def test_create_name(self, stubs):
906 project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
908 capture_stdout = cStringIO.StringIO()
910 exited = arvados_cwl.main(
911 ["--create-workflow", "--debug",
912 "--project-uuid", project_uuid,
914 "--name", "testing 123",
915 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
916 capture_stdout, sys.stderr, api_client=stubs.api)
917 self.assertEqual(exited, 0)
919 stubs.api.pipeline_instances().create.refute_called()
920 stubs.api.jobs().create.refute_called()
922 expect_component = copy.deepcopy(stubs.expect_job_spec)
923 self._adjust_script_params(expect_component)
926 "testing 123": expect_component,
928 "name": "testing 123",
929 "owner_uuid": project_uuid,
931 stubs.api.pipeline_templates().create.assert_called_with(
932 body=JsonDiffMatcher(expect_template), ensure_unique_name=True)
934 self.assertEqual(capture_stdout.getvalue(),
935 stubs.expect_pipeline_template_uuid + '\n')
939 def test_update_name(self, stubs):
940 project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
942 capture_stdout = cStringIO.StringIO()
944 exited = arvados_cwl.main(
945 ["--update-workflow", self.existing_template_uuid,
947 "--project-uuid", project_uuid,
949 "--name", "testing 123",
950 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
951 capture_stdout, sys.stderr, api_client=stubs.api)
952 self.assertEqual(exited, 0)
954 stubs.api.pipeline_instances().create.refute_called()
955 stubs.api.jobs().create.refute_called()
957 expect_component = copy.deepcopy(stubs.expect_job_spec)
958 self._adjust_script_params(expect_component)
961 "testing 123": expect_component,
963 "name": "testing 123",
964 "owner_uuid": project_uuid,
966 stubs.api.pipeline_templates().create.refute_called()
967 stubs.api.pipeline_templates().update.assert_called_with(
968 body=JsonDiffMatcher(expect_template), uuid=self.existing_template_uuid)
970 self.assertEqual(capture_stdout.getvalue(),
971 self.existing_template_uuid + '\n')
974 class TestCreateWorkflow(unittest.TestCase):
975 existing_workflow_uuid = "zzzzz-7fd4e-validworkfloyml"
976 expect_workflow = open("tests/wf/expect_packed.cwl").read()
979 def test_create(self, stubs):
980 project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
982 capture_stdout = cStringIO.StringIO()
984 exited = arvados_cwl.main(
985 ["--create-workflow", "--debug",
987 "--project-uuid", project_uuid,
988 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
989 capture_stdout, sys.stderr, api_client=stubs.api)
990 self.assertEqual(exited, 0)
992 stubs.api.pipeline_templates().create.refute_called()
993 stubs.api.container_requests().create.refute_called()
997 "owner_uuid": project_uuid,
998 "name": "submit_wf.cwl",
1000 "definition": self.expect_workflow,
1003 stubs.api.workflows().create.assert_called_with(
1004 body=JsonDiffMatcher(body))
1006 self.assertEqual(capture_stdout.getvalue(),
1007 stubs.expect_workflow_uuid + '\n')
1011 def test_create_name(self, stubs):
1012 project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
1014 capture_stdout = cStringIO.StringIO()
1016 exited = arvados_cwl.main(
1017 ["--create-workflow", "--debug",
1019 "--project-uuid", project_uuid,
1020 "--name", "testing 123",
1021 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1022 capture_stdout, sys.stderr, api_client=stubs.api)
1023 self.assertEqual(exited, 0)
1025 stubs.api.pipeline_templates().create.refute_called()
1026 stubs.api.container_requests().create.refute_called()
1030 "owner_uuid": project_uuid,
1031 "name": "testing 123",
1033 "definition": self.expect_workflow,
1036 stubs.api.workflows().create.assert_called_with(
1037 body=JsonDiffMatcher(body))
1039 self.assertEqual(capture_stdout.getvalue(),
1040 stubs.expect_workflow_uuid + '\n')
1043 def test_incompatible_api(self, stubs):
1044 capture_stderr = cStringIO.StringIO()
1045 logging.getLogger('arvados.cwl-runner').addHandler(
1046 logging.StreamHandler(capture_stderr))
1048 exited = arvados_cwl.main(
1049 ["--update-workflow", self.existing_workflow_uuid,
1052 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1053 sys.stderr, sys.stderr, api_client=stubs.api)
1054 self.assertEqual(exited, 1)
1055 self.assertRegexpMatches(
1056 capture_stderr.getvalue(),
1057 "--update-workflow arg '{}' uses 'containers' API, but --api='jobs' specified".format(self.existing_workflow_uuid))
1060 def test_update(self, stubs):
1061 capture_stdout = cStringIO.StringIO()
1063 exited = arvados_cwl.main(
1064 ["--update-workflow", self.existing_workflow_uuid,
1066 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1067 capture_stdout, sys.stderr, api_client=stubs.api)
1068 self.assertEqual(exited, 0)
1072 "name": "submit_wf.cwl",
1074 "definition": self.expect_workflow,
1077 stubs.api.workflows().update.assert_called_with(
1078 uuid=self.existing_workflow_uuid,
1079 body=JsonDiffMatcher(body))
1080 self.assertEqual(capture_stdout.getvalue(),
1081 self.existing_workflow_uuid + '\n')
1085 def test_update_name(self, stubs):
1086 capture_stdout = cStringIO.StringIO()
1088 exited = arvados_cwl.main(
1089 ["--update-workflow", self.existing_workflow_uuid,
1090 "--debug", "--name", "testing 123",
1091 "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1092 capture_stdout, sys.stderr, api_client=stubs.api)
1093 self.assertEqual(exited, 0)
1097 "name": "testing 123",
1099 "definition": self.expect_workflow,
1102 stubs.api.workflows().update.assert_called_with(
1103 uuid=self.existing_workflow_uuid,
1104 body=JsonDiffMatcher(body))
1105 self.assertEqual(capture_stdout.getvalue(),
1106 self.existing_workflow_uuid + '\n')
1109 class TestTemplateInputs(unittest.TestCase):
1112 "inputs_test.cwl": {
1113 'runtime_constraints': {
1114 'docker_image': 'arvados/jobs:'+arvados_cwl.__version__,
1115 'min_ram_mb_per_node': 1024
1117 'script_parameters': {
1119 '99999999999999999999999999999992+99/workflow.cwl#main',
1120 'optionalFloatInput': None,
1123 'dataclass': 'File',
1125 'title': "It's a file; we expect to find some characters in it.",
1126 'description': 'If there were anything further to say, it would be said here,\nor here.'
1130 'dataclass': 'number',
1132 'title': 'Floats like a duck',
1136 'optionalFloatInput': {
1137 'type': ['null', 'float'],
1138 'dataclass': 'number',
1143 'dataclass': 'boolean',
1145 'title': 'True or false?',
1148 'repository': 'arvados',
1149 'script_version': 'master',
1150 'minimum_script_version': '570509ab4d2ef93d870fd2b1f2eab178afb1bad9',
1151 'script': 'cwl-runner',
1154 "name": "inputs_test.cwl",
1158 def test_inputs_empty(self, stubs):
1159 exited = arvados_cwl.main(
1160 ["--create-template",
1161 "tests/wf/inputs_test.cwl", "tests/order/empty_order.json"],
1162 cStringIO.StringIO(), sys.stderr, api_client=stubs.api)
1163 self.assertEqual(exited, 0)
1165 stubs.api.pipeline_templates().create.assert_called_with(
1166 body=JsonDiffMatcher(self.expect_template), ensure_unique_name=True)
1169 def test_inputs(self, stubs):
1170 exited = arvados_cwl.main(
1171 ["--create-template",
1172 "tests/wf/inputs_test.cwl", "tests/order/inputs_test_order.json"],
1173 cStringIO.StringIO(), sys.stderr, api_client=stubs.api)
1174 self.assertEqual(exited, 0)
1176 expect_template = copy.deepcopy(self.expect_template)
1177 params = expect_template[
1178 "components"]["inputs_test.cwl"]["script_parameters"]
1179 params["fileInput"]["value"] = '99999999999999999999999999999992+99/blorp.txt'
1180 params["cwl:tool"] = '99999999999999999999999999999994+99/workflow.cwl#main'
1181 params["floatInput"]["value"] = 1.234
1182 params["boolInput"]["value"] = True
1184 stubs.api.pipeline_templates().create.assert_called_with(
1185 body=JsonDiffMatcher(expect_template), ensure_unique_name=True)