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, packed=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 @mock.patch("arvados.commands.run.statfile")
71 def test_statfile(self, statfile, upl):
72 """Test pathmapper handling ArvFile references."""
73 arvrunner = arvados_cwl.ArvCwlRunner(self.api)
75 # An ArvFile object returned from arvados.commands.run.statfile means the file is located on a
76 # keep mount, so we can construct a direct reference directly without upload.
77 def statfile_mock(prefix, fn, fnPattern="$(file %s/%s)", dirPattern="$(dir %s/%s/)", raiseOSError=False):
78 st = arvados.commands.run.ArvFile("", fnPattern % ("99999999999999999999999999999991+99", "hw.py"))
81 upl.side_effect = upload_mock
82 statfile.side_effect = statfile_mock
84 p = ArvPathMapper(arvrunner, [{
86 "location": "file:tests/hw.py"
87 }], "", "/test/%s", "/test/%s/%s")
89 self.assertEqual({'file:tests/hw.py': MapperEnt(resolved='keep:99999999999999999999999999999991+99/hw.py', target='/test/99999999999999999999999999999991+99/hw.py', type='File', staged=True)},
92 @mock.patch("os.stat")
93 def test_missing_file(self, stat):
94 """Test pathmapper handling missing references."""
95 arvrunner = arvados_cwl.ArvCwlRunner(self.api)
97 stat.side_effect = OSError(2, "No such file or directory")
99 with self.assertRaises(OSError):
100 p = ArvPathMapper(arvrunner, [{
102 "location": "file:tests/hw.py"
103 }], "", "/test/%s", "/test/%s/%s")