11
11
used in simcore_sdk since legacy services are planned to be deprecated.
12
12
"""
13
13
14
+ import logging
14
15
from pathlib import Path
16
+ from threading import Thread
17
+ from typing import AsyncIterator
15
18
16
19
import aiohttp
20
+ import httpx
17
21
import pytest
22
+ import uvicorn
18
23
from faker import Faker
19
- from fastapi import FastAPI
20
- from httpx import AsyncClient
21
24
from models_library .projects_nodes_io import LocationID , SimcoreS3FileID
22
25
from 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
23
31
from simcore_service_storage .simcore_s3_dsm import SimcoreS3DataManager
24
32
from 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
25
41
26
42
pytest_simcore_core_services_selection = ["postgres" ]
27
43
pytest_simcore_ops_services_selection = [
28
44
"adminer" ,
29
45
]
30
46
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
+
31
97
32
98
@pytest .fixture
33
99
def str_user_id (user_id : UserID ) -> str :
@@ -55,10 +121,8 @@ def location_name() -> str:
55
121
return SimcoreS3DataManager .get_location_name ()
56
122
57
123
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 ,
62
126
str_user_id : str ,
63
127
file_id : str ,
64
128
location_id : int ,
@@ -79,7 +143,7 @@ async def test_storage_client_used_in_simcore_sdk_0_3_2( # noqa: PLR0915
79
143
80
144
# --------
81
145
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 } "
83
147
cfg .debug = True
84
148
85
149
# assert cfg.host == f"{client.make_url('/v0')}"
0 commit comments