def test_file_nonutc_iso_nonutctz(self): temp_logfile = tempfile.mkstemp()[1] file_logger = FileLogger({ "filename": temp_logfile, "buffered": False, "dateformat": "iso8601", "tz": "Europe/Warsaw", }) monitor = MonitorNull() monitor.run_test() file_logger.save_result2("null", monitor) self.assertTrue(os.path.exists(temp_logfile)) with open(temp_logfile, "r") as fh: self.assertEqual( fh.readline().strip(), "2020-04-18 13:00:00+02:00 simplemonitor starting", ) self.assertEqual( fh.readline().strip(), "2020-04-18 13:00:00+02:00 null: ok (0.000s)", ) try: os.unlink(temp_logfile) except PermissionError: # Windows won't remove a file which is in use pass
def test_compound_partial_fail(self): m = MonitorNull() m2 = MonitorFail("fail1", {}) m3 = MonitorFail("fail2", {}) compound_monitor = CompoundMonitor("compound", {"monitors": ["m", "m2", "m3"]}) monitors = {"m": m, "m2": m2, "m3": m3} compound_monitor.set_mon_refs(monitors) compound_monitor.post_config_setup() self.assertIn("m", compound_monitor.all_monitors, "compound all_monitors not set") self.assertIn("m", compound_monitor.m, "compund m not set") m.run_test() m2.run_test() m3.run_test() result = compound_monitor.run_test() # should be ok because not all have failed self.assertEqual(result, True, "compound monitor should not have failed") self.assertEqual( compound_monitor.virtual_fail_count(), 0, "compound monitor should not report fail count", ) self.assertEqual( compound_monitor.get_result(), "2 of 3 services failed. Fail after: 3", "compound monitor did not report failures properly", )
def test_no_disabled(self): s = simplemonitor.SimpleMonitor("tests/monitor-empty.ini") m1 = MonitorFail("fail", config_options={}) m2 = MonitorNull("null", config_options={}) s.add_monitor("fail", m1) s.add_monitor("null", m2) m2.reset_dependencies() m1.run_test() failed = s._failed_monitors() self.assertListEqual(["fail"], failed)
def test_compound_no_fail(self): m = MonitorNull() m2 = MonitorNull() compound_monitor = CompoundMonitor("compound", {"monitors": ["m", "m2"]}) monitors = {"m": m, "m2": m2} compound_monitor.set_mon_refs(monitors) compound_monitor.post_config_setup() self.assertIn("m", compound_monitor.all_monitors, "compound all_monitors not set") self.assertIn("m", compound_monitor.m, "compund m not set") m.run_test() m2.run_test() result = compound_monitor.run_test() # should be ok because not all have failed self.assertEqual(result, True, "compound monitor should not have failed") self.assertEqual( compound_monitor.virtual_fail_count(), 0, "compound monitor should not report fail count", ) self.assertEqual( compound_monitor.get_result(), "All 2 services OK", "compound monitor did not report status properly", )
def test_groups(self): with patch.object(logger.Logger, "save_result2") as mock_method: this_logger = logger.Logger({"groups": "nondefault"}) s = SimpleMonitor(Path("tests/monitor-empty.ini")) s.add_monitor("test", MonitorNull()) s.log_result(this_logger) mock_method.assert_not_called() with patch.object(logger.Logger, "save_result2") as mock_method: this_logger = logger.Logger({"groups": "nondefault"}) s = SimpleMonitor(Path("tests/monitor-empty.ini")) s.add_monitor("test", MonitorNull("unnamed", {"group": "nondefault"})) s.log_result(this_logger) mock_method.assert_called_once()
def test_enabled_monitor_run(self): s = simplemonitor.SimpleMonitor(Path("tests/monitor-empty.ini")) m = MonitorNull("unnamed", {"enabled": 1}) with patch.object(m, "run_test") as mock_method: s.add_monitor("test", m) s.run_tests() mock_method.assert_called_once()
def _write_html(logger_options: dict = None) -> str: if logger_options is None: logger_options = {} with patch.object(socket, "gethostname", return_value="fake_hostname.local"): temp_htmlfile = tempfile.mkstemp()[1] logger_options.update({"filename": temp_htmlfile}) html_logger = HTMLLogger(logger_options) monitor1 = MonitorNull() monitor2 = MonitorFail("fail", {}) monitor1.run_test() monitor2.run_test() html_logger.start_batch() html_logger.save_result2("null", monitor1) html_logger.save_result2("fail", monitor2) html_logger.end_batch() return temp_htmlfile
def test_simple(self): s = simplemonitor.SimpleMonitor("tests/monitor-empty.ini") m = MonitorNull() data = { "test1": { "cls_type": m.monitor_type, "data": m.to_python_dict() }, "test2": { "cls_type": m.monitor_type, "data": m.to_python_dict() }, } s.update_remote_monitor(data, "remote.host") self.assertIn("remote.host", s.remote_monitors) self.assertIn("test1", s.remote_monitors["remote.host"]) self.assertIn("test2", s.remote_monitors["remote.host"])
def test_file_nonutc(self): temp_logfile = tempfile.mkstemp()[1] file_logger = FileLogger({"filename": temp_logfile, "buffered": False}) monitor = MonitorNull() monitor.run_test() file_logger.save_result2("null", monitor) self.assertTrue(os.path.exists(temp_logfile)) ts = str(int(time.time())) with open(temp_logfile, "r") as fh: self.assertEqual(fh.readline().strip(), "{} simplemonitor starting".format(ts)) self.assertEqual(fh.readline().strip(), "{} null: ok (0.000s)".format(ts)) try: os.unlink(temp_logfile) except PermissionError: # Windows won't remove a file which is in use pass
def test_html_logger(self): config_options = { "folder": "test_html", "copy_resources": True, "filename": "status.html", } test_logger = HTMLLogger(config_options) monitor = MonitorNull() monitor.run_test() test_logger.start_batch() test_logger.save_result2("test", monitor) test_logger.process_batch() test_logger.end_batch() self.assertTrue( os.path.exists(os.path.join("test_html", "status.html")), "status.html was not created", ) self.assertTrue( os.path.exists(os.path.join("test_html", "style.css")), "style.css was not copied", )