|
| 1 | +# pylint: disable=redefined-outer-name |
| 2 | +# pylint: disable=unused-argument |
| 3 | +# pylint: disable=unused-variable |
| 4 | + |
| 5 | +""" |
| 6 | +Tests compatibility of old services/storage/client-sdk/python client ('simcore_service_storage_sdk') |
| 7 | +against current storage OAS |
| 8 | +
|
| 9 | +
|
| 10 | +NOTE: this test coverage is intentionally limited to the functions of 'simcore_service_storage_sdk' |
| 11 | +used in simcore_sdk since legacy services are planned to be deprecated. |
| 12 | +""" |
| 13 | + |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +import aiohttp |
| 17 | +import pytest |
| 18 | +from aiohttp.test_utils import TestClient |
| 19 | +from faker import Faker |
| 20 | +from models_library.projects_nodes_io import LocationID, SimcoreS3FileID |
| 21 | +from models_library.users import UserID |
| 22 | +from simcore_service_storage.simcore_s3_dsm import SimcoreS3DataManager |
| 23 | +from simcore_service_storage_sdk import ApiClient, Configuration, UsersApi |
| 24 | + |
| 25 | +pytest_simcore_core_services_selection = [ |
| 26 | + "postgres", |
| 27 | +] |
| 28 | +pytest_simcore_ops_services_selection = [ |
| 29 | + "adminer", |
| 30 | +] |
| 31 | + |
| 32 | + |
| 33 | +# |
| 34 | +# FIXTURES: produces values to be used as arguments in the simcore_service_storage_sdk API |
| 35 | +# |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture |
| 39 | +def user_id(user_id: UserID) -> str: |
| 40 | + """overrides tests/fixtures/data_models.py::user_id |
| 41 | + and adapts to simcore_service_storage_sdk API |
| 42 | + """ |
| 43 | + return str(user_id) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture |
| 47 | +def file_id(simcore_file_id: SimcoreS3FileID) -> str: |
| 48 | + """overrides tests/conftest.py::simcore_file_id |
| 49 | + and adapts to simcore_service_storage_sdk API |
| 50 | + """ |
| 51 | + return str(simcore_file_id) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def location_id() -> LocationID: |
| 56 | + return SimcoreS3DataManager.get_location_id() |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture |
| 60 | +def location_name() -> str: |
| 61 | + return SimcoreS3DataManager.get_location_name() |
| 62 | + |
| 63 | + |
| 64 | +async def test_storage_client_used_in_simcore_sdk_0_3_2( |
| 65 | + client: TestClient, |
| 66 | + file_id: str, |
| 67 | + user_id: str, |
| 68 | + location_id: int, |
| 69 | + location_name: str, |
| 70 | + tmp_path: Path, |
| 71 | + faker: Faker, |
| 72 | +): |
| 73 | + """ |
| 74 | + This test reproduces the failure described in https://github.com/ITISFoundation/osparc-simcore/pull/3198 |
| 75 | + where a legacy service could not download data from s3. |
| 76 | +
|
| 77 | + Here we test the calls from 'simcore_service_storage_sdk' used in simcore_sdk.node_ports.filemanage (v0.3.2) |
| 78 | + SEE https://github.com/ITISFoundation/osparc-simcore/blob/cfdf4f86d844ebb362f4f39e9c6571d561b72897/packages/simcore-sdk/src/simcore_sdk/node_ports/filemanager.py |
| 79 | +
|
| 80 | + NOTICE that 'simcore_service_storage_sdk' was automatically built using OAS v0.1.0 despite the fact that at that time |
| 81 | + the OAS had already change!!! |
| 82 | + """ |
| 83 | + |
| 84 | + assert client.app |
| 85 | + assert client.server |
| 86 | + # -------- |
| 87 | + cfg = Configuration() |
| 88 | + cfg.host = f"http://{client.host}:{client.port}/v0" |
| 89 | + cfg.debug = True |
| 90 | + |
| 91 | + assert cfg.host == f'{client.make_url("/v0")}' |
| 92 | + print(f"{cfg=}") |
| 93 | + print(f"{cfg.to_debug_report()=}") |
| 94 | + |
| 95 | + # -------- |
| 96 | + api_client = ApiClient(cfg) |
| 97 | + print(f"{api_client=}") |
| 98 | + print(f"{api_client.default_headers=}") |
| 99 | + |
| 100 | + # -------- |
| 101 | + try: |
| 102 | + api = UsersApi(api_client) |
| 103 | + print(f"{api=}") |
| 104 | + |
| 105 | + ( |
| 106 | + response_payload, |
| 107 | + status_code, |
| 108 | + response_headers, |
| 109 | + ) = await api.get_storage_locations_with_http_info(user_id) |
| 110 | + print(f"{response_payload=}") |
| 111 | + print(f"{status_code=}") |
| 112 | + print(f"{response_headers=}") |
| 113 | + |
| 114 | + assert status_code == 200 |
| 115 | + |
| 116 | + # _get_upload_link |
| 117 | + # https://github.com/ITISFoundation/osparc-simcore/blob/cfdf4f86d844ebb362f4f39e9c6571d561b72897/packages/simcore-sdk/src/simcore_sdk/node_ports/filemanager.py#L132 |
| 118 | + resp_model = await api.upload_file( |
| 119 | + location_id=location_id, |
| 120 | + user_id=user_id, |
| 121 | + file_id=file_id, |
| 122 | + _request_timeout=1000, |
| 123 | + ) |
| 124 | + assert resp_model.error is None |
| 125 | + assert resp_model.data.link is not None |
| 126 | + |
| 127 | + # dumps file & upload |
| 128 | + file = tmp_path / Path(file_id).name |
| 129 | + uploaded_content = faker.text() |
| 130 | + file.write_text(uploaded_content) |
| 131 | + |
| 132 | + async with aiohttp.ClientSession() as session: |
| 133 | + with file.open("rb") as fh: |
| 134 | + async with session.put(resp_model.data.link, data=fh) as r: |
| 135 | + assert r.status == 200, await r.text() |
| 136 | + print(await r.text()) |
| 137 | + |
| 138 | + # entry_exists |
| 139 | + # https://github.com/ITISFoundation/osparc-simcore/blob/cfdf4f86d844ebb362f4f39e9c6571d561b72897/packages/simcore-sdk/src/simcore_sdk/node_ports/filemanager.py#L322 |
| 140 | + # |
| 141 | + # NOTE: jupyter-smash 2.5.9 was failing to download because |
| 142 | + # this call was not returning any 'object_name' |
| 143 | + # A bug in the response of this call was preventing downloading data |
| 144 | + # with the new storage API |
| 145 | + # |
| 146 | + resp_model = await api.get_file_metadata(file_id, location_id, user_id) |
| 147 | + print(type(resp_model), ":\n", resp_model) |
| 148 | + assert resp_model.data.object_name is not None |
| 149 | + assert resp_model.error is None |
| 150 | + |
| 151 | + # _get_location_id_from_location_name |
| 152 | + # https://github.com/ITISFoundation/osparc-simcore/blob/cfdf4f86d844ebb362f4f39e9c6571d561b72897/packages/simcore-sdk/src/simcore_sdk/node_ports/filemanager.py#L89 |
| 153 | + resp_model = await api.get_storage_locations(user_id=user_id) |
| 154 | + print(f"{resp_model=}") |
| 155 | + for location in resp_model.data: |
| 156 | + assert location["name"] == location_name |
| 157 | + assert location["id"] == location_id |
| 158 | + |
| 159 | + # _get_download_link |
| 160 | + # https://github.com/ITISFoundation/osparc-simcore/blob/cfdf4f86d844ebb362f4f39e9c6571d561b72897/packages/simcore-sdk/src/simcore_sdk/node_ports/filemanager.py#L123 |
| 161 | + resp_model = await api.download_file( |
| 162 | + location_id=location_id, |
| 163 | + user_id=user_id, |
| 164 | + file_id=file_id, |
| 165 | + _request_timeout=1000, |
| 166 | + ) |
| 167 | + print(f"{resp_model=}") |
| 168 | + assert resp_model.error is None |
| 169 | + |
| 170 | + async with aiohttp.ClientSession() as session: |
| 171 | + async with session.get(resp_model.data.link) as r: |
| 172 | + print(r.status) |
| 173 | + downloaded_content = await r.text() |
| 174 | + assert r.status == 200, downloaded_content |
| 175 | + assert uploaded_content == downloaded_content |
| 176 | + |
| 177 | + finally: |
| 178 | + del api_client |
0 commit comments