Raise an exception in case of an invalid configuration file

Until now, the "load_conf" function did return an empty dict in case of
an invalid config. This was done because this function was called on
module import, leading to crashes during tox jobs.

However, catching the exception could lead to hard to debug issues.

Since the function isn't called on module import anymore, it does now
raise an exception in case the metrics.yml config file can't be read or has an
invalid format.

Story: 2006941
Task: 37631

Change-Id: Ia09ea8cacb7a82a4baf92f310f2428e2ec5973b7
This commit is contained in:
Luka Peschke 2019-11-26 15:12:32 +01:00
parent 0dbf9a64b7
commit eabce23c37
2 changed files with 12 additions and 14 deletions

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2014 Objectif Libre
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
@ -230,21 +229,15 @@ def check_time_state(timestamp=None, period=0, wait_periods=0):
def load_conf(conf_path):
"""Return loaded yaml configuration.
"""Loads the metric collection configuration.
In case not found yaml file,
return an empty dict.
:param conf_path: Path of the file to load
:type conf_path: str
:rtype: dict
"""
# NOTE(mc): We can not raise any exception in this function as it called
# at some file imports. Default values should be used instead. This is
# done for the docs and tests in gerrit which does not copy yaml conf file.
try:
with open(conf_path) as conf:
res = yaml.safe_load(conf)
return res or {}
except Exception:
LOG.warning("Error when trying to retrieve {} file.".format(conf_path))
return {}
with open(conf_path) as conf:
res = yaml.safe_load(conf)
return res or {}
@contextlib.contextmanager

View File

@ -0,0 +1,5 @@
---
other:
- |
The ``cloudkitty.utils.load_conf`` function does now raise an exception in
case the ``metrics.yml`` file can't be read or has an invalid format.