Browse Source

Fix Check.is_down(), add tests.

pull/211/head
Pēteris Caune 6 years ago
parent
commit
0b6c317956
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
4 changed files with 33 additions and 31 deletions
  1. +12
    -9
      hc/api/models.py
  2. +17
    -0
      hc/api/tests/test_check_alert_after.py
  3. +3
    -9
      hc/front/tests/test_update_timeout.py
  4. +1
    -13
      hc/front/views.py

+ 12
- 9
hc/api/models.py View File

@ -136,20 +136,30 @@ class Check(models.Model):
it = croniter(self.schedule, last_local) it = croniter(self.schedule, last_local)
result = it.next(datetime) result = it.next(datetime)
if self.last_start:
if self.last_start and self.status != "down":
result = min(result, self.last_start) result = min(result, self.last_start)
if result != NEVER: if result != NEVER:
return result return result
def get_alert_after(self):
""" Return the datetime when check potentially goes down. """
grace_start = self.get_grace_start()
if grace_start is not None:
return grace_start + self.grace
def is_down(self): def is_down(self):
""" Return True if the check is currently in alert state. """ """ Return True if the check is currently in alert state. """
if self.status == "down":
return True
alert_after = self.get_alert_after() alert_after = self.get_alert_after()
if alert_after is None: if alert_after is None:
return False return False
return timezone.now() >= self.get_alert_after()
return timezone.now() >= alert_after
def get_status(self, now=None): def get_status(self, now=None):
""" Return current status for display. """ """ Return current status for display. """
@ -176,13 +186,6 @@ class Check(models.Model):
return "up" return "up"
def get_alert_after(self):
""" Return the datetime when check potentially goes down. """
grace_start = self.get_grace_start()
if grace_start is not None:
return grace_start + self.grace
def assign_all_channels(self): def assign_all_channels(self):
if self.user: if self.user:
channels = Channel.objects.filter(user=self.user) channels = Channel.objects.filter(user=self.user)


+ 17
- 0
hc/api/tests/test_check_alert_after.py View File

@ -10,17 +10,20 @@ class CheckModelTestCase(TestCase):
def test_it_handles_new_check(self): def test_it_handles_new_check(self):
check = Check() check = Check()
self.assertEqual(check.get_alert_after(), None) self.assertEqual(check.get_alert_after(), None)
self.assertFalse(check.is_down())
def test_it_handles_paused_check(self): def test_it_handles_paused_check(self):
check = Check() check = Check()
check.last_ping = timezone.now() - td(days=2) check.last_ping = timezone.now() - td(days=2)
self.assertEqual(check.get_alert_after(), None) self.assertEqual(check.get_alert_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.get_alert_after(), expected_aa) self.assertEqual(check.get_alert_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")
@ -28,3 +31,17 @@ class CheckModelTestCase(TestCase):
expected_aa = check.last_start + td(hours=1) expected_aa = check.last_start + td(hours=1)
self.assertEqual(check.get_alert_after(), expected_aa) self.assertEqual(check.get_alert_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.get_alert_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.get_alert_after(), None)
self.assertTrue(check.is_down())

+ 3
- 9
hc/front/tests/test_update_timeout.py View File

@ -30,7 +30,7 @@ class UpdateTimeoutTestCase(BaseTestCase):
# alert_after should be updated too # alert_after should be updated too
self.assertEqual(self.check.alert_after, self.check.get_alert_after()) self.assertEqual(self.check.alert_after, self.check.get_alert_after())
def test_it_updates_status(self):
def test_it_does_not_update_status(self):
self.check.last_ping = timezone.now() - td(days=2) self.check.last_ping = timezone.now() - td(days=2)
self.check.status = "down" self.check.status = "down"
self.check.save() self.check.save()
@ -39,16 +39,10 @@ class UpdateTimeoutTestCase(BaseTestCase):
payload = {"kind": "simple", "timeout": 3600 * 24 * 7, "grace": 60} payload = {"kind": "simple", "timeout": 3600 * 24 * 7, "grace": 60}
self.client.login(username="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, data=payload)
self.assertRedirects(r, "/checks/")
self.client.post(self.url, data=payload)
self.check.refresh_from_db() self.check.refresh_from_db()
self.assertEqual(self.check.status, "up")
flip = Flip.objects.get()
self.assertEqual(flip.owner_id, self.check.id)
self.assertEqual(flip.old_status, "down")
self.assertEqual(flip.new_status, "up")
self.assertEqual(self.check.status, "down")
def test_it_saves_cron_expression(self): def test_it_saves_cron_expression(self):
payload = { payload = {


+ 1
- 13
hc/front/views.py View File

@ -297,19 +297,7 @@ def update_timeout(request, code):
check.tz = form.cleaned_data["tz"] check.tz = form.cleaned_data["tz"]
check.grace = td(minutes=form.cleaned_data["grace"]) check.grace = td(minutes=form.cleaned_data["grace"])
if check.last_ping:
check.alert_after = check.get_alert_after()
# Changing timeout can change check's status:
if not check.is_down() and check.status == "down":
flip = Flip(owner=check)
flip.created = timezone.now()
flip.old_status = "down"
flip.new_status = "up"
flip.save()
check.status = "up"
check.alert_after = check.get_alert_after()
check.save() check.save()
if "/details/" in request.META.get("HTTP_REFERER", ""): if "/details/" in request.META.get("HTTP_REFERER", ""):


Loading…
Cancel
Save