diff --git a/src/cobbler_tftp/settings/__init__.py b/src/cobbler_tftp/settings/__init__.py index d5cd598..cb205fe 100644 --- a/src/cobbler_tftp/settings/__init__.py +++ b/src/cobbler_tftp/settings/__init__.py @@ -220,21 +220,19 @@ def load_env_variables(self) -> SettingsDict: return self._settings_dict for variable in cobbler_keys: - key_path = variable.split("__") - key_to_update = key_path[-1] - - if len(key_path) == 2: - try: - self._settings_dict.update( - {key_to_update.lower(): str(os.environ[variable])} - ) - except KeyError as exc: - print(exc) + key_path = variable.split("__")[1:] + key_path.reverse() + key_to_update = key_path[0] + + if len(key_path) == 1: + self._settings_dict.update( + {key_to_update.lower(): yaml.safe_load(os.environ[variable])} + ) else: - setting_to_update = {key_to_update.lower(): str(os.environ[variable])} + setting_to_update = yaml.safe_load(os.environ[variable]) - for pos in range(len(key_path) - 2, 1, -1): - setting_to_update = {key_path[pos]: setting_to_update} + for key in key_path: + setting_to_update = {key.lower(): setting_to_update} self._settings_dict.update(setting_to_update) # type: ignore diff --git a/tests/unittests/application_settings/test_settings.py b/tests/unittests/application_settings/test_settings.py index 09db383..0da88f2 100644 --- a/tests/unittests/application_settings/test_settings.py +++ b/tests/unittests/application_settings/test_settings.py @@ -2,7 +2,9 @@ Tests for the application settings module. """ +import os from pathlib import Path +from unittest import mock import pytest @@ -136,3 +138,19 @@ def test_build_settings_with_integer_cli_args(settings_factory: SettingsFactory) assert isinstance(settings, Settings) assert settings.tftp_port == 1969 + + +@mock.patch.dict(os.environ, {"COBBLER_TFTP__TFTP__ADDRESS": "1.2.3.4"}) +def test_build_settings_with_env_vars(settings_factory: SettingsFactory): + settings = settings_factory.build_settings(None) + + assert isinstance(settings, Settings) + assert settings.tftp_addr == "1.2.3.4" + + +@mock.patch.dict(os.environ, {"COBBLER_TFTP__TFTP__PORT": "1969"}) +def test_build_settings_with_integer_env_vars(settings_factory: SettingsFactory): + settings = settings_factory.build_settings(None) + + assert isinstance(settings, Settings) + assert settings.tftp_port == 1969