1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
12 from unittest import mock
16 import arvados_cwl.executor
17 from .mock_discovery import get_rootDesc
19 class TestMakeOutput(unittest.TestCase):
21 self.api = mock.MagicMock()
22 self.api._rootDesc = get_rootDesc()
25 root_logger = logging.getLogger('')
27 # Remove existing RuntimeStatusLoggingHandlers if they exist
28 handlers = [h for h in root_logger.handlers if not isinstance(h, arvados_cwl.executor.RuntimeStatusLoggingHandler)]
29 root_logger.handlers = handlers
31 @mock.patch("arvados.collection.Collection")
32 @mock.patch("arvados.collection.CollectionReader")
33 def test_make_output_collection(self, reader, col):
34 keep_client = mock.MagicMock()
35 runner = arvados_cwl.executor.ArvCwlExecutor(self.api, keep_client=keep_client)
36 runner.project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
38 final = mock.MagicMock()
39 col.return_value = final
40 readermock = mock.MagicMock()
41 reader.return_value = readermock
43 final_uuid = final.manifest_locator()
44 num_retries = runner.num_retries
46 cwlout = io.StringIO()
47 openmock = mock.MagicMock()
48 final.open.return_value = openmock
49 openmock.__enter__.return_value = cwlout
51 _, runner.final_output_collection = runner.make_output_collection("Test output", ["foo"], "tag0,tag1,tag2", {}, {
54 "location": "keep:99999999999999999999999999999991+99/foo.txt",
60 "location": "keep:99999999999999999999999999999992+99/bar.txt",
61 "basename": "baz.txt",
66 final.copy.assert_has_calls([mock.call('bar.txt', 'baz.txt', overwrite=False, source_collection=readermock)])
67 final.copy.assert_has_calls([mock.call('foo.txt', 'foo.txt', overwrite=False, source_collection=readermock)])
68 final.save_new.assert_has_calls([mock.call(ensure_unique_name=True, name='Test output', owner_uuid='zzzzz-j7d0g-zzzzzzzzzzzzzzz', properties={}, storage_classes=['foo'])])
71 "basename": "baz.txt",
73 "location": "baz.txt",
77 "basename": "foo.txt",
79 "location": "foo.txt",
82 }""", cwlout.getvalue())
84 self.assertIs(final, runner.final_output_collection)
85 self.assertIs(final_uuid, runner.final_output_collection.manifest_locator())
86 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)])
87 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)])
88 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)])
90 @mock.patch("arvados.collection.Collection")
91 @mock.patch("arvados.collection.CollectionReader")
92 def test_make_output_for_multiple_file_targets(self, reader, col):
93 keep_client = mock.MagicMock()
94 runner = arvados_cwl.executor.ArvCwlExecutor(self.api, keep_client=keep_client)
95 runner.project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
97 final = mock.MagicMock()
98 col.return_value = final
99 readermock = mock.MagicMock()
100 reader.return_value = readermock
102 # This output describes a single file listed in 2 different directories
103 _, runner.final_output_collection = runner.make_output_collection("Test output", ["foo"], "", {}, { 'out': [
105 'basename': 'testdir1',
108 'basename': 'test.txt',
111 'location': 'keep:99999999999999999999999999999991+99/test.txt',
116 'location': '_:99999999999999999999999999999992+99',
120 'basename': 'testdir2',
123 'basename': 'test.txt',
126 'location': 'keep:99999999999999999999999999999991+99/test.txt',
132 'location': '_:99999999999999999999999999999993+99',
136 # Check that copy is called on the collection for both locations
137 final.copy.assert_any_call("test.txt", "testdir1/test.txt", source_collection=mock.ANY, overwrite=mock.ANY)
138 final.copy.assert_any_call("test.txt", "testdir2/test.txt", source_collection=mock.ANY, overwrite=mock.ANY)
140 @mock.patch("arvados.collection.Collection")
141 @mock.patch("arvados.collection.CollectionReader")
142 def test_make_output_for_literal_name_conflicts(self, reader, col):
143 keep_client = mock.MagicMock()
144 runner = arvados_cwl.executor.ArvCwlExecutor(self.api, keep_client=keep_client)
145 runner.project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
147 final = mock.MagicMock()
148 col.return_value = final
149 readermock = mock.MagicMock()
150 reader.return_value = readermock
152 # This output describes two literals with the same basename
153 _, runner.final_output_collection = runner.make_output_collection("Test output", ["foo"], "", {}, [
157 'basename': 'a_file',
159 'nameroot': 'a_file',
160 'location': '_:f168fc0c-4291-40aa-a04e-366d57390560',
162 'contents': 'Hello file literal.'
168 'basename': 'a_file',
170 'nameroot': 'a_file',
171 'location': '_:1728da8f-c64e-4a3e-b2e2-1ee356be7bc8',
173 'contents': 'Hello file literal.'
177 # Check that the file name conflict is resolved and open is called for both
178 final.open.assert_any_call("a_file", "wb")
179 final.open.assert_any_call("a_file_2", "wb")