Commit ed597075 authored by Alain Takoudjou's avatar Alain Takoudjou

improve defaultTest method

parent 63a9e08a
...@@ -286,30 +286,44 @@ class GenericPromise(object): ...@@ -286,30 +286,44 @@ class GenericPromise(object):
line_list.reverse() line_list.reverse()
return line_list return line_list
def defaultTest(self, latest_minute=3): def defaultTest(self, latest_minute=3, failure_amount=2, exact_match=False,
is_anomaly=False):
""" """
Fail if the latest 2 messages has failed. Fail if the latest `failure_amount` messages contain failure.
If only there is only one result, fail if that result has error
@param latest_minute: test the result from now to the latest X minutes in
the past.
@param failure_amount: fail is this amount of failure is found in result
@param exact_match: bool (True|False). If True, only fail if the number
of failure found is equal to `failure_amount`. Else, fail if at least
one failure was found.
@param is_anomaly: Say if the result is an AnomalyResult of TestResult
""" """
problem = False problem = False
message = "" message = ""
module = TestResult if not is_anomaly else AnomalyResult
latest_result_list = self.getLastPromiseResultList( latest_result_list = self.getLastPromiseResultList(
latest_minute=latest_minute, latest_minute=latest_minute,
only_failure=False only_failure=False
) )
result_size = len(latest_result_list) result_size = len(latest_result_list)
if result_size == 0: if result_size == 0:
return TestResult(problem=False, message="No result!") return module(problem=False, message="No result!")
i = 0 i = 0
failure_found = 0
latest_result_list.reverse() latest_result_list.reverse()
# we test at most the 2 latest results # we test at most the `failure_amount` latest results
while i < result_size and i < 2: while i < result_size and i < failure_amount:
if latest_result_list[i][1] == 'ERROR': if latest_result_list[i][1] == 'ERROR':
failure_found += 1
problem = True problem = True
i += 1 i += 1
return TestResult(problem=problem, message=latest_result_list[0][2]) if exact_match and failure_found != failure_amount:
return module(problem=False, message=latest_result_list[0][2])
return module(problem=problem, message=latest_result_list[0][2])
@abstractmethod @abstractmethod
def sense(self): def sense(self):
...@@ -317,9 +331,11 @@ class GenericPromise(object): ...@@ -317,9 +331,11 @@ class GenericPromise(object):
def anomaly(self): def anomaly(self):
"""Called to detect if there is an anomaly which require to bang.""" """Called to detect if there is an anomaly which require to bang."""
raise NotImplementedError("'anomaly' method is not implemented yet!")
def test(self): def test(self):
"""Test promise and say if problem is detected or not""" """Test promise and say if problem is detected or not"""
raise NotImplementedError("'test' method is not implemented yet!")
def run(self, can_bang=True): def run(self, can_bang=True):
""" """
...@@ -395,7 +411,7 @@ class PromiseWrapper(GenericPromise): ...@@ -395,7 +411,7 @@ class PromiseWrapper(GenericPromise):
self.logger.info(message.strip()) self.logger.info(message.strip())
def test(self): def test(self):
return self.defaultTest(latest_minute=3) return self.defaultTest(latest_minute=3, failure_amount=2, is_anomaly=False)
class PromiseRunner(Process): class PromiseRunner(Process):
...@@ -531,9 +547,10 @@ class PromiseLauncher(object): ...@@ -531,9 +547,10 @@ class PromiseLauncher(object):
if re.match(r'[a-zA-Z_]', promise_name) is None: if re.match(r'[a-zA-Z_]', promise_name) is None:
self.logger.error("Promise plugin name %r is not valid" % promise_name) self.logger.error("Promise plugin name %r is not valid" % promise_name)
promise_module = importlib.import_module(promise_name) promise_module = importlib.import_module(os.path.splitext(promise_name)[0])
if not hasattr(promise_module, "RunPromise"): if not hasattr(promise_module, "RunPromise"):
raise AttributeError("Class RunPromise not found in %s" % promise_name) raise AttributeError("Class RunPromise not found in promise" \
"%s" % promise_name)
if not interface.IPromise.implementedBy(promise_module.RunPromise): if not interface.IPromise.implementedBy(promise_module.RunPromise):
raise RuntimeError("RunPromise class in %s must implements 'IPromise'" \ raise RuntimeError("RunPromise class in %s must implements 'IPromise'" \
" interface. zope_interface.implements(interface.IPromise) is" \ " interface. zope_interface.implements(interface.IPromise) is" \
...@@ -690,7 +707,8 @@ class PromiseLauncher(object): ...@@ -690,7 +707,8 @@ class PromiseLauncher(object):
promise_list = [] promise_list = []
# load all promises so we can catch import errors before launch them # load all promises so we can catch import errors before launch them
for promise_name in os.listdir(self.promise_dir): for promise_name in os.listdir(self.promise_dir):
if promise_name.startswith('__init__'): if promise_name.startswith('__init__') or \
not promise_name.endswith('.py'):
continue continue
promise_list.append((promise_name, promise_list.append((promise_name,
self._loadPromiseModule(promise_name))) self._loadPromiseModule(promise_name)))
...@@ -701,7 +719,7 @@ class PromiseLauncher(object): ...@@ -701,7 +719,7 @@ class PromiseLauncher(object):
'name': promise[0] 'name': promise[0]
} }
config.update(base_config) config.update(base_config)
self._launchPromise(promise_name, config, promise) self._launchPromise(promise_name, config, promise[1])
if os.path.exists(self.old_promise_dir) and os.path.isdir(self.old_promise_dir): if os.path.exists(self.old_promise_dir) and os.path.isdir(self.old_promise_dir):
# run old promise styles # run old promise styles
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment