Browse Source

More test cases. Check.is_down() is redundant, removing.

pull/211/head
Pēteris Caune 6 years ago
parent
commit
2f4b373e12
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
4 changed files with 45 additions and 21 deletions
  1. +1
    -1
      hc/api/management/commands/sendalerts.py
  2. +2
    -14
      hc/api/models.py
  3. +0
    -6
      hc/api/tests/test_check_going_down_after.py
  4. +42
    -0
      hc/api/tests/test_check_model.py

+ 1
- 1
hc/api/management/commands/sendalerts.py View File

@ -94,7 +94,7 @@ class Command(BaseCommand):
old_status = check.status old_status = check.status
q = Check.objects.filter(id=check.id, status=old_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 # It is not down yet. Update alert_after
q.update(alert_after=check.going_down_after()) q.update(alert_after=check.going_down_after())
return True return True


+ 2
- 14
hc/api/models.py View File

@ -154,28 +154,16 @@ class Check(models.Model):
if grace_start is not None: if grace_start is not None:
return grace_start + self.grace 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): def get_status(self, now=None, with_started=True):
""" Return current status for display. """ """ Return current status for display. """
if now is None: if now is None:
now = timezone.now() now = timezone.now()
if self.last_start and with_started:
if self.last_start:
if now >= self.last_start + self.grace: if now >= self.last_start + self.grace:
return "down" return "down"
else:
elif with_started:
return "started" return "started"
if self.status in ("new", "paused", "down"): if self.status in ("new", "paused", "down"):


+ 0
- 6
hc/api/tests/test_check_going_down_after.py View File

@ -10,20 +10,17 @@ class CheckModelTestCase(TestCase):
def test_it_handles_new_check(self): def test_it_handles_new_check(self):
check = Check() check = Check()
self.assertEqual(check.going_down_after(), None) self.assertEqual(check.going_down_after(), None)
self.assertFalse(check.is_down())
def test_it_handles_paused_check(self): def test_it_handles_paused_check(self):
check = Check(status="paused") check = Check(status="paused")
check.last_ping = timezone.now() - td(days=2) check.last_ping = timezone.now() - td(days=2)
self.assertEqual(check.going_down_after(), None) self.assertEqual(check.going_down_after(), None)
self.assertFalse(check.is_down())
def test_it_handles_up(self): def test_it_handles_up(self):
check = Check(status="up") check = Check(status="up")
check.last_ping = timezone.now() - td(hours=1) check.last_ping = timezone.now() - td(hours=1)
expected_aa = check.last_ping + td(days=1, hours=1) expected_aa = check.last_ping + td(days=1, hours=1)
self.assertEqual(check.going_down_after(), expected_aa) self.assertEqual(check.going_down_after(), expected_aa)
self.assertFalse(check.is_down())
def test_it_handles_paused_then_started_check(self): def test_it_handles_paused_then_started_check(self):
check = Check(status="paused") check = Check(status="paused")
@ -31,17 +28,14 @@ class CheckModelTestCase(TestCase):
expected_aa = check.last_start + td(hours=1) expected_aa = check.last_start + td(hours=1)
self.assertEqual(check.going_down_after(), expected_aa) self.assertEqual(check.going_down_after(), expected_aa)
self.assertTrue(check.is_down())
def test_it_handles_down(self): def test_it_handles_down(self):
check = Check(status="down") check = Check(status="down")
check.last_ping = timezone.now() - td(hours=1) check.last_ping = timezone.now() - td(hours=1)
self.assertEqual(check.going_down_after(), None) self.assertEqual(check.going_down_after(), None)
self.assertTrue(check.is_down())
def test_it_handles_down_then_started_check(self): def test_it_handles_down_then_started_check(self):
check = Check(status="down") check = Check(status="down")
check.last_start = timezone.now() - td(minutes=10) check.last_start = timezone.now() - td(minutes=10)
self.assertEqual(check.going_down_after(), None) self.assertEqual(check.going_down_after(), None)
self.assertTrue(check.is_down())

+ 42
- 0
hc/api/tests/test_check_model.py View File

@ -99,15 +99,57 @@ class CheckModelTestCase(TestCase):
def test_get_status_handles_started(self): def test_get_status_handles_started(self):
check = Check() check = Check()
check.last_ping = timezone.now() - timedelta(hours=2) 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) check.last_start = timezone.now() - timedelta(minutes=5)
for status in ("new", "paused", "up", "down"): for status in ("new", "paused", "up", "down"):
check.status = status check.status = status
self.assertEqual(check.get_status(), "started") 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): def test_get_status_handles_started_and_mia(self):
check = Check() check = Check()
check.last_start = timezone.now() - timedelta(hours=2) check.last_start = timezone.now() - timedelta(hours=2)
self.assertEqual(check.get_status(), "down") self.assertEqual(check.get_status(), "down")
self.assertEqual(check.get_status(with_started=False), "down")
def test_next_ping_with_cron_syntax(self): def test_next_ping_with_cron_syntax(self):
dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc) dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)


Loading…
Cancel
Save