You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.4 KiB

8 years ago
8 years ago
  1. from datetime import datetime, timedelta
  2. from django.test import TestCase
  3. from django.utils import timezone
  4. from hc.api.models import Check
  5. class CheckModelTestCase(TestCase):
  6. def test_it_strips_tags(self):
  7. check = Check()
  8. check.tags = " foo bar "
  9. self.assertEqual(check.tags_list(), ["foo", "bar"])
  10. check.tags = " "
  11. self.assertEqual(check.tags_list(), [])
  12. def test_in_grace_period_handles_new_check(self):
  13. check = Check()
  14. self.assertFalse(check.in_grace_period())
  15. def test_in_grace_period_handles_fail_ping(self):
  16. check = Check()
  17. check.status = "up"
  18. check.last_ping = timezone.now() - timedelta(days=1, minutes=30)
  19. check.last_ping_was_fail = True
  20. # If last ping was signalling a failure, we're not in grace period,
  21. # we're down
  22. self.assertFalse(check.in_grace_period())
  23. def test_status_works_with_grace_period(self):
  24. check = Check()
  25. check.status = "up"
  26. check.last_ping = timezone.now() - timedelta(days=1, minutes=30)
  27. self.assertTrue(check.in_grace_period())
  28. self.assertEqual(check.get_status(), "up")
  29. def test_paused_check_is_not_in_grace_period(self):
  30. check = Check()
  31. check.status = "up"
  32. check.last_ping = timezone.now() - timedelta(days=1, minutes=30)
  33. self.assertTrue(check.in_grace_period())
  34. check.status = "paused"
  35. self.assertFalse(check.in_grace_period())
  36. def test_status_works_with_cron_syntax(self):
  37. dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)
  38. # Expect ping every midnight, default grace is 1 hour
  39. check = Check()
  40. check.kind = "cron"
  41. check.schedule = "0 0 * * *"
  42. check.status = "up"
  43. check.last_ping = dt
  44. # 00:30am
  45. now = dt + timedelta(days=1, minutes=30)
  46. self.assertEqual(check.get_status(now), "up")
  47. # 1:30am
  48. now = dt + timedelta(days=1, minutes=90)
  49. self.assertEqual(check.get_status(now), "down")
  50. def test_status_works_with_timezone(self):
  51. dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)
  52. # Expect ping every day at 10am, default grace is 1 hour
  53. check = Check()
  54. check.kind = "cron"
  55. check.schedule = "0 10 * * *"
  56. check.status = "up"
  57. check.last_ping = dt
  58. check.tz = "Australia/Brisbane" # UTC+10
  59. # 10:30am
  60. now = dt + timedelta(days=1, minutes=30)
  61. self.assertEqual(check.get_status(now), "up")
  62. # 11:30am
  63. now = dt + timedelta(days=1, minutes=90)
  64. self.assertEqual(check.get_status(now), "down")
  65. def test_next_ping_with_cron_syntax(self):
  66. dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)
  67. # Expect ping every round hour
  68. check = Check()
  69. check.kind = "cron"
  70. check.schedule = "0 * * * *"
  71. check.status = "up"
  72. check.last_ping = dt
  73. d = check.to_dict()
  74. self.assertEqual(d["next_ping"], "2000-01-01T01:00:00+00:00")
  75. def test_status_checks_the_fail_flag(self):
  76. check = Check()
  77. check.status = "up"
  78. check.last_ping = timezone.now() - timedelta(minutes=5)
  79. check.last_ping_was_fail = True
  80. # The computed status should be "down" because last_ping_was_fail
  81. # is set.
  82. self.assertEqual(check.get_status(), "down")