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.

166 lines
5.8 KiB

8 years ago
8 years ago
6 years ago
  1. from datetime import datetime, timedelta
  2. from django.utils import timezone
  3. from hc.api.models import Check
  4. from hc.test import BaseTestCase
  5. class CheckModelTestCase(BaseTestCase):
  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_status_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_get_status_handles_past_grace(self):
  63. check = Check()
  64. check.status = "up"
  65. check.last_ping = timezone.now() - timedelta(days=2)
  66. self.assertEqual(check.get_status(), "down")
  67. def test_get_status_obeys_down_status(self):
  68. check = Check()
  69. check.status = "down"
  70. check.last_ping = timezone.now() - timedelta(minutes=1)
  71. self.assertEqual(check.get_status(), "down")
  72. def test_get_status_handles_started(self):
  73. check = Check()
  74. check.last_ping = timezone.now() - timedelta(hours=2)
  75. # Last start was 5 minutes ago, display status should be "started"
  76. check.last_start = timezone.now() - timedelta(minutes=5)
  77. for status in ("new", "paused", "up", "down"):
  78. check.status = status
  79. self.assertEqual(check.get_status(), "started")
  80. def test_get_status_handles_down_then_started_and_expired(self):
  81. check = Check(status="down")
  82. # Last ping was 2 days ago
  83. check.last_ping = timezone.now() - timedelta(days=2)
  84. # Last start was 2 hours ago - the check is past its grace time
  85. check.last_start = timezone.now() - timedelta(hours=2)
  86. self.assertEqual(check.get_status(), "down")
  87. self.assertEqual(check.get_status(with_started=False), "down")
  88. def test_get_status_handles_up_then_started(self):
  89. check = Check(status="up")
  90. # Last ping was 2 hours ago, so is still up
  91. check.last_ping = timezone.now() - timedelta(hours=2)
  92. # Last start was 5 minutes ago
  93. check.last_start = timezone.now() - timedelta(minutes=5)
  94. self.assertEqual(check.get_status(), "started")
  95. # Starting a check starts the grace period:
  96. self.assertEqual(check.get_status(with_started=False), "grace")
  97. def test_get_status_handles_up_then_started_and_expired(self):
  98. check = Check(status="up")
  99. # Last ping was 3 hours ago, so is still up
  100. check.last_ping = timezone.now() - timedelta(hours=3)
  101. # Last start was 2 hours ago - the check is past its grace time
  102. check.last_start = timezone.now() - timedelta(hours=2)
  103. self.assertEqual(check.get_status(), "down")
  104. self.assertEqual(check.get_status(with_started=False), "down")
  105. def test_get_status_handles_paused_then_started_and_expired(self):
  106. check = Check(status="paused")
  107. # Last start was 2 hours ago - the check is past its grace time
  108. check.last_start = timezone.now() - timedelta(hours=2)
  109. self.assertEqual(check.get_status(), "down")
  110. self.assertEqual(check.get_status(with_started=False), "down")
  111. def test_get_status_handles_started_and_mia(self):
  112. check = Check()
  113. check.last_start = timezone.now() - timedelta(hours=2)
  114. self.assertEqual(check.get_status(), "down")
  115. self.assertEqual(check.get_status(with_started=False), "down")
  116. def test_next_ping_with_cron_syntax(self):
  117. dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)
  118. # Expect ping every round hour
  119. check = Check(project=self.project)
  120. check.kind = "cron"
  121. check.schedule = "0 * * * *"
  122. check.status = "up"
  123. check.last_ping = dt
  124. # Need to save it for M2M relations to work:
  125. check.save()
  126. d = check.to_dict()
  127. self.assertEqual(d["next_ping"], "2000-01-01T01:00:00+00:00")