1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
15 import arvados.collection
17 import arvados_cwl.executor
19 from cwltool.pathmapper import MapperEnt
20 from .mock_discovery import get_rootDesc
22 from arvados_cwl.pathmapper import ArvPathMapper
24 def upload_mock(files, api, dry_run=False, num_retries=0, project=None, fnPattern="$(file %s/%s)", name=None, collection=None, packed=None):
25 pdh = "99999999999999999999999999999991+99"
27 c.keepref = "%s/%s" % (pdh, os.path.basename(c.fn))
28 c.fn = fnPattern % (pdh, os.path.basename(c.fn))
30 class TestPathmap(unittest.TestCase):
32 self.api = mock.MagicMock()
33 self.api._rootDesc = get_rootDesc()
35 def test_keepref(self):
36 """Test direct keep references."""
38 arvrunner = arvados_cwl.executor.ArvCwlExecutor(self.api)
40 p = ArvPathMapper(arvrunner, [{
42 "location": "keep:99999999999999999999999999999991+99/hw.py"
43 }], "", "/test/%s", "/test/%s/%s")
45 self.assertEqual({'keep:99999999999999999999999999999991+99/hw.py': MapperEnt(resolved='keep:99999999999999999999999999999991+99/hw.py', target='/test/99999999999999999999999999999991+99/hw.py', type='File', staged=True)},
48 @mock.patch("arvados.commands.run.uploadfiles")
49 @mock.patch("arvados.commands.run.statfile")
50 def test_upload(self, statfile, upl):
51 """Test pathmapper uploading files."""
53 arvrunner = arvados_cwl.executor.ArvCwlExecutor(self.api)
55 def statfile_mock(prefix, fn, fnPattern="$(file %s/%s)", dirPattern="$(dir %s/%s/)", raiseOSError=False):
56 st = arvados.commands.run.UploadFile("", "tests/hw.py")
59 upl.side_effect = upload_mock
60 statfile.side_effect = statfile_mock
62 p = ArvPathMapper(arvrunner, [{
64 "location": "file:tests/hw.py"
65 }], "", "/test/%s", "/test/%s/%s")
67 self.assertEqual({'file:tests/hw.py': MapperEnt(resolved='keep:99999999999999999999999999999991+99/hw.py', target='/test/99999999999999999999999999999991+99/hw.py', type='File', staged=True)},
70 @mock.patch("arvados.commands.run.uploadfiles")
71 @mock.patch("arvados.commands.run.statfile")
72 def test_statfile(self, statfile, upl):
73 """Test pathmapper handling ArvFile references."""
74 arvrunner = arvados_cwl.executor.ArvCwlExecutor(self.api)
76 # An ArvFile object returned from arvados.commands.run.statfile means the file is located on a
77 # keep mount, so we can construct a direct reference directly without upload.
78 def statfile_mock(prefix, fn, fnPattern="$(file %s/%s)", dirPattern="$(dir %s/%s/)", raiseOSError=False):
79 st = arvados.commands.run.ArvFile("", fnPattern % ("99999999999999999999999999999991+99", "hw.py"))
82 upl.side_effect = upload_mock
83 statfile.side_effect = statfile_mock
85 p = ArvPathMapper(arvrunner, [{
87 "location": "file:tests/hw.py"
88 }], "", "/test/%s", "/test/%s/%s")
90 self.assertEqual({'file:tests/hw.py': MapperEnt(resolved='keep:99999999999999999999999999999991+99/hw.py', target='/test/99999999999999999999999999999991+99/hw.py', type='File', staged=True)},
93 @mock.patch("os.stat")
94 def test_missing_file(self, stat):
95 """Test pathmapper handling missing references."""
96 arvrunner = arvados_cwl.executor.ArvCwlExecutor(self.api)
98 stat.side_effect = OSError(2, "No such file or directory")
100 with self.assertRaises(OSError):
101 p = ArvPathMapper(arvrunner, [{
103 "location": "file:tests/hw.py"
104 }], "", "/test/%s", "/test/%s/%s")