2 from arvados_cwl.arvdocker import arv_docker_clear_cache
9 from schema_salad.ref_resolver import Loader
10 from schema_salad.sourceline import cmap
12 from .matcher import JsonDiffMatcher
14 if not os.getenv('ARVADOS_DEBUG'):
15 logging.getLogger('arvados.cwl-runner').setLevel(logging.WARN)
16 logging.getLogger('arvados.arv-run').setLevel(logging.WARN)
19 class TestContainer(unittest.TestCase):
21 # The test passes no builder.resources
22 # Hence the default resources will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
23 @mock.patch("arvados.commands.keepdocker.list_images_in_arv")
24 def test_run(self, keepdocker):
25 for enable_reuse in (True, False):
26 arv_docker_clear_cache()
28 runner = mock.MagicMock()
29 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
30 runner.ignore_docker_for_reuse = False
31 runner.intermediate_output_ttl = 0
33 keepdocker.return_value = [("zzzzz-4zz18-zzzzzzzzzzzzzz3", "")]
34 runner.api.collections().get().execute.return_value = {
35 "portable_data_hash": "99999999999999999999999999999993+99"}
37 document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0")
43 "arguments": [{"valueFrom": "$(runtime.outdir)"}]
45 make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess,
46 collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0))
47 arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, work_api="containers", avsc_names=avsc_names,
48 basedir="", make_fs_access=make_fs_access, loader=Loader({}))
49 arvtool.formatgraph = None
50 for j in arvtool.job({}, mock.MagicMock(), basedir="", name="test_run_"+str(enable_reuse),
51 make_fs_access=make_fs_access, tmpdir="/tmp"):
52 j.run(enable_reuse=enable_reuse)
53 runner.api.container_requests().create.assert_called_with(
54 body=JsonDiffMatcher({
56 'HOME': '/var/spool/cwl',
59 'name': 'test_run_'+str(enable_reuse),
60 'runtime_constraints': {
64 'use_existing': enable_reuse,
67 '/tmp': {'kind': 'tmp',
68 "capacity": 1073741824
70 '/var/spool/cwl': {'kind': 'tmp',
71 "capacity": 1073741824 }
74 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
75 'output_path': '/var/spool/cwl',
77 'container_image': 'arvados/jobs',
78 'command': ['ls', '/var/spool/cwl'],
79 'cwd': '/var/spool/cwl',
80 'scheduling_parameters': {},
84 # The test passes some fields in builder.resources
85 # For the remaining fields, the defaults will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
86 @mock.patch("arvados.commands.keepdocker.list_images_in_arv")
87 def test_resource_requirements(self, keepdocker):
88 arv_docker_clear_cache()
89 runner = mock.MagicMock()
90 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
91 runner.ignore_docker_for_reuse = False
92 runner.intermediate_output_ttl = 3600
93 document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0")
95 keepdocker.return_value = [("zzzzz-4zz18-zzzzzzzzzzzzzz3", "")]
96 runner.api.collections().get().execute.return_value = {
97 "portable_data_hash": "99999999999999999999999999999993+99"}
103 "class": "ResourceRequirement",
109 "class": "http://arvados.org/cwl#RuntimeConstraints",
112 "class": "http://arvados.org/cwl#APIRequirement",
114 "class": "http://arvados.org/cwl#PartitionRequirement",
117 "class": "http://arvados.org/cwl#IntermediateOutput",
122 make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess,
123 collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0))
124 arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, work_api="containers",
125 avsc_names=avsc_names, make_fs_access=make_fs_access,
127 arvtool.formatgraph = None
128 for j in arvtool.job({}, mock.MagicMock(), basedir="", name="test_resource_requirements",
129 make_fs_access=make_fs_access, tmpdir="/tmp"):
132 call_args, call_kwargs = runner.api.container_requests().create.call_args
134 call_body_expected = {
136 'HOME': '/var/spool/cwl',
139 'name': 'test_resource_requirements',
140 'runtime_constraints': {
143 'keep_cache_ram': 536870912,
146 'use_existing': True,
149 '/tmp': {'kind': 'tmp',
150 "capacity": 4194304000 },
151 '/var/spool/cwl': {'kind': 'tmp',
152 "capacity": 5242880000 }
154 'state': 'Committed',
155 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
156 'output_path': '/var/spool/cwl',
158 'container_image': 'arvados/jobs',
160 'cwd': '/var/spool/cwl',
161 'scheduling_parameters': {
162 'partitions': ['blurb']
167 call_body = call_kwargs.get('body', None)
168 self.assertNotEqual(None, call_body)
169 for key in call_body:
170 self.assertEqual(call_body_expected.get(key), call_body.get(key))
173 # The test passes some fields in builder.resources
174 # For the remaining fields, the defaults will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
175 @mock.patch("arvados.commands.keepdocker.list_images_in_arv")
176 @mock.patch("arvados.collection.Collection")
177 def test_initial_work_dir(self, collection_mock, keepdocker):
178 arv_docker_clear_cache()
179 runner = mock.MagicMock()
180 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
181 runner.ignore_docker_for_reuse = False
182 runner.intermediate_output_ttl = 0
183 document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0")
185 keepdocker.return_value = [("zzzzz-4zz18-zzzzzzzzzzzzzz3", "")]
186 runner.api.collections().get().execute.return_value = {
187 "portable_data_hash": "99999999999999999999999999999993+99"}
189 sourcemock = mock.MagicMock()
190 def get_collection_mock(p):
192 return (sourcemock, p.split("/", 1)[1])
194 return (sourcemock, "")
195 runner.fs_access.get_collection.side_effect = get_collection_mock
197 vwdmock = mock.MagicMock()
198 collection_mock.return_value = vwdmock
199 vwdmock.portable_data_hash.return_value = "99999999999999999999999999999996+99"
205 "class": "InitialWorkDirRequirement",
209 "location": "keep:99999999999999999999999999999995+99/bar"
212 "class": "Directory",
214 "location": "keep:99999999999999999999999999999995+99"
218 "basename": "filename",
219 "location": "keep:99999999999999999999999999999995+99/baz/filename"
222 "class": "Directory",
223 "basename": "subdir",
224 "location": "keep:99999999999999999999999999999995+99/subdir"
229 make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess,
230 collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0))
231 arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, work_api="containers",
232 avsc_names=avsc_names, make_fs_access=make_fs_access,
234 arvtool.formatgraph = None
235 for j in arvtool.job({}, mock.MagicMock(), basedir="", name="test_initial_work_dir",
236 make_fs_access=make_fs_access, tmpdir="/tmp"):
239 call_args, call_kwargs = runner.api.container_requests().create.call_args
241 vwdmock.copy.assert_has_calls([mock.call('bar', 'foo', source_collection=sourcemock)])
242 vwdmock.copy.assert_has_calls([mock.call('', 'foo2', source_collection=sourcemock)])
243 vwdmock.copy.assert_has_calls([mock.call('baz/filename', 'filename', source_collection=sourcemock)])
244 vwdmock.copy.assert_has_calls([mock.call('subdir', 'subdir', source_collection=sourcemock)])
246 call_body_expected = {
248 'HOME': '/var/spool/cwl',
251 'name': 'test_initial_work_dir',
252 'runtime_constraints': {
256 'use_existing': True,
259 '/tmp': {'kind': 'tmp',
260 "capacity": 1073741824 },
261 '/var/spool/cwl': {'kind': 'tmp',
262 "capacity": 1073741824 },
263 '/var/spool/cwl/foo': {
264 'kind': 'collection',
266 'portable_data_hash': '99999999999999999999999999999996+99'
268 '/var/spool/cwl/foo2': {
269 'kind': 'collection',
271 'portable_data_hash': '99999999999999999999999999999996+99'
273 '/var/spool/cwl/filename': {
274 'kind': 'collection',
276 'portable_data_hash': '99999999999999999999999999999996+99'
278 '/var/spool/cwl/subdir': {
279 'kind': 'collection',
281 'portable_data_hash': '99999999999999999999999999999996+99'
284 'state': 'Committed',
285 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
286 'output_path': '/var/spool/cwl',
288 'container_image': 'arvados/jobs',
290 'cwd': '/var/spool/cwl',
291 'scheduling_parameters': {
296 call_body = call_kwargs.get('body', None)
297 self.assertNotEqual(None, call_body)
298 for key in call_body:
299 self.assertEqual(call_body_expected.get(key), call_body.get(key))
302 # Test redirecting stdin/stdout/stderr
303 @mock.patch("arvados.commands.keepdocker.list_images_in_arv")
304 def test_redirects(self, keepdocker):
305 arv_docker_clear_cache()
307 runner = mock.MagicMock()
308 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
309 runner.ignore_docker_for_reuse = False
310 runner.intermediate_output_ttl = 0
312 keepdocker.return_value = [("zzzzz-4zz18-zzzzzzzzzzzzzz3", "")]
313 runner.api.collections().get().execute.return_value = {
314 "portable_data_hash": "99999999999999999999999999999993+99"}
316 document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0")
322 "stdout": "stdout.txt",
323 "stderr": "stderr.txt",
324 "stdin": "/keep/99999999999999999999999999999996+99/file.txt",
325 "arguments": [{"valueFrom": "$(runtime.outdir)"}]
327 make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess,
328 collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0))
329 arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, work_api="containers", avsc_names=avsc_names,
330 basedir="", make_fs_access=make_fs_access, loader=Loader({}))
331 arvtool.formatgraph = None
332 for j in arvtool.job({}, mock.MagicMock(), basedir="", name="test_run_redirect",
333 make_fs_access=make_fs_access, tmpdir="/tmp"):
335 runner.api.container_requests().create.assert_called_with(
336 body=JsonDiffMatcher({
338 'HOME': '/var/spool/cwl',
341 'name': 'test_run_redirect',
342 'runtime_constraints': {
346 'use_existing': True,
349 '/tmp': {'kind': 'tmp',
350 "capacity": 1073741824 },
351 '/var/spool/cwl': {'kind': 'tmp',
352 "capacity": 1073741824 },
355 "path": "/var/spool/cwl/stderr.txt"
358 "kind": "collection",
360 "portable_data_hash": "99999999999999999999999999999996+99"
364 "path": "/var/spool/cwl/stdout.txt"
367 'state': 'Committed',
368 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
369 'output_path': '/var/spool/cwl',
371 'container_image': 'arvados/jobs',
372 'command': ['ls', '/var/spool/cwl'],
373 'cwd': '/var/spool/cwl',
374 'scheduling_parameters': {},
378 @mock.patch("arvados.collection.Collection")
379 def test_done(self, col):
380 api = mock.MagicMock()
382 runner = mock.MagicMock()
384 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
385 runner.num_retries = 0
386 runner.ignore_docker_for_reuse = False
387 runner.intermediate_output_ttl = 0
389 runner.api.containers().get().execute.return_value = {"state":"Complete",
393 col().open.return_value = []
395 arvjob = arvados_cwl.ArvadosContainer(runner)
396 arvjob.name = "testjob"
397 arvjob.builder = mock.MagicMock()
398 arvjob.output_callback = mock.MagicMock()
399 arvjob.collect_outputs = mock.MagicMock()
400 arvjob.successCodes = [0]
401 arvjob.outdir = "/var/spool/cwl"
402 arvjob.output_ttl = 3600
404 arvjob.collect_outputs.return_value = {"out": "stuff"}
408 "log_uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz1",
409 "output_uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz2",
410 "uuid": "zzzzz-xvhdp-zzzzzzzzzzzzzzz",
411 "container_uuid": "zzzzz-8i9sb-zzzzzzzzzzzzzzz",
412 "modified_at": "2017-05-26T12:01:22Z"
415 self.assertFalse(api.collections().create.called)
417 arvjob.collect_outputs.assert_called_with("keep:abc+123")
418 arvjob.output_callback.assert_called_with({"out": "stuff"}, "success")
419 runner.add_intermediate_output.assert_called_with("zzzzz-4zz18-zzzzzzzzzzzzzz2")
421 # The test passes no builder.resources
422 # Hence the default resources will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
423 @mock.patch("arvados.commands.keepdocker.list_images_in_arv")
424 def test_mounts(self, keepdocker):
425 arv_docker_clear_cache()
427 runner = mock.MagicMock()
428 runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
429 runner.ignore_docker_for_reuse = False
430 runner.intermediate_output_ttl = 0
432 keepdocker.return_value = [("zzzzz-4zz18-zzzzzzzzzzzzzz3", "")]
433 runner.api.collections().get().execute.return_value = {
434 "portable_data_hash": "99999999999999999999999999999993+99"}
436 document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.0")
445 "arguments": [{"valueFrom": "$(runtime.outdir)"}]
447 make_fs_access=functools.partial(arvados_cwl.CollectionFsAccess,
448 collection_cache=arvados_cwl.CollectionCache(runner.api, None, 0))
449 arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, work_api="containers", avsc_names=avsc_names,
450 basedir="", make_fs_access=make_fs_access, loader=Loader({}))
451 arvtool.formatgraph = None
454 "class": "Directory",
455 "location": "keep:99999999999999999999999999999994+44",
459 "location": "keep:99999999999999999999999999999994+44/file1",
463 "location": "keep:99999999999999999999999999999994+44/file2",
468 for j in arvtool.job(job_order, mock.MagicMock(), basedir="", name="test_run_mounts",
469 make_fs_access=make_fs_access, tmpdir="/tmp"):
471 runner.api.container_requests().create.assert_called_with(
472 body=JsonDiffMatcher({
474 'HOME': '/var/spool/cwl',
477 'name': 'test_run_mounts',
478 'runtime_constraints': {
482 'use_existing': True,
485 "/keep/99999999999999999999999999999994+44": {
486 "kind": "collection",
487 "portable_data_hash": "99999999999999999999999999999994+44"
489 '/tmp': {'kind': 'tmp',
490 "capacity": 1073741824 },
491 '/var/spool/cwl': {'kind': 'tmp',
492 "capacity": 1073741824 }
494 'state': 'Committed',
495 'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
496 'output_path': '/var/spool/cwl',
498 'container_image': 'arvados/jobs',
499 'command': ['ls', '/var/spool/cwl'],
500 'cwd': '/var/spool/cwl',
501 'scheduling_parameters': {},