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.

89 lines
2.7 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_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.assertTrue(check.in_grace_period())
  20. self.assertEqual(check.get_status(), "up")
  21. def test_paused_check_is_not_in_grace_period(self):
  22. check = Check()
  23. check.status = "up"
  24. check.last_ping = timezone.now() - timedelta(days=1, minutes=30)
  25. self.assertTrue(check.in_grace_period())
  26. check.status = "paused"
  27. self.assertFalse(check.in_grace_period())
  28. def test_status_works_with_cron_syntax(self):
  29. dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)
  30. # Expect ping every midnight, default grace is 1 hour
  31. check = Check()
  32. check.kind = "cron"
  33. check.schedule = "0 0 * * *"
  34. check.status = "up"
  35. check.last_ping = dt
  36. # 00:30am
  37. now = dt + timedelta(days=1, minutes=30)
  38. self.assertEqual(check.get_status(now), "up")
  39. # 1:30am
  40. now = dt + timedelta(days=1, minutes=90)
  41. self.assertEqual(check.get_status(now), "down")
  42. def test_status_works_with_timezone(self):
  43. dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)
  44. # Expect ping every day at 10am, default grace is 1 hour
  45. check = Check()
  46. check.kind = "cron"
  47. check.schedule = "0 10 * * *"
  48. check.status = "up"
  49. check.last_ping = dt
  50. check.tz = "Australia/Brisbane" # UTC+10
  51. # 10:30am
  52. now = dt + timedelta(days=1, minutes=30)
  53. self.assertEqual(check.get_status(now), "up")
  54. # 11:30am
  55. now = dt + timedelta(days=1, minutes=90)
  56. self.assertEqual(check.get_status(now), "down")
  57. def test_next_ping_with_cron_syntax(self):
  58. dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)
  59. # Expect ping every round hour
  60. check = Check()
  61. check.kind = "cron"
  62. check.schedule = "0 * * * *"
  63. check.status = "up"
  64. check.last_ping = dt
  65. d = check.to_dict()
  66. self.assertEqual(d["next_ping"], "2000-01-01T01:00:00+00:00")