Skip to content

Commit

Permalink
Create the log if config is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
jesusbv committed Aug 4, 2023
1 parent d00e716 commit 614a4ee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
9 changes: 5 additions & 4 deletions csp_billing_adapter_local/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,25 @@
CSP_LOG_FILEPATH = '/var/log/csp_billing_adapter.log'


def _set_logger_file():
def _set_log_file():
"""Set logger file with level and format."""
try:
local_config_path = Path(ADAPTER_DATA_DIR).joinpath(CSP_CONFIG_FILE)
with open(local_config_path, 'r', encoding='utf-8') as f:
csp_config = json.load(f)
csp_config_logging = csp_config.get('logging', {})
except (FileNotFoundError, JSONDecodeError):
csp_config_logging = {}
finally:
logging.basicConfig(
filename=CSP_LOG_FILEPATH,
level=csp_config_logging.get('level', 'INFO'),
format="%(asctime)s %(levelname)s: %(message)s"
)
log.debug('Logger file set')
except (FileNotFoundError, JSONDecodeError) as err:
log.debug(f'Logger file not set: {err}')


_set_logger_file()
_set_log_file()


def get_local_path(filename):
Expand Down
38 changes: 17 additions & 21 deletions tests/unit/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
update_csp_config,
save_cache,
save_csp_config,
_set_logger_file
_set_log_file
)

config_file = 'tests/data/config.yaml'
Expand All @@ -46,6 +46,8 @@
config_file,
pm.hook
)
with open('tests/data/good/csp_config.json', 'r', encoding='utf-8') as f:
content = f.read()


def test_local_get_cache():
Expand Down Expand Up @@ -298,27 +300,21 @@ def test_local_csp_config_save():
assert get_csp_config(config=local_config) == test_data2


def test_local_csp_usage_logging_not_set(caplog):
@patch('csp_billing_adapter_local.plugin.Path')
@patch('csp_billing_adapter_local.plugin.Path.joinpath')
def test_local_csp_usage_logging_set_for_non_existing_config(
mock_joinpath, mock_path, caplog
):
caplog.set_level('DEBUG')
with patch(
'csp_billing_adapter_local.plugin.Path',
return_value=Path('foo/bar')
):
with patch(
'csp_billing_adapter_local.plugin.Path.joinpath',
return_value=Path('csp_config.json')
):
_set_logger_file()
debug_message = "No such file or directory: " \
"'foo/bar/csp-config.json'"
assert 'Logger file not set' in caplog.text
assert debug_message in caplog.text
mock_path.return_value = Path('foo/bar')
mock_joinpath.return_value = Path('csp_config.json')

_set_log_file()
assert 'Logger file set' in caplog.text


def test_local_csp_usage_logging_set(caplog):
@patch('builtins.open', mock_open(read_data=content))
def test_local_csp_usage_log_file_set_for_exiting_config(caplog):
caplog.set_level('DEBUG')
with open('tests/data/good/csp_config.json', 'r', encoding='utf-8') as f:
content = f.read()
with patch('builtins.open', mock_open(read_data=content)):
_set_logger_file()
assert 'Logger file set' in caplog.text
_set_log_file()
assert 'Logger file set' in caplog.text

0 comments on commit 614a4ee

Please sign in to comment.