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()
20 import arvados.collection
22 import arvados_cwl.runner
25 from .matcher import JsonDiffMatcher, StripYAMLComments
26 from .mock_discovery import get_rootDesc
28 import arvados_cwl.http
30 import ruamel.yaml as yaml
33 class TestHttpToKeep(unittest.TestCase):
35 @mock.patch("requests.get")
36 @mock.patch("arvados.collection.Collection")
37 def test_http_get(self, collectionmock, getmock):
38 api = mock.MagicMock()
40 api.collections().list().execute.return_value = {
45 cm.manifest_locator.return_value = "zzzzz-4zz18-zzzzzzzzzzzzzz3"
46 cm.portable_data_hash.return_value = "99999999999999999999999999999998+99"
47 collectionmock.return_value = cm
49 req = mock.MagicMock()
52 req.iter_content.return_value = ["abc"]
53 getmock.return_value = req
55 utcnow = mock.MagicMock()
56 utcnow.return_value = datetime.datetime(2018, 5, 15)
58 r = arvados_cwl.http.http_to_keep(api, None, "http://example.com/file1.txt", utcnow=utcnow)
59 self.assertEqual(r, "keep:99999999999999999999999999999998+99/file1.txt")
61 getmock.assert_called_with("http://example.com/file1.txt", stream=True, allow_redirects=True)
63 cm.open.assert_called_with("file1.txt", "wb")
64 cm.save_new.assert_called_with(name="Downloaded from http%3A%2F%2Fexample.com%2Ffile1.txt",
65 owner_uuid=None, ensure_unique_name=True)
67 api.collections().update.assert_has_calls([
68 mock.call(uuid=cm.manifest_locator(),
69 body={"collection":{"properties": {'http://example.com/file1.txt': {'Date': 'Tue, 15 May 2018 00:00:00 GMT'}}}})
73 @mock.patch("requests.get")
74 @mock.patch("arvados.collection.CollectionReader")
75 def test_http_expires(self, collectionmock, getmock):
76 api = mock.MagicMock()
78 api.collections().list().execute.return_value = {
80 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz3",
81 "portable_data_hash": "99999999999999999999999999999998+99",
83 'http://example.com/file1.txt': {
84 'Date': 'Tue, 15 May 2018 00:00:00 GMT',
85 'Expires': 'Tue, 17 May 2018 00:00:00 GMT'
92 cm.manifest_locator.return_value = "zzzzz-4zz18-zzzzzzzzzzzzzz3"
93 cm.portable_data_hash.return_value = "99999999999999999999999999999998+99"
94 cm.keys.return_value = ["file1.txt"]
95 collectionmock.return_value = cm
97 req = mock.MagicMock()
100 req.iter_content.return_value = ["abc"]
101 getmock.return_value = req
103 utcnow = mock.MagicMock()
104 utcnow.return_value = datetime.datetime(2018, 5, 16)
106 r = arvados_cwl.http.http_to_keep(api, None, "http://example.com/file1.txt", utcnow=utcnow)
107 self.assertEqual(r, "keep:99999999999999999999999999999998+99/file1.txt")
109 getmock.assert_not_called()
112 @mock.patch("requests.get")
113 @mock.patch("arvados.collection.CollectionReader")
114 def test_http_cache_control(self, collectionmock, getmock):
115 api = mock.MagicMock()
117 api.collections().list().execute.return_value = {
119 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz3",
120 "portable_data_hash": "99999999999999999999999999999998+99",
122 'http://example.com/file1.txt': {
123 'Date': 'Tue, 15 May 2018 00:00:00 GMT',
124 'Cache-Control': 'max-age=172800'
130 cm = mock.MagicMock()
131 cm.manifest_locator.return_value = "zzzzz-4zz18-zzzzzzzzzzzzzz3"
132 cm.portable_data_hash.return_value = "99999999999999999999999999999998+99"
133 cm.keys.return_value = ["file1.txt"]
134 collectionmock.return_value = cm
136 req = mock.MagicMock()
137 req.status_code = 200
139 req.iter_content.return_value = ["abc"]
140 getmock.return_value = req
142 utcnow = mock.MagicMock()
143 utcnow.return_value = datetime.datetime(2018, 5, 16)
145 r = arvados_cwl.http.http_to_keep(api, None, "http://example.com/file1.txt", utcnow=utcnow)
146 self.assertEqual(r, "keep:99999999999999999999999999999998+99/file1.txt")
148 getmock.assert_not_called()
151 @mock.patch("requests.get")
152 @mock.patch("requests.head")
153 @mock.patch("arvados.collection.Collection")
154 def test_http_expired(self, collectionmock, headmock, getmock):
155 api = mock.MagicMock()
157 api.collections().list().execute.return_value = {
159 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz3",
160 "portable_data_hash": "99999999999999999999999999999998+99",
162 'http://example.com/file1.txt': {
163 'Date': 'Tue, 15 May 2018 00:00:00 GMT',
164 'Expires': 'Tue, 16 May 2018 00:00:00 GMT'
170 cm = mock.MagicMock()
171 cm.manifest_locator.return_value = "zzzzz-4zz18-zzzzzzzzzzzzzz4"
172 cm.portable_data_hash.return_value = "99999999999999999999999999999997+99"
173 cm.keys.return_value = ["file1.txt"]
174 collectionmock.return_value = cm
176 req = mock.MagicMock()
177 req.status_code = 200
178 req.headers = {'Date': 'Tue, 17 May 2018 00:00:00 GMT'}
179 req.iter_content.return_value = ["def"]
180 getmock.return_value = req
181 headmock.return_value = req
183 utcnow = mock.MagicMock()
184 utcnow.return_value = datetime.datetime(2018, 5, 17)
186 r = arvados_cwl.http.http_to_keep(api, None, "http://example.com/file1.txt", utcnow=utcnow)
187 self.assertEqual(r, "keep:99999999999999999999999999999997+99/file1.txt")
189 getmock.assert_called_with("http://example.com/file1.txt", stream=True, allow_redirects=True)
191 cm.open.assert_called_with("file1.txt", "wb")
192 cm.save_new.assert_called_with(name="Downloaded from http%3A%2F%2Fexample.com%2Ffile1.txt",
193 owner_uuid=None, ensure_unique_name=True)
195 api.collections().update.assert_has_calls([
196 mock.call(uuid=cm.manifest_locator(),
197 body={"collection":{"properties": {'http://example.com/file1.txt': {'Date': 'Tue, 17 May 2018 00:00:00 GMT'}}}})
201 @mock.patch("requests.get")
202 @mock.patch("requests.head")
203 @mock.patch("arvados.collection.CollectionReader")
204 def test_http_etag(self, collectionmock, headmock, getmock):
205 api = mock.MagicMock()
207 api.collections().list().execute.return_value = {
209 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz3",
210 "portable_data_hash": "99999999999999999999999999999998+99",
212 'http://example.com/file1.txt': {
213 'Date': 'Tue, 15 May 2018 00:00:00 GMT',
214 'Expires': 'Tue, 16 May 2018 00:00:00 GMT',
221 cm = mock.MagicMock()
222 cm.manifest_locator.return_value = "zzzzz-4zz18-zzzzzzzzzzzzzz3"
223 cm.portable_data_hash.return_value = "99999999999999999999999999999998+99"
224 cm.keys.return_value = ["file1.txt"]
225 collectionmock.return_value = cm
227 req = mock.MagicMock()
228 req.status_code = 200
230 'Date': 'Tue, 17 May 2018 00:00:00 GMT',
231 'Expires': 'Tue, 19 May 2018 00:00:00 GMT',
234 headmock.return_value = req
236 utcnow = mock.MagicMock()
237 utcnow.return_value = datetime.datetime(2018, 5, 17)
239 r = arvados_cwl.http.http_to_keep(api, None, "http://example.com/file1.txt", utcnow=utcnow)
240 self.assertEqual(r, "keep:99999999999999999999999999999998+99/file1.txt")
242 getmock.assert_not_called()
243 cm.open.assert_not_called()
245 api.collections().update.assert_has_calls([
246 mock.call(uuid=cm.manifest_locator(),
247 body={"collection":{"properties": {'http://example.com/file1.txt': {
248 'Date': 'Tue, 17 May 2018 00:00:00 GMT',
249 'Expires': 'Tue, 19 May 2018 00:00:00 GMT',
254 @mock.patch("requests.get")
255 @mock.patch("arvados.collection.Collection")
256 def test_http_content_disp(self, collectionmock, getmock):
257 api = mock.MagicMock()
259 api.collections().list().execute.return_value = {
263 cm = mock.MagicMock()
264 cm.manifest_locator.return_value = "zzzzz-4zz18-zzzzzzzzzzzzzz3"
265 cm.portable_data_hash.return_value = "99999999999999999999999999999998+99"
266 collectionmock.return_value = cm
268 req = mock.MagicMock()
269 req.status_code = 200
270 req.headers = {"Content-Disposition": "attachment; filename=file1.txt"}
271 req.iter_content.return_value = ["abc"]
272 getmock.return_value = req
274 utcnow = mock.MagicMock()
275 utcnow.return_value = datetime.datetime(2018, 5, 15)
277 r = arvados_cwl.http.http_to_keep(api, None, "http://example.com/download?fn=/file1.txt", utcnow=utcnow)
278 self.assertEqual(r, "keep:99999999999999999999999999999998+99/file1.txt")
280 getmock.assert_called_with("http://example.com/download?fn=/file1.txt", stream=True, allow_redirects=True)
282 cm.open.assert_called_with("file1.txt", "wb")
283 cm.save_new.assert_called_with(name="Downloaded from http%3A%2F%2Fexample.com%2Fdownload%3Ffn%3D%2Ffile1.txt",
284 owner_uuid=None, ensure_unique_name=True)
286 api.collections().update.assert_has_calls([
287 mock.call(uuid=cm.manifest_locator(),
288 body={"collection":{"properties": {"http://example.com/download?fn=/file1.txt": {'Date': 'Tue, 15 May 2018 00:00:00 GMT'}}}})