From 0b6c317956a5d7ddf67a65d722ad20ad96f9c6c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Wed, 19 Dec 2018 18:27:58 +0200 Subject: [PATCH] Fix Check.is_down(), add tests. --- hc/api/models.py | 21 ++++++++++++--------- hc/api/tests/test_check_alert_after.py | 17 +++++++++++++++++ hc/front/tests/test_update_timeout.py | 12 +++--------- hc/front/views.py | 14 +------------- 4 files changed, 33 insertions(+), 31 deletions(-) diff --git a/hc/api/models.py b/hc/api/models.py index 73dfda84..07e75529 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -136,20 +136,30 @@ class Check(models.Model): it = croniter(self.schedule, last_local) result = it.next(datetime) - if self.last_start: + if self.last_start and self.status != "down": result = min(result, self.last_start) if result != NEVER: 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): """ Return True if the check is currently in alert state. """ + if self.status == "down": + return True + alert_after = self.get_alert_after() if alert_after is None: return False - return timezone.now() >= self.get_alert_after() + return timezone.now() >= alert_after def get_status(self, now=None): """ Return current status for display. """ @@ -176,13 +186,6 @@ class Check(models.Model): 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): if self.user: channels = Channel.objects.filter(user=self.user) diff --git a/hc/api/tests/test_check_alert_after.py b/hc/api/tests/test_check_alert_after.py index a04e11fb..c7d1dc3f 100644 --- a/hc/api/tests/test_check_alert_after.py +++ b/hc/api/tests/test_check_alert_after.py @@ -10,17 +10,20 @@ class CheckModelTestCase(TestCase): def test_it_handles_new_check(self): check = Check() self.assertEqual(check.get_alert_after(), None) + self.assertFalse(check.is_down()) def test_it_handles_paused_check(self): check = Check() check.last_ping = timezone.now() - td(days=2) self.assertEqual(check.get_alert_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.get_alert_after(), expected_aa) + self.assertFalse(check.is_down()) def test_it_handles_paused_then_started_check(self): check = Check(status="paused") @@ -28,3 +31,17 @@ class CheckModelTestCase(TestCase): expected_aa = check.last_start + td(hours=1) 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()) diff --git a/hc/front/tests/test_update_timeout.py b/hc/front/tests/test_update_timeout.py index d90ba1de..1cafac49 100644 --- a/hc/front/tests/test_update_timeout.py +++ b/hc/front/tests/test_update_timeout.py @@ -30,7 +30,7 @@ class UpdateTimeoutTestCase(BaseTestCase): # alert_after should be updated too 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.status = "down" self.check.save() @@ -39,16 +39,10 @@ class UpdateTimeoutTestCase(BaseTestCase): payload = {"kind": "simple", "timeout": 3600 * 24 * 7, "grace": 60} self.client.login(username="alice@example.org", 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.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): payload = { diff --git a/hc/front/views.py b/hc/front/views.py index 1358197e..0b639e3e 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -297,19 +297,7 @@ def update_timeout(request, code): check.tz = form.cleaned_data["tz"] 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() if "/details/" in request.META.get("HTTP_REFERER", ""):