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.

106 lines
3.2 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_get_status_handles_new_check(self):
  13. check = Check()
  14. self.assertEqual(check.get_status(), "new")
  15. def test_status_works_with_grace_period(self):
  16. check = Check()
  17. check.status = "up"
  18. check.last_ping = timezone.now() - timedelta(days=1, minutes=30)
  19. self.assertEqual(check.get_status(), "grace")
  20. def test_get_stauts_handles_paused_check(self):
  21. check = Check()
  22. check.status = "up"
  23. check.last_ping = timezone.now() - timedelta(days=1, minutes=30)
  24. self.assertEqual(check.get_status(), "grace")
  25. check.status = "paused"
  26. self.assertEqual(check.get_status(), "paused")
  27. def test_status_works_with_cron_syntax(self):
  28. dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)
  29. # Expect ping every midnight, default grace is 1 hour
  30. check = Check()
  31. check.kind = "cron"
  32. check.schedule = "0 0 * * *"
  33. check.status = "up"
  34. check.last_ping = dt
  35. # 23:59pm
  36. now = dt + timedelta(hours=23, minutes=59)
  37. self.assertEqual(check.get_status(now), "up")
  38. # 00:00am
  39. now = dt + timedelta(days=1)
  40. self.assertEqual(check.get_status(now), "grace")
  41. # 1:30am
  42. now = dt + timedelta(days=1, minutes=60)
  43. self.assertEqual(check.get_status(now), "down")
  44. def test_status_works_with_timezone(self):
  45. dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)
  46. # Expect ping every day at 10am, default grace is 1 hour
  47. check = Check()
  48. check.kind = "cron"
  49. check.schedule = "0 10 * * *"
  50. check.status = "up"
  51. check.last_ping = dt
  52. check.tz = "Australia/Brisbane" # UTC+10
  53. # 10:30am
  54. now = dt + timedelta(hours=23, minutes=59)
  55. self.assertEqual(check.get_status(now), "up")
  56. # 10:30am
  57. now = dt + timedelta(days=1)
  58. self.assertEqual(check.get_status(now), "grace")
  59. # 11:30am
  60. now = dt + timedelta(days=1, minutes=60)
  61. self.assertEqual(check.get_status(now), "down")
  62. def test_next_ping_with_cron_syntax(self):
  63. dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)
  64. # Expect ping every round hour
  65. check = Check()
  66. check.kind = "cron"
  67. check.schedule = "0 * * * *"
  68. check.status = "up"
  69. check.last_ping = dt
  70. d = check.to_dict()
  71. self.assertEqual(d["next_ping"], "2000-01-01T01:00:00+00:00")
  72. def test_status_checks_the_fail_flag(self):
  73. check = Check()
  74. check.status = "up"
  75. check.last_ping = timezone.now() - timedelta(minutes=5)
  76. check.last_ping_was_fail = True
  77. # The computed status should be "down" because last_ping_was_fail
  78. # is set.
  79. self.assertEqual(check.get_status(), "down")