Module: check_mk
Branch: master
Commit: 83ab9706e71fb4af7d0b7d7d76bd1be75ecec527
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=83ab9706e71fb4…
Author: Moritz Kiemer <mo(a)mathias-kettner.de>
Date: Fri May 3 16:13:33 2019 +0200
cmk.special_agents.utils: DataCache: introduce public debug attribute
Change-Id: Idb5d2a98b5397a86c02d86661fd41ea476e4bd1c
---
cmk/special_agents/utils.py | 10 +++++++---
tests/unit/cmk/special_agents/test_utils_datacache.py | 4 ++--
2 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/cmk/special_agents/utils.py b/cmk/special_agents/utils.py
index 41cd020..60febc6 100644
--- a/cmk/special_agents/utils.py
+++ b/cmk/special_agents/utils.py
@@ -117,7 +117,7 @@ class DataCache(object):
def __init__(self, cache_file_dir, cache_file_name, debug=False):
self._cache_file_dir = Path(cache_file_dir)
self._cache_file = self._cache_file_dir / ("%s.cache" %
cache_file_name)
- self._exceptions = () if debug else (OSError, IOError, ValueError)
+ self.debug = debug
@abc.abstractproperty
def cache_interval(self):
@@ -191,15 +191,19 @@ class DataCache(object):
if (use_cache and self.get_validity_from_args(args) and self._cache_is_valid()):
try:
return self.get_cached_data()
- except self._exceptions as exc:
+ except (OSError, IOError, ValueError) as exc:
logging.info("Getting live data (failed to read from cache:
%s).", exc)
+ if self.debug:
+ raise
live_data = self.get_live_data(args)
if use_cache:
try:
self._write_to_cache(live_data)
- except self._exceptions as exc:
+ except (OSError, IOError, TypeError) as exc:
logging.info("Failed to write data to cache file: %s", exc)
+ if self.debug:
+ raise
return live_data
def _write_to_cache(self, raw_content):
diff --git a/tests/unit/cmk/special_agents/test_utils_datacache.py
b/tests/unit/cmk/special_agents/test_utils_datacache.py
index 4f18cd1..38c7aa1 100644
--- a/tests/unit/cmk/special_agents/test_utils_datacache.py
+++ b/tests/unit/cmk/special_agents/test_utils_datacache.py
@@ -21,10 +21,10 @@ def test_datacache_init(tmpdir):
tcache = TestCache(tmpdir, 'test')
assert isinstance(tcache._cache_file_dir, Path)
assert isinstance(tcache._cache_file, Path)
- assert tcache._exceptions == (OSError, IOError, ValueError)
+ assert not tcache.debug
tc_debug = TestCache(tmpdir, 'test', debug=True)
- assert tc_debug._exceptions == ()
+ assert tc_debug.debug
with pytest.raises(TypeError):
DataCache('foo', 'bar') # pylint:
disable=abstract-class-instantiated