1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from future import standard_library
6 standard_library.install_aliases()
18 import arvados_cwl.executor
19 from .mock_discovery import get_rootDesc
21 class TestMakeOutput(unittest.TestCase):
23 self.api = mock.MagicMock()
24 self.api._rootDesc = get_rootDesc()
27 root_logger = logging.getLogger('')
29 # Remove existing RuntimeStatusLoggingHandlers if they exist
30 handlers = [h for h in root_logger.handlers if not isinstance(h, arvados_cwl.executor.RuntimeStatusLoggingHandler)]
31 root_logger.handlers = handlers
33 @mock.patch("arvados.collection.Collection")
34 @mock.patch("arvados.collection.CollectionReader")
35 def test_make_output_collection(self, reader, col):
36 keep_client = mock.MagicMock()
37 runner = arvados_cwl.executor.ArvCwlExecutor(self.api, keep_client=keep_client)
38 runner.project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
40 final = mock.MagicMock()
41 col.return_value = final
42 readermock = mock.MagicMock()
43 reader.return_value = readermock
45 final_uuid = final.manifest_locator()
46 num_retries = runner.num_retries
48 cwlout = io.StringIO()
49 openmock = mock.MagicMock()
50 final.open.return_value = openmock
51 openmock.__enter__.return_value = cwlout
53 _, runner.final_output_collection = runner.make_output_collection("Test output", ["foo"], "tag0,tag1,tag2", {}, {
56 "location": "keep:99999999999999999999999999999991+99/foo.txt",
62 "location": "keep:99999999999999999999999999999992+99/bar.txt",
63 "basename": "baz.txt",
68 final.copy.assert_has_calls([mock.call('bar.txt', 'baz.txt', overwrite=False, source_collection=readermock)])
69 final.copy.assert_has_calls([mock.call('foo.txt', 'foo.txt', overwrite=False, source_collection=readermock)])
70 final.save_new.assert_has_calls([mock.call(ensure_unique_name=True, name='Test output', owner_uuid='zzzzz-j7d0g-zzzzzzzzzzzzzzz', properties={}, storage_classes=['foo'])])
73 "basename": "baz.txt",
75 "location": "baz.txt",
79 "basename": "foo.txt",
81 "location": "foo.txt",
84 }""", cwlout.getvalue())
86 self.assertIs(final, runner.final_output_collection)
87 self.assertIs(final_uuid, runner.final_output_collection.manifest_locator())
88 self.api.links().create.assert_has_calls([mock.call(body={"head_uuid": final_uuid, "link_class": "tag", "name": "tag0"}), mock.call().execute(num_retries=num_retries)])
89 self.api.links().create.assert_has_calls([mock.call(body={"head_uuid": final_uuid, "link_class": "tag", "name": "tag1"}), mock.call().execute(num_retries=num_retries)])
90 self.api.links().create.assert_has_calls([mock.call(body={"head_uuid": final_uuid, "link_class": "tag", "name": "tag2"}), mock.call().execute(num_retries=num_retries)])
92 @mock.patch("arvados.collection.Collection")
93 @mock.patch("arvados.collection.CollectionReader")
94 def test_make_output_for_multiple_file_targets(self, reader, col):
95 keep_client = mock.MagicMock()
96 runner = arvados_cwl.executor.ArvCwlExecutor(self.api, keep_client=keep_client)
97 runner.project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
99 final = mock.MagicMock()
100 col.return_value = final
101 readermock = mock.MagicMock()
102 reader.return_value = readermock
104 # This output describes a single file listed in 2 different directories
105 _, runner.final_output_collection = runner.make_output_collection("Test output", ["foo"], "", {}, { 'out': [
107 'basename': 'testdir1',
110 'basename': 'test.txt',
113 'location': 'keep:99999999999999999999999999999991+99/test.txt',
118 'location': '_:99999999999999999999999999999992+99',
122 'basename': 'testdir2',
125 'basename': 'test.txt',
128 'location': 'keep:99999999999999999999999999999991+99/test.txt',
134 'location': '_:99999999999999999999999999999993+99',
138 # Check that copy is called on the collection for both locations
139 final.copy.assert_any_call("test.txt", "testdir1/test.txt", source_collection=mock.ANY, overwrite=mock.ANY)
140 final.copy.assert_any_call("test.txt", "testdir2/test.txt", source_collection=mock.ANY, overwrite=mock.ANY)
142 @mock.patch("arvados.collection.Collection")
143 @mock.patch("arvados.collection.CollectionReader")
144 def test_make_output_for_literal_name_conflicts(self, reader, col):
145 keep_client = mock.MagicMock()
146 runner = arvados_cwl.executor.ArvCwlExecutor(self.api, keep_client=keep_client)
147 runner.project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
149 final = mock.MagicMock()
150 col.return_value = final
151 readermock = mock.MagicMock()
152 reader.return_value = readermock
154 # This output describes two literals with the same basename
155 _, runner.final_output_collection = runner.make_output_collection("Test output", ["foo"], "", {}, [
159 'basename': 'a_file',
161 'nameroot': 'a_file',
162 'location': '_:f168fc0c-4291-40aa-a04e-366d57390560',
164 'contents': 'Hello file literal.'
170 'basename': 'a_file',
172 'nameroot': 'a_file',
173 'location': '_:1728da8f-c64e-4a3e-b2e2-1ee356be7bc8',
175 'contents': 'Hello file literal.'
179 # Check that the file name conflict is resolved and open is called for both
180 final.open.assert_any_call("a_file", "wb")
181 final.open.assert_any_call("a_file_2", "wb")