Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean-up shinkentest and test-cases: Use a mix-in class instead of ugly code. #608

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 37 additions & 32 deletions test/shinken_test.py
Expand Up @@ -98,7 +98,43 @@ class Pluginconf(object):
pass


class ShinkenTest(unittest.TestCase):
class _Unittest2CompatMixIn:
"""
Mixin for simulating methods new in unittest2 resp. Python 2.7.

Every test-case should inherit this *after* unittest.TestCase to
make the compatiblity-methods available if they are not defined in
unittest.TestCase already. Example::

class MyTestCase(unittest.TestCase, Unittest2CompatMixIn):
...

"""
def assertNotIn(self, member, container, msg=None):
self.assertTrue(member not in container, msg)

def assertIn(self, member, container, msg=None):
self.assertTrue(member in container, msg)

def assertIsInstance(self, obj, cls, msg=None):
self.assertTrue(isinstance(obj, cls), msg)

def assertRegexpMatches(self, line, pattern, msg):
r = re.search(pattern, line)
self.assertTrue(r is not None, msg)

def assertIs(self, obj, cmp, msg=None):
self.assertTrue(obj is cmp, msg)


class TestCase(unittest.TestCase, _Unittest2CompatMixIn):
"""
`unittest.TestCase` with mix-in for simulating methods new in
unittest2 resp. Python 2.7 already mixed-in.
"""


class ShinkenTest(TestCase):
def setUp(self):
self.setup_with_file('etc/nagios_1r_1h_1s.cfg')

Expand Down Expand Up @@ -409,36 +445,5 @@ def init_livestatus(self, modconf=None):
#--- livestatus_broker.do_main


# Hook for old python some test
if not hasattr(ShinkenTest, 'assertNotIn'):
def assertNotIn(self, member, container, msg=None):
self.assertTrue(member not in container)
ShinkenTest.assertNotIn = assertNotIn


if not hasattr(ShinkenTest, 'assertIn'):
def assertIn(self, member, container, msg=None):
self.assertTrue(member in container)
ShinkenTest.assertIn = assertIn

if not hasattr(ShinkenTest, 'assertIsInstance'):
def assertIsInstance(self, obj, cls, msg=None):
self.assertTrue(isinstance(obj, cls))
ShinkenTest.assertIsInstance = assertIsInstance


if not hasattr(ShinkenTest, 'assertRegexpMatches'):
def assertRegexpMatches(self, line, patern):
r = re.search(patern, line)
self.assertTrue(r is not None)
ShinkenTest.assertRegexpMatches = assertRegexpMatches


if not hasattr(ShinkenTest, 'assertIs'):
def assertIs(self, obj, cmp, msg=None):
self.assertTrue(obj is cmp)
ShinkenTest.assertIs = assertIs


if __name__ == '__main__':
unittest.main()
19 changes: 7 additions & 12 deletions test/test_logging.py
Expand Up @@ -36,7 +36,7 @@
from shinken.log import logger, Log
import shinken.log as logging
from shinken.brok import Brok
from shinken_test import *
from shinken_test import TestCase

# The logging module requires some object for collecting broks
class Dummy:
Expand All @@ -53,17 +53,11 @@ def add(self, o):
self.list.append(o)


class NoSetup:
def setUp(self):
pass



logger.load_obj(Dummy())



class TestLevels(unittest.TestCase):
class TestLevels(TestCase):

