1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
15 import arvados.collection
18 from cwltool.pathmapper import MapperEnt
19 from .mock_discovery import get_rootDesc
21 from arvados_cwl.pathmapper import ArvPathMapper
23 def upload_mock(files, api, dry_run=False, num_retries=0, project=None, fnPattern="$(file %s/%s)", name=None, collection=None):
24 pdh = "99999999999999999999999999999991+99"
26 c.keepref = "%s/%s" % (pdh, os.path.basename(c.fn))
27 c.fn = fnPattern % (pdh, os.path.basename(c.fn))
29 class TestPathmap(unittest.TestCase):
31 self.api = mock.MagicMock()
32 self.api._rootDesc = get_rootDesc()
34 def test_keepref(self):
35 """Test direct keep references."""
37 arvrunner = arvados_cwl.ArvCwlRunner(self.api)
39 p = ArvPathMapper(arvrunner, [{
41 "location": "keep:99999999999999999999999999999991+99/hw.py"
42 }], "", "/test/%s", "/test/%s/%s")
44 self.assertEqual({'keep:99999999999999999999999999999991+99/hw.py': MapperEnt(resolved='keep:99999999999999999999999999999991+99/hw.py', target='/test/99999999999999999999999999999991+99/hw.py', type='File', staged=True)},
47 @mock.patch("arvados.commands.run.uploadfiles")
48 @mock.patch("arvados.commands.run.statfile")
49 def test_upload(self, statfile, upl):
50 """Test pathmapper uploading files."""
52 arvrunner = arvados_cwl.ArvCwlRunner(self.api)
54 def statfile_mock(prefix, fn, fnPattern="$(file %s/%s)", dirPattern="$(dir %s/%s/)", raiseOSError=False):
55 st = arvados.commands.run.UploadFile("", "tests/hw.py")
58 upl.side_effect = upload_mock
59 statfile.side_effect = statfile_mock
61 p = ArvPathMapper(arvrunner, [{
63 "location": "file:tests/hw.py"
64 }], "", "/test/%s", "/test/%s/%s")
66 self.assertEqual({'file:tests/hw.py': MapperEnt(resolved='keep:99999999999999999999999999999991+99/hw.py', target='/test/99999999999999999999999999999991+99/hw.py', type='File', staged=True)},
69 @mock.patch("arvados.commands.run.uploadfiles")
70 def test_prev_uploaded(self, upl):
71 """Test pathmapper handling previously uploaded files."""
73 arvrunner = arvados_cwl.ArvCwlRunner(self.api)
74 arvrunner.add_uploaded('file:tests/hw.py', MapperEnt(resolved='keep:99999999999999999999999999999992+99/hw.py', target='', type='File', staged=True))
76 upl.side_effect = upload_mock
78 p = ArvPathMapper(arvrunner, [{
80 "location": "file:tests/hw.py"
81 }], "", "/test/%s", "/test/%s/%s")
83 self.assertEqual({'file:tests/hw.py': MapperEnt(resolved='keep:99999999999999999999999999999992+99/hw.py', target='/test/99999999999999999999999999999992+99/hw.py', type='File', staged=True)},
86 @mock.patch("arvados.commands.run.uploadfiles")
87 @mock.patch("arvados.commands.run.statfile")
88 def test_statfile(self, statfile, upl):
89 """Test pathmapper handling ArvFile references."""
90 arvrunner = arvados_cwl.ArvCwlRunner(self.api)
92 # An ArvFile object returned from arvados.commands.run.statfile means the file is located on a
93 # keep mount, so we can construct a direct reference directly without upload.
94 def statfile_mock(prefix, fn, fnPattern="$(file %s/%s)", dirPattern="$(dir %s/%s/)", raiseOSError=False):
95 st = arvados.commands.run.ArvFile("", fnPattern % ("99999999999999999999999999999991+99", "hw.py"))
98 upl.side_effect = upload_mock
99 statfile.side_effect = statfile_mock
101 p = ArvPathMapper(arvrunner, [{
103 "location": "file:tests/hw.py"
104 }], "", "/test/%s", "/test/%s/%s")
106 self.assertEqual({'file:tests/hw.py': MapperEnt(resolved='keep:99999999999999999999999999999991+99/hw.py', target='/test/99999999999999999999999999999991+99/hw.py', type='File', staged=True)},
109 @mock.patch("os.stat")
110 def test_missing_file(self, stat):
111 """Test pathmapper handling missing references."""
112 arvrunner = arvados_cwl.ArvCwlRunner(self.api)
114 stat.side_effect = OSError(2, "No such file or directory")
116 with self.assertRaises(OSError):
117 p = ArvPathMapper(arvrunner, [{
119 "location": "file:tests/hw.py"
120 }], "", "/test/%s", "/test/%s/%s")