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()
26 @mock.patch("arvados.collection.Collection")
27 @mock.patch("arvados.collection.CollectionReader")
28 def test_make_output_collection(self, reader, col):
29 keep_client = mock.MagicMock()
30 runner = arvados_cwl.executor.ArvCwlExecutor(self.api, keep_client=keep_client)
31 runner.project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
33 final = mock.MagicMock()
34 col.return_value = final
35 readermock = mock.MagicMock()
36 reader.return_value = readermock
38 final_uuid = final.manifest_locator()
39 num_retries = runner.num_retries
41 cwlout = io.StringIO()
42 openmock = mock.MagicMock()
43 final.open.return_value = openmock
44 openmock.__enter__.return_value = cwlout
46 _, runner.final_output_collection = runner.make_output_collection("Test output", ["foo"], "tag0,tag1,tag2", {
49 "location": "keep:99999999999999999999999999999991+99/foo.txt",
55 "location": "keep:99999999999999999999999999999992+99/bar.txt",
56 "basename": "baz.txt",
61 final.copy.assert_has_calls([mock.call('bar.txt', 'baz.txt', overwrite=False, source_collection=readermock)])
62 final.copy.assert_has_calls([mock.call('foo.txt', 'foo.txt', overwrite=False, source_collection=readermock)])
63 final.save_new.assert_has_calls([mock.call(ensure_unique_name=True, name='Test output', owner_uuid='zzzzz-j7d0g-zzzzzzzzzzzzzzz', storage_classes=['foo'])])
66 "basename": "baz.txt",
68 "location": "baz.txt",
72 "basename": "foo.txt",
74 "location": "foo.txt",
77 }""", cwlout.getvalue())
79 self.assertIs(final, runner.final_output_collection)
80 self.assertIs(final_uuid, runner.final_output_collection.manifest_locator())
81 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)])
82 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)])
83 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)])
85 @mock.patch("arvados.collection.Collection")
86 @mock.patch("arvados.collection.CollectionReader")
87 def test_make_output_for_multiple_file_targets(self, reader, col):
88 keep_client = mock.MagicMock()
89 runner = arvados_cwl.executor.ArvCwlExecutor(self.api, keep_client=keep_client)
90 runner.project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
92 final = mock.MagicMock()
93 col.return_value = final
94 readermock = mock.MagicMock()
95 reader.return_value = readermock
97 # This output describes a single file listed in 2 different directories
98 _, runner.final_output_collection = runner.make_output_collection("Test output", ["foo"], "", { 'out': [
100 'basename': 'testdir1',
103 'basename': 'test.txt',
106 'location': 'keep:99999999999999999999999999999991+99/test.txt',
111 'location': '_:99999999999999999999999999999992+99',
115 'basename': 'testdir2',
118 'basename': 'test.txt',
121 'location': 'keep:99999999999999999999999999999991+99/test.txt',
127 'location': '_:99999999999999999999999999999993+99',
131 # Check that copy is called on the collection for both locations
132 final.copy.assert_any_call("test.txt", "testdir1/test.txt", source_collection=mock.ANY, overwrite=mock.ANY)
133 final.copy.assert_any_call("test.txt", "testdir2/test.txt", source_collection=mock.ANY, overwrite=mock.ANY)
135 @mock.patch("arvados.collection.Collection")
136 @mock.patch("arvados.collection.CollectionReader")
137 def test_make_output_for_literal_name_conflicts(self, reader, col):
138 keep_client = mock.MagicMock()
139 runner = arvados_cwl.executor.ArvCwlExecutor(self.api, keep_client=keep_client)
140 runner.project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
142 final = mock.MagicMock()
143 col.return_value = final
144 readermock = mock.MagicMock()
145 reader.return_value = readermock
147 # This output describes two literals with the same basename
148 _, runner.final_output_collection = runner.make_output_collection("Test output", ["foo"], "", [
152 'basename': 'a_file',
154 'nameroot': 'a_file',
155 'location': '_:f168fc0c-4291-40aa-a04e-366d57390560',
157 'contents': 'Hello file literal.'
163 'basename': 'a_file',
165 'nameroot': 'a_file',
166 'location': '_:1728da8f-c64e-4a3e-b2e2-1ee356be7bc8',
168 'contents': 'Hello file literal.'
172 # Check that the file name conflict is resolved and open is called for both
173 final.open.assert_any_call("a_file", "wb")
174 final.open.assert_any_call("a_file_2", "wb")