def test_get_level_id(self):
for name , level in (
Expand Down Expand Up @@ -99,7 +93,8 @@ def test_load_obj_must_not_change_level(self):
logger.load_obj(Dummy())
self.assertEqual(logger._level, logger.CRITICAL)

class TestBasics(unittest.TestCase):

class TestBasics(TestCase):

def test_setting_and_unsetting_human_timestamp_format(self):
# :hack: logging.human_timestamp_log is a global variable
Expand Down Expand Up @@ -150,7 +145,7 @@ def _put_log(self, log_method, *messages):
return self._get_logging_output()


class TestDefaultLoggingMethods(NoSetup, ShinkenTest, LogCollectMixin):
class TestDefaultLoggingMethods(TestCase, LogCollectMixin):

def test_basic_logging_log(self):
msgs, lines = self._put_log(logger.log, 'Some log-message')
Expand Down Expand Up @@ -233,7 +228,7 @@ def test_reset_human_timestamp_format(self):
self.test_basic_logging_info()


class TestWithLocalLogging(NoSetup, ShinkenTest, LogCollectMixin):
class TestWithLocalLogging(TestCase, LogCollectMixin):

def _prepare_logging(self):
super(TestWithLocalLogging, self)._prepare_logging()
Expand Down Expand Up @@ -349,7 +344,7 @@ def test_reset_human_timestamp_format(self):
self.test_basic_logging_info()


class TestNamedCollector(NoSetup, ShinkenTest, LogCollectMixin):
class TestNamedCollector(TestCase, LogCollectMixin):

# :todo: add a test for the local log file, too

Expand Down
25 changes: 10 additions & 15 deletions test/test_properties.py
Expand Up @@ -29,15 +29,12 @@
import shinken.property
from shinken.property import none_object

from shinken_test import *
from shinken_test import TestCase


class PropertyTests:
"""Common tests for all property classes"""

def setUp(self):
pass

def test_no_default_value(self):
p = self.prop_class()
self.assertIs(p.default, none_object)
Expand All @@ -62,9 +59,7 @@ def test_unused(self):
self.assertFalse(p.unused)


#ShinkenTest, unittest.TestCase

class TestBoolProp(PropertyTests, ShinkenTest, unittest.TestCase):
class TestBoolProp(PropertyTests, TestCase):
"""Test the BoolProp class"""

prop_class = shinken.property.BoolProp
Expand All @@ -83,7 +78,7 @@ def test_pythonize(self):
self.assertEqual(p.pythonize("off"), False)


class TestIntegerProp(PropertyTests, ShinkenTest, unittest.TestCase):
class TestIntegerProp(PropertyTests, TestCase):
"""Test the IntegerProp class"""

prop_class = shinken.property.IntegerProp
Expand All @@ -95,7 +90,7 @@ def test_pythonize(self):
self.assertEqual(p.pythonize("1000.33"), 1000)


class TestFloatProp(PropertyTests, ShinkenTest, unittest.TestCase):
class TestFloatProp(PropertyTests, TestCase):
"""Test the FloatProp class"""

prop_class = shinken.property.FloatProp
Expand All @@ -107,7 +102,7 @@ def test_pythonize(self):
self.assertEqual(p.pythonize("1000.33"), 1000.33)


class TestStringProp(PropertyTests, ShinkenTest, unittest.TestCase):
class TestStringProp(PropertyTests, TestCase):
"""Test the StringProp class"""

prop_class = shinken.property.StringProp
Expand All @@ -120,7 +115,7 @@ def test_pythonize(self):
self.assertEqual(p.pythonize("no"), "no")


class TestCharProp(PropertyTests, ShinkenTest, unittest.TestCase):
class TestCharProp(PropertyTests, TestCase):
"""Test the CharProp class"""

prop_class = shinken.property.CharProp
Expand Down Expand Up @@ -151,7 +146,7 @@ class TestConfigPathProp(TestStringProp):
# any relevant change. So no further tests are implemented here.


class TestListProp(PropertyTests, ShinkenTest, unittest.TestCase):
class TestListProp(PropertyTests, TestCase):
"""Test the ListProp class"""

prop_class = shinken.property.ListProp
Expand All @@ -162,7 +157,7 @@ def test_pythonize(self):
self.assertEqual(p.pythonize("1,2,3"), ["1", "2", "3"])


class TestLogLevelProp(PropertyTests, ShinkenTest, unittest.TestCase):
class TestLogLevelProp(PropertyTests, TestCase):
"""Test the LogLevelProp class"""

prop_class = shinken.property.LogLevelProp
Expand All @@ -181,7 +176,7 @@ def test_pythonize(self):


## :todo: fix DictProp error if no `elts_prop` are passed
## class TestDictProp(PropertyTests, ShinkenTest, unittest.TestCase):
## class TestDictProp(PropertyTests, TestCase):
## """Test the DictProp class"""
##
## prop_class = shinken.property.DictProp
Expand All @@ -191,7 +186,7 @@ def test_pythonize(self):
## self.assertEqual(p.pythonize(""), "")


class TestAddrProp(PropertyTests, ShinkenTest, unittest.TestCase):
class TestAddrProp(PropertyTests, TestCase):
"""Test the AddrProp class"""

prop_class = shinken.property.AddrProp
Expand Down