def test_notification_format_failure(self): m = monitor.MonitorFail("test", {}) with freeze_time(self.freeze_time_value): m.run_test() self.assertEqual( self.test_alerter.build_message( alerter.AlertLength.NOTIFICATION, alerter.AlertType.FAILURE, m), "Monitor test failed", )
def test_should_alert_ooh(self): config = { "times_type": "only", "time_lower": "10:00", "time_upper": "11:00" } a = alerter.Alerter(config) m = monitor.MonitorFail("fail", {}) m.run_test() with freeze_time("2020-03-10 10:30"): self.assertEqual(a.should_alert(m), alerter.AlertType.FAILURE) self.assertEqual(a._ooh_failures, [])
def test_sms_format_failure(self): m = monitor.MonitorFail("test", {}) with freeze_time(self.freeze_time_value): m.run_test() self.assertEqual( self.test_alerter.build_message(alerter.AlertLength.SMS, alerter.AlertType.FAILURE, m), "failure: test failed on {hostname} at {expected_time} (0+00:00:00): This monitor always fails." .format( hostname=util.short_hostname(), expected_time=self.expected_time_string, ), )
def test_should_not_alert_ooh(self): config = { "times_type": "only", "time_lower": "10:00", "time_upper": "11:00" } a = alerter.Alerter(config) m = monitor.MonitorFail("fail", {}) m.run_test() with freeze_time("2020-03-10 09:00"): # out of hours on the right day; shouldn't alert self.assertEqual(a.should_alert(m), alerter.AlertType.NONE) self.assertEqual(a._ooh_failures, ["fail"]) a = alerter.Alerter(config) with freeze_time("2020-03-10 12:00"): self.assertEqual(a.should_alert(m), alerter.AlertType.NONE) self.assertEqual(a._ooh_failures, ["fail"])
def test_full_format_failure(self): m = monitor.MonitorFail("test", {}) with freeze_time(self.freeze_time_value): m.run_test() self.assertEqual( self.test_alerter.build_message(alerter.AlertLength.FULL, alerter.AlertType.FAILURE, m), textwrap.dedent(""" Monitor test on {hostname} failed! Failed at: {expected_time} (down 0+00:00:00) Virtual failure count: 1 Additional info: This monitor always fails. Description: A monitor which always fails. """.format( hostname=util.short_hostname(), expected_time=self.expected_time_string, )), )
def test_should_alert_catchup(self): config = { "delay": 1, "times_type": "only", "time_lower": "10:00", "time_upper": "11:00", } a = alerter.Alerter(config) a.support_catchup = True m = monitor.MonitorFail("fail", {}) m.run_test() with freeze_time("2020-03-10 09:00"): self.assertEqual(a.should_alert(m), alerter.AlertType.NONE) self.assertEqual(a._ooh_failures, ["fail"]) with freeze_time("2020-03-10 10:30"): self.assertEqual(a.should_alert(m), alerter.AlertType.CATCHUP) self.assertEqual(a._ooh_failures, [])
def test_disabled_monitor_no_alert(self): a = alerter.Alerter() m = monitor.MonitorFail("fail", {"enabled": 0}) m.run_test() self.assertEqual(a.should_alert(m), alerter.AlertType.NONE)
def test_groups_all_match(self): a = alerter.Alerter({"groups": "_all"}) m = monitor.MonitorFail("fail", {"group": "test1"}) m.run_test() self.assertEqual(a.should_alert(m), alerter.AlertType.FAILURE)
def test_skip_group_monitor(self): a = alerter.Alerter() m = monitor.MonitorFail("fail", {"group": "test"}) m.run_test() self.assertEqual(a.should_alert(m), alerter.AlertType.NONE)
def test_should_not_alert_basic_success(self): a = alerter.Alerter({"only_failures": True}) m = monitor.MonitorFail("fail", {}) for _ in range(0, 6): m.run_test() self.assertEqual(a.should_alert(m), alerter.AlertType.NONE)
def test_should_alert_basic_success(self): a = alerter.Alerter(None) m = monitor.MonitorFail("fail", {}) for _ in range(0, 6): m.run_test() self.assertEqual(a.should_alert(m), alerter.AlertType.SUCCESS)
def test_should_alert_only_failure(self): # no special alert config a = alerter.Alerter({"only_failures": True}) m = monitor.MonitorFail("fail", {}) m.run_test() self.assertEqual(a.should_alert(m), alerter.AlertType.FAILURE)