Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get default config values from environment #998

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,22 @@ 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] = {}
self.refresh()

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:
Expand Down
17 changes: 16 additions & 1 deletion tests/unit_tests/test_configuration_refresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"
Expand Down