1111used in simcore_sdk since legacy services are planned to be deprecated.
1212"""
1313
14+ import logging
1415from pathlib import Path
16+ from threading import Thread
17+ from typing import AsyncIterator
1518
1619import aiohttp
20+ import httpx
1721import pytest
22+ import uvicorn
1823from faker import Faker
19- from fastapi import FastAPI
20- from httpx import AsyncClient
2124from models_library .projects_nodes_io import LocationID , SimcoreS3FileID
2225from models_library .users import UserID
26+ from pytest_simcore .helpers .logging_tools import log_context
27+ from servicelib .utils import unused_port
28+ from simcore_service_storage ._meta import API_VTAG
29+ from simcore_service_storage .core .application import create_app
30+ from simcore_service_storage .core .settings import ApplicationSettings
2331from simcore_service_storage .simcore_s3_dsm import SimcoreS3DataManager
2432from simcore_service_storage_sdk import ApiClient , Configuration , UsersApi
33+ from tenacity import (
34+ before_sleep_log ,
35+ retry ,
36+ retry_if_exception_type ,
37+ stop_after_delay ,
38+ wait_fixed ,
39+ )
40+ from yarl import URL
2541
2642pytest_simcore_core_services_selection = ["postgres" ]
2743pytest_simcore_ops_services_selection = [
2844 "adminer" ,
2945]
3046
47+ _logger = logging .getLogger (__name__ )
48+
49+
50+ @retry (
51+ wait = wait_fixed (1 ),
52+ stop = stop_after_delay (10 ),
53+ retry = retry_if_exception_type (),
54+ reraise = True ,
55+ before_sleep = before_sleep_log (_logger , logging .WARNING ),
56+ )
57+ async def _wait_for_server_ready (server : URL ) -> None :
58+ async with httpx .AsyncClient (follow_redirects = True ) as client :
59+ response = await client .get (f"{ server } " )
60+ response .raise_for_status ()
61+
62+
63+ @pytest .fixture
64+ async def real_storage_server (app_settings : ApplicationSettings ) -> AsyncIterator [URL ]:
65+ settings = ApplicationSettings .create_from_envs ()
66+ app = create_app (settings )
67+ storage_port = unused_port ()
68+ with log_context (
69+ logging .INFO ,
70+ msg = f"with fake storage server on 127.0.0.1:{ storage_port } /{ API_VTAG } " ,
71+ ) as ctx :
72+ config = uvicorn .Config (
73+ app ,
74+ host = "127.0.0.1" ,
75+ port = storage_port ,
76+ log_level = "error" ,
77+ )
78+ server = uvicorn .Server (config )
79+
80+ thread = Thread (target = server .run )
81+ thread .daemon = True
82+ thread .start ()
83+
84+ ctx .logger .info (
85+ "health at : %s" ,
86+ f"http://127.0.0.1:{ storage_port } /{ API_VTAG } " ,
87+ )
88+ server_url = URL (f"http://127.0.0.1:{ storage_port } " )
89+
90+ await _wait_for_server_ready (server_url / API_VTAG )
91+
92+ yield server_url
93+
94+ server .should_exit = True
95+ thread .join (timeout = 10 )
96+
3197
3298@pytest .fixture
3399def str_user_id (user_id : UserID ) -> str :
@@ -55,10 +121,8 @@ def location_name() -> str:
55121 return SimcoreS3DataManager .get_location_name ()
56122
57123
58- @pytest .mark .skip (reason = "legacy service" )
59- async def test_storage_client_used_in_simcore_sdk_0_3_2 ( # noqa: PLR0915
60- initialized_app : FastAPI ,
61- client : AsyncClient ,
124+ async def test_storage_client_used_in_simcore_sdk_0_3_2 (
125+ real_storage_server : URL ,
62126 str_user_id : str ,
63127 file_id : str ,
64128 location_id : int ,
@@ -79,7 +143,7 @@ async def test_storage_client_used_in_simcore_sdk_0_3_2( # noqa: PLR0915
79143
80144 # --------
81145 cfg = Configuration ()
82- cfg .host = f"http:// { client . base_url . host } : { client . base_url . port or '80' } /v0 "
146+ cfg .host = f"{ real_storage_server / API_VTAG } "
83147 cfg .debug = True
84148
85149 # assert cfg.host == f"{client.make_url('/v0')}"
0 commit comments