Skip to content

Commit daa896c

Browse files
authored
🎨Storage: disallow optional S3/postgres settings (#5701)
1 parent 3348bd5 commit daa896c

File tree

8 files changed

+48
-61
lines changed

8 files changed

+48
-61
lines changed

services/clusters-keeper/tests/unit/conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,10 @@ def clusters_keeper_docker_compose_file(installed_package_dir: Path) -> Path:
255255

256256
@pytest.fixture
257257
def clusters_keeper_docker_compose() -> dict[str, Any]:
258-
data = importlib.resources.read_text(
259-
simcore_service_clusters_keeper.data, "docker-compose.yml"
258+
data = (
259+
importlib.resources.files(simcore_service_clusters_keeper.data)
260+
.joinpath("docker-compose.yml")
261+
.read_text()
260262
)
261263
assert data
262264
return yaml.safe_load(data)

services/storage/.env-devel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ S3_BUCKET_NAME=simcore
2323
# 172.17.0.1 is the docker0 interface, which redirect from inside a container onto the host network interface.
2424
S3_ENDPOINT=http://172.17.0.1:9001
2525
S3_SECRET_KEY=12345678
26+
S3_REGION=us-east-1
2627

2728
BF_API_SECRET=none
2829
BF_API_KEY=none

services/storage/src/simcore_service_storage/application.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,20 @@ def create(settings: Settings) -> web.Application:
5757
skip_routes=None,
5858
)
5959

60-
if settings.STORAGE_POSTGRES:
61-
setup_db(app) # -> postgres service
62-
if settings.STORAGE_S3:
63-
setup_s3(app) # -> minio service
60+
setup_db(app)
61+
setup_s3(app)
6462

6563
setup_long_running_tasks(app)
6664
setup_rest(app)
67-
setup_redis(app)
6865

69-
if settings.STORAGE_POSTGRES and settings.STORAGE_S3:
70-
setup_dsm(app) # core subsystem. Needs s3 and db setups done
71-
if settings.STORAGE_CLEANER_INTERVAL_S:
72-
setup_dsm_cleaner(app)
66+
if settings.STORAGE_REDIS:
67+
setup_redis(app)
7368

74-
app.middlewares.append(dsm_exception_handler)
69+
setup_dsm(app)
70+
if settings.STORAGE_CLEANER_INTERVAL_S:
71+
setup_dsm_cleaner(app)
72+
73+
app.middlewares.append(dsm_exception_handler)
7574

7675
if settings.LOG_LEVEL == "DEBUG":
7776
setup_dev_error_logger(app)

services/storage/src/simcore_service_storage/settings.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from pydantic import Field, PositiveInt, validator
1+
from typing import Any
2+
3+
from pydantic import Field, PositiveInt, root_validator, validator
24
from settings_library.base import BaseCustomSettings
35
from settings_library.basic_types import LogLevel, PortInt
46
from settings_library.postgres import PostgresSettings
@@ -32,11 +34,11 @@ class Settings(BaseCustomSettings, MixinLoggingSettings):
3234
None, description="Pennsieve API secret ONLY for testing purposes"
3335
)
3436

35-
STORAGE_POSTGRES: PostgresSettings | None = Field(auto_default_from_env=True)
37+
STORAGE_POSTGRES: PostgresSettings = Field(auto_default_from_env=True)
3638

3739
STORAGE_REDIS: RedisSettings | None = Field(auto_default_from_env=True)
3840

39-
STORAGE_S3: S3Settings | None = Field(auto_default_from_env=True)
41+
STORAGE_S3: S3Settings = Field(auto_default_from_env=True)
4042

4143
STORAGE_TRACING: TracingSettings | None = Field(auto_default_from_env=True)
4244

@@ -71,3 +73,12 @@ class Settings(BaseCustomSettings, MixinLoggingSettings):
7173
def _validate_loglevel(cls, value) -> str:
7274
log_level: str = cls.validate_log_level(value)
7375
return log_level
76+
77+
@root_validator()
78+
@classmethod
79+
def ensure_settings_consistency(cls, values: dict[str, Any]):
80+
if values.get("STORAGE_CLEANER_INTERVAL_S") and not values.get("STORAGE_REDIS"):
81+
raise ValueError(
82+
"STORAGE_CLEANER_INTERVAL_S cleaner cannot be set without STORAGE_REDIS! Please correct settings."
83+
)
84+
return values

services/storage/tests/conftest.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,17 @@ def project_slug_dir(osparc_simcore_root_dir) -> Path:
118118

119119

120120
@pytest.fixture(scope="session")
121-
def project_env_devel_dict(project_slug_dir: Path) -> dict:
121+
def project_env_devel_dict(project_slug_dir: Path) -> dict[str, str]:
122122
env_devel_file = project_slug_dir / ".env-devel"
123123
assert env_devel_file.exists()
124124
environ = dotenv.dotenv_values(env_devel_file, verbose=True, interpolate=True)
125125
return environ
126126

