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