QVolution2019.2/AoM_Service/library/serviceapp/test_thresholds.py

158 lines
4.7 KiB
Python
Executable File

import unittest
class Mock_Alert_Config() :
def __init__(self) :
self.upCrit = 10
self.lowCrit = 1
def get_threshold(self, upper, warn) :
if upper and warn :
return None, False
elif upper and not warn :
return self.upCrit, True
elif not upper and warn :
return None, False
else:
return self.lowCrit, True
class Test_Thresholds(unittest.TestCase) :
def test_breached_both(self) :
import thresholds
alert_config = Mock_Alert_Config()
t = thresholds.Thresholds(alert_config)
t.set_breached(alert_config.lowCrit-1, alert_config.upCrit+1)
should_fire = [
t.critical_breached(),
t.lower_breached(),
t.upper_breached(),
t.level_breached(t.CRITICAL),
t.end_breached(t.LOWER),
t.end_breached(t.UPPER),
t.get_breached(),
t.get_breached(level=t.CRITICAL),
t.get_breached(end=t.LOWER),
t.get_breached(end=t.UPPER),
]
for i in range(len(should_fire)) :
self.assertTrue(should_fire[i], i)
should_not_fire = [
t.warning_breached(),
t.level_breached(t.WARNING),
t.get_breached(level=t.WARNING),
]
for i in range(len(should_not_fire)) :
self.assertFalse(should_not_fire[i], i)
def test_breached_lower(self) :
import thresholds
alert_config = Mock_Alert_Config()
t = thresholds.Thresholds(alert_config)
t.set_breached(alert_config.lowCrit-1, alert_config.upCrit)
should_fire = [
t.critical_breached(),
t.lower_breached(),
t.level_breached(t.CRITICAL),
t.end_breached(t.LOWER),
t.get_breached(),
t.get_breached(level=t.CRITICAL),
t.get_breached(end=t.LOWER),
]
for i in range(len(should_fire)) :
self.assertTrue(should_fire[i], i)
should_not_fire = [
t.warning_breached(),
t.upper_breached(),
t.level_breached(t.WARNING),
t.end_breached(t.UPPER),
t.get_breached(level=t.WARNING),
t.get_breached(end=t.UPPER),
]
for i in range(len(should_not_fire)) :
self.assertFalse(should_not_fire[i], i)
def test_breached_upper(self) :
import thresholds
alert_config = Mock_Alert_Config()
t = thresholds.Thresholds(alert_config)
t.set_breached(alert_config.lowCrit, alert_config.upCrit+1)
should_fire = [
t.critical_breached(),
t.upper_breached(),
t.level_breached(t.CRITICAL),
t.end_breached(t.UPPER),
t.get_breached(),
t.get_breached(level=t.CRITICAL),
t.get_breached(end=t.UPPER),
]
for i in range(len(should_fire)) :
self.assertTrue(should_fire[i], i)
for i in [
t.warning_breached(),
t.lower_breached(),
t.level_breached(t.WARNING),
t.end_breached(t.LOWER),
t.get_breached(level=t.WARNING),
t.get_breached(end=t.LOWER),
] :
self.assertFalse(i)
def test_breached_notset(self) :
import thresholds
alert_config = Mock_Alert_Config()
t = thresholds.Thresholds(alert_config)
for i in [
t.warning_breached(),
t.critical_breached(),
t.upper_breached(),
t.lower_breached(),
t.level_breached(t.CRITICAL),
t.level_breached(t.WARNING),
t.end_breached(t.UPPER),
t.end_breached(t.LOWER),
t.get_breached(),
t.get_breached(level=t.CRITICAL),
t.get_breached(level=t.WARNING),
t.get_breached(end=t.UPPER),
t.get_breached(end=t.LOWER),
] :
self.assertFalse(i)
def test_get_matching(self) :
import thresholds
alert_config = Mock_Alert_Config()
t = thresholds.Thresholds(alert_config)
self.assertEqual(4, len([i for i in t.get_thresholds_matching()]))
self.assertEqual(2, len([i for i in t.get_thresholds_matching(level=t.CRITICAL)]))
self.assertEqual(2, len([i for i in t.get_thresholds_matching(level=t.WARNING)]))
self.assertEqual(2, len([i for i in t.get_thresholds_matching(end=t.UPPER)]))
self.assertEqual(2, len([i for i in t.get_thresholds_matching(end=t.LOWER)]))
self.assertEqual(1, len([i for i in t.get_thresholds_matching(end=t.CRITICAL, level=t.LOWER)]))
self.assertEqual(1, len([i for i in t.get_thresholds_matching(end=t.CRITICAL, level=t.UPPER)]))
self.assertEqual(1, len([i for i in t.get_thresholds_matching(end=t.WARNING, level=t.LOWER)]))
self.assertEqual(1, len([i for i in t.get_thresholds_matching(end=t.WARNING, level=t.UPPER)]))
if __name__ == "__main__" :
unittest.main()