test_organization_monitors.py 5.7 KB

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