127127

128128
@pytest.fixture
129-
def project_env_devel_environment(project_env_devel_dict, monkeypatch) -> None:
129+
def project_env_devel_environment(
130+
project_env_devel_dict: dict[str, str], monkeypatch: pytest.MonkeyPatch
131+
) -> None:
130132
for key, value in project_env_devel_dict.items():
131133
monkeypatch.setenv(key, value)
132134

@@ -186,13 +188,13 @@ def mock_config(
186188
postgres_host_config: dict[str, str],
187189
mocked_s3_server_envs,
188190
datcore_adapter_service_mock: aioresponses.aioresponses,
189-
):
191+
) -> None:
190192
# NOTE: this can be overriden in tests that do not need all dependencies up
191193
...
192194

193195

194196
@pytest.fixture
195-
def app_settings(mock_config) -> Settings:
197+
def app_settings(mock_config: None) -> Settings:
196198
test_app_settings = Settings.create_from_envs()
197199
print(f"{test_app_settings.json(indent=2)=}")
198200
return test_app_settings

services/storage/tests/unit/test_s3.py

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,22 @@
33
# pylint: disable=unused-variable
44

55

6-
import pytest
76
from aiohttp.test_utils import TestClient
87
from simcore_service_storage.s3 import get_s3_client
98
from simcore_service_storage.settings import Settings
109

10+
pytest_simcore_core_services_selection = ["postgres"]
1111

12-
@pytest.fixture(params=[True, False])
13-
def enable_s3(request: pytest.FixtureRequest) -> bool:
14-
return request.param # type: ignore
1512

16-
17-
@pytest.fixture
18-
def mock_config(
19-
mocked_s3_server_envs, monkeypatch: pytest.MonkeyPatch, enable_s3: bool
20-
):
21-
# NOTE: override services/storage/tests/conftest.py::mock_config
22-
monkeypatch.setenv("STORAGE_POSTGRES", "null")
23-
if not enable_s3:
24-
# disable S3
25-
monkeypatch.setenv("STORAGE_S3", "null")
26-
27-
28-
async def test_s3_client(enable_s3: bool, app_settings: Settings, client: TestClient):
13+
async def test_s3_client(app_settings: Settings, client: TestClient):
2914
assert client.app
30-
if enable_s3:
31-
assert app_settings.STORAGE_S3
32-
s3_client = get_s3_client(client.app)
33-
assert s3_client
34-
35-
response = await s3_client.client.list_buckets()
36-
assert response
37-
assert "Buckets" in response
38-
assert len(response["Buckets"]) == 1
39-
assert "Name" in response["Buckets"][0]
40-
assert response["Buckets"][0]["Name"] == app_settings.STORAGE_S3.S3_BUCKET_NAME
41-
else:
42-
assert not app_settings.STORAGE_S3
43-
with pytest.raises(KeyError):
44-
get_s3_client(client.app)
15+
assert app_settings.STORAGE_S3
16+
s3_client = get_s3_client(client.app)
17+
assert s3_client
18+
19+
response = await s3_client.client.list_buckets()
20+
assert response
21+
assert "Buckets" in response
22+
assert len(response["Buckets"]) == 1
23+
assert "Name" in response["Buckets"][0]
24+
assert response["Buckets"][0]["Name"] == app_settings.STORAGE_S3.S3_BUCKET_NAME

services/storage/tests/unit/test_s3_client.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from models_library.projects_nodes_io import SimcoreS3FileID
3131
from pydantic import ByteSize, parse_obj_as
3232
from pytest_mock import MockFixture
33-
from pytest_simcore.helpers.utils_envs import EnvVarsDict
3433
from pytest_simcore.helpers.utils_parametrizations import byte_size_ids
3534
from simcore_service_storage.models import MultiPartUploadLinks, S3BucketName
3635
from simcore_service_storage.s3_client import (
@@ -46,13 +45,7 @@
4645

4746
DEFAULT_EXPIRATION_SECS: Final[int] = 10
4847

49-
50-
@pytest.fixture
51-
def mock_config(
52-
mocked_s3_server_envs: EnvVarsDict, monkeypatch: pytest.MonkeyPatch
53-
) -> None:
54-
# NOTE: override services/storage/tests/conftest.py::mock_config
55-
monkeypatch.setenv("STORAGE_POSTGRES", "null")
48+
pytest_simcore_core_services_selection = ["postgres"]
5649

5750

5851
async def test_storage_storage_s3_client_creation(

tests/environment-setup/test_used_python.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ def make_versions_comparable(*versions) -> list[tuple[int]]:
3535
for v in versions:
3636
if isinstance(v, str):
3737
v = to_version(v)
38-
if len(v) < n:
39-
n = len(v)
38+
n = min(n, len(v))
4039
vers.append(v)
4140
return [v[:n] for v in vers]
4241

0 commit comments

Comments
 (0)