Merge branch '10077-workbench-integration-tests' closes #10077
[arvados.git] / sdk / cwl / tests / test_submit.py
1 import arvados
2 import arvados.keep
3 import arvados.collection
4 import arvados_cwl
5 import copy
6 import cStringIO
7 import functools
8 import hashlib
9 import mock
10 import sys
11 import unittest
12 import json
13 import logging
14
15 from .matcher import JsonDiffMatcher
16
17
18 def stubs(func):
19     @functools.wraps(func)
20     @mock.patch("arvados.commands.keepdocker.list_images_in_arv")
21     @mock.patch("arvados.collection.KeepClient")
22     @mock.patch("arvados.events.subscribe")
23     def wrapped(self, events, keep_client, keepdocker, *args, **kwargs):
24         class Stubs:
25             pass
26         stubs = Stubs()
27         stubs.events = events
28         stubs.keepdocker = keepdocker
29         stubs.keep_client = keep_client
30
31         def putstub(p, **kwargs):
32             return "%s+%i" % (hashlib.md5(p).hexdigest(), len(p))
33         stubs.keep_client().put.side_effect = putstub
34         stubs.keep_client.put.side_effect = putstub
35
36         stubs.keepdocker.return_value = [("zzzzz-4zz18-zzzzzzzzzzzzzz3", "")]
37         stubs.fake_user_uuid = "zzzzz-tpzed-zzzzzzzzzzzzzzz"
38
39
40         stubs.api = mock.MagicMock()
41         stubs.api.users().current().execute.return_value = {
42             "uuid": stubs.fake_user_uuid,
43         }
44         stubs.api.collections().list().execute.return_value = {"items": []}
45         stubs.api.collections().create().execute.side_effect = ({
46             "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz1",
47             "portable_data_hash": "99999999999999999999999999999991+99",
48             "manifest_text": ""
49         }, {
50             "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz2",
51             "portable_data_hash": "99999999999999999999999999999992+99",
52             "manifest_text": "./tool 00000000000000000000000000000000+0 0:0:submit_tool.cwl 0:0:blub.txt"
53         },
54         {
55             "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz4",
56             "portable_data_hash": "99999999999999999999999999999994+99",
57             "manifest_text": ""
58         },
59         {
60             "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz5",
61             "portable_data_hash": "99999999999999999999999999999995+99",
62             "manifest_text": ""
63         }        )
64         stubs.api.collections().get().execute.return_value = {
65             "portable_data_hash": "99999999999999999999999999999993+99", "manifest_text": "./tool 00000000000000000000000000000000+0 0:0:submit_tool.cwl 0:0:blub.txt"}
66
67         stubs.expect_job_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
68         stubs.api.jobs().create().execute.return_value = {
69             "uuid": stubs.expect_job_uuid,
70             "state": "Queued",
71         }
72
73         stubs.expect_container_request_uuid = "zzzzz-xvhdp-zzzzzzzzzzzzzzz"
74         stubs.api.container_requests().create().execute.return_value = {
75             "uuid": stubs.expect_container_request_uuid,
76             "container_uuid": "zzzzz-dz642-zzzzzzzzzzzzzzz",
77             "state": "Queued"
78         }
79
80         stubs.expect_pipeline_template_uuid = "zzzzz-d1hrv-zzzzzzzzzzzzzzz"
81         stubs.api.pipeline_templates().create().execute.return_value = {
82             "uuid": stubs.expect_pipeline_template_uuid,
83         }
84         stubs.expect_job_spec = {
85             'runtime_constraints': {
86                 'docker_image': 'arvados/jobs'
87             },
88             'script_parameters': {
89                 'x': {
90                     'basename': 'blorp.txt',
91                     'location': 'keep:99999999999999999999999999999994+99/blorp.txt',
92                     'class': 'File'
93                 },
94                 'y': {
95                     'basename': '99999999999999999999999999999998+99',
96                     'location': 'keep:99999999999999999999999999999998+99',
97                     'class': 'Directory'
98                 },
99                 'z': {
100                     'basename': 'anonymous',
101                     "listing": [{
102                         "basename": "renamed.txt",
103                         "class": "File",
104                         "location": "keep:99999999999999999999999999999998+99/file1.txt"
105                     }],
106                     'class': 'Directory'
107                 },
108                 'cwl:tool':
109                 '99999999999999999999999999999991+99/wf/submit_wf.cwl'
110             },
111             'repository': 'arvados',
112             'script_version': 'master',
113             'script': 'cwl-runner'
114         }
115
116         stubs.expect_container_spec = {
117             'priority': 1,
118             'mounts': {
119                 '/var/spool/cwl': {
120                     'writable': True,
121                     'kind': 'collection'
122                 },
123                 '/var/lib/cwl/workflow': {
124                     'portable_data_hash': '99999999999999999999999999999991+99',
125                     'kind': 'collection'
126                 },
127                 'stdout': {
128                     'path': '/var/spool/cwl/cwl.output.json',
129                     'kind': 'file'
130                 },
131                 '/var/lib/cwl/job/cwl.input.json': {
132                     'portable_data_hash': 'd20d7cddd1984f105dd3702c7f125afb+60/cwl.input.json',
133                     'kind': 'collection'
134                 }
135             },
136             'state': 'Committed',
137             'owner_uuid': 'zzzzz-tpzed-zzzzzzzzzzzzzzz',
138             'command': ['arvados-cwl-runner', '--local', '--api=containers', '/var/lib/cwl/workflow/submit_wf.cwl', '/var/lib/cwl/job/cwl.input.json'],
139             'name': 'submit_wf.cwl',
140             'container_image': '99999999999999999999999999999993+99',
141             'output_path': '/var/spool/cwl',
142             'cwd': '/var/spool/cwl',
143             'runtime_constraints': {
144                 'API': True,
145                 'vcpus': 1,
146                 'ram': 268435456
147             }
148         }
149
150         stubs.expect_workflow_uuid = "zzzzz-7fd4e-zzzzzzzzzzzzzzz"
151         stubs.api.workflows().create().execute.return_value = {
152             "uuid": stubs.expect_workflow_uuid,
153         }
154
155         return func(self, stubs, *args, **kwargs)
156     return wrapped
157
158
159 class TestSubmit(unittest.TestCase):
160     @stubs
161     def test_submit(self, stubs):
162         capture_stdout = cStringIO.StringIO()
163         exited = arvados_cwl.main(
164             ["--submit", "--no-wait",
165              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
166             capture_stdout, sys.stderr, api_client=stubs.api)
167         self.assertEqual(exited, 0)
168
169         stubs.api.collections().create.assert_has_calls([
170             mock.call(),
171             mock.call(body={
172                 'manifest_text':
173                 './tool d51232d96b6116d964a69bfb7e0c73bf+450 '
174                 '0:16:blub.txt 16:434:submit_tool.cwl\n./wf '
175                 'cc2ffb940e60adf1b2b282c67587e43d+413 0:413:submit_wf.cwl\n',
176                 'owner_uuid': 'zzzzz-tpzed-zzzzzzzzzzzzzzz',
177                 'name': 'submit_wf.cwl',
178             }, ensure_unique_name=True),
179             mock.call().execute(),
180             mock.call(body={'manifest_text': '. d41d8cd98f00b204e9800998ecf8427e+0 '
181                             '0:0:blub.txt 0:0:submit_tool.cwl\n',
182                             'owner_uuid': 'zzzzz-tpzed-zzzzzzzzzzzzzzz',
183                             'replication_desired': None,
184                             'name': 'New collection'
185             }, ensure_unique_name=True),
186             mock.call().execute(num_retries=4),
187             mock.call(body={
188                 'manifest_text':
189                 '. 979af1245a12a1fed634d4222473bfdc+16 0:16:blorp.txt\n',
190                 'owner_uuid': 'zzzzz-tpzed-zzzzzzzzzzzzzzz',
191                 'name': '#',
192             }, ensure_unique_name=True),
193             mock.call().execute()])
194
195         expect_job = copy.deepcopy(stubs.expect_job_spec)
196         expect_job["owner_uuid"] = stubs.fake_user_uuid
197         stubs.api.jobs().create.assert_called_with(
198             body=expect_job,
199             find_or_create=True)
200         self.assertEqual(capture_stdout.getvalue(),
201                          stubs.expect_job_uuid + '\n')
202
203     @stubs
204     def test_submit_with_project_uuid(self, stubs):
205         project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
206
207         exited = arvados_cwl.main(
208             ["--submit", "--no-wait",
209              "--project-uuid", project_uuid,
210              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
211             sys.stdout, sys.stderr, api_client=stubs.api)
212         self.assertEqual(exited, 0)
213
214         expect_body = copy.deepcopy(stubs.expect_job_spec)
215         expect_body["owner_uuid"] = project_uuid
216         stubs.api.jobs().create.assert_called_with(
217             body=expect_body,
218             find_or_create=True)
219
220     @stubs
221     def test_submit_container(self, stubs):
222         capture_stdout = cStringIO.StringIO()
223         try:
224             exited = arvados_cwl.main(
225                 ["--submit", "--no-wait", "--api=containers", "--debug",
226                  "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
227                 capture_stdout, sys.stderr, api_client=stubs.api, keep_client=stubs.keep_client)
228             self.assertEqual(exited, 0)
229         except:
230             logging.exception("")
231
232         stubs.api.collections().create.assert_has_calls([
233             mock.call(),
234             mock.call(body={
235                 'manifest_text':
236                 './tool d51232d96b6116d964a69bfb7e0c73bf+450 '
237                 '0:16:blub.txt 16:434:submit_tool.cwl\n./wf '
238                 'cc2ffb940e60adf1b2b282c67587e43d+413 0:413:submit_wf.cwl\n',
239                 'owner_uuid': 'zzzzz-tpzed-zzzzzzzzzzzzzzz',
240                 'name': 'submit_wf.cwl',
241             }, ensure_unique_name=True),
242             mock.call().execute(),
243             mock.call(body={'manifest_text': '. d41d8cd98f00b204e9800998ecf8427e+0 '
244                             '0:0:blub.txt 0:0:submit_tool.cwl\n',
245                             'owner_uuid': 'zzzzz-tpzed-zzzzzzzzzzzzzzz',
246                             'name': 'New collection',
247                             'replication_desired': None,
248             }, ensure_unique_name=True),
249             mock.call().execute(num_retries=4),
250             mock.call(body={
251                 'manifest_text':
252                 '. 979af1245a12a1fed634d4222473bfdc+16 0:16:blorp.txt\n',
253                 'owner_uuid': 'zzzzz-tpzed-zzzzzzzzzzzzzzz',
254                 'name': '#',
255             }, ensure_unique_name=True),
256             mock.call().execute()])
257
258         expect_container = copy.deepcopy(stubs.expect_container_spec)
259         expect_container["owner_uuid"] = stubs.fake_user_uuid
260         stubs.api.container_requests().create.assert_called_with(
261             body=expect_container)
262         self.assertEqual(capture_stdout.getvalue(),
263                          stubs.expect_container_request_uuid + '\n')
264
265
266 class TestCreateTemplate(unittest.TestCase):
267     @stubs
268     def test_create(self, stubs):
269         project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
270
271         capture_stdout = cStringIO.StringIO()
272
273         exited = arvados_cwl.main(
274             ["--create-template", "--debug",
275              "--project-uuid", project_uuid,
276              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
277             capture_stdout, sys.stderr, api_client=stubs.api)
278         self.assertEqual(exited, 0)
279
280         stubs.api.pipeline_instances().create.refute_called()
281         stubs.api.jobs().create.refute_called()
282
283         expect_component = copy.deepcopy(stubs.expect_job_spec)
284         expect_component['script_parameters']['x'] = {
285             'dataclass': 'File',
286             'required': True,
287             'type': 'File',
288             'value': '99999999999999999999999999999994+99/blorp.txt',
289         }
290         expect_component['script_parameters']['y'] = {
291             'dataclass': 'Collection',
292             'required': True,
293             'type': 'Directory',
294             'value': '99999999999999999999999999999998+99',
295         }
296         expect_component['script_parameters']['z'] = {
297             'dataclass': 'Collection',
298             'required': True,
299             'type': 'Directory',
300         }
301         expect_template = {
302             "components": {
303                 "submit_wf.cwl": expect_component,
304             },
305             "name": "submit_wf.cwl",
306             "owner_uuid": project_uuid,
307         }
308         stubs.api.pipeline_templates().create.assert_called_with(
309             body=JsonDiffMatcher(expect_template), ensure_unique_name=True)
310
311         self.assertEqual(capture_stdout.getvalue(),
312                          stubs.expect_pipeline_template_uuid + '\n')
313
314
315 class TestCreateWorkflow(unittest.TestCase):
316     @stubs
317     def test_create(self, stubs):
318         project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
319
320         capture_stdout = cStringIO.StringIO()
321
322         exited = arvados_cwl.main(
323             ["--create-workflow", "--debug",
324              "--project-uuid", project_uuid,
325              "tests/wf/submit_wf.cwl", "tests/submit_test_job.json"],
326             capture_stdout, sys.stderr, api_client=stubs.api)
327         self.assertEqual(exited, 0)
328
329         stubs.api.pipeline_templates().create.refute_called()
330         stubs.api.container_requests().create.refute_called()
331
332         with open("tests/wf/expect_packed.cwl") as f:
333             expect_workflow = f.read()
334
335         body = {
336             "workflow": {
337                 "owner_uuid": project_uuid,
338                 "name": "submit_wf.cwl",
339                 "description": "",
340                 "definition": expect_workflow
341                 }
342         }
343         stubs.api.workflows().create.assert_called_with(
344             body=JsonDiffMatcher(body))
345
346         self.assertEqual(capture_stdout.getvalue(),
347                          stubs.expect_workflow_uuid + '\n')
348
349
350 class TestTemplateInputs(unittest.TestCase):
351     expect_template = {
352         "components": {
353             "inputs_test.cwl": {
354                 'runtime_constraints': {
355                     'docker_image': 'arvados/jobs',
356                 },
357                 'script_parameters': {
358                     'cwl:tool':
359                     '99999999999999999999999999999991+99/'
360                     'wf/inputs_test.cwl',
361                     'optionalFloatInput': None,
362                     'fileInput': {
363                         'type': 'File',
364                         'dataclass': 'File',
365                         'required': True,
366                         'title': "It's a file; we expect to find some characters in it.",
367                         'description': 'If there were anything further to say, it would be said here,\nor here.'
368                     },
369                     'floatInput': {
370                         'type': 'float',
371                         'dataclass': 'number',
372                         'required': True,
373                         'title': 'Floats like a duck',
374                         'default': 0.1,
375                         'value': 0.1,
376                     },
377                     'optionalFloatInput': {
378                         'type': ['null', 'float'],
379                         'dataclass': 'number',
380                         'required': False,
381                     },
382                     'boolInput': {
383                         'type': 'boolean',
384                         'dataclass': 'boolean',
385                         'required': True,
386                         'title': 'True or false?',
387                     },
388                 },
389                 'repository': 'arvados',
390                 'script_version': 'master',
391                 'script': 'cwl-runner',
392             },
393         },
394         "name": "inputs_test.cwl",
395     }
396
397     @stubs
398     def test_inputs_empty(self, stubs):
399         exited = arvados_cwl.main(
400             ["--create-template", "--no-wait",
401              "tests/wf/inputs_test.cwl", "tests/order/empty_order.json"],
402             cStringIO.StringIO(), sys.stderr, api_client=stubs.api)
403         self.assertEqual(exited, 0)
404
405         expect_template = copy.deepcopy(self.expect_template)
406         expect_template["owner_uuid"] = stubs.fake_user_uuid
407
408         stubs.api.pipeline_templates().create.assert_called_with(
409             body=JsonDiffMatcher(expect_template), ensure_unique_name=True)
410
411     @stubs
412     def test_inputs(self, stubs):
413         exited = arvados_cwl.main(
414             ["--create-template", "--no-wait",
415              "tests/wf/inputs_test.cwl", "tests/order/inputs_test_order.json"],
416             cStringIO.StringIO(), sys.stderr, api_client=stubs.api)
417         self.assertEqual(exited, 0)
418
419         self.expect_template["owner_uuid"] = stubs.fake_user_uuid
420
421         expect_template = copy.deepcopy(self.expect_template)
422         expect_template["owner_uuid"] = stubs.fake_user_uuid
423         params = expect_template[
424             "components"]["inputs_test.cwl"]["script_parameters"]
425         params["fileInput"]["value"] = '99999999999999999999999999999994+99/blorp.txt'
426         params["floatInput"]["value"] = 1.234
427         params["boolInput"]["value"] = True
428
429         stubs.api.pipeline_templates().create.assert_called_with(
430             body=JsonDiffMatcher(expect_template), ensure_unique_name=True)