forked from GithubBackups/healthchecks
Fix Check.is_down(), add tests.
This commit is contained in:
parent
b803d877d0
commit
0b6c317956
@ -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)
|
||||
|
@ -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())
|
||||
|
@ -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 = {
|
||||
|
@ -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", ""):
|
||||
|
Loading…
x
Reference in New Issue
Block a user