test_organization_monitors.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from datetime import UTC, datetime, timedelta
  2. from unittest import mock
  3. from django.utils import timezone
  4. from sentry.monitors.models import (
  5. CheckInStatus,
  6. Monitor,
  7. MonitorCheckIn,
  8. MonitorEnvironment,
  9. MonitorStatus,
  10. MonitorType,
  11. ScheduleType,
  12. )
  13. from sentry.testutils.cases import AcceptanceTestCase
  14. from sentry.testutils.silo import no_silo_test
  15. @no_silo_test
  16. class OrganizationMontorsTest(AcceptanceTestCase):
  17. def setUp(self):
  18. super().setUp()
  19. self.path = f"/organizations/{self.organization.slug}/crons/"
  20. self.team = self.create_team(organization=self.organization, name="Mariachi Band")
  21. self.project = self.create_project(
  22. organization=self.organization, teams=[self.team], name="Bengal"
  23. )
  24. self.create_team_membership(self.team, user=self.user)
  25. self.login_as(self.user)
  26. def test_empty_crons_page(self):
  27. self.browser.get(self.path)
  28. self.browser.wait_until(xpath="//h3[text()='Monitor Your Cron Jobs']")
  29. def test_quick_start_flow(self):
  30. self.browser.get(self.path)
  31. self.browser.wait_until_not('[data-test-id="loading-indicator"]')
  32. self.browser.click_when_visible("[aria-label='Create php Monitor']")
  33. self.browser.click_when_visible(xpath="//li[@role='tab']//*[text()='Manual']")
  34. self.browser.wait_until('[name="name"]')
  35. name_input = self.browser.find_element_by_name("name")
  36. name_input.send_keys("My Monitor")
  37. schedule_input = self.browser.find_element_by_name("config.schedule")
  38. schedule_input.clear()
  39. schedule_input.send_keys("10 0 * * *")
  40. self.browser.click_when_visible('button[aria-label="Create"]')
  41. self.browser.wait_until(xpath="//h1[text()='My Monitor']")
  42. def test_create_cron_monitor(self):
  43. self.browser.get(self.path)
  44. self.browser.wait_until_not('[data-test-id="loading-indicator"]')
  45. self.browser.click_when_visible("a[aria-label='Add Monitor']")
  46. self.browser.wait_until('[name="name"]')
  47. name_input = self.browser.find_element_by_name("name")
  48. name_input.send_keys("My Monitor")
  49. schedule_input = self.browser.find_element_by_name("config.schedule")
  50. schedule_input.clear()
  51. schedule_input.send_keys("10 0 * * *")
  52. margin = self.browser.find_element_by_name("config.checkinMargin")
  53. margin.send_keys("5")
  54. max_runtime = self.browser.find_element_by_name("config.maxRuntime")
  55. max_runtime.send_keys("10")
  56. self.browser.click_when_visible('button[aria-label="Create"]')
  57. self.browser.wait_until(xpath="//h1[text()='My Monitor']")
  58. self.browser.element_exists(xpath="//*[text()='At 12:10 AM']")
  59. self.browser.element_exists(xpath="//*[text()='Check-ins missed after 5 mins']")
  60. self.browser.element_exists(xpath="//*[text()='Check-ins longer than 10 mins or errors']")
  61. def test_list_monitors(self):
  62. monitor = Monitor.objects.create(
  63. organization_id=self.organization.id,
  64. project_id=self.project.id,
  65. type=MonitorType.CRON_JOB,
  66. name="My Monitor",
  67. config={
  68. "schedule": "0 0 * * *",
  69. "schedule_type": ScheduleType.CRONTAB,
  70. "max_runtime": None,
  71. "checkin_margin": None,
  72. },
  73. )
  74. with mock.patch(
  75. "django.utils.timezone.now",
  76. return_value=(datetime.now(tz=UTC)),
  77. ):
  78. ts = timezone.now() - timedelta(days=1)
  79. monitor_environment = MonitorEnvironment.objects.create(
  80. monitor=monitor,
  81. environment_id=self.environment.id,
  82. status=MonitorStatus.OK,
  83. last_checkin=ts,
  84. )
  85. MonitorCheckIn.objects.create(
  86. monitor=monitor,
  87. monitor_environment=monitor_environment,
  88. project_id=self.project.id,
  89. status=CheckInStatus.OK,
  90. date_added=ts,
  91. )
  92. self.browser.get(self.path)
  93. self.browser.wait_until_not('[data-test-id="loading-indicator"]')
  94. self.browser.wait_until(xpath="//a//*[text()='My Monitor']")
  95. self.browser.wait_until('[data-test-id="monitor-checkin-tick"]')
  96. def test_edit_monitor(self):
  97. Monitor.objects.create(
  98. organization_id=self.organization.id,
  99. project_id=self.project.id,
  100. type=MonitorType.CRON_JOB,
  101. name="My Monitor",
  102. config={
  103. "schedule": "0 0 * * *",
  104. "schedule_type": ScheduleType.CRONTAB,
  105. "max_runtime": None,
  106. "checkin_margin": None,
  107. },
  108. )
  109. self.browser.get(self.path)
  110. self.browser.wait_until_not('[data-test-id="loading-indicator"]')
  111. self.browser.click_when_visible(xpath="//a//*[text()='My Monitor']")
  112. self.browser.click_when_visible('a[aria-label="Edit"]')
  113. self.browser.wait_until_not('[data-test-id="loading-indicator"]')
  114. name_input = self.browser.find_element_by_name("name")
  115. name_input.clear()
  116. name_input.send_keys("My Edited Monitor")
  117. slug_input = self.browser.find_element_by_name("slug")
  118. slug_input.clear()
  119. slug_input.send_keys("my-monitor-edited-slug")
  120. schedule_input = self.browser.find_element_by_name("config.schedule")
  121. schedule_input.clear()
  122. schedule_input.send_keys("5 0 * * *")
  123. self.browser.click_when_visible('button[aria-label="Save Changes"]')
  124. self.browser.wait_until(xpath="//h1[text()='My Edited Monitor']")
  125. assert self.browser.element_exists(xpath="//*[text()='At 12:05 AM']")
  126. assert "my-monitor-edited-slug" in self.browser.current_url