Fix ZeroDivision when no results

ZeroDivisionError raised in sla/base.py if something goes wrong
and no results available.

This fix adds check for result. If no result is provided, the
default action is returning False.

Change-Id: I3800768e962bb43b1fe0d7dabd7a23a87cca69f9
Closes-Bug: bug/1376724
This commit is contained in:
Li Ma 2014-11-23 00:11:27 -08:00
parent c8d5425434
commit 72c41f2702
2 changed files with 7 additions and 2 deletions

View File

@ -99,12 +99,13 @@ class FailureRate(SLA):
@staticmethod @staticmethod
def check(criterion_value, result): def check(criterion_value, result):
errors = len(filter(lambda x: x['error'], result)) errors = len(filter(lambda x: x['error'], result))
if criterion_value < errors * 100.0 / len(result): error_rate = errors * 100.0 / len(result) if len(result) > 0 else 100.0
if criterion_value < error_rate:
success = False success = False
else: else:
success = True success = True
msg = (_("Maximum failure percent %s%% failures, actually %s%%") % msg = (_("Maximum failure percent %s%% failures, actually %s%%") %
(criterion_value * 100.0, errors * 100.0 / len(result))) (criterion_value * 100.0, error_rate))
return SLAResult(success, msg) return SLAResult(success, msg)

View File

@ -74,6 +74,10 @@ class FailureRateTestCase(test.TestCase):
# 50% > 25% # 50% > 25%
self.assertFalse(base.FailureRate.check(25, result).success) self.assertFalse(base.FailureRate.check(25, result).success)
def test_check_with_no_results(self):
result = []
self.assertFalse(base.FailureRate.check(10.0, result).success)
class IterationTimeTestCase(test.TestCase): class IterationTimeTestCase(test.TestCase):
def test_config_schema(self): def test_config_schema(self):