Skip to content

Commit

Permalink
Merge pull request #1351 from jamesoff/bugfix/tz-tests
Browse files Browse the repository at this point in the history
Attempt to fix TZ-related test failures
  • Loading branch information
jamesoff authored Feb 18, 2024
2 parents 5bfb9e0 + a844c4b commit ab53b39
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
10 changes: 8 additions & 2 deletions simplemonitor/Alerters/alerter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import datetime
import logging
import os
import textwrap
from enum import Enum
from socket import gethostname
Expand Down Expand Up @@ -158,8 +159,13 @@ def __init__(self, config_options: Optional[dict] = None) -> None:
self._only_failures = self.get_config_option(
"only_failures", required_type="bool", default=False
)
self._tz = cast(str, self.get_config_option("tz", default="UTC"))
self._times_tz = cast(str, self.get_config_option("times_tz", default="local"))
self._tz = cast(
str, self.get_config_option("tz", default=os.environ.get("TZ", "UTC"))
)
self._times_tz = cast(
str,
self.get_config_option("times_tz", default=os.environ.get("TZ", "local")),
)
self.urgent = cast(
bool,
self.get_config_option("urgent", default=self.urgent, required_type="bool"),
Expand Down
62 changes: 42 additions & 20 deletions tests/test_alerter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# type: ignore
import datetime
import os
import textwrap
import unittest

Expand All @@ -15,6 +16,7 @@ class TestAlerter(unittest.TestCase):
def setUp(self):
# Work around to fix the freezegun times later to our local TZ, else they're UTC
# and the time compared to is local (as it should be)
self.tz = os.environ.get("TZ", "local")
a = arrow.now()
self.utcoffset = a.utcoffset()

Expand Down Expand Up @@ -137,10 +139,11 @@ def test_should_not_alert_basic_success(self):
m.run_test()
self.assertEqual(a.should_alert(m), alerter.AlertType.NONE)

@freeze_time("2020-03-10") # a Tuesday
def test_not_allowed_today(self):
a = alerter.Alerter({"days": "0,2,3,4,5,6"})
self.assertFalse(a._allowed_today())
# a Tuesday
with freeze_time("2020-03-10", tz_offset=self.utcoffset):
a = alerter.Alerter({"days": "0,2,3,4,5,6"})
self.assertFalse(a._allowed_today())

@freeze_time(
"2020-03-09 22:00:00+00:00"
Expand All @@ -149,10 +152,10 @@ def test_not_allowed_today_tz(self):
a = alerter.Alerter({"days": "0,2,3,4,5,6", "times_tz": "+05:00"})
self.assertFalse(a._allowed_today())

@freeze_time("2020-03-10")
def test_allowed_today(self):
a = alerter.Alerter({"days": "1"})
self.assertTrue(a._allowed_today())
with freeze_time("2020-03-10", tz_offset=self.utcoffset):
a = alerter.Alerter({"days": "1"})
self.assertTrue(a._allowed_today())

@freeze_time(
"2020-03-09 22:00:00+00:00"
Expand All @@ -173,7 +176,12 @@ def test_allowed_always(self):

def test_allowed_only(self):
a = alerter.Alerter(
{"times_type": "only", "time_lower": "10:00", "time_upper": "11:00"}
{
"times_type": "only",
"time_lower": "10:00",
"time_upper": "11:00",
"times_tz": "local",
}
)
with freeze_time("09:00", tz_offset=-self.utcoffset):
self.assertFalse(a._allowed_time())
Expand All @@ -200,11 +208,17 @@ def test_allowed_only_tz(self):

def test_allowed_not(self):
a = alerter.Alerter(
{"times_type": "not", "time_lower": "10:00", "time_upper": "11:00"}
{
"times_type": "not",
"time_lower": "10:00",
"time_upper": "11:00",
"times_tz": "local",
}
)
with freeze_time("09:00", tz_offset=-self.utcoffset):
self.assertTrue(a._allowed_time())
with freeze_time("10:30", tz_offset=-self.utcoffset):
print(arrow.get())
self.assertFalse(a._allowed_time())
with freeze_time("12:00", tz_offset=-self.utcoffset):
self.assertTrue(a._allowed_time())
Expand Down Expand Up @@ -244,7 +258,7 @@ def test_should_alert_ooh(self):
a = alerter.Alerter(config)
m = monitor.MonitorFail("fail", {})
m.run_test()
with freeze_time("2020-03-10 10:30"):
with freeze_time("2020-03-10 10:30", tz_offset=self.utcoffset):
self.assertEqual(a.should_alert(m), alerter.AlertType.FAILURE)
self.assertEqual(a._ooh_failures, [])

Expand All @@ -258,7 +272,7 @@ def test_should_alert_limit(self):
a = alerter.Alerter(config)
m = monitor.MonitorFail("fail", {})
m.run_test()
with freeze_time("2020-03-10 10:30"):
with freeze_time("2020-03-10 10:30", tz_offset=self.utcoffset):
self.assertEqual(a.should_alert(m), alerter.AlertType.NONE)
self.assertEqual(a._ooh_failures, [])

Expand All @@ -280,7 +294,7 @@ def test_should_alert_limit_ooh(self):
a = alerter.Alerter(config)
m = monitor.MonitorFail("fail", {})
m.run_test()
with freeze_time("2020-03-10 09:00"):
with freeze_time("2020-03-10 09:00", tz_offset=self.utcoffset):
self.assertEqual(a.should_alert(m), alerter.AlertType.NONE)
self.assertEqual(a._ooh_failures, [])

Expand All @@ -305,11 +319,11 @@ def test_should_alert_catchup(self):
a.support_catchup = True
m = monitor.MonitorFail("fail", {})
m.run_test()
with freeze_time("2020-03-10 09:00"):
with freeze_time("2020-03-10 09:00", tz_offset=self.utcoffset):
self.assertEqual(a.should_alert(m), alerter.AlertType.NONE)
self.assertEqual(a._ooh_failures, ["fail"])

with freeze_time("2020-03-10 10:30"):
with freeze_time("2020-03-10 10:30", tz_offset=self.utcoffset):
self.assertEqual(a.should_alert(m), alerter.AlertType.CATCHUP)
self.assertEqual(a._ooh_failures, [])

Expand All @@ -323,11 +337,11 @@ def test_should_alert_no_catchup(self):
a = alerter.Alerter(config)
m = monitor.MonitorFail("fail", {})
m.run_test()
with freeze_time("2020-03-10 09:00"):
with freeze_time("2020-03-10 09:00", tz_offset=self.utcoffset):
self.assertEqual(a.should_alert(m), alerter.AlertType.NONE)
self.assertEqual(a._ooh_failures, ["fail"])

with freeze_time("2020-03-10 10:30"):
with freeze_time("2020-03-10 10:30", tz_offset=self.utcoffset):
self.assertEqual(a.should_alert(m), alerter.AlertType.FAILURE)
self.assertEqual(a._ooh_failures, [])

Expand Down Expand Up @@ -396,7 +410,12 @@ class TestMessageBuilding(unittest.TestCase):
def setUp(self):
self.test_alerter = alerter.Alerter()
self.freeze_time_value = "2020-03-10 09:00"
self.expected_time_string = "2020-03-10 09:00:00+00:00"
self.tz = os.environ.get("TZ", "local")
self.expected_time_string = (
arrow.get("2020-03-10 09:00:00+00:00").to(self.tz).format()
)
a = arrow.now()
self.utcoffset = a.utcoffset()

def test_notification_format_failure(self):
m = monitor.MonitorFail("test", {})
Expand Down Expand Up @@ -585,6 +604,9 @@ def test_not_urgent(self):


class TestDescription(unittest.TestCase):
def setUp(self) -> None:
self.tz = os.environ.get("TZ", "local")

def test_times_always(self):
a = alerter.Alerter({"times_type": "always"})
self.assertEqual(a._describe_times(), "(always)")
Expand All @@ -594,7 +616,7 @@ def test_times_only_times(self):
{"times_type": "only", "time_lower": "09:00", "time_upper": "10:00"}
)
self.assertEqual(
a._describe_times(), "only between 09:00 and 10:00 (local) on any day"
a._describe_times(), f"only between 09:00 and 10:00 ({self.tz}) on any day"
)

def test_times_only_days(self):
Expand All @@ -608,7 +630,7 @@ def test_times_only_days(self):
)
self.assertEqual(
a._describe_times(),
"only between 09:00 and 10:00 (local) on Mon, Tue, Wed",
f"only between 09:00 and 10:00 ({self.tz}) on Mon, Tue, Wed",
)

def test_times_not_time(self):
Expand All @@ -617,7 +639,7 @@ def test_times_not_time(self):
)
self.assertEqual(
a._describe_times(),
"any time except between 09:00 and 10:00 (local) on any day",
f"any time except between 09:00 and 10:00 ({self.tz}) on any day",
)

def test_times_not_days(self):
Expand All @@ -631,5 +653,5 @@ def test_times_not_days(self):
)
self.assertEqual(
a._describe_times(),
"any time except between 09:00 and 10:00 (local) on Thu, Fri, Sat, Sun",
f"any time except between 09:00 and 10:00 ({self.tz}) on Thu, Fri, Sat, Sun",
)

0 comments on commit ab53b39

Please sign in to comment.