tests.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import asyncio
  2. from unittest import mock
  3. from aioresponses import aioresponses
  4. from django.core import mail
  5. from django.urls import reverse
  6. from freezegun import freeze_time
  7. from model_bakery import baker
  8. from glitchtip.test_utils.test_case import GlitchTipTestCase
  9. from ..constants import MonitorType
  10. from ..models import Monitor, MonitorCheck
  11. from ..tasks import dispatch_checks
  12. from ..utils import fetch_all
  13. class UptimeTestCase(GlitchTipTestCase):
  14. @mock.patch("glitchtip.uptime.tasks.perform_checks.run")
  15. def test_dispatch_checks(self, mocked):
  16. mock.return_value = None
  17. test_url = "https://example.com"
  18. with freeze_time("2020-01-01"):
  19. mon1 = baker.make(Monitor, url=test_url, monitor_type=MonitorType.GET)
  20. mon2 = baker.make(Monitor, url=test_url, monitor_type=MonitorType.GET)
  21. baker.make(MonitorCheck, monitor=mon1)
  22. self.assertEqual(mocked.call_count, 2)
  23. with freeze_time("2020-01-02"):
  24. baker.make(MonitorCheck, monitor=mon2)
  25. dispatch_checks()
  26. self.assertEqual(mocked.call_count, 3)
  27. @aioresponses()
  28. def test_fetch_all(self, mocked):
  29. test_url = "https://example.com"
  30. mocked.get(test_url, status=200)
  31. mon1 = baker.make(Monitor, url=test_url, monitor_type=MonitorType.GET)
  32. mocked.get(test_url, status=200)
  33. loop = asyncio.get_event_loop()
  34. monitors = list(Monitor.objects.all().values())
  35. results = loop.run_until_complete(fetch_all(monitors, loop))
  36. self.assertEqual(results[0]["id"], mon1.pk)
  37. @aioresponses()
  38. def test_monitor_checks_integration(self, mocked):
  39. test_url = "https://example.com"
  40. mocked.get(test_url, status=200)
  41. with freeze_time("2020-01-01"):
  42. mon = baker.make(Monitor, url=test_url, monitor_type=MonitorType.GET)
  43. self.assertEqual(mon.checks.count(), 1)
  44. mocked.get(test_url, status=200)
  45. with freeze_time("2020-01-01"):
  46. dispatch_checks()
  47. self.assertEqual(mon.checks.count(), 1)
  48. with freeze_time("2020-01-02"):
  49. with self.assertNumQueries(5):
  50. dispatch_checks()
  51. self.assertEqual(mon.checks.count(), 2)
  52. @aioresponses()
  53. def test_monitor_email(self, mocked):
  54. self.create_user_and_project()
  55. test_url = "https://example.com"
  56. mocked.get(test_url, status=200)
  57. with freeze_time("2020-01-01"):
  58. baker.make(
  59. Monitor,
  60. name=test_url,
  61. url=test_url,
  62. monitor_type=MonitorType.GET,
  63. project=self.project,
  64. )
  65. mocked.get(test_url, status=500)
  66. with freeze_time("2020-01-02"):
  67. dispatch_checks()
  68. self.assertEqual(len(mail.outbox), 1)
  69. self.assertIn("is down", mail.outbox[0].body)
  70. mocked.get(test_url, status=500)
  71. with freeze_time("2020-01-03"):
  72. dispatch_checks()
  73. self.assertEqual(len(mail.outbox), 1)
  74. mocked.get(test_url, status=200)
  75. with freeze_time("2020-01-04"):
  76. dispatch_checks()
  77. self.assertEqual(len(mail.outbox), 2)
  78. self.assertIn("is up", mail.outbox[1].body)
  79. def test_heartbeat(self):
  80. self.create_user_and_project()
  81. with freeze_time("2020-01-01"):
  82. monitor = baker.make(
  83. Monitor, monitor_type=MonitorType.HEARTBEAT, project=self.project,
  84. )
  85. url = reverse(
  86. "heartbeat-check",
  87. kwargs={
  88. "organization_slug": monitor.organization.slug,
  89. "endpoint_id": monitor.endpoint_id,
  90. },
  91. )
  92. self.assertFalse(monitor.checks.exists())
  93. self.client.post(url)
  94. self.assertTrue(monitor.checks.filter(is_up=True).exists())
  95. dispatch_checks()
  96. self.assertTrue(monitor.checks.filter(is_up=True).exists())
  97. self.assertEqual(len(mail.outbox), 0)
  98. with freeze_time("2020-01-02"):
  99. dispatch_checks()
  100. self.assertEqual(len(mail.outbox), 1)
  101. with freeze_time("2020-01-03"):
  102. dispatch_checks() # Still down
  103. self.assertEqual(len(mail.outbox), 1)
  104. with freeze_time("2020-01-04"):
  105. self.client.post(url) # Back up
  106. self.assertEqual(len(mail.outbox), 2)
  107. def test_heartbeat_grace_period(self):
  108. # Don't alert users when heartbeat check has never come in
  109. self.create_user_and_project()
  110. baker.make(Monitor, monitor_type=MonitorType.HEARTBEAT, project=self.project)
  111. dispatch_checks()
  112. self.assertEqual(len(mail.outbox), 0)