From 2f4b373e12fd9a3c37dd28f9444615ce034aceda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 21 Dec 2018 11:25:49 +0200 Subject: [PATCH] More test cases. Check.is_down() is redundant, removing. --- hc/api/management/commands/sendalerts.py | 2 +- hc/api/models.py | 16 +------- hc/api/tests/test_check_going_down_after.py | 6 --- hc/api/tests/test_check_model.py | 42 +++++++++++++++++++++ 4 files changed, 45 insertions(+), 21 deletions(-) diff --git a/hc/api/management/commands/sendalerts.py b/hc/api/management/commands/sendalerts.py index 30e78656..0886f3b6 100644 --- a/hc/api/management/commands/sendalerts.py +++ b/hc/api/management/commands/sendalerts.py @@ -94,7 +94,7 @@ class Command(BaseCommand): old_status = check.status q = Check.objects.filter(id=check.id, status=old_status) - if not check.is_down(): + if check.get_status(with_started=False) != "down": # It is not down yet. Update alert_after q.update(alert_after=check.going_down_after()) return True diff --git a/hc/api/models.py b/hc/api/models.py index 418c5a65..4929bb89 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -154,28 +154,16 @@ class Check(models.Model): if grace_start is not None: return grace_start + self.grace - def is_down(self): - """ Return True if the check is currently in alert state. """ - - if self.status == "down": - return True - - down_after = self.going_down_after() - if down_after is None: - return False - - return timezone.now() >= down_after - def get_status(self, now=None, with_started=True): """ Return current status for display. """ if now is None: now = timezone.now() - if self.last_start and with_started: + if self.last_start: if now >= self.last_start + self.grace: return "down" - else: + elif with_started: return "started" if self.status in ("new", "paused", "down"): diff --git a/hc/api/tests/test_check_going_down_after.py b/hc/api/tests/test_check_going_down_after.py index 631dfb12..10763e23 100644 --- a/hc/api/tests/test_check_going_down_after.py +++ b/hc/api/tests/test_check_going_down_after.py @@ -10,20 +10,17 @@ class CheckModelTestCase(TestCase): def test_it_handles_new_check(self): check = Check() self.assertEqual(check.going_down_after(), None) - self.assertFalse(check.is_down()) def test_it_handles_paused_check(self): check = Check(status="paused") check.last_ping = timezone.now() - td(days=2) self.assertEqual(check.going_down_after(), None) - self.assertFalse(check.is_down()) def test_it_handles_up(self): check = Check(status="up") check.last_ping = timezone.now() - td(hours=1) expected_aa = check.last_ping + td(days=1, hours=1) self.assertEqual(check.going_down_after(), expected_aa) - self.assertFalse(check.is_down()) def test_it_handles_paused_then_started_check(self): check = Check(status="paused") @@ -31,17 +28,14 @@ class CheckModelTestCase(TestCase): expected_aa = check.last_start + td(hours=1) self.assertEqual(check.going_down_after(), expected_aa) - self.assertTrue(check.is_down()) def test_it_handles_down(self): check = Check(status="down") check.last_ping = timezone.now() - td(hours=1) self.assertEqual(check.going_down_after(), None) - self.assertTrue(check.is_down()) def test_it_handles_down_then_started_check(self): check = Check(status="down") check.last_start = timezone.now() - td(minutes=10) self.assertEqual(check.going_down_after(), None) - self.assertTrue(check.is_down()) diff --git a/hc/api/tests/test_check_model.py b/hc/api/tests/test_check_model.py index f92bb336..858e3954 100644 --- a/hc/api/tests/test_check_model.py +++ b/hc/api/tests/test_check_model.py @@ -99,15 +99,57 @@ class CheckModelTestCase(TestCase): def test_get_status_handles_started(self): check = Check() check.last_ping = timezone.now() - timedelta(hours=2) + # Last start was 5 minutes ago, display status should be "started" check.last_start = timezone.now() - timedelta(minutes=5) for status in ("new", "paused", "up", "down"): check.status = status self.assertEqual(check.get_status(), "started") + def test_get_status_handles_down_then_started_and_expired(self): + check = Check(status="down") + # Last ping was 2 days ago + check.last_ping = timezone.now() - timedelta(days=2) + # Last start was 2 hours ago - the check is past its grace time + check.last_start = timezone.now() - timedelta(hours=2) + + self.assertEqual(check.get_status(), "down") + self.assertEqual(check.get_status(with_started=False), "down") + + def test_get_status_handles_up_then_started(self): + check = Check(status="up") + # Last ping was 2 hours ago, so is still up + check.last_ping = timezone.now() - timedelta(hours=2) + # Last start was 5 minutes ago + check.last_start = timezone.now() - timedelta(minutes=5) + + self.assertEqual(check.get_status(), "started") + # Starting a check starts the grace period: + self.assertEqual(check.get_status(with_started=False), "grace") + + def test_get_status_handles_up_then_started_and_expired(self): + check = Check(status="up") + # Last ping was 3 hours ago, so is still up + check.last_ping = timezone.now() - timedelta(hours=3) + # Last start was 2 hours ago - the check is past its grace time + check.last_start = timezone.now() - timedelta(hours=2) + + self.assertEqual(check.get_status(), "down") + self.assertEqual(check.get_status(with_started=False), "down") + + def test_get_status_handles_paused_then_started_and_expired(self): + check = Check(status="paused") + # Last start was 2 hours ago - the check is past its grace time + check.last_start = timezone.now() - timedelta(hours=2) + + self.assertEqual(check.get_status(), "down") + self.assertEqual(check.get_status(with_started=False), "down") + def test_get_status_handles_started_and_mia(self): check = Check() check.last_start = timezone.now() - timedelta(hours=2) + self.assertEqual(check.get_status(), "down") + self.assertEqual(check.get_status(with_started=False), "down") def test_next_ping_with_cron_syntax(self): dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)