14198: test_submit passes
[arvados.git] / sdk / cwl / tests / test_submit.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 import copy
6 import cStringIO
7 import functools
8 import hashlib
9 import json
10 import logging
11 import mock
12 import sys
13 import unittest
14
15 import arvados
16 import arvados.collection
17 import arvados_cwl
18 import arvados_cwl.executor
19 import arvados_cwl.runner
20 import arvados.keep
21
22 from .matcher import JsonDiffMatcher, StripYAMLComments
23 from .mock_discovery import get_rootDesc
24
25 import ruamel.yaml as yaml
26
27 _rootDesc = None
28
29 def stubs(func):
30     @functools.wraps(func)
31     @mock.patch("arvados.commands.keepdocker.list_images_in_arv")
32     @mock.patch("arvados.collection.KeepClient")
33     @mock.patch("arvados.keep.KeepClient")
34     @mock.patch("arvados.events.subscribe")
35     def wrapped(self, events, keep_client1, keep_client2, keepdocker, *args, **kwargs):
36         class Stubs:
37             pass
38         stubs = Stubs()
39         stubs.events = events
40         stubs.keepdocker = keepdocker
41
42         def putstub(p, **kwargs):
43             return "%s+%i" % (hashlib.md5(p).hexdigest(), len(p))
44         keep_client1().put.side_effect = putstub
45         keep_client1.put.side_effect = putstub
46         keep_client2().put.side_effect = putstub
47         keep_client2.put.side_effect = putstub
48
49         stubs.keep_client = keep_client2
50         stubs.docker_images = {
51             "arvados/jobs:"+arvados_cwl.__version__: [("zzzzz-4zz18-zzzzzzzzzzzzzd3", "")],
52             "debian:8": [("zzzzz-4zz18-zzzzzzzzzzzzzd4", "")],
53             "arvados/jobs:123": [("zzzzz-4zz18-zzzzzzzzzzzzzd5", "")],
54             "arvados/jobs:latest": [("zzzzz-4zz18-zzzzzzzzzzzzzd6", "")],
55         }
56         def kd(a, b, image_name=None, image_tag=None):
57             return stubs.docker_images.get("%s:%s" % (image_name, image_tag), [])
58         stubs.keepdocker.side_effect = kd
59
60         stubs.fake_user_uuid = "zzzzz-tpzed-zzzzzzzzzzzzzzz"
61         stubs.fake_container_uuid = "zzzzz-dz642-zzzzzzzzzzzzzzz"
62
63         stubs.api = mock.MagicMock()
64         stubs.api._rootDesc = get_rootDesc()
65
66         stubs.api.users().current().execute.return_value = {
67             "uuid": stubs.fake_user_uuid,
68         }
69         stubs.api.collections().list().execute.return_value = {"items": []}
70         stubs.api.containers().current().execute.return_value = {
71             "uuid": stubs.fake_container_uuid,
72         }
73
74         class CollectionExecute(object):
75             def __init__(self, exe):
76                 self.exe = exe
77             def execute(self, num_retries=None):
78                 return self.exe
79
80         def collection_createstub(created_collections, body, ensure_unique_name=None):
81             mt = body["manifest_text"]
82             uuid = "zzzzz-4zz18-zzzzzzzzzzzzzx%d" % len(created_collections)
83             pdh = "%s+%i" % (hashlib.md5(mt).hexdigest(), len(mt))
84             created_collections[uuid] = {
85                 "uuid": uuid,
86                 "portable_data_hash": pdh,
87                 "manifest_text": mt
88             }
89             return CollectionExecute(created_collections[uuid])
90
91         def collection_getstub(created_collections, uuid):
92             for v in created_collections.itervalues():
93                 if uuid in (v["uuid"], v["portable_data_hash"]):
94                     return CollectionExecute(v)
95
96         created_collections = {
97             "99999999999999999999999999999998+99": {
98                 "uuid": "",
99                 "portable_data_hash": "99999999999999999999999999999998+99",
100                 "manifest_text": ". 99999999999999999999999999999998+99 0:0:file1.txt"
101             },
102             "99999999999999999999999999999994+99": {
103                 "uuid": "",
104                 "portable_data_hash": "99999999999999999999999999999994+99",
105                 "manifest_text": ". 99999999999999999999999999999994+99 0:0:expect_arvworkflow.cwl"
106             },
107             "zzzzz-4zz18-zzzzzzzzzzzzzd3": {
108                 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzd3",
109                 "portable_data_hash": "999999999999999999999999999999d3+99",
110                 "manifest_text": ""
111             },
112             "zzzzz-4zz18-zzzzzzzzzzzzzd4": {
113                 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzd4",
114                 "portable_data_hash": "999999999999999999999999999999d4+99",
115                 "manifest_text": ""
116             },
117             "zzzzz-4zz18-zzzzzzzzzzzzzd5": {
118                 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzd5",
119                 "portable_data_hash": "999999999999999999999999999999d5+99",
120                 "manifest_text": ""
121             },
122             "zzzzz-4zz18-zzzzzzzzzzzzzd6": {
123                 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzd6",
124                 "portable_data_hash": "999999999999999999999999999999d6+99",
125                 "manifest_text": ""
126             }
127         }
128         stubs.api.collections().create.side_effect = functools.partial(collection_createstub, created_collections)
129         stubs.api.collections().get.side_effect = functools.partial(collection_getstub, created_collections)
130
131         stubs.expect_job_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
132         stubs.api.jobs().create().execute.return_value = {
133             "uuid": stubs.expect_job_uuid,
134             "state": "Queued",
135         }
136
137         stubs.expect_container_request_uuid = "zzzzz-xvhdp-zzzzzzzzzzzzzzz"
138         stubs.api.container_requests().create().execute.return_value = {
139             "uuid": stubs.expect_container_request_uuid,
140             "container_uuid": "zzzzz-dz642-zzzzzzzzzzzzzzz",
141             "state": "Queued"
142         }
143
144         stubs.expect_pipeline_template_uuid = "zzzzz-d1hrv-zzzzzzzzzzzzzzz"
145         stubs.api.pipeline_templates().create().execute.return_value = {
146             "uuid": stubs.expect_pipeline_template_uuid,
147         }
148         stubs.expect_job_spec = {
149             'runtime_constraints': {
150                 'docker_image': '999999999999999999999999999999d3+99',
151                 'min_ram_mb_per_node': 1024
152             },
153             'script_parameters': {
154                 'x': {
155                     'basename': 'blorp.txt',
156                     'location': 'keep:169f39d466a5438ac4a90e779bf750c7+53/blorp.txt',
157                     'class': 'File'
158                 },
159                 'y': {
160                     'basename': '99999999999999999999999999999998+99',
161                     'location': 'keep:99999999999999999999999999999998+99',
162                     'class': 'Directory'
163                 },
164                 'z': {
165                     'basename': 'anonymous',
166                     "listing": [{
167                         "basename": "renamed.txt",
168                         "class": "File",
169                         "location": "keep:99999999999999999999999999999998+99/file1.txt",
170                         "size": 0
171                     }],
172                     'class': 'Directory'
173                 },
174                 'cwl:tool': '57ad063d64c60dbddc027791f0649211+60/workflow.cwl#main'
175             },
176             'repository': 'arvados',
177             'script_version': 'master',
178             'minimum_script_version': '570509ab4d2ef93d870fd2b1f2eab178afb1bad9',
179             'script': 'cwl-runner'
180         }
181         stubs.pipeline_component = stubs.expect_job_spec.copy()
182         stubs.expect_pipeline_instance = {
183             'name': 'submit_wf.cwl',
184             'state': 'RunningOnServer',
185             'owner_uuid': None,
186             "components": {
187                 "cwl-runner": {
188                     'runtime_constraints': {'docker_image': '999999999999999999999999999999d3+99', 'min_ram_mb_per_node': 1024},
189                     'script_parameters': {
190                         'y': {"value": {'basename': '99999999999999999999999999999998+99', 'location': 'keep:99999999999999999999999999999998+99', 'class': 'Directory'}},
191                         'x': {"value": {
192                             'basename': 'blorp.txt',
193                             'class': 'File',
194                             'location': 'keep:169f39d466a5438ac4a90e779bf750c7+53/blorp.txt',
195                             "size": 16
196                         }},
197                         'z': {"value": {'basename': 'anonymous', 'class': 'Directory',
198                               'listing': [
199                                   {
200                                       'basename': 'renamed.txt',
201                                       'class': 'File', 'location':
202                                       'keep:99999999999999999999999999999998+99/file1.txt',
203                                       'size': 0
204                                   }
205                               ]}},
206                         'cwl:tool': '57ad063d64c60dbddc027791f0649211+60/workflow.cwl#main',
207                         'arv:debug': True,
208                         'arv:enable_reuse': True,
209                         'arv:on_error': 'continue'
210                     },
211                     'repository': 'arvados',
212                     'script_version': 'master',
213                     'minimum_script_version': '570509ab4d2ef93d870fd2b1f2eab178afb1bad9',
214                     'script': 'cwl-runner',
215                     'job': {'state': 'Queued', 'uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'}
216                 }
217             }
218         }
219         stubs.pipeline_create = copy.deepcopy(stubs.expect_pipeline_instance)
220         stubs.expect_pipeline_uuid = "zzzzz-d1hrv-zzzzzzzzzzzzzzz"
221         stubs.pipeline_create["uuid"] = stubs.expect_pipeline_uuid
222         stubs.pipeline_with_job = copy.deepcopy(stubs.pipeline_create)
223         stubs.pipeline_with_job["components"]["cwl-runner"]["job"] = {
224             "uuid": "zzzzz-8i9sb-zzzzzzzzzzzzzzz",
225             "state": "Queued"
226         }
227         stubs.api.pipeline_instances().create().execute.return_value = stubs.pipeline_create
228         stubs.api.pipeline_instances().get().execute.return_value = stubs.pipeline_with_job
229
230         with open("tests/wf/submit_wf_packed.cwl") as f:
231             expect_packed_workflow = yaml.round_trip_load(f)
232
233         stubs.expect_container_spec = {
234             'priority': 500,
235             'mounts': {
236                 '/var/spool/cwl': {
237                     'writable': True,
238                     'kind': 'collection'
239                 },
240                 '/var/lib/cwl/workflow.json': {
241                     'content': expect_packed_workflow,
242                     'kind': 'json'
243                 },
244                 'stdout': {
245                     'path': '/var/spool/cwl/cwl.output.json',
246                     'kind': 'file'
247                 },
248                 '/var/lib/cwl/cwl.input.json': {
249                     'kind': 'json',
250                     'content': {
251                         'y': {
252                             'basename': '99999999999999999999999999999998+99',
253                             'location': 'keep:99999999999999999999999999999998+99',
254                             'class': 'Directory'},
255                         'x': {
256                             'basename': u'blorp.txt',
257                             'class': 'File',
258                             'location': u'keep:169f39d466a5438ac4a90e779bf750c7+53/blorp.txt',
259                             "size": 16
260                         },
261                         'z': {'basename': 'anonymous', 'class': 'Directory', 'listing': [
262                             {'basename': 'renamed.txt',
263                              'class': 'File',
264                              'location': 'keep:99999999999999999999999999999998+99/file1.txt',
265                              'size': 0
266                             }
267                         ]}
268                     },
269                     'kind': 'json'
270                 }
271             },
272             'secret_mounts': {},
273             'state': 'Committed',
274             'command': ['arvados-cwl-runner', '--local', '--api=containers',
275                         '--no-log-timestamps', '--disable-validate',
276                         '--eval-timeout=20', '--thread-count=4',
277                         '--enable-reuse', '--debug', '--on-error=continue',
278                         '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json'],
279             'name': 'submit_wf.cwl',
280             'container_image': '999999999999999999999999999999d3+99',
281             'output_path': '/var/spool/cwl',
282             'cwd': '/var/spool/cwl',
283             'runtime_constraints': {
284                 'API': True,
285                 'vcpus': 1,
286                 'ram': 1024*1024*1024
287             },
288             'use_existing': True,
289             'properties': {},
290             'secret_mounts': {}
291         }
292
293         stubs.expect_workflow_uuid = "zzzzz-7fd4e-zzzzzzzzzzzzzzz"
294         stubs.api.workflows().create().execute.return_value = {
295             "uuid": stubs.expect_workflow_uuid,
296         }
297         def update_mock(**kwargs):
298             stubs.updated_uuid = kwargs.get('uuid')
299             return mock.DEFAULT
300         stubs.api.workflows().update.side_effect = update_mock
301         stubs.api.workflows().update().execute.side_effect = lambda **kwargs: {
302             "uuid": stubs.updated_uuid,
303         }
304
305         return func(self, stubs, *args, **kwargs)
306     return wrapped
307
308
309 class TestSubmit(unittest.TestCase):
310     @mock.patch("arvados_cwl.arvdocker.arv_docker_get_image")
311     @mock.patch("time.sleep")
312     @stubs
313     def test_submit(self, stubs, tm, arvdock):
314         def get_image(api_client, dockerRequirement, pull_image, project_uuid):
315             if dockerRequirement["dockerPull"] == 'arvados/jobs:'+arvados_cwl.__version__:
316                 return '999999999999999999999999999999d3+99'
317             elif dockerRequirement["dockerPull"] == "debian:8":
318                 return '999999999999999999999999999999d4+99'
319         arvdock.side_effect = get_image
320
321         capture_stdout = cStringIO.StringIO()
322         exited = arvados_cwl.main(
323             ["--submit", "--no-wait", "--api=jobs", "--debug",
324              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
325             capture_stdout, sys.stderr, api_client=stubs.api)
326         self.assertEqual(exited, 0)
327
328         stubs.api.collections().create.assert_has_calls([
329             mock.call(body=JsonDiffMatcher({
330                 'manifest_text':
331                 '. 5bcc9fe8f8d5992e6cf418dc7ce4dbb3+16 0:16:blub.txt\n',
332                 'replication_desired': None,
333                 'name': 'submit_tool.cwl dependencies (5d373e7629203ce39e7c22af98a0f881+52)',
334             }), ensure_unique_name=False),
335             mock.call(body=JsonDiffMatcher({
336                 'manifest_text':
337                 '. 979af1245a12a1fed634d4222473bfdc+16 0:16:blorp.txt\n',
338                 'replication_desired': None,
339                 'name': 'submit_wf.cwl input (169f39d466a5438ac4a90e779bf750c7+53)',
340             }), ensure_unique_name=False),
341             mock.call(body=JsonDiffMatcher({
342                 'manifest_text':
343                 ". 68089141fbf7e020ac90a9d6a575bc8f+1312 0:1312:workflow.cwl\n",
344                 'replication_desired': None,
345                 'name': 'submit_wf.cwl',
346             }), ensure_unique_name=True)        ])
347
348         arvdock.assert_has_calls([
349             mock.call(stubs.api, {"class": "DockerRequirement", "dockerPull": "debian:8"}, True, None),
350             mock.call(stubs.api, {"class": "DockerRequirement", "dockerPull": "debian:8", 'http://arvados.org/cwl#dockerCollectionPDH': '999999999999999999999999999999d4+99'}, True, None),
351             mock.call(stubs.api, {'dockerPull': 'arvados/jobs:'+arvados_cwl.__version__}, True, None)
352         ])
353
354         expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
355         stubs.api.pipeline_instances().create.assert_called_with(
356             body=JsonDiffMatcher(expect_pipeline))
357         self.assertEqual(capture_stdout.getvalue(),
358                          stubs.expect_pipeline_uuid + '\n')
359
360
361     @mock.patch("time.sleep")
362     @stubs
363     def test_submit_no_reuse(self, stubs, tm):
364         capture_stdout = cStringIO.StringIO()
365         exited = arvados_cwl.main(
366             ["--submit", "--no-wait", "--api=jobs", "--debug", "--disable-reuse",
367              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
368             capture_stdout, sys.stderr, api_client=stubs.api)
369         self.assertEqual(exited, 0)
370
371         expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
372         expect_pipeline["components"]["cwl-runner"]["script_parameters"]["arv:enable_reuse"] = {"value": False}
373         expect_pipeline["properties"] = {"run_options": {"enable_job_reuse": False}}
374
375         stubs.api.pipeline_instances().create.assert_called_with(
376             body=JsonDiffMatcher(expect_pipeline))
377         self.assertEqual(capture_stdout.getvalue(),
378                          stubs.expect_pipeline_uuid + '\n')
379
380     @stubs
381     def test_error_when_multiple_storage_classes_specified(self, stubs):
382         storage_classes = "foo,bar"
383         exited = arvados_cwl.main(
384                 ["--debug", "--storage-classes", storage_classes,
385                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
386                 sys.stdin, sys.stderr, api_client=stubs.api)
387         self.assertEqual(exited, 1)
388
389     @mock.patch("time.sleep")
390     @stubs
391     def test_submit_on_error(self, stubs, tm):
392         capture_stdout = cStringIO.StringIO()
393         exited = arvados_cwl.main(
394             ["--submit", "--no-wait", "--api=jobs", "--debug", "--on-error=stop",
395              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
396             capture_stdout, sys.stderr, api_client=stubs.api)
397         self.assertEqual(exited, 0)
398
399         expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
400         expect_pipeline["components"]["cwl-runner"]["script_parameters"]["arv:on_error"] = "stop"
401
402         stubs.api.pipeline_instances().create.assert_called_with(
403             body=JsonDiffMatcher(expect_pipeline))
404         self.assertEqual(capture_stdout.getvalue(),
405                          stubs.expect_pipeline_uuid + '\n')
406
407
408     @mock.patch("time.sleep")
409     @stubs
410     def test_submit_runner_ram(self, stubs, tm):
411         capture_stdout = cStringIO.StringIO()
412         exited = arvados_cwl.main(
413             ["--submit", "--no-wait", "--debug", "--submit-runner-ram=2048",
414              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
415             capture_stdout, sys.stderr, api_client=stubs.api)
416         self.assertEqual(exited, 0)
417
418         expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
419         expect_pipeline["components"]["cwl-runner"]["runtime_constraints"]["min_ram_mb_per_node"] = 2048
420
421         stubs.api.pipeline_instances().create.assert_called_with(
422             body=JsonDiffMatcher(expect_pipeline))
423         self.assertEqual(capture_stdout.getvalue(),
424                          stubs.expect_pipeline_uuid + '\n')
425
426
427     @mock.patch("time.sleep")
428     @stubs
429     def test_submit_invalid_runner_ram(self, stubs, tm):
430         capture_stdout = cStringIO.StringIO()
431         exited = arvados_cwl.main(
432             ["--submit", "--no-wait", "--debug", "--submit-runner-ram=-2048",
433              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
434             capture_stdout, sys.stderr, api_client=stubs.api)
435         self.assertEqual(exited, 1)
436
437     @mock.patch("time.sleep")
438     @stubs
439     def test_submit_output_name(self, stubs, tm):
440         output_name = "test_output_name"
441
442         capture_stdout = cStringIO.StringIO()
443         exited = arvados_cwl.main(
444             ["--submit", "--no-wait", "--debug", "--output-name", output_name,
445              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
446             capture_stdout, sys.stderr, api_client=stubs.api)
447         self.assertEqual(exited, 0)
448
449         expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
450         expect_pipeline["components"]["cwl-runner"]["script_parameters"]["arv:output_name"] = output_name
451
452         stubs.api.pipeline_instances().create.assert_called_with(
453             body=JsonDiffMatcher(expect_pipeline))
454         self.assertEqual(capture_stdout.getvalue(),
455                          stubs.expect_pipeline_uuid + '\n')
456
457
458     @mock.patch("time.sleep")
459     @stubs
460     def test_submit_pipeline_name(self, stubs, tm):
461         capture_stdout = cStringIO.StringIO()
462         exited = arvados_cwl.main(
463             ["--submit", "--no-wait", "--debug", "--name=hello job 123",
464              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
465             capture_stdout, sys.stderr, api_client=stubs.api)
466         self.assertEqual(exited, 0)
467
468         expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
469         expect_pipeline["name"] = "hello job 123"
470
471         stubs.api.pipeline_instances().create.assert_called_with(
472             body=JsonDiffMatcher(expect_pipeline))
473         self.assertEqual(capture_stdout.getvalue(),
474                          stubs.expect_pipeline_uuid + '\n')
475
476     @mock.patch("time.sleep")
477     @stubs
478     def test_submit_output_tags(self, stubs, tm):
479         output_tags = "tag0,tag1,tag2"
480
481         capture_stdout = cStringIO.StringIO()
482         exited = arvados_cwl.main(
483             ["--submit", "--no-wait", "--debug", "--output-tags", output_tags,
484              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
485             capture_stdout, sys.stderr, api_client=stubs.api)
486         self.assertEqual(exited, 0)
487
488         expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
489         expect_pipeline["components"]["cwl-runner"]["script_parameters"]["arv:output_tags"] = output_tags
490
491         stubs.api.pipeline_instances().create.assert_called_with(
492             body=JsonDiffMatcher(expect_pipeline))
493         self.assertEqual(capture_stdout.getvalue(),
494                          stubs.expect_pipeline_uuid + '\n')
495
496     @mock.patch("time.sleep")
497     @stubs
498     def test_submit_with_project_uuid(self, stubs, tm):
499         project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
500
501         exited = arvados_cwl.main(
502             ["--submit", "--no-wait", "--debug",
503              "--project-uuid", project_uuid,
504              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
505             sys.stdout, sys.stderr, api_client=stubs.api)
506         self.assertEqual(exited, 0)
507
508         expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
509         expect_pipeline["owner_uuid"] = project_uuid
510         stubs.api.pipeline_instances().create.assert_called_with(
511             body=JsonDiffMatcher(expect_pipeline))
512
513     @stubs
514     def test_submit_container(self, stubs):
515         capture_stdout = cStringIO.StringIO()
516         try:
517             exited = arvados_cwl.main(
518                 ["--submit", "--no-wait", "--api=containers", "--debug",
519                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
520                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
521             self.assertEqual(exited, 0)
522         except:
523             logging.exception("")
524
525         stubs.api.collections().create.assert_has_calls([
526             mock.call(body=JsonDiffMatcher({
527                 'manifest_text':
528                 '. 5bcc9fe8f8d5992e6cf418dc7ce4dbb3+16 0:16:blub.txt\n',
529                 'replication_desired': None,
530                 'name': 'submit_tool.cwl dependencies (5d373e7629203ce39e7c22af98a0f881+52)',
531             }), ensure_unique_name=False),
532             mock.call(body=JsonDiffMatcher({
533                 'manifest_text':
534                 '. 979af1245a12a1fed634d4222473bfdc+16 0:16:blorp.txt\n',
535                 'replication_desired': None,
536                 'name': 'submit_wf.cwl input (169f39d466a5438ac4a90e779bf750c7+53)',
537             }), ensure_unique_name=False)])
538
539         expect_container = copy.deepcopy(stubs.expect_container_spec)
540         stubs.api.container_requests().create.assert_called_with(
541             body=JsonDiffMatcher(expect_container))
542         self.assertEqual(capture_stdout.getvalue(),
543                          stubs.expect_container_request_uuid + '\n')
544
545     @stubs
546     def test_submit_container_no_reuse(self, stubs):
547         capture_stdout = cStringIO.StringIO()
548         try:
549             exited = arvados_cwl.main(
550                 ["--submit", "--no-wait", "--api=containers", "--debug", "--disable-reuse",
551                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
552                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
553             self.assertEqual(exited, 0)
554         except:
555             logging.exception("")
556
557         expect_container = copy.deepcopy(stubs.expect_container_spec)
558         expect_container["command"] = [
559             'arvados-cwl-runner', '--local', '--api=containers',
560             '--no-log-timestamps', '--disable-validate',
561             '--eval-timeout=20', '--thread-count=4',
562             '--disable-reuse', '--debug', '--on-error=continue',
563             '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
564         expect_container["use_existing"] = False
565
566         stubs.api.container_requests().create.assert_called_with(
567             body=JsonDiffMatcher(expect_container))
568         self.assertEqual(capture_stdout.getvalue(),
569                          stubs.expect_container_request_uuid + '\n')
570
571
572     @stubs
573     def test_submit_container_reuse_disabled_by_workflow(self, stubs):
574         capture_stdout = cStringIO.StringIO()
575
576         exited = arvados_cwl.main(
577             ["--submit", "--no-wait", "--api=containers", "--debug",
578              "tests/wf/submit_wf_no_reuse.cwl", "tests/submit_test_job.json"],
579             capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
580         self.assertEqual(exited, 0)
581
582         expect_container = copy.deepcopy(stubs.expect_container_spec)
583         expect_container["command"] = [
584             'arvados-cwl-runner', '--local', '--api=containers',
585             '--no-log-timestamps', '--disable-validate',
586             '--eval-timeout=20', '--thread-count=4',
587             '--disable-reuse', '--debug', '--on-error=continue',
588             '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
589         expect_container["use_existing"] = False
590         expect_container["name"] = "submit_wf_no_reuse.cwl"
591         expect_container["mounts"]["/var/lib/cwl/workflow.json"]["content"]["$graph"][1]["hints"] = [
592             {
593                 "class": "http://arvados.org/cwl#ReuseRequirement",
594                 "enableReuse": False,
595             },
596         ]
597         expect_container["mounts"]["/var/lib/cwl/workflow.json"]["content"]["$graph"][0]["$namespaces"] = {
598             "arv": "http://arvados.org/cwl#",
599             "cwltool": "http://commonwl.org/cwltool#"
600         }
601
602         stubs.api.container_requests().create.assert_called_with(
603             body=JsonDiffMatcher(expect_container))
604         self.assertEqual(capture_stdout.getvalue(),
605                          stubs.expect_container_request_uuid + '\n')
606
607
608     @stubs
609     def test_submit_container_on_error(self, stubs):
610         capture_stdout = cStringIO.StringIO()
611         try:
612             exited = arvados_cwl.main(
613                 ["--submit", "--no-wait", "--api=containers", "--debug", "--on-error=stop",
614                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
615                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
616             self.assertEqual(exited, 0)
617         except:
618             logging.exception("")
619
620         expect_container = copy.deepcopy(stubs.expect_container_spec)
621         expect_container["command"] = ['arvados-cwl-runner', '--local', '--api=containers',
622                                        '--no-log-timestamps', '--disable-validate',
623                                        '--eval-timeout=20', '--thread-count=4',
624                                        '--enable-reuse', '--debug', '--on-error=stop',
625                                        '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
626
627         stubs.api.container_requests().create.assert_called_with(
628             body=JsonDiffMatcher(expect_container))
629         self.assertEqual(capture_stdout.getvalue(),
630                          stubs.expect_container_request_uuid + '\n')
631
632     @stubs
633     def test_submit_container_output_name(self, stubs):
634         output_name = "test_output_name"
635
636         capture_stdout = cStringIO.StringIO()
637         try:
638             exited = arvados_cwl.main(
639                 ["--submit", "--no-wait", "--api=containers", "--debug", "--output-name", output_name,
640                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
641                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
642             self.assertEqual(exited, 0)
643         except:
644             logging.exception("")
645
646         expect_container = copy.deepcopy(stubs.expect_container_spec)
647         expect_container["command"] = ['arvados-cwl-runner', '--local', '--api=containers',
648                                        '--no-log-timestamps', '--disable-validate',
649                                        '--eval-timeout=20', '--thread-count=4',
650                                        '--enable-reuse',
651                                        "--output-name="+output_name, '--debug', '--on-error=continue',
652                                        '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
653         expect_container["output_name"] = output_name
654
655         stubs.api.container_requests().create.assert_called_with(
656             body=JsonDiffMatcher(expect_container))
657         self.assertEqual(capture_stdout.getvalue(),
658                          stubs.expect_container_request_uuid + '\n')
659
660     @stubs
661     def test_submit_storage_classes(self, stubs):
662         capture_stdout = cStringIO.StringIO()
663         try:
664             exited = arvados_cwl.main(
665                 ["--debug", "--submit", "--no-wait", "--api=containers", "--storage-classes=foo",
666                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
667                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
668             self.assertEqual(exited, 0)
669         except:
670             logging.exception("")
671
672         expect_container = copy.deepcopy(stubs.expect_container_spec)
673         expect_container["command"] = ['arvados-cwl-runner', '--local', '--api=containers',
674                                        '--no-log-timestamps', '--disable-validate',
675                                        '--eval-timeout=20', '--thread-count=4',
676                                        '--enable-reuse', "--debug",
677                                        "--storage-classes=foo", '--on-error=continue',
678                                        '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
679
680         stubs.api.container_requests().create.assert_called_with(
681             body=JsonDiffMatcher(expect_container))
682         self.assertEqual(capture_stdout.getvalue(),
683                          stubs.expect_container_request_uuid + '\n')
684
685     @mock.patch("arvados_cwl.task_queue.TaskQueue")
686     @mock.patch("arvados_cwl.arvworkflow.ArvadosWorkflow.job")
687     @mock.patch("arvados_cwl.executor.ArvCwlExecutor.make_output_collection", return_value = (None, None))
688     @stubs
689     def test_storage_classes_correctly_propagate_to_make_output_collection(self, stubs, make_output, job, tq):
690         def set_final_output(job_order, output_callback, runtimeContext):
691             output_callback("zzzzz-4zz18-zzzzzzzzzzzzzzzz", "success")
692             return []
693         job.side_effect = set_final_output
694
695         try:
696             exited = arvados_cwl.main(
697                 ["--debug", "--local", "--storage-classes=foo",
698                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
699                 sys.stdin, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
700             self.assertEqual(exited, 0)
701         except:
702             logging.exception("")
703
704         make_output.assert_called_with(u'Output of submit_wf.cwl', ['foo'], '', 'zzzzz-4zz18-zzzzzzzzzzzzzzzz')
705
706     @mock.patch("arvados_cwl.task_queue.TaskQueue")
707     @mock.patch("arvados_cwl.arvworkflow.ArvadosWorkflow.job")
708     @mock.patch("arvados_cwl.executor.ArvCwlExecutor.make_output_collection", return_value = (None, None))
709     @stubs
710     def test_default_storage_classes_correctly_propagate_to_make_output_collection(self, stubs, make_output, job, tq):
711         def set_final_output(job_order, output_callback, runtimeContext):
712             output_callback("zzzzz-4zz18-zzzzzzzzzzzzzzzz", "success")
713             return []
714         job.side_effect = set_final_output
715
716         try:
717             exited = arvados_cwl.main(
718                 ["--debug", "--local",
719                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
720                 sys.stdin, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
721             self.assertEqual(exited, 0)
722         except:
723             logging.exception("")
724
725         make_output.assert_called_with(u'Output of submit_wf.cwl', ['default'], '', 'zzzzz-4zz18-zzzzzzzzzzzzzzzz')
726
727     @stubs
728     def test_submit_container_output_ttl(self, stubs):
729         capture_stdout = cStringIO.StringIO()
730         try:
731             exited = arvados_cwl.main(
732                 ["--submit", "--no-wait", "--api=containers", "--debug", "--intermediate-output-ttl", "3600",
733                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
734                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
735             self.assertEqual(exited, 0)
736         except:
737             logging.exception("")
738
739         expect_container = copy.deepcopy(stubs.expect_container_spec)
740         expect_container["command"] = ['arvados-cwl-runner', '--local', '--api=containers',
741                                        '--no-log-timestamps', '--disable-validate',
742                                        '--eval-timeout=20', '--thread-count=4',
743                                        '--enable-reuse', '--debug', '--on-error=continue',
744                                        "--intermediate-output-ttl=3600",
745                                        '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
746
747         stubs.api.container_requests().create.assert_called_with(
748             body=JsonDiffMatcher(expect_container))
749         self.assertEqual(capture_stdout.getvalue(),
750                          stubs.expect_container_request_uuid + '\n')
751
752     @stubs
753     def test_submit_container_trash_intermediate(self, stubs):
754         capture_stdout = cStringIO.StringIO()
755         try:
756             exited = arvados_cwl.main(
757                 ["--submit", "--no-wait", "--api=containers", "--debug", "--trash-intermediate",
758                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
759                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
760             self.assertEqual(exited, 0)
761         except:
762             logging.exception("")
763
764         expect_container = copy.deepcopy(stubs.expect_container_spec)
765         expect_container["command"] = ['arvados-cwl-runner', '--local', '--api=containers',
766                                        '--no-log-timestamps', '--disable-validate',
767                                        '--eval-timeout=20', '--thread-count=4',
768                                        '--enable-reuse', '--debug', '--on-error=continue',
769                                        "--trash-intermediate",
770                                        '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
771
772         stubs.api.container_requests().create.assert_called_with(
773             body=JsonDiffMatcher(expect_container))
774         self.assertEqual(capture_stdout.getvalue(),
775                          stubs.expect_container_request_uuid + '\n')
776
777     @stubs
778     def test_submit_container_output_tags(self, stubs):
779         output_tags = "tag0,tag1,tag2"
780
781         capture_stdout = cStringIO.StringIO()
782         try:
783             exited = arvados_cwl.main(
784                 ["--submit", "--no-wait", "--api=containers", "--debug", "--output-tags", output_tags,
785                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
786                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
787             self.assertEqual(exited, 0)
788         except:
789             logging.exception("")
790
791         expect_container = copy.deepcopy(stubs.expect_container_spec)
792         expect_container["command"] = ['arvados-cwl-runner', '--local', '--api=containers',
793                                        '--no-log-timestamps', '--disable-validate',
794                                        '--eval-timeout=20', '--thread-count=4',
795                                        '--enable-reuse',
796                                        "--output-tags="+output_tags, '--debug', '--on-error=continue',
797                                        '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
798
799         stubs.api.container_requests().create.assert_called_with(
800             body=JsonDiffMatcher(expect_container))
801         self.assertEqual(capture_stdout.getvalue(),
802                          stubs.expect_container_request_uuid + '\n')
803
804     @stubs
805     def test_submit_container_runner_ram(self, stubs):
806         capture_stdout = cStringIO.StringIO()
807         try:
808             exited = arvados_cwl.main(
809                 ["--submit", "--no-wait", "--api=containers", "--debug", "--submit-runner-ram=2048",
810                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
811                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
812             self.assertEqual(exited, 0)
813         except:
814             logging.exception("")
815
816         expect_container = copy.deepcopy(stubs.expect_container_spec)
817         expect_container["runtime_constraints"]["ram"] = 2048*1024*1024
818
819         stubs.api.container_requests().create.assert_called_with(
820             body=JsonDiffMatcher(expect_container))
821         self.assertEqual(capture_stdout.getvalue(),
822                          stubs.expect_container_request_uuid + '\n')
823
824     @mock.patch("arvados.collection.CollectionReader")
825     @mock.patch("time.sleep")
826     @stubs
827     def test_submit_file_keepref(self, stubs, tm, collectionReader):
828         capture_stdout = cStringIO.StringIO()
829         collectionReader().find.return_value = arvados.arvfile.ArvadosFile(mock.MagicMock(), "blorp.txt")
830         exited = arvados_cwl.main(
831             ["--submit", "--no-wait", "--api=containers", "--debug",
832              "tests/wf/submit_keepref_wf.cwl"],
833             capture_stdout, sys.stderr, api_client=stubs.api)
834         self.assertEqual(exited, 0)
835
836
837     @mock.patch("arvados.collection.CollectionReader")
838     @mock.patch("time.sleep")
839     @stubs
840     def test_submit_keepref(self, stubs, tm, reader):
841         capture_stdout = cStringIO.StringIO()
842
843         with open("tests/wf/expect_arvworkflow.cwl") as f:
844             reader().open().__enter__().read.return_value = f.read()
845
846         exited = arvados_cwl.main(
847             ["--submit", "--no-wait", "--api=containers", "--debug",
848              "keep:99999999999999999999999999999994+99/expect_arvworkflow.cwl#main", "-x", "XxX"],
849             capture_stdout, sys.stderr, api_client=stubs.api)
850         self.assertEqual(exited, 0)
851
852         expect_container = {
853             'priority': 500,
854             'mounts': {
855                 '/var/spool/cwl': {
856                     'writable': True,
857                     'kind': 'collection'
858                 },
859                 'stdout': {
860                     'path': '/var/spool/cwl/cwl.output.json',
861                     'kind': 'file'
862                 },
863                 '/var/lib/cwl/workflow': {
864                     'portable_data_hash': '99999999999999999999999999999994+99',
865                     'kind': 'collection'
866                 },
867                 '/var/lib/cwl/cwl.input.json': {
868                     'content': {
869                         'x': 'XxX'
870                     },
871                     'kind': 'json'
872                 }
873             }, 'state': 'Committed',
874             'output_path': '/var/spool/cwl',
875             'name': 'expect_arvworkflow.cwl#main',
876             'container_image': '999999999999999999999999999999d3+99',
877             'command': ['arvados-cwl-runner', '--local', '--api=containers',
878                         '--no-log-timestamps', '--disable-validate',
879                         '--eval-timeout=20', '--thread-count=4',
880                         '--enable-reuse', '--debug', '--on-error=continue',
881                         '/var/lib/cwl/workflow/expect_arvworkflow.cwl#main', '/var/lib/cwl/cwl.input.json'],
882             'cwd': '/var/spool/cwl',
883             'runtime_constraints': {
884                 'API': True,
885                 'vcpus': 1,
886                 'ram': 1073741824
887             },
888             'use_existing': True,
889             'properties': {},
890             'secret_mounts': {}
891         }
892
893         stubs.api.container_requests().create.assert_called_with(
894             body=JsonDiffMatcher(expect_container))
895         self.assertEqual(capture_stdout.getvalue(),
896                          stubs.expect_container_request_uuid + '\n')
897
898
899     @mock.patch("arvados.collection.CollectionReader")
900     @mock.patch("time.sleep")
901     @stubs
902     def test_submit_jobs_keepref(self, stubs, tm, reader):
903         capture_stdout = cStringIO.StringIO()
904
905         with open("tests/wf/expect_arvworkflow.cwl") as f:
906             reader().open().__enter__().read.return_value = f.read()
907
908         exited = arvados_cwl.main(
909             ["--submit", "--no-wait", "--api=jobs", "--debug",
910              "keep:99999999999999999999999999999994+99/expect_arvworkflow.cwl#main", "-x", "XxX"],
911             capture_stdout, sys.stderr, api_client=stubs.api)
912         self.assertEqual(exited, 0)
913
914         expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
915         expect_pipeline["components"]["cwl-runner"]["script_parameters"]["x"] = "XxX"
916         del expect_pipeline["components"]["cwl-runner"]["script_parameters"]["y"]
917         del expect_pipeline["components"]["cwl-runner"]["script_parameters"]["z"]
918         expect_pipeline["components"]["cwl-runner"]["script_parameters"]["cwl:tool"] = "99999999999999999999999999999994+99/expect_arvworkflow.cwl#main"
919         expect_pipeline["name"] = "expect_arvworkflow.cwl#main"
920         stubs.api.pipeline_instances().create.assert_called_with(
921             body=JsonDiffMatcher(expect_pipeline))
922
923     @mock.patch("time.sleep")
924     @stubs
925     def test_submit_arvworkflow(self, stubs, tm):
926         capture_stdout = cStringIO.StringIO()
927
928         with open("tests/wf/expect_arvworkflow.cwl") as f:
929             stubs.api.workflows().get().execute.return_value = {"definition": f.read(), "name": "a test workflow"}
930
931         exited = arvados_cwl.main(
932             ["--submit", "--no-wait", "--api=containers", "--debug",
933              "962eh-7fd4e-gkbzl62qqtfig37", "-x", "XxX"],
934             capture_stdout, sys.stderr, api_client=stubs.api)
935         self.assertEqual(exited, 0)
936
937         expect_container = {
938             'priority': 500,
939             'mounts': {
940                 '/var/spool/cwl': {
941                     'writable': True,
942                     'kind': 'collection'
943                 },
944                 'stdout': {
945                     'path': '/var/spool/cwl/cwl.output.json',
946                     'kind': 'file'
947                 },
948                 '/var/lib/cwl/workflow.json': {
949                     'kind': 'json',
950                     'content': {
951                         'cwlVersion': 'v1.0',
952                         '$graph': [
953                             {
954                                 'id': '#main',
955                                 'inputs': [
956                                     {'type': 'string', 'id': '#main/x'}
957                                 ],
958                                 'steps': [
959                                     {'in': [{'source': '#main/x', 'id': '#main/step1/x'}],
960                                      'run': '#submit_tool.cwl',
961                                      'id': '#main/step1',
962                                      'out': []}
963                                 ],
964                                 'class': 'Workflow',
965                                 'outputs': []
966                             },
967                             {
968                                 'inputs': [
969                                     {
970                                         'inputBinding': {'position': 1},
971                                         'type': 'string',
972                                         'id': '#submit_tool.cwl/x'}
973                                 ],
974                                 'requirements': [
975                                     {
976                                         'dockerPull': 'debian:8',
977                                         'class': 'DockerRequirement',
978                                         "http://arvados.org/cwl#dockerCollectionPDH": "999999999999999999999999999999d4+99"
979                                     }
980                                 ],
981                                 'id': '#submit_tool.cwl',
982                                 'outputs': [],
983                                 'baseCommand': 'cat',
984                                 'class': 'CommandLineTool'
985                             }
986                         ]
987                     }
988                 },
989                 '/var/lib/cwl/cwl.input.json': {
990                     'content': {
991                         'x': 'XxX'
992                     },
993                     'kind': 'json'
994                 }
995             }, 'state': 'Committed',
996             'output_path': '/var/spool/cwl',
997             'name': 'a test workflow',
998             'container_image': "999999999999999999999999999999d3+99",
999             'command': ['arvados-cwl-runner', '--local', '--api=containers',
1000                         '--no-log-timestamps', '--disable-validate',
1001                         '--eval-timeout=20', '--thread-count=4',
1002                         '--enable-reuse', '--debug', '--on-error=continue',
1003                         '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json'],
1004             'cwd': '/var/spool/cwl',
1005             'runtime_constraints': {
1006                 'API': True,
1007                 'vcpus': 1,
1008                 'ram': 1073741824
1009             },
1010             'use_existing': True,
1011             'properties': {
1012                 "template_uuid": "962eh-7fd4e-gkbzl62qqtfig37"
1013             },
1014             'secret_mounts': {}
1015         }
1016
1017         stubs.api.container_requests().create.assert_called_with(
1018             body=JsonDiffMatcher(expect_container))
1019         self.assertEqual(capture_stdout.getvalue(),
1020                          stubs.expect_container_request_uuid + '\n')
1021
1022
1023     @stubs
1024     def test_submit_container_name(self, stubs):
1025         capture_stdout = cStringIO.StringIO()
1026         try:
1027             exited = arvados_cwl.main(
1028                 ["--submit", "--no-wait", "--api=containers", "--debug", "--name=hello container 123",
1029                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1030                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
1031             self.assertEqual(exited, 0)
1032         except:
1033             logging.exception("")
1034
1035         expect_container = copy.deepcopy(stubs.expect_container_spec)
1036         expect_container["name"] = "hello container 123"
1037
1038         stubs.api.container_requests().create.assert_called_with(
1039             body=JsonDiffMatcher(expect_container))
1040         self.assertEqual(capture_stdout.getvalue(),
1041                          stubs.expect_container_request_uuid + '\n')
1042
1043
1044     @stubs
1045     def test_submit_container_project(self, stubs):
1046         project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
1047         capture_stdout = cStringIO.StringIO()
1048         try:
1049             exited = arvados_cwl.main(
1050                 ["--submit", "--no-wait", "--api=containers", "--debug", "--project-uuid="+project_uuid,
1051                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1052                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
1053             self.assertEqual(exited, 0)
1054         except:
1055             logging.exception("")
1056
1057         expect_container = copy.deepcopy(stubs.expect_container_spec)
1058         expect_container["owner_uuid"] = project_uuid
1059         expect_container["command"] = ['arvados-cwl-runner', '--local', '--api=containers',
1060                                        '--no-log-timestamps', '--disable-validate',
1061                                        "--eval-timeout=20", "--thread-count=4",
1062                                        '--enable-reuse', '--debug', '--on-error=continue',
1063                                        '--project-uuid='+project_uuid,
1064                                        '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
1065
1066         stubs.api.container_requests().create.assert_called_with(
1067             body=JsonDiffMatcher(expect_container))
1068         self.assertEqual(capture_stdout.getvalue(),
1069                          stubs.expect_container_request_uuid + '\n')
1070
1071     @stubs
1072     def test_submit_container_eval_timeout(self, stubs):
1073         project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
1074         capture_stdout = cStringIO.StringIO()
1075         try:
1076             exited = arvados_cwl.main(
1077                 ["--submit", "--no-wait", "--api=containers", "--debug", "--eval-timeout=60",
1078                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1079                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
1080             self.assertEqual(exited, 0)
1081         except:
1082             logging.exception("")
1083
1084         expect_container = copy.deepcopy(stubs.expect_container_spec)
1085         expect_container["command"] = ['arvados-cwl-runner', '--local', '--api=containers',
1086                                        '--no-log-timestamps', '--disable-validate',
1087                                        '--eval-timeout=60.0', '--thread-count=4',
1088                                        '--enable-reuse', '--debug', '--on-error=continue',
1089                                        '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
1090
1091         stubs.api.container_requests().create.assert_called_with(
1092             body=JsonDiffMatcher(expect_container))
1093         self.assertEqual(capture_stdout.getvalue(),
1094                          stubs.expect_container_request_uuid + '\n')
1095
1096
1097     @stubs
1098     def test_submit_container_thread_count(self, stubs):
1099         project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
1100         capture_stdout = cStringIO.StringIO()
1101         try:
1102             exited = arvados_cwl.main(
1103                 ["--submit", "--no-wait", "--api=containers", "--debug", "--thread-count=20",
1104                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1105                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
1106             self.assertEqual(exited, 0)
1107         except:
1108             logging.exception("")
1109
1110         expect_container = copy.deepcopy(stubs.expect_container_spec)
1111         expect_container["command"] = ['arvados-cwl-runner', '--local', '--api=containers',
1112                                        '--no-log-timestamps', '--disable-validate',
1113                                        '--eval-timeout=20', '--thread-count=20',
1114                                        '--enable-reuse', '--debug', '--on-error=continue',
1115                                        '/var/lib/cwl/workflow.json#main', '/var/lib/cwl/cwl.input.json']
1116
1117         stubs.api.container_requests().create.assert_called_with(
1118             body=JsonDiffMatcher(expect_container))
1119         self.assertEqual(capture_stdout.getvalue(),
1120                          stubs.expect_container_request_uuid + '\n')
1121
1122
1123     @stubs
1124     def test_submit_job_runner_image(self, stubs):
1125         capture_stdout = cStringIO.StringIO()
1126         try:
1127             exited = arvados_cwl.main(
1128                 ["--submit", "--no-wait", "--api=jobs", "--debug", "--submit-runner-image=arvados/jobs:123",
1129                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1130                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
1131             self.assertEqual(exited, 0)
1132         except:
1133             logging.exception("")
1134
1135         stubs.expect_pipeline_instance["components"]["cwl-runner"]["runtime_constraints"]["docker_image"] = "999999999999999999999999999999d5+99"
1136
1137         expect_pipeline = copy.deepcopy(stubs.expect_pipeline_instance)
1138         stubs.api.pipeline_instances().create.assert_called_with(
1139             body=JsonDiffMatcher(expect_pipeline))
1140         self.assertEqual(capture_stdout.getvalue(),
1141                          stubs.expect_pipeline_uuid + '\n')
1142
1143     @stubs
1144     def test_submit_container_runner_image(self, stubs):
1145         capture_stdout = cStringIO.StringIO()
1146         try:
1147             exited = arvados_cwl.main(
1148                 ["--submit", "--no-wait", "--api=containers", "--debug", "--submit-runner-image=arvados/jobs:123",
1149                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1150                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
1151             self.assertEqual(exited, 0)
1152         except:
1153             logging.exception("")
1154
1155         stubs.expect_container_spec["container_image"] = "999999999999999999999999999999d5+99"
1156
1157         expect_container = copy.deepcopy(stubs.expect_container_spec)
1158         stubs.api.container_requests().create.assert_called_with(
1159             body=JsonDiffMatcher(expect_container))
1160         self.assertEqual(capture_stdout.getvalue(),
1161                          stubs.expect_container_request_uuid + '\n')
1162
1163     @stubs
1164     def test_submit_priority(self, stubs):
1165         capture_stdout = cStringIO.StringIO()
1166         try:
1167             exited = arvados_cwl.main(
1168                 ["--submit", "--no-wait", "--api=containers", "--debug", "--priority=669",
1169                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1170                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
1171             self.assertEqual(exited, 0)
1172         except:
1173             logging.exception("")
1174
1175         stubs.expect_container_spec["priority"] = 669
1176
1177         expect_container = copy.deepcopy(stubs.expect_container_spec)
1178         stubs.api.container_requests().create.assert_called_with(
1179             body=JsonDiffMatcher(expect_container))
1180         self.assertEqual(capture_stdout.getvalue(),
1181                          stubs.expect_container_request_uuid + '\n')
1182
1183
1184     @stubs
1185     def test_submit_wf_runner_resources(self, stubs):
1186         capture_stdout = cStringIO.StringIO()
1187         try:
1188             exited = arvados_cwl.main(
1189                 ["--submit", "--no-wait", "--api=containers", "--debug",
1190                  "tests/wf/submit_wf_runner_resources.cwl", "tests/submit_test_job.json"],
1191                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
1192             self.assertEqual(exited, 0)
1193         except:
1194             logging.exception("")
1195
1196         expect_container = copy.deepcopy(stubs.expect_container_spec)
1197         expect_container["runtime_constraints"] = {
1198             "API": True,
1199             "vcpus": 2,
1200             "ram": 2000 * 2**20
1201         }
1202         expect_container["name"] = "submit_wf_runner_resources.cwl"
1203         expect_container["mounts"]["/var/lib/cwl/workflow.json"]["content"]["$graph"][1]["hints"] = [
1204             {
1205                 "class": "http://arvados.org/cwl#WorkflowRunnerResources",
1206                 "coresMin": 2,
1207                 "ramMin": 2000
1208             }
1209         ]
1210         expect_container["mounts"]["/var/lib/cwl/workflow.json"]["content"]["$graph"][0]["$namespaces"] = {
1211             "arv": "http://arvados.org/cwl#",
1212         }
1213
1214         stubs.api.container_requests().create.assert_called_with(
1215             body=JsonDiffMatcher(expect_container))
1216         self.assertEqual(capture_stdout.getvalue(),
1217                          stubs.expect_container_request_uuid + '\n')
1218
1219     def tearDown(self):
1220         arvados_cwl.arvdocker.arv_docker_clear_cache()
1221
1222     @mock.patch("arvados.commands.keepdocker.find_one_image_hash")
1223     @mock.patch("cwltool.docker.DockerCommandLineJob.get_image")
1224     @mock.patch("arvados.api")
1225     def test_arvados_jobs_image(self, api, get_image, find_one_image_hash):
1226         arvados_cwl.arvdocker.arv_docker_clear_cache()
1227
1228         arvrunner = mock.MagicMock()
1229         arvrunner.project_uuid = ""
1230         api.return_value = mock.MagicMock()
1231         arvrunner.api = api.return_value
1232         arvrunner.api.links().list().execute.side_effect = ({"items": [{"created_at": "",
1233                                                                         "head_uuid": "zzzzz-4zz18-zzzzzzzzzzzzzzb",
1234                                                                         "link_class": "docker_image_repo+tag",
1235                                                                         "name": "arvados/jobs:"+arvados_cwl.__version__,
1236                                                                         "owner_uuid": "",
1237                                                                         "properties": {"image_timestamp": ""}}], "items_available": 1, "offset": 0},
1238                                                             {"items": [{"created_at": "",
1239                                                                         "head_uuid": "",
1240                                                                         "link_class": "docker_image_hash",
1241                                                                         "name": "123456",
1242                                                                         "owner_uuid": "",
1243                                                                         "properties": {"image_timestamp": ""}}], "items_available": 1, "offset": 0}
1244         )
1245         find_one_image_hash.return_value = "123456"
1246
1247         arvrunner.api.collections().list().execute.side_effect = ({"items": [{"uuid": "zzzzz-4zz18-zzzzzzzzzzzzzzb",
1248                                                                               "owner_uuid": "",
1249                                                                               "manifest_text": "",
1250                                                                               "properties": ""
1251                                                                           }], "items_available": 1, "offset": 0},)
1252         arvrunner.api.collections().create().execute.return_value = {"uuid": ""}
1253         arvrunner.api.collections().get().execute.return_value = {"uuid": "zzzzz-4zz18-zzzzzzzzzzzzzzb",
1254                                                                   "portable_data_hash": "9999999999999999999999999999999b+99"}
1255         self.assertEqual("9999999999999999999999999999999b+99",
1256                          arvados_cwl.runner.arvados_jobs_image(arvrunner, "arvados/jobs:"+arvados_cwl.__version__))
1257
1258
1259     @stubs
1260     def test_submit_secrets(self, stubs):
1261         capture_stdout = cStringIO.StringIO()
1262         try:
1263             exited = arvados_cwl.main(
1264                 ["--submit", "--no-wait", "--api=containers", "--debug",
1265                  "tests/wf/secret_wf.cwl", "tests/secret_test_job.yml"],
1266                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
1267             self.assertEqual(exited, 0)
1268         except:
1269             logging.exception("")
1270
1271
1272         expect_container = {
1273             "command": [
1274                 "arvados-cwl-runner",
1275                 "--local",
1276                 "--api=containers",
1277                 "--no-log-timestamps",
1278                 "--disable-validate",
1279                 "--eval-timeout=20",
1280                 '--thread-count=4',
1281                 "--enable-reuse",
1282                 '--debug',
1283                 "--on-error=continue",
1284                 "/var/lib/cwl/workflow.json#main",
1285                 "/var/lib/cwl/cwl.input.json"
1286             ],
1287             "container_image": "999999999999999999999999999999d3+99",
1288             "cwd": "/var/spool/cwl",
1289             "mounts": {
1290                 "/var/lib/cwl/cwl.input.json": {
1291                     "content": {
1292                         "pw": {
1293                             "$include": "/secrets/s0"
1294                         }
1295                     },
1296                     "kind": "json"
1297                 },
1298                 "/var/lib/cwl/workflow.json": {
1299                     "content": {
1300                         "$graph": [
1301                             {
1302                                 "$namespaces": {
1303                                     "cwltool": "http://commonwl.org/cwltool#"
1304                                 },
1305                                 "arguments": [
1306                                     "md5sum",
1307                                     "example.conf"
1308                                 ],
1309                                 "class": "CommandLineTool",
1310                                 "hints": [
1311                                     {
1312                                         "class": "http://commonwl.org/cwltool#Secrets",
1313                                         "secrets": [
1314                                             "#secret_job.cwl/pw"
1315                                         ]
1316                                     }
1317                                 ],
1318                                 "id": "#secret_job.cwl",
1319                                 "inputs": [
1320                                     {
1321                                         "id": "#secret_job.cwl/pw",
1322                                         "type": "string"
1323                                     }
1324                                 ],
1325                                 "outputs": [
1326                                     {
1327                                         "id": "#secret_job.cwl/out",
1328                                         "type": "stdout"
1329                                     }
1330                                 ],
1331                                 "stdout": "hashed_example.txt",
1332                                 "requirements": [
1333                                     {
1334                                         "class": "InitialWorkDirRequirement",
1335                                         "listing": [
1336                                             {
1337                                                 "entry": "username: user\npassword: $(inputs.pw)\n",
1338                                                 "entryname": "example.conf"
1339                                             }
1340                                         ]
1341                                     }
1342                                 ]
1343                             },
1344                             {
1345                                 "class": "Workflow",
1346                                 "hints": [
1347                                     {
1348                                         "class": "DockerRequirement",
1349                                         "dockerPull": "debian:8",
1350                                         "http://arvados.org/cwl#dockerCollectionPDH": "999999999999999999999999999999d4+99"
1351                                     },
1352                                     {
1353                                         "class": "http://commonwl.org/cwltool#Secrets",
1354                                         "secrets": [
1355                                             "#main/pw"
1356                                         ]
1357                                     }
1358                                 ],
1359                                 "id": "#main",
1360                                 "inputs": [
1361                                     {
1362                                         "id": "#main/pw",
1363                                         "type": "string"
1364                                     }
1365                                 ],
1366                                 "outputs": [
1367                                     {
1368                                         "id": "#main/out",
1369                                         "outputSource": "#main/step1/out",
1370                                         "type": "File"
1371                                     }
1372                                 ],
1373                                 "steps": [
1374                                     {
1375                                         "id": "#main/step1",
1376                                         "in": [
1377                                             {
1378                                                 "id": "#main/step1/pw",
1379                                                 "source": "#main/pw"
1380                                             }
1381                                         ],
1382                                         "out": [
1383                                             "#main/step1/out"
1384                                         ],
1385                                         "run": "#secret_job.cwl"
1386                                     }
1387                                 ]
1388                             }
1389                         ],
1390                         "cwlVersion": "v1.0"
1391                     },
1392                     "kind": "json"
1393                 },
1394                 "/var/spool/cwl": {
1395                     "kind": "collection",
1396                     "writable": True
1397                 },
1398                 "stdout": {
1399                     "kind": "file",
1400                     "path": "/var/spool/cwl/cwl.output.json"
1401                 }
1402             },
1403             "name": "secret_wf.cwl",
1404             "output_path": "/var/spool/cwl",
1405             "priority": 500,
1406             "properties": {},
1407             "runtime_constraints": {
1408                 "API": True,
1409                 "ram": 1073741824,
1410                 "vcpus": 1
1411             },
1412             "secret_mounts": {
1413                 "/secrets/s0": {
1414                     "content": "blorp",
1415                     "kind": "text"
1416                 }
1417             },
1418             "state": "Committed",
1419             "use_existing": True
1420         }
1421
1422         stubs.api.container_requests().create.assert_called_with(
1423             body=JsonDiffMatcher(expect_container))
1424         self.assertEqual(capture_stdout.getvalue(),
1425                          stubs.expect_container_request_uuid + '\n')
1426
1427     @stubs
1428     def test_submit_request_uuid(self, stubs):
1429         stubs.expect_container_request_uuid = "zzzzz-xvhdp-yyyyyyyyyyyyyyy"
1430
1431         stubs.api.container_requests().update().execute.return_value = {
1432             "uuid": stubs.expect_container_request_uuid,
1433             "container_uuid": "zzzzz-dz642-zzzzzzzzzzzzzzz",
1434             "state": "Queued"
1435         }
1436
1437         capture_stdout = cStringIO.StringIO()
1438         try:
1439             exited = arvados_cwl.main(
1440                 ["--submit", "--no-wait", "--api=containers", "--debug", "--submit-request-uuid=zzzzz-xvhdp-yyyyyyyyyyyyyyy",
1441                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1442                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
1443             self.assertEqual(exited, 0)
1444         except:
1445             logging.exception("")
1446
1447         stubs.api.container_requests().update.assert_called_with(
1448             uuid="zzzzz-xvhdp-yyyyyyyyyyyyyyy", body=JsonDiffMatcher(stubs.expect_container_spec), cluster_id="zzzzz")
1449         self.assertEqual(capture_stdout.getvalue(),
1450                          stubs.expect_container_request_uuid + '\n')
1451
1452
1453 class TestCreateTemplate(unittest.TestCase):
1454     existing_template_uuid = "zzzzz-d1hrv-validworkfloyml"
1455
1456     def _adjust_script_params(self, expect_component):
1457         expect_component['script_parameters']['x'] = {
1458             'dataclass': 'File',
1459             'required': True,
1460             'type': 'File',
1461             'value': '169f39d466a5438ac4a90e779bf750c7+53/blorp.txt',
1462         }
1463         expect_component['script_parameters']['y'] = {
1464             'dataclass': 'Collection',
1465             'required': True,
1466             'type': 'Directory',
1467             'value': '99999999999999999999999999999998+99',
1468         }
1469         expect_component['script_parameters']['z'] = {
1470             'dataclass': 'Collection',
1471             'required': True,
1472             'type': 'Directory',
1473         }
1474
1475     @stubs
1476     def test_create(self, stubs):
1477         project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
1478
1479         capture_stdout = cStringIO.StringIO()
1480
1481         exited = arvados_cwl.main(
1482             ["--create-workflow", "--debug",
1483              "--api=jobs",
1484              "--project-uuid", project_uuid,
1485              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1486             capture_stdout, sys.stderr, api_client=stubs.api)
1487         self.assertEqual(exited, 0)
1488
1489         stubs.api.pipeline_instances().create.refute_called()
1490         stubs.api.jobs().create.refute_called()
1491
1492         expect_component = copy.deepcopy(stubs.expect_job_spec)
1493         self._adjust_script_params(expect_component)
1494         expect_template = {
1495             "components": {
1496                 "submit_wf.cwl": expect_component,
1497             },
1498             "name": "submit_wf.cwl",
1499             "owner_uuid": project_uuid,
1500         }
1501         stubs.api.pipeline_templates().create.assert_called_with(
1502             body=JsonDiffMatcher(expect_template), ensure_unique_name=True)
1503
1504         self.assertEqual(capture_stdout.getvalue(),
1505                          stubs.expect_pipeline_template_uuid + '\n')
1506
1507
1508     @stubs
1509     def test_create_name(self, stubs):
1510         project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
1511
1512         capture_stdout = cStringIO.StringIO()
1513
1514         exited = arvados_cwl.main(
1515             ["--create-workflow", "--debug",
1516              "--project-uuid", project_uuid,
1517              "--api=jobs",
1518              "--name", "testing 123",
1519              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1520             capture_stdout, sys.stderr, api_client=stubs.api)
1521         self.assertEqual(exited, 0)
1522
1523         stubs.api.pipeline_instances().create.refute_called()
1524         stubs.api.jobs().create.refute_called()
1525
1526         expect_component = copy.deepcopy(stubs.expect_job_spec)
1527         self._adjust_script_params(expect_component)
1528         expect_template = {
1529             "components": {
1530                 "testing 123": expect_component,
1531             },
1532             "name": "testing 123",
1533             "owner_uuid": project_uuid,
1534         }
1535         stubs.api.pipeline_templates().create.assert_called_with(
1536             body=JsonDiffMatcher(expect_template), ensure_unique_name=True)
1537
1538         self.assertEqual(capture_stdout.getvalue(),
1539                          stubs.expect_pipeline_template_uuid + '\n')
1540
1541
1542     @stubs
1543     def test_update_name(self, stubs):
1544         project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
1545
1546         capture_stdout = cStringIO.StringIO()
1547
1548         exited = arvados_cwl.main(
1549             ["--update-workflow", self.existing_template_uuid,
1550              "--debug",
1551              "--project-uuid", project_uuid,
1552              "--api=jobs",
1553              "--name", "testing 123",
1554              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1555             capture_stdout, sys.stderr, api_client=stubs.api)
1556         self.assertEqual(exited, 0)
1557
1558         stubs.api.pipeline_instances().create.refute_called()
1559         stubs.api.jobs().create.refute_called()
1560
1561         expect_component = copy.deepcopy(stubs.expect_job_spec)
1562         self._adjust_script_params(expect_component)
1563         expect_template = {
1564             "components": {
1565                 "testing 123": expect_component,
1566             },
1567             "name": "testing 123",
1568             "owner_uuid": project_uuid,
1569         }
1570         stubs.api.pipeline_templates().create.refute_called()
1571         stubs.api.pipeline_templates().update.assert_called_with(
1572             body=JsonDiffMatcher(expect_template), uuid=self.existing_template_uuid)
1573
1574         self.assertEqual(capture_stdout.getvalue(),
1575                          self.existing_template_uuid + '\n')
1576
1577
1578 class TestCreateWorkflow(unittest.TestCase):
1579     existing_workflow_uuid = "zzzzz-7fd4e-validworkfloyml"
1580     expect_workflow = StripYAMLComments(
1581         open("tests/wf/expect_packed.cwl").read())
1582
1583     @stubs
1584     def test_create(self, stubs):
1585         project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
1586
1587         capture_stdout = cStringIO.StringIO()
1588
1589         exited = arvados_cwl.main(
1590             ["--create-workflow", "--debug",
1591              "--api=containers",
1592              "--project-uuid", project_uuid,
1593              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1594             capture_stdout, sys.stderr, api_client=stubs.api)
1595         self.assertEqual(exited, 0)
1596
1597         stubs.api.pipeline_templates().create.refute_called()
1598         stubs.api.container_requests().create.refute_called()
1599
1600         body = {
1601             "workflow": {
1602                 "owner_uuid": project_uuid,
1603                 "name": "submit_wf.cwl",
1604                 "description": "",
1605                 "definition": self.expect_workflow,
1606             }
1607         }
1608         stubs.api.workflows().create.assert_called_with(
1609             body=JsonDiffMatcher(body))
1610
1611         self.assertEqual(capture_stdout.getvalue(),
1612                          stubs.expect_workflow_uuid + '\n')
1613
1614
1615     @stubs
1616     def test_create_name(self, stubs):
1617         project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
1618
1619         capture_stdout = cStringIO.StringIO()
1620
1621         exited = arvados_cwl.main(
1622             ["--create-workflow", "--debug",
1623              "--api=containers",
1624              "--project-uuid", project_uuid,
1625              "--name", "testing 123",
1626              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1627             capture_stdout, sys.stderr, api_client=stubs.api)
1628         self.assertEqual(exited, 0)
1629
1630         stubs.api.pipeline_templates().create.refute_called()
1631         stubs.api.container_requests().create.refute_called()
1632
1633         body = {
1634             "workflow": {
1635                 "owner_uuid": project_uuid,
1636                 "name": "testing 123",
1637                 "description": "",
1638                 "definition": self.expect_workflow,
1639             }
1640         }
1641         stubs.api.workflows().create.assert_called_with(
1642             body=JsonDiffMatcher(body))
1643
1644         self.assertEqual(capture_stdout.getvalue(),
1645                          stubs.expect_workflow_uuid + '\n')
1646
1647     @stubs
1648     def test_incompatible_api(self, stubs):
1649         capture_stderr = cStringIO.StringIO()
1650         logging.getLogger('arvados.cwl-runner').addHandler(
1651             logging.StreamHandler(capture_stderr))
1652
1653         exited = arvados_cwl.main(
1654             ["--update-workflow", self.existing_workflow_uuid,
1655              "--api=jobs",
1656              "--debug",
1657              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1658             sys.stderr, sys.stderr, api_client=stubs.api)
1659         self.assertEqual(exited, 1)
1660         self.assertRegexpMatches(
1661             capture_stderr.getvalue(),
1662             "--update-workflow arg '{}' uses 'containers' API, but --api='jobs' specified".format(self.existing_workflow_uuid))
1663
1664     @stubs
1665     def test_update(self, stubs):
1666         capture_stdout = cStringIO.StringIO()
1667
1668         exited = arvados_cwl.main(
1669             ["--update-workflow", self.existing_workflow_uuid,
1670              "--debug",
1671              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1672             capture_stdout, sys.stderr, api_client=stubs.api)
1673         self.assertEqual(exited, 0)
1674
1675         body = {
1676             "workflow": {
1677                 "name": "submit_wf.cwl",
1678                 "description": "",
1679                 "definition": self.expect_workflow,
1680             }
1681         }
1682         stubs.api.workflows().update.assert_called_with(
1683             uuid=self.existing_workflow_uuid,
1684             body=JsonDiffMatcher(body))
1685         self.assertEqual(capture_stdout.getvalue(),
1686                          self.existing_workflow_uuid + '\n')
1687
1688
1689     @stubs
1690     def test_update_name(self, stubs):
1691         capture_stdout = cStringIO.StringIO()
1692
1693         exited = arvados_cwl.main(
1694             ["--update-workflow", self.existing_workflow_uuid,
1695              "--debug", "--name", "testing 123",
1696              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
1697             capture_stdout, sys.stderr, api_client=stubs.api)
1698         self.assertEqual(exited, 0)
1699
1700         body = {
1701             "workflow": {
1702                 "name": "testing 123",
1703                 "description": "",
1704                 "definition": self.expect_workflow,
1705             }
1706         }
1707         stubs.api.workflows().update.assert_called_with(
1708             uuid=self.existing_workflow_uuid,
1709             body=JsonDiffMatcher(body))
1710         self.assertEqual(capture_stdout.getvalue(),
1711                          self.existing_workflow_uuid + '\n')
1712
1713
1714     @stubs
1715     def test_create_collection_per_tool(self, stubs):
1716         project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
1717
1718         capture_stdout = cStringIO.StringIO()
1719
1720         exited = arvados_cwl.main(
1721             ["--create-workflow", "--debug",
1722              "--api=containers",
1723              "--project-uuid", project_uuid,
1724              "tests/collection_per_tool/collection_per_tool.cwl"],
1725             capture_stdout, sys.stderr, api_client=stubs.api)
1726         self.assertEqual(exited, 0)
1727
1728         toolfile = "tests/collection_per_tool/collection_per_tool_packed.cwl"
1729         expect_workflow = StripYAMLComments(open(toolfile).read())
1730
1731         body = {
1732             "workflow": {
1733                 "owner_uuid": project_uuid,
1734                 "name": "collection_per_tool.cwl",
1735                 "description": "",
1736                 "definition": expect_workflow,
1737             }
1738         }
1739         stubs.api.workflows().create.assert_called_with(
1740             body=JsonDiffMatcher(body))
1741
1742         self.assertEqual(capture_stdout.getvalue(),
1743                          stubs.expect_workflow_uuid + '\n')
1744
1745 class TestTemplateInputs(unittest.TestCase):
1746     expect_template = {
1747         "components": {
1748             "inputs_test.cwl": {
1749                 'runtime_constraints': {
1750                     'docker_image': '999999999999999999999999999999d3+99',
1751                     'min_ram_mb_per_node': 1024
1752                 },
1753                 'script_parameters': {
1754                     'cwl:tool':
1755                     'a2de777156fb700f1363b1f2e370adca+60/workflow.cwl#main',
1756                     'optionalFloatInput': None,
1757                     'fileInput': {
1758                         'type': 'File',
1759                         'dataclass': 'File',
1760                         'required': True,
1761                         'title': "It's a file; we expect to find some characters in it.",
1762                         'description': 'If there were anything further to say, it would be said here,\nor here.'
1763                     },
1764                     'floatInput': {
1765                         'type': 'float',
1766                         'dataclass': 'number',
1767                         'required': True,
1768                         'title': 'Floats like a duck',
1769                         'default': 0.1,
1770                         'value': 0.1,
1771                     },
1772                     'optionalFloatInput': {
1773                         'type': ['null', 'float'],
1774                         'dataclass': 'number',
1775                         'required': False,
1776                     },
1777                     'boolInput': {
1778                         'type': 'boolean',
1779                         'dataclass': 'boolean',
1780                         'required': True,
1781                         'title': 'True or false?',
1782                     },
1783                 },
1784                 'repository': 'arvados',
1785                 'script_version': 'master',
1786                 'minimum_script_version': '570509ab4d2ef93d870fd2b1f2eab178afb1bad9',
1787                 'script': 'cwl-runner',
1788             },
1789         },
1790         "name": "inputs_test.cwl",
1791     }
1792
1793     @stubs
1794     def test_inputs_empty(self, stubs):
1795         exited = arvados_cwl.main(
1796             ["--create-template",
1797              "tests/wf/inputs_test.cwl", "tests/order/empty_order.json"],
1798             cStringIO.StringIO(), sys.stderr, api_client=stubs.api)
1799         self.assertEqual(exited, 0)
1800
1801         stubs.api.pipeline_templates().create.assert_called_with(
1802             body=JsonDiffMatcher(self.expect_template), ensure_unique_name=True)
1803
1804     @stubs
1805     def test_inputs(self, stubs):
1806         exited = arvados_cwl.main(
1807             ["--create-template",
1808              "tests/wf/inputs_test.cwl", "tests/order/inputs_test_order.json"],
1809             cStringIO.StringIO(), sys.stderr, api_client=stubs.api)
1810         self.assertEqual(exited, 0)
1811
1812         expect_template = copy.deepcopy(self.expect_template)
1813         params = expect_template[
1814             "components"]["inputs_test.cwl"]["script_parameters"]
1815         params["fileInput"]["value"] = '169f39d466a5438ac4a90e779bf750c7+53/blorp.txt'
1816         params["cwl:tool"] = 'a2de777156fb700f1363b1f2e370adca+60/workflow.cwl#main'
1817         params["floatInput"]["value"] = 1.234
1818         params["boolInput"]["value"] = True
1819
1820         stubs.api.pipeline_templates().create.assert_called_with(
1821             body=JsonDiffMatcher(expect_template), ensure_unique_name=True)