1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
17 import arvados.collection
19 import arvados_cwl.runner
22 from .matcher import JsonDiffMatcher, StripYAMLComments
23 from .mock_discovery import get_rootDesc
25 import arvados_cwl.http
27 import ruamel.yaml as yaml
30 class TestHttpToKeep(unittest.TestCase):
32 @mock.patch("requests.get")
33 @mock.patch("arvados.collection.Collection")
34 def test_http_get(self, collectionmock, getmock):
35 api = mock.MagicMock()
37 api.collections().list().execute.return_value = {
42 cm.manifest_locator.return_value = "zzzzz-4zz18-zzzzzzzzzzzzzz3"
43 cm.portable_data_hash.return_value = "99999999999999999999999999999998+99"
44 collectionmock.return_value = cm
46 req = mock.MagicMock()
49 req.iter_content.return_value = ["abc"]
50 getmock.return_value = req
52 utcnow = mock.MagicMock()
53 utcnow.return_value = datetime.datetime(2018, 5, 15)
55 r = arvados_cwl.http.http_to_keep(api, None, "http://example.com/file1.txt", utcnow=utcnow)
56 self.assertEqual(r, "keep:99999999999999999999999999999998+99/file1.txt")
58 getmock.assert_called_with("http://example.com/file1.txt", stream=True, allow_redirects=True)
60 cm.open.assert_called_with("file1.txt", "w")
61 cm.save_new.assert_called_with(name="Downloaded from http://example.com/file1.txt",
62 owner_uuid=None, ensure_unique_name=True)
64 api.collections().update.assert_has_calls([
65 mock.call(uuid=cm.manifest_locator(),
66 body={"collection":{"properties": {'http://example.com/file1.txt': {'Date': 'Tue, 15 May 2018 00:00:00 GMT'}}}})
70 @mock.patch("requests.get")
71 @mock.patch("arvados.collection.CollectionReader")
72 def test_http_expires(self, collectionmock, getmock):
73 api = mock.MagicMock()
75 api.collections().list().execute.return_value = {
77 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz3",
78 "portable_data_hash": "99999999999999999999999999999998+99",
80 'http://example.com/file1.txt': {
81 'Date': 'Tue, 15 May 2018 00:00:00 GMT',
82 'Expires': 'Tue, 17 May 2018 00:00:00 GMT'
89 cm.manifest_locator.return_value = "zzzzz-4zz18-zzzzzzzzzzzzzz3"
90 cm.portable_data_hash.return_value = "99999999999999999999999999999998+99"
91 cm.keys.return_value = ["file1.txt"]
92 collectionmock.return_value = cm
94 req = mock.MagicMock()
97 req.iter_content.return_value = ["abc"]
98 getmock.return_value = req
100 utcnow = mock.MagicMock()
101 utcnow.return_value = datetime.datetime(2018, 5, 16)
103 r = arvados_cwl.http.http_to_keep(api, None, "http://example.com/file1.txt", utcnow=utcnow)
104 self.assertEqual(r, "keep:99999999999999999999999999999998+99/file1.txt")
106 getmock.assert_not_called()
109 @mock.patch("requests.get")
110 @mock.patch("arvados.collection.CollectionReader")
111 def test_http_cache_control(self, collectionmock, getmock):
112 api = mock.MagicMock()
114 api.collections().list().execute.return_value = {
116 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz3",
117 "portable_data_hash": "99999999999999999999999999999998+99",
119 'http://example.com/file1.txt': {
120 'Date': 'Tue, 15 May 2018 00:00:00 GMT',
121 'Cache-Control': 'max-age=172800'
127 cm = mock.MagicMock()
128 cm.manifest_locator.return_value = "zzzzz-4zz18-zzzzzzzzzzzzzz3"
129 cm.portable_data_hash.return_value = "99999999999999999999999999999998+99"
130 cm.keys.return_value = ["file1.txt"]
131 collectionmock.return_value = cm
133 req = mock.MagicMock()
134 req.status_code = 200
136 req.iter_content.return_value = ["abc"]
137 getmock.return_value = req
139 utcnow = mock.MagicMock()
140 utcnow.return_value = datetime.datetime(2018, 5, 16)
142 r = arvados_cwl.http.http_to_keep(api, None, "http://example.com/file1.txt", utcnow=utcnow)
143 self.assertEqual(r, "keep:99999999999999999999999999999998+99/file1.txt")
145 getmock.assert_not_called()
148 @mock.patch("requests.get")
149 @mock.patch("requests.head")
150 @mock.patch("arvados.collection.Collection")
151 def test_http_expired(self, collectionmock, headmock, getmock):
152 api = mock.MagicMock()
154 api.collections().list().execute.return_value = {
156 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz3",
157 "portable_data_hash": "99999999999999999999999999999998+99",
159 'http://example.com/file1.txt': {
160 'Date': 'Tue, 15 May 2018 00:00:00 GMT',
161 'Expires': 'Tue, 16 May 2018 00:00:00 GMT'
167 cm = mock.MagicMock()
168 cm.manifest_locator.return_value = "zzzzz-4zz18-zzzzzzzzzzzzzz4"
169 cm.portable_data_hash.return_value = "99999999999999999999999999999997+99"
170 cm.keys.return_value = ["file1.txt"]
171 collectionmock.return_value = cm
173 req = mock.MagicMock()
174 req.status_code = 200
175 req.headers = {'Date': 'Tue, 17 May 2018 00:00:00 GMT'}
176 req.iter_content.return_value = ["def"]
177 getmock.return_value = req
178 headmock.return_value = req
180 utcnow = mock.MagicMock()
181 utcnow.return_value = datetime.datetime(2018, 5, 17)
183 r = arvados_cwl.http.http_to_keep(api, None, "http://example.com/file1.txt", utcnow=utcnow)
184 self.assertEqual(r, "keep:99999999999999999999999999999997+99/file1.txt")
186 getmock.assert_called_with("http://example.com/file1.txt", stream=True, allow_redirects=True)
188 cm.open.assert_called_with("file1.txt", "w")
189 cm.save_new.assert_called_with(name="Downloaded from http://example.com/file1.txt",
190 owner_uuid=None, ensure_unique_name=True)
192 api.collections().update.assert_has_calls([
193 mock.call(uuid=cm.manifest_locator(),
194 body={"collection":{"properties": {'http://example.com/file1.txt': {'Date': 'Tue, 17 May 2018 00:00:00 GMT'}}}})
198 @mock.patch("requests.get")
199 @mock.patch("requests.head")
200 @mock.patch("arvados.collection.CollectionReader")
201 def test_http_etag(self, collectionmock, headmock, getmock):
202 api = mock.MagicMock()
204 api.collections().list().execute.return_value = {
206 "uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz3",
207 "portable_data_hash": "99999999999999999999999999999998+99",
209 'http://example.com/file1.txt': {
210 'Date': 'Tue, 15 May 2018 00:00:00 GMT',
211 'Expires': 'Tue, 16 May 2018 00:00:00 GMT',
218 cm = mock.MagicMock()
219 cm.manifest_locator.return_value = "zzzzz-4zz18-zzzzzzzzzzzzzz3"
220 cm.portable_data_hash.return_value = "99999999999999999999999999999998+99"
221 cm.keys.return_value = ["file1.txt"]
222 collectionmock.return_value = cm
224 req = mock.MagicMock()
225 req.status_code = 200
227 'Date': 'Tue, 17 May 2018 00:00:00 GMT',
228 'Expires': 'Tue, 19 May 2018 00:00:00 GMT',
231 headmock.return_value = req
233 utcnow = mock.MagicMock()
234 utcnow.return_value = datetime.datetime(2018, 5, 17)
236 r = arvados_cwl.http.http_to_keep(api, None, "http://example.com/file1.txt", utcnow=utcnow)
237 self.assertEqual(r, "keep:99999999999999999999999999999998+99/file1.txt")
239 getmock.assert_not_called()
240 cm.open.assert_not_called()
242 api.collections().update.assert_has_calls([
243 mock.call(uuid=cm.manifest_locator(),
244 body={"collection":{"properties": {'http://example.com/file1.txt': {
245 'Date': 'Tue, 17 May 2018 00:00:00 GMT',
246 'Expires': 'Tue, 19 May 2018 00:00:00 GMT',
251 @mock.patch("requests.get")
252 @mock.patch("arvados.collection.Collection")
253 def test_http_content_disp(self, collectionmock, getmock):
254 api = mock.MagicMock()
256 api.collections().list().execute.return_value = {
260 cm = mock.MagicMock()
261 cm.manifest_locator.return_value = "zzzzz-4zz18-zzzzzzzzzzzzzz3"
262 cm.portable_data_hash.return_value = "99999999999999999999999999999998+99"
263 collectionmock.return_value = cm
265 req = mock.MagicMock()
266 req.status_code = 200
267 req.headers = {"Content-Disposition": "attachment; filename=file1.txt"}
268 req.iter_content.return_value = ["abc"]
269 getmock.return_value = req
271 utcnow = mock.MagicMock()
272 utcnow.return_value = datetime.datetime(2018, 5, 15)
274 r = arvados_cwl.http.http_to_keep(api, None, "http://example.com/download?fn=/file1.txt", utcnow=utcnow)
275 self.assertEqual(r, "keep:99999999999999999999999999999998+99/file1.txt")
277 getmock.assert_called_with("http://example.com/download?fn=/file1.txt", stream=True, allow_redirects=True)
279 cm.open.assert_called_with("file1.txt", "w")
280 cm.save_new.assert_called_with(name="Downloaded from http://example.com/download?fn=/file1.txt",
281 owner_uuid=None, ensure_unique_name=True)
283 api.collections().update.assert_has_calls([
284 mock.call(uuid=cm.manifest_locator(),
285 body={"collection":{"properties": {"http://example.com/download?fn=/file1.txt": {'Date': 'Tue, 15 May 2018 00:00:00 GMT'}}}})