Merge branch '13627-update-cwltool' refs #13627
[arvados.git] / sdk / cwl / tests / test_make_output.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 import functools
6 import json
7 import logging
8 import mock
9 import os
10 import StringIO
11 import unittest
12
13 import arvados
14 import arvados_cwl
15 from .mock_discovery import get_rootDesc
16
17 class TestMakeOutput(unittest.TestCase):
18     def setUp(self):
19         self.api = mock.MagicMock()
20         self.api._rootDesc = get_rootDesc()
21
22     @mock.patch("arvados.collection.Collection")
23     @mock.patch("arvados.collection.CollectionReader")
24     def test_make_output_collection(self, reader, col):
25         keep_client = mock.MagicMock()
26         runner = arvados_cwl.ArvCwlRunner(self.api, keep_client=keep_client)
27         runner.project_uuid = 'zzzzz-j7d0g-zzzzzzzzzzzzzzz'
28
29         final = mock.MagicMock()
30         col.return_value = final
31         readermock = mock.MagicMock()
32         reader.return_value = readermock
33
34         final_uuid = final.manifest_locator()
35         num_retries = runner.num_retries
36
37         cwlout = StringIO.StringIO()
38         openmock = mock.MagicMock()
39         final.open.return_value = openmock
40         openmock.__enter__.return_value = cwlout
41
42         _, runner.final_output_collection = runner.make_output_collection("Test output", ["foo"], "tag0,tag1,tag2", {
43             "foo": {
44                 "class": "File",
45                 "location": "keep:99999999999999999999999999999991+99/foo.txt",
46                 "size": 3,
47                 "basename": "foo.txt"
48             },
49             "bar": {
50                 "class": "File",
51                 "location": "keep:99999999999999999999999999999992+99/bar.txt",
52                 "basename": "baz.txt",
53                 "size": 4
54             }
55         })
56
57         final.copy.assert_has_calls([mock.call('bar.txt', 'baz.txt', overwrite=False, source_collection=readermock)])
58         final.copy.assert_has_calls([mock.call('foo.txt', 'foo.txt', overwrite=False, source_collection=readermock)])
59         final.save_new.assert_has_calls([mock.call(ensure_unique_name=True, name='Test output', owner_uuid='zzzzz-j7d0g-zzzzzzzzzzzzzzz', storage_classes=['foo'])])
60         self.assertEqual("""{
61     "bar": {
62         "basename": "baz.txt",
63         "class": "File",
64         "location": "baz.txt",
65         "size": 4
66     },
67     "foo": {
68         "basename": "foo.txt",
69         "class": "File",
70         "location": "foo.txt",
71         "size": 3
72     }
73 }""", cwlout.getvalue())
74
75         self.assertIs(final, runner.final_output_collection)
76         self.assertIs(final_uuid, runner.final_output_collection.manifest_locator())
77         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)])
78         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)])
79         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)])