2020-01-19 16:06:21 +01:00
|
|
|
import unittest
|
|
|
|
|
2020-03-06 14:45:09 +01:00
|
|
|
import sys
|
2020-02-04 21:09:11 +01:00
|
|
|
import shlex
|
|
|
|
|
2020-01-19 16:06:21 +01:00
|
|
|
import core.module
|
2020-01-26 14:06:09 +01:00
|
|
|
import core.widget
|
2020-02-04 21:09:11 +01:00
|
|
|
import core.config
|
2020-01-19 16:06:21 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-03 21:30:06 +01:00
|
|
|
class TestModule(core.module.Module):
|
2020-03-04 21:06:09 +01:00
|
|
|
def update(self):
|
|
|
|
if self.fail:
|
|
|
|
raise Exception(self.error)
|
|
|
|
pass
|
2020-02-03 21:30:06 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-01-19 16:06:21 +01:00
|
|
|
class module(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.invalidModuleName = "invalid-module-name"
|
|
|
|
self.validModuleName = "test"
|
|
|
|
self.someWidget = core.widget.Widget("randomeWidgetContent", name="A")
|
|
|
|
self.anotherWidget = core.widget.Widget("more Widget content", name="B")
|
|
|
|
self.unusedWidgetName = "C"
|
2020-01-19 16:06:21 +01:00
|
|
|
|
2020-02-04 21:09:11 +01:00
|
|
|
def test_loadinvalid_module(self):
|
2020-02-23 14:55:13 +01:00
|
|
|
config = unittest.mock.MagicMock()
|
|
|
|
module = core.module.load(module_name=self.invalidModuleName, config=config)
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(
|
|
|
|
"core.module", module.__class__.__module__, "module must be a module object"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
"Error",
|
|
|
|
module.__class__.__name__,
|
|
|
|
"an invalid module must be a core.module.Error",
|
|
|
|
)
|
|
|
|
|
|
|
|
@unittest.skipIf(
|
|
|
|
sys.version_info.major == 3 and sys.version_info.minor in [4, 5],
|
|
|
|
"importlib error reporting in Python 3.{4,5} different",
|
|
|
|
)
|
2020-03-06 14:14:34 +01:00
|
|
|
def test_importerror(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
with unittest.mock.patch("core.module.importlib") as importlib:
|
|
|
|
importlib.import_module.side_effect = ImportError("some-error")
|
2020-03-06 14:14:34 +01:00
|
|
|
|
|
|
|
config = unittest.mock.MagicMock()
|
|
|
|
module = core.module.load(module_name=self.validModuleName, config=config)
|
|
|
|
module.widget().full_text()
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(
|
|
|
|
"Error",
|
|
|
|
module.__class__.__name__,
|
|
|
|
"an invalid module must be a core.module.Error",
|
|
|
|
)
|
|
|
|
self.assertEqual(module.widget().full_text(), "test: some-error")
|
2020-03-06 14:14:34 +01:00
|
|
|
|
2020-02-04 21:09:11 +01:00
|
|
|
def test_loadvalid_module(self):
|
|
|
|
module = core.module.load(module_name=self.validModuleName)
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(
|
|
|
|
"modules.core.{}".format(self.validModuleName),
|
|
|
|
module.__class__.__module__,
|
|
|
|
"module must be a modules.core.<name> object",
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
"Module",
|
|
|
|
module.__class__.__name__,
|
|
|
|
"a valid module must have a Module class",
|
|
|
|
)
|
|
|
|
self.assertEqual([], module.state(None), "default state of module is empty")
|
2020-01-19 16:06:21 +01:00
|
|
|
|
2020-01-26 14:06:09 +01:00
|
|
|
def test_empty_widgets(self):
|
|
|
|
module = core.module.Module(widgets=[])
|
|
|
|
self.assertEqual([], module.widgets())
|
|
|
|
|
2020-03-01 14:08:16 +01:00
|
|
|
def test_error_widget(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
cfg = core.config.Config(shlex.split("-p test_module.foo=5"))
|
|
|
|
module = core.module.Error(cfg, "test-mod", "xyz")
|
|
|
|
self.assertEqual(
|
|
|
|
["critical"], module.state(None), "error module must have critical state"
|
|
|
|
)
|
2020-03-01 14:36:12 +01:00
|
|
|
full_text = module.full_text(module.widget())
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertTrue("test-mod" in full_text)
|
|
|
|
self.assertTrue("xyz" in full_text)
|
2020-03-01 14:08:16 +01:00
|
|
|
|
2020-01-26 14:06:09 +01:00
|
|
|
def test_single_widget(self):
|
|
|
|
module = core.module.Module(widgets=self.someWidget)
|
|
|
|
self.assertEqual([self.someWidget], module.widgets())
|
|
|
|
|
|
|
|
def test_widget_list(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
module = core.module.Module(widgets=[self.someWidget, self.anotherWidget])
|
|
|
|
self.assertEqual([self.someWidget, self.anotherWidget], module.widgets())
|
2020-01-26 14:06:09 +01:00
|
|
|
|
2020-02-03 21:30:06 +01:00
|
|
|
def test_module_Name(self):
|
2020-02-04 21:09:11 +01:00
|
|
|
module = TestModule()
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual("test_module", module.name, "module has wrong name")
|
|
|
|
self.assertEqual("test_module", module.module_name, "module has wrong name")
|
2020-02-04 21:09:11 +01:00
|
|
|
|
|
|
|
def testvalid_parameter(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
cfg = core.config.Config(shlex.split("-p test_module.foo=5"))
|
2020-02-04 21:09:11 +01:00
|
|
|
module = TestModule(config=cfg)
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(5, int(module.parameter("foo")))
|
2020-02-04 21:09:11 +01:00
|
|
|
|
|
|
|
def test_default_parameter(self):
|
|
|
|
cfg = core.config.Config([])
|
|
|
|
module = TestModule(config=cfg)
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual("default", module.parameter("foo", "default"))
|
2020-02-04 21:09:11 +01:00
|
|
|
|
|
|
|
def test_default_is_none(self):
|
|
|
|
cfg = core.config.Config([])
|
|
|
|
module = TestModule(config=cfg)
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(None, module.parameter("foo"))
|
2020-02-03 21:30:06 +01:00
|
|
|
|
2020-03-04 21:06:09 +01:00
|
|
|
def test_error_widget(self):
|
|
|
|
cfg = core.config.Config([])
|
|
|
|
module = TestModule(config=cfg)
|
|
|
|
module.fail = True
|
2020-05-03 11:15:52 +02:00
|
|
|
module.error = "!!"
|
2020-03-04 21:06:09 +01:00
|
|
|
module.update_wrapper()
|
|
|
|
self.assertEqual(1, len(module.widgets()))
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual("error: !!", module.widget().full_text())
|
2020-03-04 21:06:09 +01:00
|
|
|
|
2020-03-05 21:17:06 +01:00
|
|
|
def test_get_widget_by_name(self):
|
|
|
|
cfg = core.config.Config([])
|
|
|
|
module = TestModule(config=cfg, widgets=[self.someWidget, self.anotherWidget])
|
|
|
|
|
2020-04-30 12:42:34 +02:00
|
|
|
self.assertEqual(self.someWidget, module.widget(self.someWidget.name))
|
|
|
|
self.assertEqual(self.anotherWidget, module.widget(self.anotherWidget.name))
|
2020-03-05 21:17:06 +01:00
|
|
|
self.assertEqual(None, module.widget(self.unusedWidgetName))
|
|
|
|
self.assertEqual(self.someWidget, module.widget())
|
|
|
|
|
2020-03-06 14:14:34 +01:00
|
|
|
def test_default_thresholds(self):
|
|
|
|
cfg = core.config.Config([])
|
|
|
|
module = TestModule(config=cfg, widgets=[self.someWidget, self.anotherWidget])
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual("critical", module.threshold_state(100, 80, 99))
|
|
|
|
self.assertEqual("warning", module.threshold_state(100, 80, 100))
|
|
|
|
self.assertEqual("warning", module.threshold_state(81, 80, 100))
|
2020-03-06 14:14:34 +01:00
|
|
|
self.assertEqual(None, module.threshold_state(80, 80, 100))
|
|
|
|
self.assertEqual(None, module.threshold_state(10, 80, 100))
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-01-19 16:06:21 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|