diff --git a/server/config.py b/server/config.py index ffa9ff93d..98082dfdc 100644 --- a/server/config.py +++ b/server/config.py @@ -156,7 +156,9 @@ def __init__(self): self.QUEUE_POP_TIME_MOVING_AVG_SIZE = 5 self._defaults = { - key: value for key, value in vars(self).items() if key.isupper() + key: value + for key, value in vars(self).items() + if key.isupper() } self._callbacks: dict[str, Callable] = {} @@ -164,6 +166,12 @@ def __init__(self): def refresh(self) -> None: new_values = self._defaults.copy() + # Add fallback values from environment. + # NOTE: Only works for string values! + for key in new_values: + value = os.getenv(key) + if value is not None: + new_values[key] = value config_file = os.getenv("CONFIGURATION_FILE") if config_file is not None: diff --git a/tests/unit_tests/test_configuration_refresh.py b/tests/unit_tests/test_configuration_refresh.py index ecca1d799..5f73c5dbc 100644 --- a/tests/unit_tests/test_configuration_refresh.py +++ b/tests/unit_tests/test_configuration_refresh.py @@ -22,12 +22,27 @@ async def config_service(monkeypatch): async def test_configuration_refresh(monkeypatch): config.refresh() assert config.DB_PASSWORD == "banana" + assert config.DB_LOGIN == "root" + monkeypatch.setenv("CONFIGURATION_FILE", "tests/data/refresh_conf.yaml") assert config.DB_PASSWORD == "banana" + # Values in config file will override defaults + config.refresh() + assert config.DB_PASSWORD == "apple" + assert config.DB_LOGIN == "root" + + # Values in config file will override environment + monkeypatch.setenv("DB_PASSWORD", "plantain") config.refresh() + assert config.DB_PASSWORD == "apple" + assert config.DB_LOGIN == "root" + # Values in environment will override defaults + monkeypatch.setenv("DB_LOGIN", "beet") + config.refresh() assert config.DB_PASSWORD == "apple" + assert config.DB_LOGIN == "beet" async def test_config_refresh_file_not_found(monkeypatch): @@ -80,7 +95,7 @@ async def test_config_callback_on_change(config_service, monkeypatch): @fast_forward(20) -async def test_config_no_callback_without_change(config_service, monkeypatch): +async def test_config_no_callback_without_change(config_service): callback = mock.Mock() config.register_callback("DB_PASSWORD", callback) assert config.DB_PASSWORD == "banana"