-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_storage_client.py
More file actions
379 lines (314 loc) · 17.1 KB
/
Copy pathtest_storage_client.py
File metadata and controls
379 lines (314 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import re
from datetime import timedelta
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch
import pytest
from httpx import AsyncClient
from hypothesis import HealthCheck, given, settings
from obstore.store import LocalStore
from pytest_httpx import HTTPXMock, IteratorStream
from tests.storage_data import ers_granules, landsat_granules, s5p_granules, umbra_granules
from tilebox.storage.aio import (
ASFStorageClient,
CopernicusStorageClient,
UmbraStorageClient,
USGSLandsatStorageClient,
_HttpClient,
list_object_paths,
)
from tilebox.storage.granule import (
ASFStorageGranule,
CopernicusStorageGranule,
UmbraStorageGranule,
USGSLandsatStorageGranule,
)
@pytest.mark.asyncio
async def test_client_login(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response()
client = _HttpClient(auth={"ASF": ("username", "password")})
await client._client("ASF")
assert isinstance(client._clients["ASF"], AsyncClient)
@pytest.mark.asyncio
async def test_client_login_failed(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(401)
client = _HttpClient(auth={"ASF": ("invalid-username", "password")})
with pytest.raises(ValueError, match=re.escape("Invalid username or password.")):
await client._client("ASF")
@pytest.mark.asyncio
async def test_client_missing_credentials() -> None:
client = _HttpClient(auth={})
with pytest.raises(ValueError, match=r"Missing credentials.*"):
await client._client("ASF")
@pytest.mark.asyncio
@given(ers_granules(ensure_quicklook=True))
@settings(max_examples=1, suppress_health_check=[HealthCheck.function_scoped_fixture])
async def test_download_quicklook(httpx_mock: HTTPXMock, tmp_path: Path, granule: ASFStorageGranule) -> None:
assert granule.urls.quicklook is not None # for type checker
httpx_mock.add_response(content=b"login-response")
httpx_mock.add_response(content=b"my-quicklook-image")
client = _HttpClient(auth={"ASF": ("username", "password")})
downloaded = await client.download_quicklook(granule, tmp_path)
expected = tmp_path / granule.urls.quicklook.rsplit("/", 1)[-1]
assert downloaded == expected
assert downloaded.exists()
assert downloaded.read_bytes() == b"my-quicklook-image"
@pytest.mark.asyncio
@given(ers_granules(ensure_quicklook=True))
@settings(max_examples=1, suppress_health_check=[HealthCheck.function_scoped_fixture])
async def test_quicklook(httpx_mock: HTTPXMock, granule: ASFStorageGranule) -> None:
assert granule.urls.quicklook is not None # for type checker
httpx_mock.add_response(content=b"login-response")
httpx_mock.add_response(content=b"my-quicklook-image")
client = _HttpClient(auth={"ASF": ("username", "password")})
with patch("tilebox.storage.aio.Image"), patch("tilebox.storage.aio._display_quicklook") as display_mock:
await client.quicklook(granule)
display_mock.assert_called_once()
assert display_mock.call_args[0][0] == b"my-quicklook-image"
img_name = granule.urls.quicklook.rsplit("/", 1)[-1]
assert display_mock.call_args[0][-1].startswith(f"<code>Image {img_name} © ASF")
@pytest.mark.asyncio
@given(ers_granules())
@settings(max_examples=1, suppress_health_check=[HealthCheck.function_scoped_fixture])
async def test_download(httpx_mock: HTTPXMock, tmp_path: Path, granule: ASFStorageGranule) -> None:
mock_data = ["my-granule", "some-data", "some-more-data"]
granule.md5sum = "1c3cd9bf5dd29c2a79d4783ca2aee55e" # real md5sum of the above data
httpx_mock.add_response(content=b"login-response")
httpx_mock.add_response(stream=IteratorStream([d.encode() for d in mock_data]))
client = _HttpClient(auth={"ASF": ("username", "password")})
downloaded = await client.download(granule, tmp_path, extract=False, show_progress=False)
expected = tmp_path / granule.urls.data.rsplit("/", 1)[-1]
assert downloaded == expected
assert expected.exists()
assert expected.read_bytes() == "".join(mock_data).encode()
@pytest.mark.asyncio
@given(ers_granules())
@settings(max_examples=1, suppress_health_check=[HealthCheck.function_scoped_fixture])
async def test_download_verify_md5(httpx_mock: HTTPXMock, tmp_path: Path, granule: ASFStorageGranule) -> None:
httpx_mock.add_response(content=b"login-response")
httpx_mock.add_response(stream=IteratorStream([b"my-granule"]))
client = _HttpClient(auth={"ASF": ("username", "password")})
with pytest.raises(ValueError, match=r".*md5sum mismatch.*"):
await client.download(granule, tmp_path, extract=False, show_progress=False)
@pytest.mark.asyncio
@given(ers_granules(ensure_quicklook=True))
@settings(max_examples=1, suppress_health_check=[HealthCheck.function_scoped_fixture])
async def test_cached_download_quicklook(httpx_mock: HTTPXMock, tmp_path: Path, granule: ASFStorageGranule) -> None:
assert granule.urls.quicklook is not None # for type checker
httpx_mock.reset() # so we get an accurate request count below
httpx_mock.add_response(content=b"login-response")
httpx_mock.add_response(content=b"my-quicklook-image")
client = ASFStorageClient("username", "password", cache_directory=tmp_path)
downloaded = await client.download_quicklook(granule)
expected = tmp_path / "ASF" / granule.granule_name / granule.urls.quicklook.rsplit("/", 1)[-1]
assert downloaded == expected
assert downloaded.exists()
assert downloaded.read_bytes() == b"my-quicklook-image"
for _ in range(10):
await client.download_quicklook(granule)
assert len(httpx_mock.get_requests(url=granule.urls.quicklook)) == 1
@pytest.mark.asyncio
@given(ers_granules())
@settings(max_examples=1, suppress_health_check=[HealthCheck.function_scoped_fixture])
async def test_cached_download(httpx_mock: HTTPXMock, tmp_path: Path, granule: ASFStorageGranule) -> None:
httpx_mock.reset() # so we get an accurate request count below
mock_data = ["my-granule", "some-data", "some-more-data"]
granule.md5sum = "1c3cd9bf5dd29c2a79d4783ca2aee55e" # real md5sum of the above data
httpx_mock.add_response(content=b"login-response")
httpx_mock.add_response(stream=IteratorStream([d.encode() for d in mock_data]))
client = ASFStorageClient("username", "password", cache_directory=tmp_path)
downloaded = await client.download(granule, extract=False, show_progress=False)
expected = tmp_path / "ASF" / granule.granule_name / granule.urls.data.rsplit("/", 1)[-1]
assert downloaded == expected
assert expected.exists()
assert expected.read_bytes() == "".join(mock_data).encode()
for _ in range(10):
await client.download(granule, extract=False, show_progress=False)
assert len(httpx_mock.get_requests(url=granule.urls.data)) == 1
@pytest.mark.asyncio
async def test_list_object_paths() -> None:
with TemporaryDirectory() as tmp_path:
store_path = Path(tmp_path) / "store"
store_path.mkdir(exist_ok=True, parents=True)
store = LocalStore(store_path)
await store.put_async("prefix/object1", b"content1")
await store.put_async("prefix/object2", b"content2")
await store.put_async("prefix/subdir/object3", b"content3")
objects = await list_object_paths(store, "prefix")
# we always need a forward slash in our paths, even on windows
assert objects == ["object1", "object2", "subdir/object3"]
@pytest.mark.asyncio
@given(umbra_granules())
@settings(max_examples=1, deadline=timedelta(milliseconds=100))
async def test_umbra_storage_client_download(granule: UmbraStorageGranule) -> None:
with TemporaryDirectory() as tmp_path:
store_path = Path(tmp_path) / "store"
store_path.mkdir(exist_ok=True, parents=True)
store = LocalStore(store_path)
await store.put_async(f"sar-data/tasks/{granule.location}/{granule.granule_name}_GEC.tif", b"content1")
await store.put_async(f"sar-data/tasks/{granule.location}/{granule.granule_name}_CPHD.cphd", b"content2")
with patch("tilebox.storage.aio.S3Store") as store_mock:
store_mock.return_value = store
umbra = UmbraStorageClient(Path(tmp_path) / "cache")
folder = await umbra.download(granule, show_progress=False)
assert folder.exists()
assert folder.parent.parent.parent == Path(tmp_path) / "cache" / "Umbra" / "sar-data" / "tasks"
assert (folder / f"{granule.granule_name}_GEC.tif").read_bytes() == b"content1"
assert (folder / f"{granule.granule_name}_CPHD.cphd").read_bytes() == b"content2"
@pytest.mark.asyncio
@given(umbra_granules())
@settings(max_examples=1, deadline=timedelta(milliseconds=100))
async def test_umbra_storage_client_list_objects(granule: UmbraStorageGranule) -> None:
with TemporaryDirectory() as tmp_path:
store_path = Path(tmp_path) / "store"
store_path.mkdir(exist_ok=True, parents=True)
store = LocalStore(store_path)
await store.put_async(f"sar-data/tasks/{granule.location}/{granule.granule_name}_GEC.tif", b"content1")
await store.put_async(f"sar-data/tasks/{granule.location}/{granule.granule_name}_CPHD.cphd", b"content2")
await store.put_async(
f"sar-data/tasks/another_{granule.location}/another_{granule.granule_name}_CPHD.cphd", b"content2"
)
with patch("tilebox.storage.aio.S3Store") as store_mock:
store_mock.return_value = store
umbra = UmbraStorageClient(Path(tmp_path) / "cache")
objects = await umbra.list_objects(granule)
assert sorted(objects) == sorted([f"{granule.granule_name}_GEC.tif", f"{granule.granule_name}_CPHD.cphd"])
@pytest.mark.asyncio
@given(umbra_granules())
@settings(max_examples=1, deadline=timedelta(milliseconds=100))
async def test_umbra_storage_client_download_objects(granule: UmbraStorageGranule) -> None:
with TemporaryDirectory() as tmp_path:
store_path = Path(tmp_path) / "store"
store_path.mkdir(exist_ok=True, parents=True)
store = LocalStore(store_path)
await store.put_async(f"sar-data/tasks/{granule.location}/{granule.granule_name}_GEC.tif", b"content1")
await store.put_async(f"sar-data/tasks/{granule.location}/{granule.granule_name}_CPHD.cphd", b"content2")
with patch("tilebox.storage.aio.S3Store") as store_mock:
store_mock.return_value = store
umbra = UmbraStorageClient(Path(tmp_path) / "cache")
folder = await umbra.download_objects(granule, [f"{granule.granule_name}_GEC.tif"], show_progress=False)
assert (folder / f"{granule.granule_name}_GEC.tif").read_bytes() == b"content1"
assert not (folder / f"{granule.granule_name}_CPHD.cphd").exists()
@pytest.mark.asyncio
@given(s5p_granules())
@settings(max_examples=1, deadline=timedelta(milliseconds=100))
async def test_copernicus_storage_client_download(granule: CopernicusStorageGranule) -> None:
with TemporaryDirectory() as tmp_path:
store_path = Path(tmp_path) / "store"
store_path.mkdir(exist_ok=True, parents=True)
store = LocalStore(store_path)
await store.put_async(f"{granule.location.removeprefix('/eodata/')}/{granule.granule_name}", b"content1")
with patch("tilebox.storage.aio.S3Store") as store_mock:
store_mock.return_value = store
copernicus = CopernicusStorageClient(
access_key="testing",
secret_access_key="testing", # noqa: S106
cache_directory=Path(tmp_path),
)
folder = await copernicus.download(granule, show_progress=False)
assert folder.exists()
assert (folder / granule.granule_name).read_bytes() == b"content1"
@pytest.mark.asyncio
@given(s5p_granules())
@settings(max_examples=1, deadline=timedelta(milliseconds=100))
async def test_copernicus_storage_client_list_objects(granule: CopernicusStorageGranule) -> None:
with TemporaryDirectory() as tmp_path:
store_path = Path(tmp_path) / "store"
store_path.mkdir(exist_ok=True, parents=True)
store = LocalStore(store_path)
await store.put_async(f"{granule.location.removeprefix('/eodata/')}/{granule.granule_name}", b"content1")
await store.put_async(
f"{granule.location.removeprefix('/eodata/')}_other_granule/{granule.granule_name}", b"content2"
)
with patch("tilebox.storage.aio.S3Store") as store_mock:
store_mock.return_value = store
copernicus = CopernicusStorageClient(
access_key="testing",
secret_access_key="testing", # noqa: S106
cache_directory=Path(tmp_path),
)
objects = await copernicus.list_objects(granule)
assert len(objects) == 1
assert objects[0] == granule.granule_name
@pytest.mark.asyncio
@given(s5p_granules())
@settings(max_examples=1, deadline=timedelta(milliseconds=100))
async def test_copernicus_storage_client_download_objects(granule: CopernicusStorageGranule) -> None:
with TemporaryDirectory() as tmp_path:
store_path = Path(tmp_path) / "store"
store_path.mkdir(exist_ok=True, parents=True)
store = LocalStore(store_path)
await store.put_async(f"{granule.location.removeprefix('/eodata/')}/{granule.granule_name}", b"content1")
await store.put_async(
f"{granule.location.removeprefix('/eodata/')}/other_product_{granule.granule_name}", b"content2"
)
with patch("tilebox.storage.aio.S3Store") as store_mock:
store_mock.return_value = store
copernicus = CopernicusStorageClient(
access_key="testing",
secret_access_key="testing", # noqa: S106
cache_directory=Path(tmp_path),
)
folder = await copernicus.download_objects(granule, [granule.granule_name], show_progress=False)
assert folder.exists()
assert (folder / granule.granule_name).read_bytes() == b"content1"
assert not (folder / f"other_product_{granule.granule_name}").exists()
@pytest.mark.asyncio
@given(landsat_granules())
@settings(max_examples=1, deadline=timedelta(milliseconds=100))
async def test_landsat_storage_client_download(granule: USGSLandsatStorageGranule) -> None:
with TemporaryDirectory() as tmp_path:
store_path = Path(tmp_path) / "store"
store_path.mkdir(exist_ok=True, parents=True)
store = LocalStore(store_path)
await store.put_async(
f"{granule.location.removeprefix('s3://usgs-landsat/')}/{granule.granule_name}", b"content1"
)
with patch("tilebox.storage.aio.S3Store") as store_mock, patch("tilebox.storage.aio.Boto3CredentialProvider"):
store_mock.return_value = store
landsat = USGSLandsatStorageClient(cache_directory=Path(tmp_path))
folder = await landsat.download(granule, show_progress=False)
assert folder.exists()
assert (folder / granule.granule_name).read_bytes() == b"content1"
@pytest.mark.asyncio
@given(landsat_granules())
@settings(max_examples=1, deadline=timedelta(milliseconds=100))
async def test_landsat_storage_client_list_objects(granule: USGSLandsatStorageGranule) -> None:
with TemporaryDirectory() as tmp_path:
store_path = Path(tmp_path) / "store"
store_path.mkdir(exist_ok=True, parents=True)
store = LocalStore(store_path)
await store.put_async(
f"{granule.location.removeprefix('s3://usgs-landsat/')}/{granule.granule_name}", b"content1"
)
await store.put_async(
f"{granule.location.removeprefix('s3://usgs-landsat/')}/{granule.granule_name}_thumb_small.jpeg",
b"content2",
)
with patch("tilebox.storage.aio.S3Store") as store_mock, patch("tilebox.storage.aio.Boto3CredentialProvider"):
store_mock.return_value = store
landsat = USGSLandsatStorageClient(cache_directory=Path(tmp_path))
objects = await landsat.list_objects(granule)
assert len(objects) == 2
assert sorted(objects) == sorted([granule.granule_name, f"{granule.granule_name}_thumb_small.jpeg"])
@pytest.mark.asyncio
@given(landsat_granules())
@settings(max_examples=1, deadline=timedelta(milliseconds=100))
async def test_landsat_storage_client_download_objects(granule: USGSLandsatStorageGranule) -> None:
with TemporaryDirectory() as tmp_path:
store_path = Path(tmp_path) / "store"
store_path.mkdir(exist_ok=True, parents=True)
store = LocalStore(store_path)
await store.put_async(
f"{granule.location.removeprefix('s3://usgs-landsat/')}/{granule.granule_name}", b"content1"
)
await store.put_async(
f"{granule.location.removeprefix('s3://usgs-landsat/')}/other_product_{granule.granule_name}", b"content2"
)
with patch("tilebox.storage.aio.S3Store") as store_mock, patch("tilebox.storage.aio.Boto3CredentialProvider"):
store_mock.return_value = store
landsat = USGSLandsatStorageClient(cache_directory=Path(tmp_path))
folder = await landsat.download_objects(granule, [granule.granule_name], show_progress=False)
assert folder.exists()
assert (folder / granule.granule_name).read_bytes() == b"content1"
assert not (folder / f"other_product_{granule.granule_name}").exists()