Merge branch '10291-discovery-blacklist' refs #10291
[arvados.git] / sdk / cwl / tests / test_pathmapper.py
1 import functools
2 import mock
3 import sys
4 import unittest
5 import json
6 import logging
7 import os
8
9 import arvados
10 import arvados.keep
11 import arvados.collection
12 import arvados_cwl
13
14 from cwltool.pathmapper import MapperEnt
15
16 from arvados_cwl.pathmapper import ArvPathMapper
17
18 def upload_mock(files, api, dry_run=False, num_retries=0, project=None, fnPattern="$(file %s/%s)", name=None):
19     pdh = "99999999999999999999999999999991+99"
20     for c in files:
21         c.fn = fnPattern % (pdh, os.path.basename(c.fn))
22
23 class TestPathmap(unittest.TestCase):
24     def setUp(self):
25         self.api = mock.MagicMock()
26         self.api._rootDesc = arvados.api('v1')._rootDesc
27
28     def test_keepref(self):
29         """Test direct keep references."""
30
31         arvrunner = arvados_cwl.ArvCwlRunner(self.api)
32
33         p = ArvPathMapper(arvrunner, [{
34             "class": "File",
35             "location": "keep:99999999999999999999999999999991+99/hw.py"
36         }], "", "/test/%s", "/test/%s/%s")
37
38         self.assertEqual({'keep:99999999999999999999999999999991+99/hw.py': MapperEnt(resolved='keep:99999999999999999999999999999991+99/hw.py', target='/test/99999999999999999999999999999991+99/hw.py', type='File')},
39                          p._pathmap)
40
41     @mock.patch("arvados.commands.run.uploadfiles")
42     def test_upload(self, upl):
43         """Test pathmapper uploading files."""
44
45         arvrunner = arvados_cwl.ArvCwlRunner(self.api)
46
47         upl.side_effect = upload_mock
48
49         p = ArvPathMapper(arvrunner, [{
50             "class": "File",
51             "location": "tests/hw.py"
52         }], "", "/test/%s", "/test/%s/%s")
53
54         self.assertEqual({'tests/hw.py': MapperEnt(resolved='keep:99999999999999999999999999999991+99/hw.py', target='/test/99999999999999999999999999999991+99/hw.py', type='File')},
55                          p._pathmap)
56
57     @mock.patch("arvados.commands.run.uploadfiles")
58     def test_prev_uploaded(self, upl):
59         """Test pathmapper handling previously uploaded files."""
60
61         arvrunner = arvados_cwl.ArvCwlRunner(self.api)
62         arvrunner.add_uploaded('tests/hw.py', MapperEnt(resolved='keep:99999999999999999999999999999991+99/hw.py', target='', type='File'))
63
64         upl.side_effect = upload_mock
65
66         p = ArvPathMapper(arvrunner, [{
67             "class": "File",
68             "location": "tests/hw.py"
69         }], "", "/test/%s", "/test/%s/%s")
70
71         self.assertEqual({'tests/hw.py': MapperEnt(resolved='keep:99999999999999999999999999999991+99/hw.py', target='/test/99999999999999999999999999999991+99/hw.py', type='File')},
72                          p._pathmap)
73
74     @mock.patch("arvados.commands.run.uploadfiles")
75     @mock.patch("arvados.commands.run.statfile")
76     def test_statfile(self, statfile, upl):
77         """Test pathmapper handling ArvFile references."""
78         arvrunner = arvados_cwl.ArvCwlRunner(self.api)
79
80         # An ArvFile object returned from arvados.commands.run.statfile means the file is located on a
81         # keep mount, so we can construct a direct reference directly without upload.
82         def statfile_mock(prefix, fn, fnPattern="$(file %s/%s)", dirPattern="$(dir %s/%s/)"):
83             st = arvados.commands.run.ArvFile("", fnPattern % ("99999999999999999999999999999991+99", "hw.py"))
84             return st
85
86         upl.side_effect = upload_mock
87         statfile.side_effect = statfile_mock
88
89         p = ArvPathMapper(arvrunner, [{
90             "class": "File",
91             "location": "tests/hw.py"
92         }], "", "/test/%s", "/test/%s/%s")
93
94         self.assertEqual({'tests/hw.py': MapperEnt(resolved='keep:99999999999999999999999999999991+99/hw.py', target='/test/99999999999999999999999999999991+99/hw.py', type='File')},
95                          p._pathmap)