diff --git a/swh/storage/api/server.py b/swh/storage/api/server.py --- a/swh/storage/api/server.py +++ b/swh/storage/api/server.py @@ -5,7 +5,7 @@ import logging import os -from typing import Any, Dict +from typing import Any, Dict, Optional from swh.core import config from swh.core.api import RPCServerApp @@ -79,7 +79,7 @@ api_cfg = None -def load_and_check_config(config_path: str) -> Dict[str, Any]: +def load_and_check_config(config_path: Optional[str]) -> Dict[str, Any]: """Check the minimal configuration is set to run the api or raise an error explanation. @@ -106,7 +106,7 @@ return cfg -def make_app_from_configfile(): +def make_app_from_configfile() -> StorageServerApp: """Run the WSGI app from the webserver, loading the configuration from a configuration file. diff --git a/swh/storage/tests/test_server.py b/swh/storage/tests/test_server.py --- a/swh/storage/tests/test_server.py +++ b/swh/storage/tests/test_server.py @@ -3,10 +3,18 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +import os +from typing import Any, Dict + import pytest import yaml -from swh.storage.api.server import load_and_check_config +from swh.core.config import load_from_envvar +from swh.storage.api.server import ( + StorageServerApp, + load_and_check_config, + make_app_from_configfile, +) def prepare_config_file(tmpdir, content, name="config.yml"): @@ -58,3 +66,31 @@ config_path = prepare_config_file(tmpdir, config) cfg = load_and_check_config(config_path) assert cfg == config + + +@pytest.fixture +def swh_storage_server_config( + swh_storage_backend_config: Dict[str, Any] +) -> Dict[str, Any]: + return {"storage": swh_storage_backend_config} + + +@pytest.fixture +def swh_storage_config(monkeypatch, swh_storage_server_config, tmp_path): + conf_path = os.path.join(str(tmp_path), "storage.yml") + with open(conf_path, "w") as f: + f.write(yaml.dump(swh_storage_server_config)) + monkeypatch.setenv("SWH_CONFIG_FILENAME", conf_path) + return conf_path + + +def test_server_make_app_from_config_file(swh_storage_config): + app = make_app_from_configfile() + expected_cfg = load_from_envvar() + + assert app is not None + assert isinstance(app, StorageServerApp) + assert app.config["storage"] == expected_cfg["storage"] + + app2 = make_app_from_configfile() + assert app is app2